diff --git a/server/src/game.rs b/server/src/game.rs index bae14e5..95bc97d 100644 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -1,5 +1,6 @@ use crate::AppState; use crate::User; +use lib::*; use rand::prelude::IteratorRandom; use rand::thread_rng; use std::{ @@ -124,6 +125,51 @@ impl Game { } } + /// Process player move + pub fn player_move( + &mut self, + request: PlayerMoveRequest, + player_user_id: String, + ) -> Result, String> { + if self.czar.read().unwrap().uuid == player_user_id { + tracing::debug!("Player is czar"); + Err("You can't submit cards to judge, you ARE the judge!".to_string()) + } else { + // Ignore extra cards + let trimmed = request.card_ids[..self.current_black.pick.into()].to_vec(); + // TODO: handle not enough cards + tracing::debug!("Trimmed: {:#?}", trimmed); + // Put cards into judge pool + self.judge_pool.insert(trimmed, player_user_id.clone()); + + // Start judging if this is last player to submit + if self.judge_pool.len() == self.players.len() - 1 { + Ok(Some(( + JudgeRound { + cards_to_judge: self + .judge_pool + .keys() + .into_iter() + .map(|group| { + group + .iter() + .map(|id| WhiteCardMeta { + uuid: id.to_string(), + text: "test".to_string(), + }) + .collect() + }) + .collect(), + }, + self.czar.read().unwrap().uuid.clone(), + ))) + } else { + // User submitted cards + Ok(None) + } + } + } + /// Tick forward pub fn finish_round(&mut self, winner_id: String) { self.players @@ -133,6 +179,7 @@ impl Game { .push(self.current_black.clone()); self.current_black = self.draw_one_black().unwrap(); + self.judge_pool.clear(); } /// Draw one black card at random from play deck. diff --git a/server/src/game_handler.rs b/server/src/game_handler.rs index e86eb8b..4282933 100644 --- a/server/src/game_handler.rs +++ b/server/src/game_handler.rs @@ -119,7 +119,7 @@ impl GameHandler { /// Process player move request async fn handle_player_move(&self, request: PlayerMoveRequest, addr: SocketAddr) { // Get pointers - let this_user_id = self + let player_user_id = self .state .online_users .read() @@ -142,79 +142,29 @@ impl GameHandler { // Do the stuff - // Check if player is currently Czar - if this_game - .read() + match this_game + .write() .unwrap() - .czar - .read() - .unwrap() - .uuid - .to_string() - == this_user_id + .player_move(request, player_user_id) { - // Tell player no - let message = ChatMessage { - text: "You can't submit cards to judge, you ARE the judge!".to_string(), - }; - let _ = self - .state - .users_tx - .send(DmUser(SendChatMessage(message), Addr(addr))) - .await; - } else { - // Ignore extra cards - let current_round_pick: usize = this_game.read().unwrap().current_black.pick.into(); - // TODO: handle not enough cards submitted - let trimmed = &request.card_ids[..current_round_pick]; - - // Put cards into game judge pool - this_game - .write() - .unwrap() - .judge_pool - .insert(trimmed.to_vec(), this_user_id.clone()); - - // Check if this is the last player to submit - if this_game.read().unwrap().judge_pool.len() - == this_game.read().unwrap().players.len() - 1 - { - let message = SendJudgeRound(JudgeRound { - cards_to_judge: this_game - .read() - .unwrap() - .judge_pool - .keys() - .into_iter() - .map(|group| { - group - .iter() - .map(|id| WhiteCardMeta { - uuid: self - .state - .white_cards_by_id - .get(id) - .unwrap() - .uuid - .to_string(), - text: self - .state - .white_cards_by_id - .get(id) - .unwrap() - .text - .clone(), - }) - .collect() - }) - .collect(), - }); - - tracing::debug!("send for judging"); - let czar_id = this_game.read().unwrap().czar.read().unwrap().uuid.clone(); - let _ = self.state.users_tx.send(DmUser(message, Id(czar_id))).await; + Err(err) => { + let message = ChatMessage { text: err }; + let tx = self.state.users_tx.clone(); + tokio::spawn(async move { + let _ = tx.send(DmUser(SendChatMessage(message), Addr(addr))).await; + }) } - } + Ok(None) => tokio::spawn(async move { tracing::debug!("None") }), + Ok(Some((judge_round, czar_id))) => { + let tx = self.state.users_tx.clone(); + + tokio::spawn(async move { + let _ = tx + .send(DmUser(SendJudgeRound(judge_round), Id(czar_id))) + .await; + }) + } + }; } /// Puts a user in a game