File tree 4 files changed +48
-23
lines changed
4 files changed +48
-23
lines changed Original file line number Diff line number Diff line change @@ -760,9 +760,9 @@ mod tests {
760
760
let as_string = numeric. to_string ( false ) ;
761
761
762
762
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 ]" ) ;
766
766
}
767
767
768
768
#[ test]
Original file line number Diff line number Diff line change @@ -144,22 +144,30 @@ fn compute_reroll<RNG: DiceRollSource>(
144
144
) -> ( TotalModifier , Vec < DiceResult > ) {
145
145
let value = extract_option_value ( option) . unwrap ( ) ;
146
146
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 ( )
149
150
. map ( |x| {
150
- if x. res <= value {
151
+ let mut inner = vec ! [ * x] ;
152
+ let result = if x. res <= value {
151
153
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
153
157
} else {
154
- x
155
- }
158
+ * x
159
+ } ;
160
+ rerolls. push ( inner) ;
161
+ result
156
162
} )
157
163
. collect ( ) ;
158
164
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 ) ;
161
169
162
- ( TotalModifier :: None ( Rule :: reroll) , res )
170
+ ( TotalModifier :: None ( Rule :: reroll) , res_new )
163
171
}
164
172
165
173
fn compute_i_reroll < RNG : DiceRollSource > (
Original file line number Diff line number Diff line change @@ -39,6 +39,9 @@ impl Display for Value {
39
39
#[ non_exhaustive]
40
40
#[ derive( Debug , Clone ) ]
41
41
pub enum RollHistory {
42
+ /// Rolls which include rerolls.
43
+ /// Should be followed by a Roll with the final results.
44
+ ReRolls ( Vec < Vec < DiceResult > > ) ,
42
45
/// A roll with normal dices
43
46
Roll ( Vec < DiceResult > ) ,
44
47
/// A roll with Fudge dices
@@ -56,18 +59,26 @@ pub enum RollHistory {
56
59
impl Display for RollHistory {
57
60
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
58
61
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
+ }
59
75
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)
71
82
}
72
83
RollHistory :: Fudge ( v) => {
73
84
let mut s = String :: new ( ) ;
Original file line number Diff line number Diff line change @@ -76,6 +76,12 @@ impl SingleRollResult {
76
76
} ) ;
77
77
}
78
78
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
+
79
85
pub ( crate ) fn add_parenthesis ( & mut self ) {
80
86
self . history . insert ( 0 , RollHistory :: OpenParenthesis ) ;
81
87
self . history . push ( RollHistory :: CloseParenthesis ) ;
You can’t perform that action at this time.
0 commit comments