restructured StarResult to unify modes

This commit is contained in:
MaxOhn
2021-01-19 20:59:44 +01:00
parent bd6130d79c
commit f12a190d4f
11 changed files with 98 additions and 80 deletions
+9 -2
View File
@@ -16,7 +16,7 @@ pub fn stars(map: &Beatmap, mods: impl Mods, passed_objects: Option<usize>) -> S
let take = passed_objects.unwrap_or_else(|| map.hit_objects.len());
if take < 2 {
return StarResult::Mania { stars: 0.0 };
return StarResult::Mania(DifficultyAttributes { stars: 0.0 });
}
let clock_rate = mods.speed();
@@ -60,7 +60,7 @@ pub fn stars(map: &Beatmap, mods: impl Mods, passed_objects: Option<usize>) -> S
let stars = strain.difficulty_value() * STAR_SCALING_FACTOR;
StarResult::Mania { stars }
return StarResult::Mania(DifficultyAttributes { stars });
}
/// Essentially the same as the `stars` function but instead of
@@ -136,6 +136,13 @@ impl<'o> DifficultyHitObject<'o> {
}
}
/// Various data created through the star calculation.
/// This data is necessary to calculate PP.
#[derive(Clone, Debug, Default)]
pub struct DifficultyAttributes {
pub stars: f32,
}
#[cfg(test)]
mod tests {
use super::*;
+14 -8
View File
@@ -1,4 +1,4 @@
use super::stars;
use super::{stars, DifficultyAttributes};
use crate::{Beatmap, Mods, PpResult, StarResult};
pub trait ManiaStarProvider {
@@ -11,10 +11,16 @@ impl ManiaStarProvider for f32 {
}
}
impl ManiaStarProvider for DifficultyAttributes {
fn attributes(self) -> Option<f32> {
Some(self.stars)
}
}
impl ManiaStarProvider for StarResult {
fn attributes(self) -> Option<f32> {
if let Self::Mania { stars } = self {
Some(stars)
if let Self::Mania(attributes) = self {
Some(attributes.stars)
} else {
None
}
@@ -49,13 +55,13 @@ impl<'m> ManiaPP<'m> {
}
/// [`ManiaStarsProvider`] is implemented by `f32`, [`StarResult`](crate::StarResult),
/// and by [`PpResult`](crate::PpResult) meaning you can give the
/// result of a star calculation or a pp calculation.
/// and by [`PpResult`](crate::PpResult) meaning you can give the star rating,
/// the result of a star calculation, or the result of a pp calculation.
/// If you already calculated the attributes for the current map-mod combination,
/// be sure to put them in here so that they don't have to be recalculated.
#[inline]
pub fn stars(mut self, stars: impl ManiaStarProvider) -> Self {
if let Some(stars) = stars.attributes() {
pub fn attributes(mut self, attributes: impl ManiaStarProvider) -> Self {
if let Some(stars) = attributes.attributes() {
self.stars.replace(stars);
}
@@ -141,7 +147,7 @@ impl<'m> ManiaPP<'m> {
PpResult {
pp,
attributes: StarResult::Mania { stars },
attributes: StarResult::Mania(DifficultyAttributes { stars }),
}
}