Skip to content

Commit

Permalink
tests for buy jokers
Browse files Browse the repository at this point in the history
  • Loading branch information
evanofslack committed Dec 27, 2024
1 parent 2fbec06 commit d3f02bf
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 25 deletions.
38 changes: 21 additions & 17 deletions core/src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ impl Game {
self.money += self.reward;
self.reward = 0;
self.stage = Stage::Shop();
self.shop.refresh();
return Ok(());
}

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}
1 change: 1 addition & 0 deletions core/src/joker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 33 additions & 8 deletions core/src/shop.rs
Original file line number Diff line number Diff line change
@@ -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))]
Expand Down Expand Up @@ -29,11 +29,11 @@ impl Shop {
return Some(self.jokers[i].clone());
}

pub(crate) fn buy_joker(&mut self, joker: Jokers) -> Result<Jokers, GameError> {
pub(crate) fn buy_joker(&mut self, joker: &Jokers) -> Result<Jokers, GameError> {
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);
Expand Down Expand Up @@ -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
Expand All @@ -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");
}
}

0 comments on commit d3f02bf

Please sign in to comment.