diff --git a/server/src/game.rs b/server/src/game.rs index 95bc97d..fa4a3a6 100644 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -64,15 +64,20 @@ pub struct Game { pub czar: Arc>, /// Packs selected for this game pub packs: Vec, - /// White draw pile - pub white: Vec>, - /// Black draw pile - pub black: Vec>, + /// List of players in the game pub players: HashMap, /// Black card for the current round pub current_black: Arc, /// Judge pool - pub judge_pool: HashMap, String>, + pub judge_pile_meta: HashMap, String>, + /// Judge pile that contains cards + judge_pile: HashMap>, + /// White draw pile + white: Vec>, + /// White discard pile + white_discard: Vec>, + /// Black draw pile + black: Vec>, } impl Game { @@ -121,7 +126,9 @@ impl Game { players: HashMap::new(), current_black, packs: request.packs.clone(), - judge_pool: HashMap::new(), + judge_pile_meta: HashMap::new(), + judge_pile: HashMap::new(), + white_discard: vec![], } } @@ -139,23 +146,48 @@ impl Game { 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()); + + // Move card from player's hand to judge pile + for id in &trimmed { + let index = self + .players + .get(&player_user_id) + .unwrap() + .white + .iter() + .position(|card| card.uuid.to_string() == *id) + .unwrap(); + + let card = self + .players + .get_mut(&player_user_id) + .unwrap() + .white + .remove(index); + + self.judge_pile.insert(card.uuid.to_string(), card); + } + + // Meta for convenience + self.judge_pile_meta.insert(trimmed, player_user_id.clone()); // Start judging if this is last player to submit - if self.judge_pool.len() == self.players.len() - 1 { + if self.judge_pile_meta.len() == self.players.len() - 1 { Ok(Some(( JudgeRound { cards_to_judge: self - .judge_pool + .judge_pile_meta .keys() .into_iter() .map(|group| { group .iter() - .map(|id| WhiteCardMeta { - uuid: id.to_string(), - text: "test".to_string(), + .map(|id| { + let card = self.judge_pile.get(id).unwrap().clone(); + WhiteCardMeta { + uuid: card.uuid.to_string(), + text: card.text.clone(), + } }) .collect() }) @@ -172,14 +204,35 @@ impl Game { /// Tick forward pub fn finish_round(&mut self, winner_id: String) { + // Top off player hands + let user_ids: Vec = self.judge_pile_meta.drain().map(|pair| pair.1).collect(); + + for id in user_ids { + for _ in 0..self.current_black.pick.into() { + let card = self.draw_one_white().unwrap(); + let player = self.players.get_mut(&id).unwrap(); + + player.white.push(card); + } + } + + // Award card/point to winning player self.players .get_mut(&winner_id) .unwrap() .black .push(self.current_black.clone()); + // Draw new black card self.current_black = self.draw_one_black().unwrap(); - self.judge_pool.clear(); + + // End of round clean-up + self.white_discard.extend( + self.judge_pile + .drain() + .map(|pair| pair.1) + .collect::>>(), + ); } /// Draw one black card at random from play deck. diff --git a/server/src/game_handler.rs b/server/src/game_handler.rs index 4282933..d1896f0 100644 --- a/server/src/game_handler.rs +++ b/server/src/game_handler.rs @@ -101,7 +101,7 @@ impl GameHandler { let winning_player_id = this_game .read() .unwrap() - .judge_pool + .judge_pile_meta .get(&request.winning_cards) .unwrap() .clone(); @@ -141,7 +141,6 @@ impl GameHandler { .clone(); // Do the stuff - match this_game .write() .unwrap()