diff options
author | stale <redkugelblitzin@gmail.com> | 2022-05-20 06:39:58 -0300 |
---|---|---|
committer | stale <redkugelblitzin@gmail.com> | 2022-05-20 06:39:58 -0300 |
commit | fae0fca7aabb81325a296a1d6202239c3db44b60 (patch) | |
tree | 687a95f381ee13ee3de9f8eabdf4cd371a258091 /src/types.rs | |
parent | d89b9ea43ac65af549279f6e86c68c66243dcdf3 (diff) |
wip room support
Diffstat (limited to 'src/types.rs')
-rw-r--r-- | src/types.rs | 27 |
1 files changed, 25 insertions, 2 deletions
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<T>(map: &HashMap<RoomId, T>) -> Self { + use rand::{ thread_rng, Rng, distributions::Alphanumeric }; + let id = RoomId { 0: thread_rng() + .sample_iter(&Alphanumeric) + .take(16) + .map(char::from) + .collect::<String>() }; + if map.contains_key(&id) { RoomId::new_in(map) } + else { id } + } +} + pub type CmdTx = tokio::sync::mpsc::UnboundedSender<MetaMove>; +pub type RoomMap = Arc<RwLock<HashMap<RoomId, Arc<RwLock<Room>>>>>; pub type PlayerMapData = Arc<RwLock<HashMap<SocketAddr, Player>>>; #[derive(Debug)] pub struct PlayerMap { |