From fae0fca7aabb81325a296a1d6202239c3db44b60 Mon Sep 17 00:00:00 2001 From: stale Date: Fri, 20 May 2022 06:39:58 -0300 Subject: wip room support --- src/types.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'src/types.rs') diff --git a/src/types.rs b/src/types.rs index fb9f7ae..df9e168 100644 --- a/src/types.rs +++ b/src/types.rs @@ -16,8 +16,8 @@ use crate::minesweeper; pub struct Config { pub cert: String, pub pkey: String, + pub index_pg: String, pub room_pg: String, - pub form_pg: String, pub client_code: String, pub stylesheet: String, pub socket_addr: SocketAddr, @@ -41,7 +41,8 @@ impl Display for BoardConf { pub struct Room { pub name: String, pub players: PlayerMap, - pub peer_limit: u32, + pub peer_limit: usize, + pub public: bool, pub driver: tokio::task::JoinHandle<()>, pub cmd_stream: CmdTx, pub board_conf: BoardConf, @@ -75,7 +76,29 @@ impl Display for Player { } } +#[derive(Eq, PartialEq, Hash, Debug, Clone)] +pub struct RoomId(pub String); +impl Display for RoomId { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} + +impl RoomId { + pub fn new_in(map: &HashMap) -> Self { + use rand::{ thread_rng, Rng, distributions::Alphanumeric }; + let id = RoomId { 0: thread_rng() + .sample_iter(&Alphanumeric) + .take(16) + .map(char::from) + .collect::() }; + if map.contains_key(&id) { RoomId::new_in(map) } + else { id } + } +} + pub type CmdTx = tokio::sync::mpsc::UnboundedSender; +pub type RoomMap = Arc>>>>; pub type PlayerMapData = Arc>>; #[derive(Debug)] pub struct PlayerMap { -- cgit v1.2.3