Auto-convert dice pools to chance die if below 0.

This commit is contained in:
projectmoon 2020-10-22 22:43:31 +00:00 committed by ProjectMoon
parent 7f971703e2
commit 1a980aa608
1 changed files with 33 additions and 2 deletions

View File

@ -378,8 +378,16 @@ pub async fn roll_pool(pool: &DicePoolWithContext<'_>) -> Result<RolledDicePool,
let num_dice = calculate_dice_amount(&pool).await?; let num_dice = calculate_dice_amount(&pool).await?;
let mut roller = RngDieRoller(rand::thread_rng()); 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)] #[cfg(test)]
@ -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] #[tokio::test]
async fn can_resolve_variables_test() { async fn can_resolve_variables_test() {
let db = Database::new(&tempdir().unwrap()).unwrap(); let db = Database::new(&tempdir().unwrap()).unwrap();