Skip to content

Commit

Permalink
Auto-convert dice pools to chance die if below 0.
Browse files Browse the repository at this point in the history
  • Loading branch information
ProjectMoon committed Oct 22, 2020
1 parent 7f97170 commit 1a980aa
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/cofd/dice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,16 @@ pub async fn roll_pool(pool: &DicePoolWithContext<'_>) -> Result<RolledDicePool,

let num_dice = calculate_dice_amount(&pool).await?;
let mut roller = RngDieRoller(rand::thread_rng());
let rolls = roll_dice(&pool.0, num_dice, &mut roller);
Ok(RolledDicePool::from(&pool.0, num_dice, rolls))

if num_dice > 0 {
let rolls = roll_dice(&pool.0, num_dice, &mut roller);
Ok(RolledDicePool::from(&pool.0, num_dice, rolls))
} else {
let chance_die = DicePool::chance_die();
let pool = DicePoolWithContext(&chance_die, &pool.1);
let rolls = roll_dice(&pool.0, num_dice, &mut roller);
Ok(RolledDicePool::from(&pool.0, num_dice, rolls))
}
}

#[cfg(test)]
Expand Down Expand Up @@ -548,6 +556,29 @@ mod tests {
));
}

#[tokio::test]
async fn converts_to_chance_die_test() {
let db = Database::new(&tempdir().unwrap()).unwrap();
let ctx = Context::new(&db, "roomid", "username", "message");

let mut amounts = vec![];

amounts.push(Amount {
operator: Operator::Plus,
element: Element::Number(-1),
});

let pool = DicePool::new(amounts, DicePoolModifiers::default());
let pool_with_ctx = DicePoolWithContext(&pool, &ctx);
let result = roll_pool(&pool_with_ctx).await;
assert!(result.is_ok());

assert_eq!(
DicePoolQuality::ChanceDie,
result.unwrap().modifiers.quality
);
}

#[tokio::test]
async fn can_resolve_variables_test() {
let db = Database::new(&tempdir().unwrap()).unwrap();
Expand Down

0 comments on commit 1a980aa

Please sign in to comment.