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 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<HashMap<String, Vec<rooms::Board>>> = 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/<roomname>/<token>")]
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?<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
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![]);
}