1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//! Provides functionality for calculating the Sum of Best Segments and the Sum
//! of Worst Segments for whole runs or specific parts. The Sum of Best Segments
//! is the fastest time possible to complete a run of a category, based on
//! information collected from all the previous attempts. This often matches up
//! with the sum of the best segment times of all the segments, but that may not
//! always be the case, as skipped segments may introduce combined segments that
//! may be faster than the actual sum of their best segment times. The name is
//! therefore a bit misleading, but sticks around for historical reasons.

pub mod best;
pub mod worst;

#[cfg(test)]
mod tests;

use crate::{Segment, Time, TimeSpan, TimingMethod};

/// Describes the shortest amount of time it takes to reach a certain segment.
/// Since there is the possibility that the shortest path is actually skipping
/// segments, there's an additional predecessor index that describes the segment
/// this prediction is based on. By following all the predecessors backwards,
/// you can get access to the single fastest route.
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq)]
pub struct Prediction {
    /// The shortest amount of time it takes to reach the segment.
    pub time: TimeSpan,
    /// The index of the predecessor that directly leads to this segment.
    pub predecessor: usize,
}

/// Calculates the Sum of Best Segments for the timing method provided. This is
/// the fastest time possible to complete a run of a category, based on
/// information collected from all the previous attempts. This often matches up
/// with the sum of the best segment times of all the segments, but that may not
/// always be the case, as skipped segments may introduce combined segments that
/// may be faster than the actual sum of their best segment times. The name is
/// therefore a bit misleading, but sticks around for historical reasons. You
/// can choose to do a simple calculation instead, which excludes the Segment
/// History from the calculation process. If there's an active attempt, you can
/// choose to take it into account as well.
pub fn calculate_best(
    segments: &[Segment],
    simple_calculation: bool,
    use_current_run: bool,
    method: TimingMethod,
) -> Option<TimeSpan> {
    let mut predictions = vec![None; segments.len() + 1];
    best::calculate(
        segments,
        &mut predictions,
        simple_calculation,
        use_current_run,
        method,
    )
}

/// Calculates the Sum of Worst Segments for the timing method provided. This is
/// the slowest time possible to complete a run of a category, based on
/// information collected from all the previous attempts. This obviously isn't
/// really the worst possible time, but may be useful information regardless.
/// If there's an active attempt, you can choose to take it into account as
/// well.
pub fn calculate_worst(
    segments: &[Segment],
    use_current_run: bool,
    method: TimingMethod,
) -> Option<TimeSpan> {
    let mut predictions = vec![None; segments.len() + 1];
    worst::calculate(segments, &mut predictions, use_current_run, method)
}

fn track_current_run(
    segments: &[Segment],
    current_time: Option<TimeSpan>,
    segment_index: usize,
    method: TimingMethod,
) -> (usize, Time) {
    if let Some(first_split_time) = segment_index
        .checked_sub(1)
        .map_or(Some(TimeSpan::zero()), |i| segments[i].split_time()[method])
    {
        for (segment_index, segment) in segments.iter().enumerate().skip(segment_index) {
            let second_split_time = segment.split_time()[method];
            if let Some(second_split_time) = second_split_time {
                return (
                    segment_index + 1,
                    Time::new().with_timing_method(
                        method,
                        current_time.map(|t| second_split_time - first_split_time + t),
                    ),
                );
            }
        }
    }
    (0, Time::default())
}

fn track_personal_best_run(
    segments: &[Segment],
    current_time: Option<TimeSpan>,
    segment_index: usize,
    method: TimingMethod,
) -> (usize, Time) {
    if let Some(first_split_time) = segment_index
        .checked_sub(1)
        .map_or(Some(TimeSpan::zero()), |i| {
            segments[i].personal_best_split_time()[method]
        })
    {
        for (segment_index, segment) in segments.iter().enumerate().skip(segment_index) {
            let second_split_time = segment.personal_best_split_time()[method];
            if let Some(second_split_time) = second_split_time {
                return (
                    segment_index + 1,
                    Time::new().with_timing_method(
                        method,
                        current_time.map(|t| second_split_time - first_split_time + t),
                    ),
                );
            }
        }
    }
    (0, Time::default())
}

/// Follows a path starting from a certain segment in a certain attempt to the
/// next split that didn't get skipped. Returns the index of the segment after
/// the segment that has the next split time and a sum of the combined segment
/// times and the current time provided. If the tracked attempt ends before a
/// split time is found, the index returned is 0.
pub fn track_branch(
    segments: &[Segment],
    current_time: Option<TimeSpan>,
    segment_index: usize,
    run_index: i32,
    method: TimingMethod,
) -> (usize, Time) {
    for (segment_index, segment) in segments.iter().enumerate().skip(segment_index) {
        if let Some(cur_time) = segment.segment_history().get(run_index) {
            if let Some(cur_time) = cur_time[method] {
                return (
                    segment_index + 1,
                    Time::new().with_timing_method(method, current_time.map(|t| cur_time + t)),
                );
            }
        } else {
            break;
        }
    }
    (0, Time::default())
}