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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use crate::{
    mania::{ManiaBeatmap, ManiaGradualDifficulty},
    Difficulty,
};

use super::{ManiaPerformanceAttributes, ManiaScoreState};

/// Gradually calculate the performance attributes of an osu!mania map.
///
/// After each hit object you can call [`next`] and it will return the
/// resulting current [`ManiaPerformanceAttributes`]. To process multiple
/// objects at once, use [`nth`] instead.
///
/// Both methods require a play's current score so far. Be sure the given score
/// is adjusted with respect to mods.
///
/// If you only want to calculate difficulty attributes use
/// [`ManiaGradualDifficulty`] instead.
///
/// # Example
///
/// ```
/// use rosu_pp::{Beatmap, Difficulty};
/// use rosu_pp::mania::{Mania, ManiaGradualPerformance, ManiaScoreState};
///
/// let converted = Beatmap::from_path("./resources/1638954.osu")
///     .unwrap()
///     .unchecked_into_converted::<Mania>();
///
/// let difficulty = Difficulty::new().mods(64); // DT
/// let mut gradual = ManiaGradualPerformance::new(difficulty, &converted);
/// let mut state = ManiaScoreState::new(); // empty state, everything is on 0.
///
/// // The first 10 hitresults are 320s
/// for _ in 0..10 {
///     state.n320 += 1;
///
///     let attrs = gradual.next(state.clone()).unwrap();
///     println!("PP: {}", attrs.pp);
/// }
///
/// // Then comes a miss.
/// state.misses += 1;
/// let attrs = gradual.next(state.clone()).unwrap();
/// println!("PP: {}", attrs.pp);
///
/// // The next 10 objects will be a mixture of 320s and 100s.
/// // Notice how all 10 objects will be processed in one go.
/// state.n320 += 3;
/// state.n100 += 7;
/// // The `nth` method takes a zero-based value.
/// let attrs = gradual.nth(state.clone(), 9).unwrap();
/// println!("PP: {}", attrs.pp);
///
/// // Skip to the end
/// # /*
/// state.max_combo = ...
/// state.n300 = ...
/// state.n100 = ...
/// state.misses = ...
/// # */
/// let attrs = gradual.last(state.clone()).unwrap();
/// println!("PP: {}", attrs.pp);
///
/// // Once the final performance was calculated,
/// // attempting to process further objects will return `None`.
/// assert!(gradual.next(state).is_none());
/// ```
///
/// [`next`]: ManiaGradualPerformance::next
/// [`nth`]: ManiaGradualPerformance::nth
pub struct ManiaGradualPerformance {
    difficulty: ManiaGradualDifficulty,
}

impl ManiaGradualPerformance {
    /// Create a new gradual performance calculator for osu!mania maps.
    pub fn new(difficulty: Difficulty, converted: &ManiaBeatmap<'_>) -> Self {
        let difficulty = ManiaGradualDifficulty::new(difficulty, converted);

        Self { difficulty }
    }

    /// Process the next hit object and calculate the performance attributes
    /// for the resulting score.
    pub fn next(&mut self, state: ManiaScoreState) -> Option<ManiaPerformanceAttributes> {
        self.nth(state, 0)
    }

    /// Process all remaining hit objects and calculate the final performance
    /// attributes.
    pub fn last(&mut self, state: ManiaScoreState) -> Option<ManiaPerformanceAttributes> {
        self.nth(state, usize::MAX)
    }

    /// Process everything up the the next `n`th hit object and calculate the
    /// performance attributes for the resulting score state.
    ///
    /// Note that the count is zero-indexed, so `n=0` will process 1 object,
    /// `n=1` will process 2, and so on.
    pub fn nth(&mut self, state: ManiaScoreState, n: usize) -> Option<ManiaPerformanceAttributes> {
        let performance = self
            .difficulty
            .nth(n)?
            .performance()
            .state(state)
            .difficulty(self.difficulty.difficulty.clone())
            .passed_objects(self.difficulty.idx as u32)
            .calculate();

        Some(performance)
    }

    /// Returns the amount of remaining objects.
    #[allow(clippy::len_without_is_empty)]
    pub fn len(&self) -> usize {
        self.difficulty.len()
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        mania::{Mania, ManiaPerformance},
        Beatmap,
    };

    use super::*;

    #[test]
    fn next_and_nth() {
        let converted = Beatmap::from_path("./resources/1638954.osu")
            .unwrap()
            .unchecked_into_converted::<Mania>();

        let difficulty = Difficulty::new().mods(88); // HDHRDT

        let mut gradual = ManiaGradualPerformance::new(difficulty.clone(), &converted);
        let mut gradual_2nd = ManiaGradualPerformance::new(difficulty.clone(), &converted);
        let mut gradual_3rd = ManiaGradualPerformance::new(difficulty.clone(), &converted);

        let mut state = ManiaScoreState::default();

        let hit_objects_len = converted.hit_objects.len();

        for i in 1.. {
            state.misses += 1;

            let Some(next_gradual) = gradual.next(state.clone()) else {
                assert_eq!(i, hit_objects_len + 1);
                assert!(gradual_2nd.last(state.clone()).is_some() || hit_objects_len % 2 == 0);
                assert!(gradual_3rd.last(state.clone()).is_some() || hit_objects_len % 3 == 0);
                break;
            };

            if i % 2 == 0 {
                let next_gradual_2nd = gradual_2nd.nth(state.clone(), 1).unwrap();
                assert_eq!(next_gradual, next_gradual_2nd);
            }

            if i % 3 == 0 {
                let next_gradual_3rd = gradual_3rd.nth(state.clone(), 2).unwrap();
                assert_eq!(next_gradual, next_gradual_3rd);
            }

            let mut regular_calc = ManiaPerformance::new(converted.as_owned())
                .difficulty(difficulty.clone())
                .passed_objects(i as u32)
                .state(state.clone());

            let regular_state = regular_calc.generate_state();
            assert_eq!(state, regular_state);

            let expected = regular_calc.calculate();

            assert_eq!(next_gradual, expected);
        }
    }
}