|
| 1 | +use {Attempt, Run, Segment, TimeSpan, TimingMethod}; |
| 2 | +use analysis::sum_of_segments::{best, track_branch}; |
| 3 | +use std::mem::replace; |
| 4 | +use std::fmt; |
| 5 | +use time::formatter::{Short, TimeFormatter}; |
| 6 | +use chrono::Local; |
| 7 | + |
| 8 | +pub struct SumOfBestCleaner<'r> { |
| 9 | + run: &'r mut Run, |
| 10 | + predictions: Vec<Option<TimeSpan>>, |
| 11 | + state: State, |
| 12 | +} |
| 13 | + |
| 14 | +enum State { |
| 15 | + Poisoned, |
| 16 | + Done, |
| 17 | + WithTimingMethod(TimingMethod), |
| 18 | + IteratingRun(IteratingRunState), |
| 19 | + IteratingHistory(IteratingHistoryState), |
| 20 | +} |
| 21 | + |
| 22 | +struct IteratingRunState { |
| 23 | + method: TimingMethod, |
| 24 | + segment_index: usize, |
| 25 | +} |
| 26 | + |
| 27 | +struct IteratingHistoryState { |
| 28 | + parent: IteratingRunState, |
| 29 | + current_time: Option<TimeSpan>, |
| 30 | + skip_count: usize, |
| 31 | +} |
| 32 | + |
| 33 | +pub struct PotentialCleanUp<'r> { |
| 34 | + starting_segment: Option<&'r Segment>, |
| 35 | + ending_segment: &'r Segment, |
| 36 | + time_between: TimeSpan, |
| 37 | + combined_sum_of_best: Option<TimeSpan>, |
| 38 | + attempt: &'r Attempt, |
| 39 | + method: TimingMethod, |
| 40 | + clean_up: CleanUp, |
| 41 | +} |
| 42 | + |
| 43 | +pub struct CleanUp { |
| 44 | + ending_index: usize, |
| 45 | + run_index: i32, |
| 46 | +} |
| 47 | + |
| 48 | +impl<'r> fmt::Display for PotentialCleanUp<'r> { |
| 49 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 50 | + let short = Short::new(); |
| 51 | + |
| 52 | + let method = match self.method { |
| 53 | + TimingMethod::RealTime => "Real Time", |
| 54 | + TimingMethod::GameTime => "Game Time", |
| 55 | + }; |
| 56 | + |
| 57 | + write!( |
| 58 | + f, |
| 59 | + "You had a {} segment time of {} between ", |
| 60 | + method, |
| 61 | + short.format(self.time_between) |
| 62 | + )?; |
| 63 | + |
| 64 | + if let Some(starting_segment) = self.starting_segment { |
| 65 | + write!(f, "{}", starting_segment.name())?; |
| 66 | + } else { |
| 67 | + write!(f, "the start of the run")?; |
| 68 | + } |
| 69 | + |
| 70 | + write!(f, " and {}", self.ending_segment.name())?; |
| 71 | + |
| 72 | + if let Some(combined) = self.combined_sum_of_best { |
| 73 | + write!( |
| 74 | + f, |
| 75 | + ", which is faster than the Combined Best Segments of {}", |
| 76 | + short.format(combined) |
| 77 | + )?; |
| 78 | + } |
| 79 | + |
| 80 | + if let Some(ended) = self.attempt.ended() { |
| 81 | + write!( |
| 82 | + f, |
| 83 | + " in a run on {}", |
| 84 | + ended.time.with_timezone(&Local).format("%F") |
| 85 | + )?; |
| 86 | + } |
| 87 | + |
| 88 | + write!( |
| 89 | + f, |
| 90 | + ". Do you think that this segment time is inaccurate and should be removed?" |
| 91 | + ) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl<'a> From<PotentialCleanUp<'a>> for CleanUp { |
| 96 | + fn from(potential: PotentialCleanUp) -> Self { |
| 97 | + potential.clean_up |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +impl<'r> SumOfBestCleaner<'r> { |
| 102 | + pub fn new(run: &'r mut Run) -> Self { |
| 103 | + let predictions = Vec::with_capacity(run.len() + 1); |
| 104 | + Self { |
| 105 | + run, |
| 106 | + predictions, |
| 107 | + state: State::WithTimingMethod(TimingMethod::RealTime), |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + pub fn apply(&mut self, clean_up: CleanUp) { |
| 112 | + self.run |
| 113 | + .segment_mut(clean_up.ending_index) |
| 114 | + .segment_history_mut() |
| 115 | + .remove(clean_up.run_index); |
| 116 | + |
| 117 | + self.run.mark_as_changed(); |
| 118 | + } |
| 119 | + |
| 120 | + pub fn next_potential_clean_up(&mut self) -> Option<PotentialCleanUp> { |
| 121 | + loop { |
| 122 | + match replace(&mut self.state, State::Poisoned) { |
| 123 | + State::Poisoned => unreachable!(), |
| 124 | + State::Done => return None, |
| 125 | + State::WithTimingMethod(method) => { |
| 126 | + next_timing_method(&self.run, &mut self.predictions, method); |
| 127 | + self.state = State::IteratingRun(IteratingRunState { |
| 128 | + method: method, |
| 129 | + segment_index: 0, |
| 130 | + }); |
| 131 | + } |
| 132 | + State::IteratingRun(state) => { |
| 133 | + self.state = if state.segment_index < self.run.len() { |
| 134 | + let current_time = self.predictions[state.segment_index]; |
| 135 | + State::IteratingHistory(IteratingHistoryState { |
| 136 | + parent: state, |
| 137 | + current_time, |
| 138 | + skip_count: 0, |
| 139 | + }) |
| 140 | + } else if state.method == TimingMethod::RealTime { |
| 141 | + State::WithTimingMethod(TimingMethod::GameTime) |
| 142 | + } else { |
| 143 | + State::Done |
| 144 | + }; |
| 145 | + } |
| 146 | + State::IteratingHistory(state) => { |
| 147 | + let iter = self.run |
| 148 | + .segment(state.parent.segment_index) |
| 149 | + .segment_history() |
| 150 | + .iter() |
| 151 | + .enumerate() |
| 152 | + .skip(state.skip_count); |
| 153 | + |
| 154 | + for (skip_count, &(run_index, time)) in iter { |
| 155 | + if time[state.parent.method].is_none() { |
| 156 | + let (prediction_index, prediction_time) = track_branch( |
| 157 | + self.run.segments(), |
| 158 | + state.current_time, |
| 159 | + state.parent.segment_index + 1, |
| 160 | + run_index, |
| 161 | + state.parent.method, |
| 162 | + ); |
| 163 | + if prediction_index > 0 { |
| 164 | + if let Some(question) = check_prediction( |
| 165 | + &self.run, |
| 166 | + &self.predictions, |
| 167 | + prediction_time[state.parent.method], |
| 168 | + state.parent.segment_index as isize - 1, |
| 169 | + prediction_index - 1, |
| 170 | + run_index, |
| 171 | + state.parent.method, |
| 172 | + ) { |
| 173 | + self.state = State::IteratingHistory(IteratingHistoryState { |
| 174 | + skip_count: skip_count + 1, |
| 175 | + ..state |
| 176 | + }); |
| 177 | + return Some(question); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + self.state = State::IteratingRun(IteratingRunState { |
| 183 | + method: state.parent.method, |
| 184 | + segment_index: state.parent.segment_index + 1, |
| 185 | + }); |
| 186 | + } |
| 187 | + }; |
| 188 | + } |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +fn check_prediction<'a>( |
| 193 | + run: &'a Run, |
| 194 | + predictions: &[Option<TimeSpan>], |
| 195 | + predicted_time: Option<TimeSpan>, |
| 196 | + starting_index: isize, |
| 197 | + ending_index: usize, |
| 198 | + run_index: i32, |
| 199 | + method: TimingMethod, |
| 200 | +) -> Option<PotentialCleanUp<'a>> { |
| 201 | + if let Some(predicted_time) = predicted_time { |
| 202 | + if predictions[ending_index + 1].map_or(true, |t| predicted_time < t) { |
| 203 | + if let Some(segment_history_element) = |
| 204 | + run.segment(ending_index).segment_history().get(run_index) |
| 205 | + { |
| 206 | + return Some(PotentialCleanUp { |
| 207 | + starting_segment: if starting_index >= 0 { |
| 208 | + Some(run.segment(starting_index as usize)) |
| 209 | + } else { |
| 210 | + None |
| 211 | + }, |
| 212 | + ending_segment: run.segment(ending_index), |
| 213 | + time_between: segment_history_element[method] |
| 214 | + .expect("Cleanup path is shorter but doesn't have a time"), |
| 215 | + combined_sum_of_best: predictions[ending_index + 1].map(|t| { |
| 216 | + t - |
| 217 | + predictions[(starting_index + 1) as usize] |
| 218 | + .expect("Start time must not be empty") |
| 219 | + }), |
| 220 | + attempt: run.attempt_history() |
| 221 | + .iter() |
| 222 | + .find(|attempt| attempt.index() == run_index) |
| 223 | + .expect("The attempt has to exist"), |
| 224 | + method, |
| 225 | + clean_up: CleanUp { |
| 226 | + ending_index, |
| 227 | + run_index, |
| 228 | + }, |
| 229 | + }); |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | + None |
| 234 | +} |
| 235 | + |
| 236 | +fn next_timing_method(run: &Run, predictions: &mut Vec<Option<TimeSpan>>, method: TimingMethod) { |
| 237 | + let segments = run.segments(); |
| 238 | + |
| 239 | + predictions.clear(); |
| 240 | + predictions.resize(segments.len() + 1, None); |
| 241 | + best::calculate( |
| 242 | + segments, |
| 243 | + 0, |
| 244 | + segments.len(), |
| 245 | + predictions, |
| 246 | + true, |
| 247 | + false, |
| 248 | + method, |
| 249 | + ); |
| 250 | +} |
0 commit comments