stream should KINDA work now

This commit is contained in:
zoe 2022-02-06 19:49:44 +01:00
parent 58066c8abc
commit bc77e08e4d
2 changed files with 25 additions and 16 deletions

View file

@ -7,13 +7,11 @@ use clap::Parser;
use rocket::fs::{relative, FileServer}; use rocket::fs::{relative, FileServer};
use rocket::response::stream::{Event, EventStream}; use rocket::response::stream::{Event, EventStream};
use rocket::tokio::sync::{Mutex, RwLock};
use rocket_dyn_templates::Template; use rocket_dyn_templates::Template;
use rocket::tokio::time::{sleep, Duration};
use std::{ use std::{collections::HashMap, mem::drop};
collections::HashMap,
mem::drop,
sync::{mpsc, Mutex, RwLock},
};
mod names; mod names;
mod rooms; mod rooms;
@ -23,7 +21,7 @@ lazy_static! {
static ref ARGS: Args = Args::parse(); static ref ARGS: Args = Args::parse();
static ref ROOMS: Mutex<HashMap<String, rooms::Room>> = Mutex::new(HashMap::new()); static ref ROOMS: Mutex<HashMap<String, rooms::Room>> = Mutex::new(HashMap::new());
/// String is the room list /// String is the room list
static ref PLAYS: Mutex<HashMap<String, Vec<rooms::Board>>> = Mutex::new(HashMap::new()); static ref BOARDS: RwLock<HashMap<String, Vec<rooms::Board>>> = RwLock::new(HashMap::new());
} }
/* /*
@ -64,7 +62,7 @@ async fn index(mut roomname: String) -> Template {
print!("{}", roomname) print!("{}", roomname)
} }
// lock list of rooms // lock list of rooms
let mut rooms = ROOMS.lock().unwrap(); let mut rooms = ROOMS.lock().await;
// do this if there are already people in the room // do this if there are already people in the room
// first let the new player join and then lock the room for others // first let the new player join and then lock the room for others
if rooms.contains_key(&roomname) { if rooms.contains_key(&roomname) {
@ -96,7 +94,7 @@ async fn index(mut roomname: String) -> Template {
#[get("/room?<roomname>&<token>&<player1>")] #[get("/room?<roomname>&<token>&<player1>")]
async fn room(roomname: &str, token: usize, player1: bool) -> Template { async fn room(roomname: &str, token: usize, player1: bool) -> Template {
// lock room list mutex, don't forget to drop again // lock room list mutex, don't forget to drop again
let rooms = ROOMS.lock().unwrap(); let rooms = ROOMS.lock().await;
if rooms.contains_key(roomname) { if rooms.contains_key(roomname) {
let room = rooms.get(roomname).unwrap(); let room = rooms.get(roomname).unwrap();
// the room is real and the token is correct // the room is real and the token is correct
@ -122,15 +120,27 @@ async fn room(roomname: &str, token: usize, player1: bool) -> Template {
//* //*
#[get("/stream/<roomname>/<token>")] #[get("/stream/<roomname>/<token>")]
async fn stream(roomname: String, token: usize) -> EventStream![] { async fn stream(roomname: String, token: usize) -> EventStream![] {
let roomlist = ROOMS.lock().unwrap(); let mut events_read: usize = 1;
drop(roomlist);
EventStream! { EventStream! {
loop { loop {
// TODO sleep(Duration::from_secs(3)).await;
// 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
yield Event::json(&"oof".to_string()); // TODO this should break if sending a message fails
let boards_r = BOARDS.read().await;
if boards_r.contains_key(&roomname) {
let board_r = boards_r.get(&roomname).unwrap();
if board_r.len() > events_read {
let rooms = ROOMS.lock().await;
if rooms::room_and_token_correct(&rooms, &roomname, &token){
drop(rooms);
let event = board_r.last();
yield Event::json(&event);
events_read += 1;
} else {continue}
} else {continue}
} else {continue}
} }
} }
} }
@ -139,16 +149,16 @@ async fn stream(roomname: String, token: usize) -> EventStream![] {
// 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) { async 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().await;
// check if room is legal // check if room is legal
if rooms::room_and_token_correct(&roomlist, &roomname, &token) { if rooms::room_and_token_correct(&roomlist, &roomname, &token) {
let board = &roomlist.get_mut(roomname).unwrap().board; let board = &roomlist.get_mut(roomname).unwrap().board;
if board.play_is_legal(&x, &y, &player1) { if board.play_is_legal(&x, &y, &player1) {
// change the board by making a new play // change the board by making a new play
board.make_play(&x, &y); board.make_play(&x, &y);
let mut playlist = PLAYS.lock().unwrap(); let mut playlist = BOARDS.write().await;
// 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();

View file

@ -3,7 +3,6 @@
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="/style.css" media="all" /> <link rel="stylesheet" type="text/css" href="/style.css" media="all" />
<script src="/script.js" charset="utf-8" defer></script>
<title>Reversi Online</title> <title>Reversi Online</title>
<meta <meta
charset="utf-8" charset="utf-8"