From cc2eb4ef053da7a53663bdecac912a1c49490301 Mon Sep 17 00:00:00 2001 From: stale Date: Fri, 1 Jul 2022 22:14:52 -0300 Subject: fix the mess i made of type aliases --- src/main.rs | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index abb2e48..eec9cf8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,7 +51,7 @@ fn main() -> Result<(), Box> { #[tokio::main] async fn tokio_main(conf: Conf) -> Result<(), Box> { let conf = Arc::new(conf); - let rooms: RoomMap = Arc::new(RwLock::new(HashMap::new())); + let rooms = Arc::new(RwLock::new(RoomMap::new())); let public_rooms = Arc::new(RwLock::new(HashMap::new())); use warp::*; @@ -60,6 +60,7 @@ async fn tokio_main(conf: Conf) -> Result<(), Box> { let listing = { let rooms = rooms.clone(); let pubs = public_rooms.clone(); + path!("rlist").and_then(move || { let rooms = rooms.clone(); let pubs = pubs.clone(); @@ -91,8 +92,9 @@ async fn tokio_main(conf: Conf) -> Result<(), Box> { let r = rooms.clone(); let conf = conf.clone(); async move { - let empty_len = empty_rooms(r.clone()).await.len(); - let space = conf.limits.room_slots - r.read().await.len() + empty_len; + let r = r.read().await; + let empty_len = empty_rooms(&r).await.len(); + let space = conf.limits.room_slots - r.len() + empty_len; Ok::(space.to_string()) } }) @@ -101,6 +103,7 @@ async fn tokio_main(conf: Conf) -> Result<(), Box> { let rooms = rooms.clone(); let pubs = public_rooms.clone(); let conf = conf.clone(); + post().and(path("r")).and(body::content_length_limit(conf.limits.form_size)).and(body::form()) .and_then(move |rinfo: HashMap| { println!("{:?}", rinfo); @@ -109,10 +112,12 @@ async fn tokio_main(conf: Conf) -> Result<(), Box> { let conf = conf.clone(); async move { let slots_available = conf.limits.room_slots - rooms.read().await.len(); - let empty = empty_rooms(rooms.clone()).await; + let empty = empty_rooms(&*rooms.read().await).await; if slots_available < 1 { if slots_available + empty.len() > 0 { - remove_room(rooms.clone(), pubs.clone(), empty[0].clone()).await; + let mut roomsl = rooms.write().await; + let mut pubsl = pubs.write().await; + remove_room(&mut *roomsl, &mut *pubsl, empty[0].clone()); } else { return Err(reject::custom(NoRoomSlots)); } @@ -204,7 +209,7 @@ async fn tokio_main(conf: Conf) -> Result<(), Box> { } // If a move is made, broadcast new board, else just send current board -async fn gameloop(mut move_rx: tokio::sync::mpsc::UnboundedReceiver, players: PlayerMapData, bconf: minesweeper::BoardConf) { +async fn gameloop(mut move_rx: tokio::sync::mpsc::UnboundedReceiver, players: Arc>, bconf: minesweeper::BoardConf) { // FIXME: push new board if and only if there aren't any remaining commands in the queue use minesweeper::*; use flate2::{ Compression, write::DeflateEncoder }; @@ -275,9 +280,8 @@ async fn error_handler(err: Rejection) -> Result Vec { - let rl = rooms.read().await; - futures::stream::iter(rl.iter()) +async fn empty_rooms(rooms: &RoomMap) -> Vec { + futures::stream::iter(rooms.iter()) .filter_map(|(id,roomarc)| async move { let rrl = roomarc.read().await; let rrrl = rrl.players.read().await; @@ -305,7 +309,7 @@ fn room_from_form(uid: RoomId, rinfo: &HashMap, conf: &Conf) -> R if n.is_empty() { uid.to_string() } else { n } }; - let players = PlayerMap::default(); + let players = Arc::new(RwLock::new(PlayerMap::default())); let (cmd_tx, cmd_rx) = tokio::sync::mpsc::unbounded_channel(); let game_handle = tokio::spawn(gameloop(cmd_rx, players.clone(), board_conf)); @@ -330,14 +334,8 @@ fn room_from_form(uid: RoomId, rinfo: &HashMap, conf: &Conf) -> R } else { Err(warp::reject::custom(BadFormData)) } } -async fn remove_room(rooms: RoomMap, pubs: Arc>>, id: RoomId) { - { - let mut rwl = rooms.write().await; - rwl.remove(&id); - } - { - let mut pwl = pubs.write().await; - pwl.remove(&id); - } +fn remove_room(rooms: &mut RoomMap, pubs: &mut HashMap, id: RoomId) { + rooms.remove(&id); + pubs.remove(&id); } -- cgit v1.2.3