diff --git a/server/src/game_handler.rs b/server/src/game_handler.rs index 4392e63..323ff35 100644 --- a/server/src/game_handler.rs +++ b/server/src/game_handler.rs @@ -107,93 +107,86 @@ impl GameHandler { return; } + // Get host pointer + let host = self + .state + .online_users + .read() + .unwrap() + .get(&addr) + .unwrap() + .clone(); + + // Create manifest let manifest = NewGameManifest { name: new_game.name, - host: self - .state - .online_users - .read() - .unwrap() - .get(&addr) - .unwrap() - .clone(), + host: host.clone(), packs: new_game.packs, }; - // Create game - if let Ok(new_game_object) = Game::new(self.state.clone(), manifest) { - let tx = self - .state - .online_users - .read() + // Create game using manifest + let new_game_object = Game::new(self.state.clone(), manifest); + + // Create update for user's game view + let meta = GameStateMeta { + uuid: new_game_object.uuid.to_string(), + name: new_game_object.name.clone(), + host: new_game_object.host.read().unwrap().name.clone(), + players: new_game_object + .players + .iter() + .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: ( + new_game_object.current_black.text.clone(), + new_game_object.current_black.pick, + ), + white: new_game_object + .players + .get(&new_game_object.host.read().unwrap().uuid) .unwrap() - .get(&addr) - .unwrap() - .read() - .unwrap() - .tx - .clone(); + .white + .iter() + .map(|card| card.text.clone()) + .collect(), + packs: new_game_object.packs.clone(), + }; - // Create update for user's game view - let meta = GameStateMeta { - uuid: new_game_object.uuid.to_string(), - name: new_game_object.name.clone(), - host: new_game_object.host.read().unwrap().name.clone(), - players: new_game_object - .players - .iter() - .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: ( - new_game_object.current_black.text.clone(), - new_game_object.current_black.pick, - ), - white: new_game_object - .players - .get(&new_game_object.host.read().unwrap().uuid) - .unwrap() - .white - .iter() - .map(|card| card.text.clone()) - .collect(), - packs: new_game_object.packs.clone(), - }; + // Send user's update + let tx = host.read().unwrap().tx.clone(); + tx.send(serde_json::to_string(&meta).unwrap()) + .await + .unwrap(); - // Send user's update - tx.send(serde_json::to_string(&meta).unwrap()) - .await - .unwrap(); + // Add game to active list + self.state.games.write().unwrap().insert( + new_game_object.uuid.to_string(), + Arc::new(RwLock::new(new_game_object)), + ); - // Add game to active list - self.state.games.write().unwrap().insert( - new_game_object.uuid.to_string(), - Arc::new(RwLock::new(new_game_object)), - ); + // Broadcast game browser update + self.state + .broadcast_tx + .send(meta_games_browser_update(&self.state)) + .unwrap(); - // Broadcast game browser update - self.state - .broadcast_tx - .send(meta_games_browser_update(&self.state)) - .unwrap(); - - // Broadcast server meta update - self.state - .broadcast_tx - .send(meta_server_summary_update(&self.state)) - .unwrap(); - } + // Broadcast server meta update + self.state + .broadcast_tx + .send(meta_server_summary_update(&self.state)) + .unwrap(); } } @@ -288,7 +281,7 @@ pub struct Game { } impl Game { - fn new(state: Arc, request: NewGameManifest) -> Result { + fn new(state: Arc, request: NewGameManifest) -> Self { tracing::debug!( "Creating game {} with {} as host", &request.name, @@ -320,7 +313,7 @@ impl Game { white.shrink_to_fit(); black.shrink_to_fit(); - Ok(Game { + Game { uuid: Uuid::now_v7(), name: request.name, host: request.host.clone(), @@ -329,7 +322,7 @@ impl Game { players: HashMap::new(), current_black, packs: request.packs.clone(), - }) + } } /// Draw one white card at random from play deck.