From f7ffb5bb206ef4dbdac9fc7b078581e3e80e41e8 Mon Sep 17 00:00:00 2001 From: Adam <24621027+adoyle0@users.noreply.github.com> Date: Sun, 25 Aug 2024 17:32:04 -0400 Subject: [PATCH] let game handle more of its own logic --- lib/src/lib.rs | 2 +- server/src/game.rs | 67 ++++++++++++++++++++++++-------------- server/src/game_handler.rs | 47 +++++--------------------- 3 files changed, 53 insertions(+), 63 deletions(-) diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 918ebdf..6268cca 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; /// Judge decision -#[derive(Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct JudgeDecisionRequest { pub game_id: String, pub winning_cards: Vec, diff --git a/server/src/game.rs b/server/src/game.rs index fbb9d3e..37c90f8 100644 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -138,6 +138,22 @@ impl Game { } } + /// Judge Game + pub fn judge_round(&mut self, request: JudgeDecisionRequest, player_user_id: String) { + // Check if player is czar + if self.czar.read().unwrap().uuid.to_string() == player_user_id { + let winner_id = self + .judge_pile_meta + .get(&request.winning_cards) + .unwrap() + .clone(); + + self.end_round(winner_id) + } else { + tracing::error!("Player is not czar!"); + } + } + /// Process player move pub fn player_move( &mut self, @@ -179,6 +195,7 @@ impl Game { } // Meta for convenience + // TODO: don't do this extra work self.judge_pile_meta.insert(trimmed, player_user_id.clone()); // Start judging if this is last player to submit @@ -207,13 +224,14 @@ impl Game { ))) } else { // User submitted cards + // TODO: player submitted move indication Ok(None) } } } /// Tick forward - pub fn finish_round(&mut self, winner_id: String) { + fn end_round(&mut self, winner_id: String) { // Top off player hands let user_ids: Vec = self.judge_pile_meta.drain().map(|pair| pair.1).collect(); @@ -244,16 +262,12 @@ impl Game { .collect::>>(), ); - tracing::debug!("{:#?}", self.rotation); - tracing::debug!("{:#?}", self.rotation_index); // Choose new czar if self.rotation_index == self.rotation.len() - 1 { self.rotation_index = 0; } else { self.rotation_index += 1; } - tracing::debug!("{:#?}", self.rotation); - tracing::debug!("{:#?}", self.rotation_index); self.czar = self .players .get(&self.rotation[self.rotation_index]) @@ -288,26 +302,31 @@ impl Game { /// Create a new player and add them to the game. pub fn create_player(&mut self, user: Arc>) { - // Build hand of 10 white cards - let mut white = vec![]; - for _ in 0..10 { - if let Some(card) = self.draw_one_white() { - white.push(card); + // Check if player already exists + if !self.players.contains_key(&user.read().unwrap().uuid) { + // Build hand of 10 white cards + let mut white = vec![]; + for _ in 0..10 { + if let Some(card) = self.draw_one_white() { + white.push(card); + } } + + // New player object + let new_player = Player { + user: user.clone(), + white, + black: vec![], + }; + + // Add player to game + self.players + .insert(user.read().unwrap().uuid.clone(), new_player); + + // Add player to rotation + self.rotation.push(user.read().unwrap().uuid.to_string()); + } else { + tracing::debug!("User already has a player in this game!"); } - - // New player object - let new_player = Player { - user: user.clone(), - white, - black: vec![], - }; - - // Add player to game - self.players - .insert(user.read().unwrap().uuid.clone(), new_player); - - // Add player to rotation - self.rotation.push(user.read().unwrap().uuid.to_string()); } } diff --git a/server/src/game_handler.rs b/server/src/game_handler.rs index b870fd6..eb28e0c 100644 --- a/server/src/game_handler.rs +++ b/server/src/game_handler.rs @@ -64,7 +64,7 @@ impl GameHandler { /// Process judging async fn handle_judging(&self, request: JudgeDecisionRequest, addr: SocketAddr) { // Get pointers - let this_user_id = self + let player_user_id = self .state .online_users .read() @@ -85,35 +85,14 @@ impl GameHandler { .unwrap() .clone(); - // Check if this player is czar - // Check if player is currently Czar - if this_game - .read() + // Send to game + this_game + .write() .unwrap() - .czar - .read() - .unwrap() - .uuid - .to_string() - == this_user_id - { - // Find user who submitted the card - let winning_player_id = this_game - .read() - .unwrap() - .judge_pile_meta - .get(&request.winning_cards) - .unwrap() - .clone(); + .judge_round(request.clone(), player_user_id.clone()); - this_game - .write() - .unwrap() - .finish_round(winning_player_id.clone()); - - let this_game_id = this_game.read().unwrap().uuid.to_string(); - self.send_game_state_update(this_game_id).await - } + let this_game_id = this_game.read().unwrap().uuid.to_string(); + self.send_game_state_update(this_game_id).await } /// Process player move request @@ -179,16 +158,8 @@ impl GameHandler { .unwrap() .clone(); - // Check if player already exists - if !this_game - .read() - .unwrap() - .players - .contains_key(&this_user.read().unwrap().uuid) - { - // Create player - this_game.write().unwrap().create_player(this_user); - } + // Create player + this_game.write().unwrap().create_player(this_user); // Send updates for all players self.send_game_state_update(id).await;