From 1a980aa608b55f9deb3f5b15c13f0a6408243c0a Mon Sep 17 00:00:00 2001 From: projectmoon Date: Thu, 22 Oct 2020 22:43:31 +0000 Subject: [PATCH] Auto-convert dice pools to chance die if below 0. --- src/cofd/dice.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/cofd/dice.rs b/src/cofd/dice.rs index 879df89..f1a5199 100644 --- a/src/cofd/dice.rs +++ b/src/cofd/dice.rs @@ -378,8 +378,16 @@ pub async fn roll_pool(pool: &DicePoolWithContext<'_>) -> Result 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)] @@ -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();