diff --git a/Cargo.lock b/Cargo.lock index 72c4d83..67fdc90 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1741,6 +1741,7 @@ dependencies = [ "tower-http", "tracing", "tracing-subscriber", + "uuid", ] [[package]] @@ -2297,6 +2298,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314" dependencies = [ "getrandom", + "rand", + "serde", ] [[package]] diff --git a/server/Cargo.toml b/server/Cargo.toml index 24ffe60..14bc2d1 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -18,3 +18,4 @@ anyhow = "1.0.82" tower-http = { version = "0.5.2", features = ["fs", "trace"] } lib = { workspace = true } +uuid = { version = "1.10.0", features = ["v7", "serde", "fast-rng"] } diff --git a/server/src/game_handler.rs b/server/src/game_handler.rs index cb84873..2205f61 100644 --- a/server/src/game_handler.rs +++ b/server/src/game_handler.rs @@ -89,11 +89,25 @@ impl GameHandler { players: new_game_object .players .iter() - .map(|player| player.user.read().unwrap().name.clone()) + .map(|player| { + self.state + .user_uuid + .read() + .unwrap() + .get(&player.0) + .unwrap() + .read() + .unwrap() + .name + .clone() + }) .collect(), czar: new_game_object.host.read().unwrap().name.clone(), black: black_text, white: new_game_object + .players + .get(&new_game_object.host.read().unwrap().uuid) + .unwrap() .white .iter() .map(|card| card.text.clone()) diff --git a/server/src/lib.rs b/server/src/lib.rs index d5da201..c9b6221 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -17,6 +17,7 @@ use std::{ use tokio::sync::mpsc::Sender; use tokio::sync::{broadcast, mpsc}; use user_handler::UserHandlerMessage; +use uuid::Uuid; pub mod game_handler; pub mod message_handler; pub mod user_handler; @@ -25,6 +26,7 @@ pub mod websocket; /// User #[derive(Debug)] pub struct User { + pub uuid: Uuid, pub name: String, pub tx: Sender, } @@ -32,7 +34,11 @@ pub struct User { impl User { /// Create a new user object from incoming data pub fn new(name: String, tx: Sender) -> User { - User { name, tx } + User { + uuid: Uuid::now_v7(), + name, + tx, + } } pub fn change_name(&mut self, new_name: String) { @@ -99,8 +105,8 @@ pub struct NewGameManifest { /// A struct that represents a player #[derive(Debug)] pub struct Player { - /// Player's username - pub user: Arc>, + /// Player's user's uuid + pub user_uuid: Uuid, /// The player's hand pub white: Vec, /// The player's wins @@ -125,7 +131,7 @@ pub struct Game { /// Indicates game active/game over pub game_active: bool, /// List of current players - pub players: Vec, + pub players: HashMap, // /// Reference to current card czar // czar: &Player, /// Black card for the current round @@ -157,7 +163,7 @@ impl Game { white_discard: vec![], black_discard: vec![], game_active: false, - players: vec![], + players: HashMap::new(), current_black: Option::None, }; tracing::debug!( @@ -228,7 +234,7 @@ impl Game { /// Create a new player and add them to the game. pub fn create_player(&mut self, user: Arc>) -> Result<()> { let mut new_player = Player { - user: user.clone(), + user_uuid: user.read().unwrap().uuid.clone(), white: vec![], black: vec![], }; @@ -244,7 +250,8 @@ impl Game { new_player.white.extend(hand_buf); - self.players.push(new_player); + self.players + .insert(user.read().unwrap().uuid.clone(), new_player); Ok(()) } @@ -369,6 +376,7 @@ pub struct AppState { pub first_names: Vec, pub last_names: Vec, pub reserved_names: RwLock>, + pub user_uuid: RwLock>>>, pub online_users: RwLock>>>, pub offline_users: RwLock>>>, pub packs: CardPacks, diff --git a/server/src/main.rs b/server/src/main.rs index 5c2b856..8e49896 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -13,6 +13,7 @@ use tokio::sync::{broadcast, mpsc}; use tower_http::services::ServeDir; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use user_handler::UserHandler; +use uuid::Uuid; #[tokio::main] async fn main() -> Result<()> { @@ -33,6 +34,7 @@ async fn main() -> Result<()> { let first_names = load_names("data/first.txt")?; let last_names = load_names("data/last.txt")?; let reserved_names = RwLock::new(HashSet::::new()); + let user_uuid = RwLock::new(HashMap::>>::new()); let online_users = RwLock::new(HashMap::>>::new()); let offline_users = RwLock::new(HashMap::>>::new()); let (packs, packs_meta) = load_cards_from_json("data/cah-cards-full.json")?; @@ -46,6 +48,7 @@ async fn main() -> Result<()> { first_names, last_names, reserved_names, + user_uuid, online_users, offline_users, packs, diff --git a/server/src/user_handler.rs b/server/src/user_handler.rs index dffa1fb..82a21e0 100644 --- a/server/src/user_handler.rs +++ b/server/src/user_handler.rs @@ -41,7 +41,14 @@ impl UserHandler { // Notify client of new username tx.send(user_client_self_update(&new_user)).await.unwrap(); - // Register using `addr` as key until something longer lived exists + // Register uuid + self.state + .user_uuid + .write() + .unwrap() + .insert(new_user.read().unwrap().uuid.clone(), new_user.clone()); + + // Register online using `addr` as key until something longer lived exists self.state .online_users .write()