diff options
author | stale <redkugelblitzin@gmail.com> | 2022-05-31 04:04:53 -0300 |
---|---|---|
committer | stale <redkugelblitzin@gmail.com> | 2022-05-31 04:04:53 -0300 |
commit | d9eededb736e4ada13ee6e7fe517adcc6a606f88 (patch) | |
tree | 726e1e2a335cab290963dbd547e0dd0093de3f99 /src/main.rs | |
parent | 746a810eea15e52f7d1b17778239825daad90e01 (diff) |
room limits and player counts
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 52 |
1 files changed, 48 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index 66b2cfd..36ee5ce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use std::{ collections::HashMap, num::NonZeroUsize, }; +use futures::stream::StreamExt; mod types; mod conn; @@ -15,6 +16,7 @@ use tokio::sync::RwLock; const FONT_FILE: &[u8] = include_bytes!("../assets/VT323-Regular.ttf"); const AREA_LIMIT: usize = 150*150; +const ROOM_LIMIT: usize = 8; fn main() -> Result<(), Box<dyn Error>> { let conf = Config { @@ -41,15 +43,45 @@ async fn tokio_main(conf: Config) -> Result<(), Box<dyn Error>> { let code = path!("c.js").and(fs::file(conf.client_code.clone())); let font = path!("f.ttf").map(|| FONT_FILE); let listing = { + let rooms = rooms.clone(); let pubs = public_rooms.clone(); path!("rlist").and_then(move || { + let rooms = rooms.clone(); let pubs = pubs.clone(); async move { - let map = pubs.read().await; + let roomsl = rooms.read().await; + let pubsl = pubs.read().await; + let rooms_pcount = futures::stream::iter(pubsl.iter()) + .then(|(id, _):(&RoomId,_)| { + let roomsl = roomsl.clone(); + async move { + (id.clone(), roomsl.get(id).unwrap().read().await.players.read().await.len()) + } + }) + .collect::<HashMap<RoomId,_>>().await; + let resp = (&*pubsl, rooms_pcount); Ok::<_,std::convert::Infallible>( - reply::json(&*map) - ) - }}) + reply::json(&resp) + ) + } + }) + }; + let roomspace = { + let rooms = rooms.clone(); + + path!("rspace").and_then(move || { + let r = rooms.clone(); + async move { + let empty_len = empty_rooms(r.clone()).await.len(); + let space = ROOM_LIMIT - r.read().await.len() + empty_len; + Ok::<_,std::convert::Infallible>( + hyper::Response::builder() + .status(hyper::StatusCode::OK) + .body(hyper::Body::from(space.to_string())) + .unwrap() + ) + } + }) }; let rform_recv = { let rooms = rooms.clone(); @@ -159,6 +191,7 @@ async fn tokio_main(conf: Config) -> Result<(), Box<dyn Error>> { .or(code) .or(font) .or(listing) + .or(roomspace) .or(rform_recv) .or(room) .recover(error_handler); @@ -238,3 +271,14 @@ async fn error_handler(err: Rejection) -> Result<impl Reply, std::convert::Infal } } +async fn empty_rooms(rooms: RoomMap) -> Vec<RoomId> { + let rl = rooms.read().await; + futures::stream::iter(rl.iter()) + .filter_map(|(id,roomarc)| async move { + let rrl = roomarc.read().await; + let rrrl = rrl.players.read().await; + if rrrl.len() == 0 { Some(id.clone()) } else { None } + }) + .collect::<Vec<RoomId>>().await +} + |