From afaf695f2d2569722c45ed9390bf99e4ae29f771 Mon Sep 17 00:00:00 2001 From: Adam <24621027+adoyle0@users.noreply.github.com> Date: Wed, 14 Aug 2024 23:59:06 -0400 Subject: [PATCH] clean up game setup --- server/src/game_handler.rs | 120 +++++++++++++------------------------ 1 file changed, 41 insertions(+), 79 deletions(-) diff --git a/server/src/game_handler.rs b/server/src/game_handler.rs index adf9228..4392e63 100644 --- a/server/src/game_handler.rs +++ b/server/src/game_handler.rs @@ -54,7 +54,7 @@ impl GameHandler { .clone(); // Create player - this_game.write().unwrap().create_player(this_user).unwrap(); + this_game.write().unwrap().create_player(this_user); // Send updates for all players for player in this_game.read().unwrap().players.values() { @@ -289,58 +289,47 @@ pub struct Game { impl Game { fn new(state: Arc, request: NewGameManifest) -> Result { - let mut game = Game { - uuid: Uuid::now_v7(), - name: request.host.read().unwrap().name.clone(), - host: request.host.clone(), - white: vec![], - black: vec![], - players: HashMap::new(), - current_black: Arc::new(CardBlack { - text: "test".to_string(), - pack: 0u8, - pick: 0u8, - }), - packs: request.packs.clone(), - }; tracing::debug!( "Creating game {} with {} as host", &request.name, - request.host.read().unwrap().name + &request.host.read().unwrap().name ); - game.name = request.name; - game.host = request.host.clone(); - game.build_decks(state, request.packs)?; - game.create_player(request.host)?; - game.deal_black(); - - Ok(game) - } - - /// Build game decks from input data for game start. - /// This should only run once and at startup. - fn build_decks(&mut self, state: Arc, selected_packs: Vec) -> Result<()> { - // TODO: Make this right -- remove the clones, use references to single cards - for pack_num in selected_packs { + // Build the decks + let mut white = vec![]; + let mut black = vec![]; + for pack_num in &request.packs { if let Some(pack) = state.packs.official.get(&pack_num) { - if let Some(white) = &pack.white { - self.white.extend(white.clone()) + if let Some(card) = &pack.white { + white.extend(card.clone()) } - if let Some(black) = &pack.black { - self.black.extend(black.clone()) + if let Some(card) = &pack.black { + black.extend(card.clone()) } } else if let Some(pack) = state.packs.unofficial.get(&pack_num) { - if let Some(white) = &pack.white { - self.white.extend(white.clone()) + if let Some(card) = &pack.white { + white.extend(card.clone()) } - if let Some(black) = &pack.black { - self.black.extend(black.clone()) + if let Some(card) = &pack.black { + black.extend(card.clone()) } } } - Ok(()) + let current_black = black.swap_remove((0..black.len()).choose(&mut thread_rng()).unwrap()); + white.shrink_to_fit(); + black.shrink_to_fit(); + + Ok(Game { + uuid: Uuid::now_v7(), + name: request.name, + host: request.host.clone(), + white, + black, + players: HashMap::new(), + current_black, + packs: request.packs.clone(), + }) } /// Draw one white card at random from play deck. @@ -355,53 +344,26 @@ impl Game { } } - /// Draw one black card at random from play deck. - fn draw_one_black(&mut self) -> Option> { - let deck = &mut self.black; - - // TODO: this feels sloppy - if let Some(index) = (0..deck.len()).choose(&mut thread_rng()) { - Some(deck.swap_remove(index)) - } else { - tracing::error!("Tried to draw black card that doesn't exist!"); - None - } - } - - /// Deal a black card and use it for the current round - fn deal_black(&mut self) { - if let Some(black_card) = self.draw_one_black() { - self.current_black = black_card; - } else { - tracing::error!("Tried to deal black card that doesn't exist!"); - } - } - /// Create a new player and add them to the game. - pub fn create_player(&mut self, user: Arc>) -> Result<()> { - let mut new_player = Player { + 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); + } + } + + // New player object + let new_player = Player { user: user.clone(), - white: vec![], + white, black: vec![], }; - let new_player_name = user.read().unwrap().name.clone(); - tracing::debug!("Creating player for {}", &new_player_name); - - let mut hand_buf = vec![]; - for _ in 0..10 { - if let Some(card) = self.draw_one_white() { - hand_buf.push(card); - } - } - tracing::debug!("Dealing hand to {}", &new_player_name); - - new_player.white.extend(hand_buf); - + // Add player to game self.players .insert(user.read().unwrap().uuid.clone(), new_player); - - Ok(()) } }