This commit is contained in:
zoe 2022-02-06 18:32:36 +01:00
parent d692ace55f
commit 58066c8abc

View file

@ -6,10 +6,14 @@ extern crate lazy_static;
use clap::Parser; use clap::Parser;
use rocket::fs::{relative, FileServer}; use rocket::fs::{relative, FileServer};
use rocket::response::stream::{Event, EventStream};
use rocket_dyn_templates::Template; 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 names;
mod rooms; mod rooms;
@ -22,7 +26,6 @@ lazy_static! {
static ref PLAYS: Mutex<HashMap<String, Vec<rooms::Board>>> = Mutex::new(HashMap::new()); static ref PLAYS: Mutex<HashMap<String, Vec<rooms::Board>>> = Mutex::new(HashMap::new());
} }
/* /*
* Config options * Config options
*/ */
@ -45,7 +48,7 @@ fn rocket() -> _ {
rocket::custom(config) rocket::custom(config)
.attach(Template::fairing()) .attach(Template::fairing())
.mount("/", FileServer::from(relative!("static"))) .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 //sends new board if there have been updates
//* //*
#[get("/stream/<roomname>/<token>")] #[get("/stream/<roomname>/<token>")]
fn stream(roomname: String, token: usize) -> EventStream![]{ async fn stream(roomname: String, token: usize) -> EventStream![] {
EventStream!{ let roomlist = ROOMS.lock().unwrap();
drop(roomlist);
EventStream! {
loop { loop {
// TODO // TODO
// check if there are any new events, and then if the room token is correct // 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 // if that's the case send the current playing board
// otherwise don't do anything // otherwise don't do anything
let mut i = 0; yield Event::json(&"oof".to_string());
if i == 0 {
let event = "something";
yield Event::json(&event);
}
i += 1
} }
} }
} }
//* //*
// adds new boards to the send queue after calculating move // adds new boards to the send queue after calculating move
//* //*
#[post("/play?<roomname>&<token>&<player1>&<x>&<y>")] #[post("/play?<roomname>&<token>&<player1>&<x>&<y>")]
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 // get room mutex
let mut roomlist = ROOMS.lock().unwrap(); let mut roomlist = ROOMS.lock().unwrap();
// check if room is legal // 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); board.make_play(&x, &y);
let mut playlist = PLAYS.lock().unwrap(); let mut playlist = PLAYS.lock().unwrap();
// if the board doesn't exist yet, make a new one // 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(); let rn = roomname.to_string();
playlist.insert(rn, vec![]); playlist.insert(rn, vec![]);
} }