From e8ce058e5d049347d8d9be3aacc07b3cb0c657a5 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Thu, 27 Aug 2020 19:44:17 +0000 Subject: [PATCH] Properly format dramatic failures. --- src/cofd.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/cofd.rs b/src/cofd.rs index aa81ba1..1a5f3e1 100644 --- a/src/cofd.rs +++ b/src/cofd.rs @@ -70,11 +70,22 @@ impl DicePool { ///Store all rolls of the dice pool dice into one struct. #[derive(Debug, PartialEq, Eq, Clone)] pub struct DicePoolRoll { + quality: DicePoolQuality, success_on: u32, exceptional_on: u32, rolls: Vec, } +fn fmt_for_failure(pool: &DicePoolRoll) -> String { + match pool.quality { + //There should only be 1 die in a chance die roll. + DicePoolQuality::ChanceDie if pool.rolls().first() == Some(&1) => { + String::from("dramatic failure!") + } + _ => String::from("failure!"), + } +} + impl DicePoolRoll { pub fn rolls(&self) -> &[u32] { &self.rolls @@ -121,7 +132,12 @@ impl fmt::Display for DicePoolRoll { write!(f, "{} ({})", success_msg, self.rolls().iter().join(", "))?; } else { - write!(f, "failure! ({})", self.rolls().iter().join(", "))?; + write!( + f, + "{} ({})", + fmt_for_failure(&self), + self.rolls().iter().join(", ") + )?; } Ok(()) @@ -200,6 +216,7 @@ fn roll_dice(pool: &DicePool) -> DicePoolRoll { .collect(); DicePoolRoll { + quality: pool.quality, rolls: rolls, exceptional_on: pool.exceptional_success, success_on: pool.success_on, @@ -281,6 +298,7 @@ mod tests { #[test] fn is_successful_on_equal_test() { let result = DicePoolRoll { + quality: DicePoolQuality::TenAgain, rolls: vec![8], exceptional_on: 5, success_on: 8, @@ -292,6 +310,7 @@ mod tests { #[test] fn is_exceptional_test() { let result = DicePoolRoll { + quality: DicePoolQuality::TenAgain, rolls: vec![8, 8, 9, 10, 8], exceptional_on: 5, success_on: 8, @@ -304,6 +323,7 @@ mod tests { #[test] fn is_not_exceptional_test() { let result = DicePoolRoll { + quality: DicePoolQuality::TenAgain, rolls: vec![8, 8, 9, 10], exceptional_on: 5, success_on: 8, @@ -312,4 +332,29 @@ mod tests { assert_eq!(4, result.successes()); assert_eq!(false, result.is_exceptional()); } + + //Format tests + #[test] + fn formats_dramatic_failure_test() { + let result = DicePoolRoll { + quality: DicePoolQuality::ChanceDie, + rolls: vec![1], + exceptional_on: 5, + success_on: 10, + }; + + assert_eq!("dramatic failure!", fmt_for_failure(&result)); + } + + #[test] + fn formats_regular_failure_when_not_chance_die_test() { + let result = DicePoolRoll { + quality: DicePoolQuality::TenAgain, + rolls: vec![1], + exceptional_on: 5, + success_on: 10, + }; + + assert_eq!("failure!", fmt_for_failure(&result)); + } }