From 5983592eb3ef60b32e21965541f75ec62f7a2833 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Fri, 28 Aug 2020 00:30:28 +0000 Subject: [PATCH] Limit large the display of large dice pools. --- src/cofd/dice.rs | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/src/cofd/dice.rs b/src/cofd/dice.rs index 78e9192..40e0f5d 100644 --- a/src/cofd/dice.rs +++ b/src/cofd/dice.rs @@ -74,6 +74,21 @@ pub struct DicePoolRoll { rolls: Vec, } +fn fmt_rolls(pool: &DicePoolRoll) -> String { + let max_displayed_rolls = 15; + let rolls = pool.rolls(); + if rolls.len() > max_displayed_rolls { + let first_ten = rolls.iter().take(max_displayed_rolls).join(", "); + format!( + "{}, and {} more", + first_ten, + rolls.len() - max_displayed_rolls + ) + } else { + rolls.iter().take(10).join(", ") + } +} + fn fmt_for_failure(pool: &DicePoolRoll) -> String { match pool.quality { //There should only be 1 die in a chance die roll. @@ -128,14 +143,9 @@ impl fmt::Display for DicePoolRoll { format!("{} successes", successes) }; - write!(f, "{} ({})", success_msg, self.rolls().iter().join(", "))?; + write!(f, "{} ({})", success_msg, fmt_rolls(&self))?; } else { - write!( - f, - "{} ({})", - fmt_for_failure(&self), - self.rolls().iter().join(", ") - )?; + write!(f, "{} ({})", fmt_for_failure(&self), fmt_rolls(&self))?; } Ok(()) @@ -179,6 +189,7 @@ fn roll_exploding_die( ///can keep track of the actual dice that were rolled. fn roll_rote_die(roller: &mut R, sides: u32) -> Vec { let mut rolls = roll_exploding_die(roller, sides, 10); + if rolls.len() == 1 && rolls[0] < 8 { rolls.append(&mut roll_exploding_die(roller, sides, 10)); } @@ -355,4 +366,19 @@ mod tests { assert_eq!("failure!", fmt_for_failure(&result)); } + + #[test] + fn formats_lots_of_dice_test() { + let result = DicePoolRoll { + quality: DicePoolQuality::TenAgain, + rolls: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9], + exceptional_on: 5, + success_on: 10, + }; + + assert_eq!( + "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, and 4 more", + fmt_rolls(&result) + ); + } }