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
180
use std::borrow::Cow;

use rosu_map::section::general::GameMode;

use crate::{
    any::{PerformanceAttributes, ScoreState},
    catch::{Catch, CatchBeatmap, CatchGradualPerformance},
    mania::{Mania, ManiaBeatmap, ManiaGradualPerformance},
    model::mode::IGameMode,
    osu::{Osu, OsuBeatmap, OsuGradualPerformance},
    taiko::{Taiko, TaikoBeatmap, TaikoGradualPerformance},
    Beatmap, Converted, Difficulty,
};

/// Gradually calculate the performance attributes on maps of any mode.
///
/// After each hit object you can call [`next`] and it will return the
/// resulting current [`PerformanceAttributes`]. To process multiple objects at
/// the once, use [`nth`] instead.
///
/// Both methods require a [`ScoreState`] that contains the current hitresults
/// as well as the maximum combo so far. Since the map could have any mode, all
/// fields of `ScoreState` could be of use and should be updated properly.
///
/// Alternatively, you can match on the map's mode yourself and use the gradual
/// performance attribute struct for the corresponding mode, i.e.
/// [`OsuGradualPerformance`], [`TaikoGradualPerformance`],
/// [`CatchGradualPerformance`], or [`ManiaGradualPerformance`].
///
/// If you only want to calculate difficulty attributes use [`GradualDifficulty`] instead.
///
/// # Example
///
/// ```
/// use rosu_pp::{Beatmap, GradualPerformance, Difficulty, any::ScoreState};
///
/// let map = Beatmap::from_path("./resources/2785319.osu").unwrap();
/// let difficulty = Difficulty::new().mods(64); // DT
/// let mut gradual = GradualPerformance::new(difficulty, &map);
/// let mut state = ScoreState::new(); // empty state, everything is on 0.
///
/// // The first 10 hitresults are 300s
/// for _ in 0..10 {
///     state.n300 += 1;
///     state.max_combo += 1;
///
///     let attrs = gradual.next(state.clone()).unwrap();
///     println!("PP: {}", attrs.pp());
/// }
///
/// // Then comes a miss.
/// // Note that state's max combo won't be incremented for
/// // the next few objects because the combo is reset.
/// state.misses += 1;
///
/// let attrs = gradual.next(state.clone()).unwrap();
/// println!("PP: {}", attrs.pp());
///
/// // The next 10 objects will be a mixture of 300s, 100s, and 50s.
/// // Notice how all 10 objects will be processed in one go.
/// state.n300 += 2;
/// state.n100 += 7;
/// state.n50 += 1;
///
/// // The `nth` method takes a zero-based value.
/// let attrs = gradual.nth(state.clone(), 9).unwrap();
/// println!("PP: {}", attrs.pp());
///
/// // Now comes another 300. Note that the max combo gets incremented again.
/// state.n300 += 1;
/// state.max_combo += 1;
///
/// let attrs = gradual.next(state.clone()).unwrap();
/// println!("PP: {}", attrs.pp());
///
/// // Skip to the end
/// # /*
/// state.max_combo = ...
/// state.n300 = ...
/// ...
/// # */
/// let attrs = gradual.last(state.clone()).unwrap();
/// println!("PP: {}", attrs.pp());
///
/// // Once the final performance has been calculated, attempting to process
/// // further objects will return `None`.
/// assert!(gradual.next(state).is_none());
/// ```
///
/// [`next`]: GradualPerformance::next
/// [`nth`]: GradualPerformance::nth
/// [`GradualDifficulty`]: crate::GradualDifficulty
// 504 vs 184 bytes is an acceptable difference and the Osu variant (424 bytes)
// is likely the most used one anyway.
#[allow(clippy::large_enum_variant)]
pub enum GradualPerformance {
    Osu(OsuGradualPerformance),
    Taiko(TaikoGradualPerformance),
    Catch(CatchGradualPerformance),
    Mania(ManiaGradualPerformance),
}

macro_rules! from_converted {
    ( $fn:ident, $mode:ident, $converted:ident ) => {
        #[doc = concat!("Create a [`GradualPerformance`] for a [`", stringify!($converted), "`]")]
        pub fn $fn(difficulty: Difficulty, converted: &$converted<'_>) -> Self {
            Self::$mode($mode::gradual_performance(difficulty, converted))
        }
    };
}

impl GradualPerformance {
    /// Create a [`GradualPerformance`] for a map of any mode.
    pub fn new(difficulty: Difficulty, map: &Beatmap) -> Self {
        let map = Cow::Borrowed(map);

        match map.mode {
            GameMode::Osu => Self::Osu(Osu::gradual_performance(difficulty, &Converted::new(map))),
            GameMode::Taiko => {
                Self::Taiko(Taiko::gradual_performance(difficulty, &Converted::new(map)))
            }
            GameMode::Catch => {
                Self::Catch(Catch::gradual_performance(difficulty, &Converted::new(map)))
            }
            GameMode::Mania => {
                Self::Mania(Mania::gradual_performance(difficulty, &Converted::new(map)))
            }
        }
    }

    from_converted!(from_osu_map, Osu, OsuBeatmap);
    from_converted!(from_taiko_map, Taiko, TaikoBeatmap);
    from_converted!(from_catch_map, Catch, CatchBeatmap);
    from_converted!(from_mania_map, Mania, ManiaBeatmap);

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

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

    /// Process everything up to the next `n`th hitobject 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: ScoreState, n: usize) -> Option<PerformanceAttributes> {
        match self {
            GradualPerformance::Osu(gradual) => {
                gradual.nth(state.into(), n).map(PerformanceAttributes::Osu)
            }
            GradualPerformance::Taiko(gradual) => gradual
                .nth(state.into(), n)
                .map(PerformanceAttributes::Taiko),
            GradualPerformance::Catch(gradual) => gradual
                .nth(state.into(), n)
                .map(PerformanceAttributes::Catch),
            GradualPerformance::Mania(gradual) => gradual
                .nth(state.into(), n)
                .map(PerformanceAttributes::Mania),
        }
    }

    /// Returns the amount of remaining objects.
    #[allow(clippy::len_without_is_empty)]
    pub fn len(&self) -> usize {
        match self {
            GradualPerformance::Osu(gradual) => gradual.len(),
            GradualPerformance::Taiko(gradual) => gradual.len(),
            GradualPerformance::Catch(gradual) => gradual.len(),
            GradualPerformance::Mania(gradual) => gradual.len(),
        }
    }
}