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::response::stream::{Event, EventStream};
use rocket::tokio::sync::{Mutex, RwLock};
use rocket_dyn_templates::Template;
use rocket::tokio::time::{sleep, Duration};
use std::{
collections::HashMap,
mem::drop,
sync::{mpsc, Mutex, RwLock},
};
use std::{collections::HashMap, mem::drop};
mod names;
mod rooms;
@ -23,7 +21,7 @@ lazy_static! {
static ref ARGS: Args = Args::parse();
static ref ROOMS: Mutex<HashMap<String, rooms::Room>> = Mutex::new(HashMap::new());
/// 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)
}
// 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
// first let the new player join and then lock the room for others
if rooms.contains_key(&roomname) {
@ -96,7 +94,7 @@ async fn index(mut roomname: String) -> Template {
#[get("/room?<roomname>&<token>&<player1>")]
async fn room(roomname: &str, token: usize, player1: bool) -> Template {
// 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) {
let room = rooms.get(roomname).unwrap();
// 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>")]
async fn stream(roomname: String, token: usize) -> EventStream![] {
let roomlist = ROOMS.lock().unwrap();
drop(roomlist);
let mut events_read: usize = 1;
EventStream! {
loop {
// TODO
sleep(Duration::from_secs(3)).await;
// 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
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
//*
#[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
let mut roomlist = ROOMS.lock().unwrap();
let mut roomlist = ROOMS.lock().await;
// check if room is legal
if rooms::room_and_token_correct(&roomlist, &roomname, &token) {
let board = &roomlist.get_mut(roomname).unwrap().board;
if board.play_is_legal(&x, &y, &player1) {
// change the board by making a new play
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 !(playlist.contains_key(roomname)) {
let rn = roomname.to_string();

View File

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