From f65da8eaca5ed9a6be7d149a1ea2a7958a656388 Mon Sep 17 00:00:00 2001 From: Adam <24621027+adoyle0@users.noreply.github.com> Date: Thu, 22 Aug 2024 21:19:34 -0400 Subject: [PATCH] score round and draw new black card --- client/src/components/game.rs | 22 ++++++++++--- client/src/components/websocket.rs | 2 +- lib/src/lib.rs | 9 +++++- server/src/game_handler.rs | 52 ++++++++++++++++++++++++------ 4 files changed, 70 insertions(+), 15 deletions(-) diff --git a/client/src/components/game.rs b/client/src/components/game.rs index ce96565..57772c3 100644 --- a/client/src/components/game.rs +++ b/client/src/components/game.rs @@ -179,15 +179,29 @@ pub fn Game() -> impl IntoView { Players: - + + + + Name + Score + + {move || { game_players() .iter() - .map(|player| view! { {player} }) + .map(|player| { + view! { + + {&player.name} + + {&player.score.to_string()} + + + } + }) .collect_view() }} - - + diff --git a/client/src/components/websocket.rs b/client/src/components/websocket.rs index 87923f7..ae6fe37 100644 --- a/client/src/components/websocket.rs +++ b/client/src/components/websocket.rs @@ -100,7 +100,7 @@ pub fn Websocket() -> impl IntoView { } else if let Ok(judge_update) = from_str::(message) { set_judge_round(Some(judge_update)); } else { - logging::log!("Unhandled message: {}", message); + logging::log!("Unhandled message: {:#?}", message); } } }); diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 5d99664..c9a5bbd 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -39,13 +39,20 @@ pub struct WhiteCardMeta { pub text: String, } +/// Game Player Meta +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct GamePlayerMeta { + pub name: String, + pub score: usize, +} + /// Game meta #[derive(Clone, Debug, Serialize, Deserialize)] pub struct GameStateMeta { pub uuid: String, pub name: String, pub host: String, - pub players: Vec, + pub players: Vec, pub czar: String, pub black: (String, u8), pub white: Vec, diff --git a/server/src/game_handler.rs b/server/src/game_handler.rs index 72edafb..fb73bab 100644 --- a/server/src/game_handler.rs +++ b/server/src/game_handler.rs @@ -105,7 +105,7 @@ impl GameHandler { == this_user_id { // Find user who submitted the card - let winning_user_id = this_game + let winning_player_id = this_game .read() .unwrap() .judge_pool @@ -113,7 +113,13 @@ impl GameHandler { .unwrap() .clone(); - tracing::debug!("{:#?} Won the round!", winning_user_id); + 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 } } @@ -269,19 +275,24 @@ impl GameHandler { .unwrap() .clone(); + let players = this_game + .read() + .unwrap() + .players + .values() + .map(|player| GamePlayerMeta { + name: player.user.read().unwrap().name.clone(), + score: player.black.len(), + }) + .collect::>(); + for player in this_game.read().unwrap().players.values() { // Create update for user's game view let meta = GameStateMeta { uuid: game_id.clone(), name: this_game.read().unwrap().name.clone(), host: this_game.read().unwrap().host.read().unwrap().name.clone(), - players: this_game - .read() - .unwrap() - .players - .values() - .map(|player| player.user.read().unwrap().name.clone()) - .collect(), + players: players.clone(), czar: this_game.read().unwrap().host.read().unwrap().name.clone(), black: ( this_game.read().unwrap().current_black.text.clone(), @@ -525,6 +536,29 @@ impl Game { } } + /// Tick forward + pub fn finish_round(&mut self, winner_id: String) { + self.players + .get_mut(&winner_id) + .unwrap() + .black + .push(self.current_black.clone()); + + self.current_black = self.draw_one_black().unwrap(); + } + + /// Draw one black card at random from play deck. + fn draw_one_black(&mut self) -> Option> { + let deck = &mut self.black; + + if let Some(index) = (0..deck.len()).choose(&mut thread_rng()) { + Some(deck.swap_remove(index)) + } else { + tracing::error!("Tried to draw white card that doesn't exist!"); + None + } + } + /// Draw one white card at random from play deck. fn draw_one_white(&mut self) -> Option> { let deck = &mut self.white;
Players: