From 58066c8abc9bdf7f0e74bb7f6442fe446d335936 Mon Sep 17 00:00:00 2001 From: zoe Date: Sun, 6 Feb 2022 18:32:36 +0100 Subject: [PATCH] oof --- src/main.rs | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index b1b49d1..d5ba36e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,10 +6,14 @@ extern crate lazy_static; use clap::Parser; use rocket::fs::{relative, FileServer}; +use rocket::response::stream::{Event, EventStream}; use rocket_dyn_templates::Template; -use rocket::response::stream::{EventStream, Event}; -use std::{collections::HashMap, mem::drop, sync::Mutex}; +use std::{ + collections::HashMap, + mem::drop, + sync::{mpsc, Mutex, RwLock}, +}; mod names; mod rooms; @@ -22,7 +26,6 @@ lazy_static! { static ref PLAYS: Mutex>> = Mutex::new(HashMap::new()); } - /* * Config options */ @@ -45,7 +48,7 @@ fn rocket() -> _ { rocket::custom(config) .attach(Template::fairing()) .mount("/", FileServer::from(relative!("static"))) - .mount("/", routes![index, room, stream]) + .mount("/", routes![index, room, stream, play]) } /* @@ -118,29 +121,25 @@ async fn room(roomname: &str, token: usize, player1: bool) -> Template { //sends new board if there have been updates //* #[get("/stream//")] -fn stream(roomname: String, token: usize) -> EventStream![]{ - EventStream!{ +async fn stream(roomname: String, token: usize) -> EventStream![] { + let roomlist = ROOMS.lock().unwrap(); + drop(roomlist); + EventStream! { loop { // TODO // check if there are any new events, and then if the room token is correct // if that's the case send the current playing board // otherwise don't do anything - let mut i = 0; - if i == 0 { - let event = "something"; - yield Event::json(&event); - } - i += 1 + yield Event::json(&"oof".to_string()); } } } - //* // adds new boards to the send queue after calculating move //* #[post("/play?&&&&")] -fn play (roomname: &str, token: usize, player1: bool, x: u8, y: u8){ +fn play(roomname: &str, token: usize, player1: bool, x: u8, y: u8) { // get room mutex let mut roomlist = ROOMS.lock().unwrap(); // check if room is legal @@ -151,7 +150,7 @@ fn play (roomname: &str, token: usize, player1: bool, x: u8, y: u8){ board.make_play(&x, &y); let mut playlist = PLAYS.lock().unwrap(); // if the board doesn't exist yet, make a new one - if !(playlist.contains_key(roomname)){ + if !(playlist.contains_key(roomname)) { let rn = roomname.to_string(); playlist.insert(rn, vec![]); }