From d3f02bf427d4b5d6a5b345758e82a5211656eabb Mon Sep 17 00:00:00 2001 From: Evan Slack Date: Thu, 26 Dec 2024 23:58:11 -0500 Subject: [PATCH] tests for buy jokers --- core/src/game.rs | 38 +++++++++++++++++++++----------------- core/src/joker.rs | 1 + core/src/shop.rs | 41 +++++++++++++++++++++++++++++++++-------- 3 files changed, 55 insertions(+), 25 deletions(-) diff --git a/core/src/game.rs b/core/src/game.rs index a98ecad..ff0d746 100644 --- a/core/src/game.rs +++ b/core/src/game.rs @@ -213,6 +213,7 @@ impl Game { self.money += self.reward; self.reward = 0; self.stage = Stage::Shop(); + self.shop.refresh(); return Ok(()); } @@ -226,6 +227,7 @@ impl Game { if joker.cost() > self.money { return Err(GameError::InvalidBalance); } + self.shop.buy_joker(&joker)?; self.money -= joker.cost(); self.jokers.push(joker); self.effect_registry @@ -515,28 +517,16 @@ mod tests { #[test] fn test_play_selected() { let mut g = Game::default(); + g.start(); g.stage = Stage::Blind(Blind::Small); g.blind = Some(Blind::Small); - let j0 = Card::new(Value::Jack, Suit::Club); - let j1 = Card::new(Value::Jack, Suit::Club); - let j2 = Card::new(Value::Jack, Suit::Club); - let j3 = Card::new(Value::Jack, Suit::Club); - let j4 = Card::new(Value::Jack, Suit::Club); - - // Score [Jc, Jc, Jc, Jc, Jc] - // Flush five (level 1) -> chips=160, mult=16 - // Played cards (5 jacks) -> 10 + 10 + 10 + 10 + 10 == 50 chips - // (160 + 50) * 16 = 3360 - let cards = vec![j0, j1, j2, j3, j4]; - - let mut available = Available::default(); - available.extend(cards.clone()); - for c in cards { - available.select_card(c).expect("can select card"); + for card in g.available.cards().iter().take(5) { + g.available.select_card(*card).expect("can select card"); } - g.available = available; assert_eq!(g.available.selected().len(), 5); + // Artifically set score so blind passes + g.score += g.required_score(); g.play_selected().expect("can play selected"); // Should have cleared blind @@ -553,4 +543,18 @@ mod tests { // Available should be length available assert_eq!(g.available.cards().len(), g.config.available); } + + #[test] + fn test_buy_joker() { + let mut g = Game::default(); + g.start(); + g.stage = Stage::Shop(); + g.money = 10; + g.shop.refresh(); + + let j1 = g.shop.joker_from_index(0).expect("is joker"); + g.buy_joker(j1.clone()).expect("buy joker"); + assert_eq!(g.money, 10 - j1.cost()); + assert_eq!(g.jokers.len(), 1); + } } diff --git a/core/src/joker.rs b/core/src/joker.rs index d4c959d..9318930 100644 --- a/core/src/joker.rs +++ b/core/src/joker.rs @@ -605,6 +605,7 @@ mod tests { // Buy (and apply) the joker g.money += 1000; // Give adequate money to buy g.stage = Stage::Shop(); + g.shop.jokers.push(joker.clone()); g.buy_joker(joker).unwrap(); g.stage = Stage::Blind(Blind::Small); // Second score with joker applied diff --git a/core/src/shop.rs b/core/src/shop.rs index 9497a8c..a9ba45f 100644 --- a/core/src/shop.rs +++ b/core/src/shop.rs @@ -1,7 +1,7 @@ use crate::action::Action; use crate::error::GameError; use crate::joker::{Joker, Jokers, Rarity}; -use rand::distributions::WeightedIndex; +// use rand::distributions::WeightedIndex; use rand::prelude::*; #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] @@ -29,11 +29,11 @@ impl Shop { return Some(self.jokers[i].clone()); } - pub(crate) fn buy_joker(&mut self, joker: Jokers) -> Result { + pub(crate) fn buy_joker(&mut self, joker: &Jokers) -> Result { let i = self .jokers .iter() - .position(|j| *j == joker) + .position(|j| j == joker) .ok_or(GameError::NoJokerMatch)?; let out = self.jokers.remove(i); return Ok(out); @@ -65,11 +65,13 @@ impl JokerGenerator { // 70% chance Common, 25% chance Uncommon, 5% chance Rare. // Legendary can only appear from Soul Spectral Card. fn gen_rarity(&self) -> Rarity { - let choices = [Rarity::Common, Rarity::Uncommon, Rarity::Rare]; - let weights = [70, 25, 5]; - let dist = WeightedIndex::new(&weights).unwrap(); - let mut rng = thread_rng(); - return choices[dist.sample(&mut rng)].clone(); + // For now, we only have common jokers... + return Rarity::Common; + // let choices = [Rarity::Common, Rarity::Uncommon, Rarity::Rare]; + // let weights = [70, 25, 5]; + // let dist = WeightedIndex::new(&weights).unwrap(); + // let mut rng = thread_rng(); + // return choices[dist.sample(&mut rng)].clone(); } // Generate a random new joker @@ -82,3 +84,26 @@ impl JokerGenerator { return choices[i].clone(); } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_shop_refresh() { + let mut shop = Shop::new(); + assert_eq!(shop.jokers.len(), 0); + shop.refresh(); + assert_eq!(shop.jokers.len(), 2); + } + + #[test] + fn test_shop_buy_joker() { + let mut shop = Shop::new(); + shop.refresh(); + assert_eq!(shop.jokers.len(), 2); + let j1 = shop.jokers[0].clone(); + assert_eq!(shop.joker_from_index(0).expect("first joker"), j1.clone()); + shop.buy_joker(&j1).expect("buy joker"); + } +}