Skip to content

Commit e2dd1d8

Browse files
Include rerolls in history
1 parent 3d2b939 commit e2dd1d8

File tree

4 files changed

+48
-23
lines changed

4 files changed

+48
-23
lines changed

src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -760,9 +760,9 @@ mod tests {
760760
let as_string = numeric.to_string(false);
761761

762762
assert_eq!(numeric.get_total(), 1);
763-
assert_eq!(as_string, "[1] = 1");
764-
// Rerolls are currently not displayed in the history
765-
assert_eq!(history, "[1]");
763+
assert_eq!(as_string, "[1 -> 1] -> [1] = 1");
764+
// Rerolls are now displayed in the history
765+
assert_eq!(history, "[1 -> 1] -> [1]");
766766
}
767767

768768
#[test]

src/parser.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -144,22 +144,30 @@ fn compute_reroll<RNG: DiceRollSource>(
144144
) -> (TotalModifier, Vec<DiceResult>) {
145145
let value = extract_option_value(option).unwrap();
146146
let mut has_rerolled = false;
147-
let res: Vec<DiceResult> = res
148-
.into_iter()
147+
let mut rerolls: Vec<Vec<DiceResult>> = vec![];
148+
let res_new: Vec<DiceResult> = res
149+
.iter()
149150
.map(|x| {
150-
if x.res <= value {
151+
let mut inner = vec![*x];
152+
let result = if x.res <= value {
151153
has_rerolled = true;
152-
roll_dice(1, sides, rng)[0]
154+
let rerolled = roll_dice(1, sides, rng)[0];
155+
inner.push(rerolled);
156+
rerolled
153157
} else {
154-
x
155-
}
158+
*x
159+
};
160+
rerolls.push(inner);
161+
result
156162
})
157163
.collect();
158164

159-
// Original roll is not included in history, so add the result, regardless of if a reroll occurred.
160-
rolls.add_history(res.clone(), false);
165+
if has_rerolled {
166+
rolls.add_rerolled_history(rerolls);
167+
}
168+
rolls.add_history(res_new.clone(), false);
161169

162-
(TotalModifier::None(Rule::reroll), res)
170+
(TotalModifier::None(Rule::reroll), res_new)
163171
}
164172

165173
fn compute_i_reroll<RNG: DiceRollSource>(

src/rollresult/rollhistory.rs

+22-11
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ impl Display for Value {
3939
#[non_exhaustive]
4040
#[derive(Debug, Clone)]
4141
pub enum RollHistory {
42+
/// Rolls which include rerolls.
43+
/// Should be followed by a Roll with the final results.
44+
ReRolls(Vec<Vec<DiceResult>>),
4245
/// A roll with normal dices
4346
Roll(Vec<DiceResult>),
4447
/// A roll with Fudge dices
@@ -56,18 +59,26 @@ pub enum RollHistory {
5659
impl Display for RollHistory {
5760
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5861
let s = match self {
62+
RollHistory::ReRolls(v) => {
63+
let s2 = v
64+
.iter()
65+
.map(|r| {
66+
r.iter()
67+
.map(|r| r.res.to_string())
68+
.collect::<Vec<_>>()
69+
.join(" -> ")
70+
})
71+
.collect::<Vec<_>>()
72+
.join(", ");
73+
format!("[{}] -> ", s2)
74+
}
5975
RollHistory::Roll(v) => {
60-
let mut s = String::new();
61-
s.push('[');
62-
let len = v.len();
63-
v.iter().enumerate().for_each(|(i, r)| {
64-
s.push_str(&r.res.to_string());
65-
if i < len - 1 {
66-
s.push_str(", ");
67-
}
68-
});
69-
s.push(']');
70-
s
76+
let s2 = v
77+
.iter()
78+
.map(|r| r.res.to_string())
79+
.collect::<Vec<_>>()
80+
.join(", ");
81+
format!("[{}]", s2)
7182
}
7283
RollHistory::Fudge(v) => {
7384
let mut s = String::new();

src/rollresult/singlerollresult.rs

+6
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ impl SingleRollResult {
7676
});
7777
}
7878

79+
pub(crate) fn add_rerolled_history(&mut self, mut history: Vec<Vec<DiceResult>>) {
80+
self.dirty = true;
81+
history.sort_unstable_by(|a, b| b.cmp(a));
82+
self.history.push(RollHistory::ReRolls(history));
83+
}
84+
7985
pub(crate) fn add_parenthesis(&mut self) {
8086
self.history.insert(0, RollHistory::OpenParenthesis);
8187
self.history.push(RollHistory::CloseParenthesis);

0 commit comments

Comments
 (0)