refactor: create performance calculators through traits (#33)

* create perf calc through IntoPerformance traits

* added tests

* adjusted documentation

* fix doctest

* simplify From impls
This commit is contained in:
Badewanne3
2024-04-01 19:14:38 +02:00
committed by GitHub
parent 616f2b5758
commit 9acf85e934
24 changed files with 772 additions and 446 deletions
+1 -1
View File
@@ -34,7 +34,7 @@ let diff_attrs = rosu_pp::Difficulty::new()
let stars = diff_attrs.stars(); let stars = diff_attrs.stars();
// Calculate performance attributes // Calculate performance attributes
let perf_attrs = rosu_pp::Performance::from_attributes(diff_attrs) let perf_attrs = rosu_pp::Performance::new(diff_attrs)
// To speed up the calculation, we used the previous attributes. // To speed up the calculation, we used the previous attributes.
// **Note** that this should only be done if the map and all difficulty // **Note** that this should only be done if the map and all difficulty
// settings stay the same, otherwise the final attributes will be incorrect! // settings stay the same, otherwise the final attributes will be incorrect!
+3 -47
View File
@@ -5,7 +5,7 @@ use crate::{
taiko::{TaikoDifficultyAttributes, TaikoPerformanceAttributes}, taiko::{TaikoDifficultyAttributes, TaikoPerformanceAttributes},
}; };
use super::performance::Performance; use super::performance::{into::IntoPerformance, Performance};
/// The result of a difficulty calculation based on the mode. /// The result of a difficulty calculation based on the mode.
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
@@ -43,7 +43,7 @@ impl DifficultyAttributes {
/// Returns a builder for performance calculation. /// Returns a builder for performance calculation.
pub fn performance<'a>(self) -> Performance<'a> { pub fn performance<'a>(self) -> Performance<'a> {
self.into() self.into_performance()
} }
} }
@@ -103,7 +103,7 @@ impl PerformanceAttributes {
/// Returns a builder for performance calculation. /// Returns a builder for performance calculation.
pub fn performance<'a>(self) -> Performance<'a> { pub fn performance<'a>(self) -> Performance<'a> {
self.into() self.into_performance()
} }
} }
@@ -112,47 +112,3 @@ impl From<PerformanceAttributes> for DifficultyAttributes {
attrs.difficulty_attributes() attrs.difficulty_attributes()
} }
} }
/// Abstract type to provide flexibility when passing difficulty attributes to a performance calculation.
pub trait AttributeProvider {
/// Provide the actual difficulty attributes.
fn attributes(self) -> DifficultyAttributes;
}
impl AttributeProvider for DifficultyAttributes {
fn attributes(self) -> DifficultyAttributes {
self
}
}
impl AttributeProvider for PerformanceAttributes {
fn attributes(self) -> DifficultyAttributes {
match self {
Self::Osu(attrs) => DifficultyAttributes::Osu(attrs.difficulty),
Self::Taiko(attrs) => DifficultyAttributes::Taiko(attrs.difficulty),
Self::Catch(attrs) => DifficultyAttributes::Catch(attrs.difficulty),
Self::Mania(attrs) => DifficultyAttributes::Mania(attrs.difficulty),
}
}
}
macro_rules! impl_attr_provider {
( $mode:ident: $difficulty:ident, $performance:ident ) => {
impl AttributeProvider for $difficulty {
fn attributes(self) -> DifficultyAttributes {
DifficultyAttributes::$mode(self)
}
}
impl AttributeProvider for $performance {
fn attributes(self) -> DifficultyAttributes {
DifficultyAttributes::$mode(self.difficulty)
}
}
};
}
impl_attr_provider!(Catch: CatchDifficultyAttributes, CatchPerformanceAttributes);
impl_attr_provider!(Mania: ManiaDifficultyAttributes, ManiaPerformanceAttributes);
impl_attr_provider!(Osu: OsuDifficultyAttributes, OsuPerformanceAttributes);
impl_attr_provider!(Taiko: TaikoDifficultyAttributes, TaikoPerformanceAttributes);
+6 -2
View File
@@ -1,10 +1,14 @@
pub use self::{ pub use self::{
attributes::{AttributeProvider, DifficultyAttributes, PerformanceAttributes}, attributes::{DifficultyAttributes, PerformanceAttributes},
difficulty::{ difficulty::{
converted::ConvertedDifficulty, gradual::GradualDifficulty, inspect::InspectDifficulty, converted::ConvertedDifficulty, gradual::GradualDifficulty, inspect::InspectDifficulty,
Difficulty, ModsDependent, Difficulty, ModsDependent,
}, },
performance::{gradual::GradualPerformance, HitResultPriority, Performance}, performance::{
gradual::GradualPerformance,
into::{IntoModePerformance, IntoPerformance},
HitResultPriority, Performance,
},
score_state::ScoreState, score_state::ScoreState,
strains::Strains, strains::Strains,
}; };
+157
View File
@@ -0,0 +1,157 @@
use std::borrow::Cow;
use rosu_map::section::general::GameMode;
use crate::{
any::{DifficultyAttributes, PerformanceAttributes},
model::mode::IGameMode,
Beatmap, Converted, Performance,
};
/// Turning a type into the generic [`IGameMode`]'s performance calculator.
pub trait IntoModePerformance<'map, M: IGameMode> {
fn into_performance(self) -> M::Performance<'map>;
}
/// Turning a type into a performance calculator of any mode.
pub trait IntoPerformance<'a> {
fn into_performance(self) -> Performance<'a>;
}
macro_rules! impl_from_mode {
(
$(
$module:ident {
$mode:ident, $diff:ident, $perf:ident
}
,)*
) => {
$(
macro_rules! mode {
() => { crate::$module::$mode };
}
impl<'map> IntoModePerformance<'map, mode!()> for Converted<'map, mode!()> {
fn into_performance(self) -> <mode!() as IGameMode>::Performance<'map> {
<mode!() as IGameMode>::Performance::from_map_or_attrs(self.into())
}
}
impl<'map> IntoModePerformance<'map, mode!()> for &'map Converted<'_, mode!()> {
fn into_performance(self) -> <mode!() as IGameMode>::Performance<'map> {
<mode!() as IGameMode>::Performance::from_map_or_attrs(self.as_owned().into())
}
}
impl<'map> IntoModePerformance<'map, mode!()> for crate::$module::$diff {
fn into_performance(self) -> <mode!() as IGameMode>::Performance<'map> {
<mode!() as IGameMode>::Performance::from_map_or_attrs(self.into())
}
}
impl<'map> IntoModePerformance<'map, mode!()> for crate::$module::$perf {
fn into_performance(self) -> <mode!() as IGameMode>::Performance<'map> {
<mode!() as IGameMode>::Performance::from_map_or_attrs(self.difficulty.into())
}
}
impl<'map> IntoPerformance<'map> for Converted<'map, mode!()> {
fn into_performance(self) -> Performance<'map> {
Performance::$mode(
<Self as IntoModePerformance<'map, mode!()>>::into_performance(self)
)
}
}
impl<'map> IntoPerformance<'map> for &'map Converted<'_, mode!()> {
fn into_performance(self) -> Performance<'map> {
Performance::$mode(
<Self as IntoModePerformance<'map, mode!()>>::into_performance(self)
)
}
}
impl<'a> IntoPerformance<'a> for crate::$module::$diff {
fn into_performance(self) -> Performance<'a> {
Performance::$mode(
<Self as IntoModePerformance<'a, mode!()>>::into_performance(self)
)
}
}
impl<'a> IntoPerformance<'a> for crate::$module::$perf {
fn into_performance(self) -> Performance<'a> {
Performance::$mode(
<Self as IntoModePerformance<'a, mode!()>>::into_performance(self)
)
}
}
)*
};
}
impl_from_mode!(
osu {
Osu,
OsuDifficultyAttributes,
OsuPerformanceAttributes
},
taiko {
Taiko,
TaikoDifficultyAttributes,
TaikoPerformanceAttributes
},
catch {
Catch,
CatchDifficultyAttributes,
CatchPerformanceAttributes
},
mania {
Mania,
ManiaDifficultyAttributes,
ManiaPerformanceAttributes
},
);
impl<'a> IntoPerformance<'a> for Beatmap {
fn into_performance(self) -> Performance<'a> {
map_to_performance(self.mode, Cow::Owned(self))
}
}
impl<'map> IntoPerformance<'map> for &'map Beatmap {
fn into_performance(self) -> Performance<'map> {
map_to_performance(self.mode, Cow::Borrowed(self))
}
}
fn map_to_performance(mode: GameMode, map: Cow<'_, Beatmap>) -> Performance<'_> {
match mode {
GameMode::Osu => Performance::Osu(Converted::new(map).into()),
GameMode::Taiko => Performance::Taiko(Converted::new(map).into()),
GameMode::Catch => Performance::Catch(Converted::new(map).into()),
GameMode::Mania => Performance::Mania(Converted::new(map).into()),
}
}
impl<'a> IntoPerformance<'a> for DifficultyAttributes {
fn into_performance(self) -> Performance<'a> {
match self {
Self::Osu(attrs) => Performance::Osu(attrs.into()),
Self::Taiko(attrs) => Performance::Taiko(attrs.into()),
Self::Catch(attrs) => Performance::Catch(attrs.into()),
Self::Mania(attrs) => Performance::Mania(attrs.into()),
}
}
}
impl<'a> IntoPerformance<'a> for PerformanceAttributes {
fn into_performance(self) -> Performance<'a> {
match self {
Self::Osu(attrs) => Performance::Osu(attrs.difficulty.into()),
Self::Taiko(attrs) => Performance::Taiko(attrs.difficulty.into()),
Self::Catch(attrs) => Performance::Catch(attrs.difficulty.into()),
Self::Mania(attrs) => Performance::Mania(attrs.difficulty.into()),
}
}
}
+99 -79
View File
@@ -1,23 +1,16 @@
use std::borrow::Cow;
use rosu_map::section::general::GameMode; use rosu_map::section::general::GameMode;
use crate::{ use crate::{
any::attributes::DifficultyAttributes, catch::CatchPerformance, mania::ManiaPerformance, osu::OsuPerformance, taiko::TaikoPerformance,
catch::{Catch, CatchPerformance},
mania::{Mania, ManiaPerformance},
model::beatmap::{Beatmap, Converted},
osu::{Osu, OsuPerformance},
taiko::{Taiko, TaikoPerformance},
Difficulty, Difficulty,
}; };
use super::{ use self::into::IntoPerformance;
attributes::{AttributeProvider, PerformanceAttributes},
score_state::ScoreState, use super::{attributes::PerformanceAttributes, score_state::ScoreState};
};
pub mod gradual; pub mod gradual;
pub mod into;
/// Performance calculator on maps of any mode. /// Performance calculator on maps of any mode.
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
@@ -30,43 +23,29 @@ pub enum Performance<'map> {
} }
impl<'map> Performance<'map> { impl<'map> Performance<'map> {
/// Create a new performance calculator for maps of any mode. /// Create a new performance calculator for any mode.
/// ///
/// Note that creating [`Performance`] this way will require to perform the /// The argument `map_or_attrs` must be either
/// costly computation of difficulty attributes internally. If attributes /// - previously calculated attributes ([`DifficultyAttributes`],
/// for the current [`Difficulty`] settings are already available, consider /// [`PerformanceAttributes`], or mode-specific attributes like
/// using [`from_attributes`] instead. /// [`TaikoDifficultyAttributes`], [`ManiaPerformanceAttributes`], ...)
/// - a beatmap ([`Beatmap`] or [`Converted<'_, M>`])
/// ///
/// [`from_attributes`]: Self::from_attributes /// If a map is given, difficulty attributes will need to be calculated
pub const fn from_map(map: &'map Beatmap) -> Self { /// internally which is a costly operation. Hence, passing attributes
let mode = map.mode; /// should be prefered.
let map = Cow::Borrowed(map);
match mode {
GameMode::Osu => Self::Osu(OsuPerformance::from_map(Converted::new(map))),
GameMode::Taiko => Self::Taiko(TaikoPerformance::from_map(Converted::new(map))),
GameMode::Catch => Self::Catch(CatchPerformance::from_map(Converted::new(map))),
GameMode::Mania => Self::Mania(ManiaPerformance::from_map(Converted::new(map))),
}
}
/// Create a new performance calculator through previously calculated
/// attributes.
/// ///
/// Note that `attrs` must have been calculated for the same beatmap and /// However, when passing previously calculated attributes, make sure they
/// [`Difficulty`] settings, otherwise the final attributes will be /// have been calculated for the same map and [`Difficulty`] settings.
/// incorrect. /// Otherwise, the final attributes will be incorrect.
pub fn from_attributes(attrs: impl AttributeProvider) -> Self { ///
const fn inner(attrs: DifficultyAttributes) -> Performance<'static> { /// [`Beatmap`]: crate::model::beatmap::Beatmap
match attrs { /// [`Converted<'_, M>`]: crate::model::beatmap::Converted
DifficultyAttributes::Osu(attrs) => Performance::Osu(attrs.performance()), /// [`DifficultyAttributes`]: crate::any::DifficultyAttributes
DifficultyAttributes::Taiko(attrs) => Performance::Taiko(attrs.performance()), /// [`TaikoDifficultyAttributes`]: crate::taiko::TaikoDifficultyAttributes
DifficultyAttributes::Catch(attrs) => Performance::Catch(attrs.performance()), /// [`ManiaPerformanceAttributes`]: crate::mania::ManiaPerformanceAttributes
DifficultyAttributes::Mania(attrs) => Performance::Mania(attrs.performance()), pub fn new(map_or_attrs: impl IntoPerformance<'map>) -> Self {
} map_or_attrs.into_performance()
}
inner(attrs.attributes())
} }
/// Consume the performance calculator and calculate /// Consume the performance calculator and calculate
@@ -82,13 +61,12 @@ impl<'map> Performance<'map> {
/// Attempt to convert the map to the specified mode. /// Attempt to convert the map to the specified mode.
/// ///
/// Returns `Err(self)` if the conversion is incompatible or the internal /// Returns `Err(self)` if the conversion is incompatible or no beatmap is
/// beatmap was already replaced with difficulty attributes, i.e. if /// contained, i.e. if this [`Performance`] was created through attributes
/// [`Performance::from_attributes`] or [`Performance::generate_state`] was /// or [`Performance::generate_state`] was called.
/// called.
/// ///
/// If the given mode should be ignored in case it is incompatible or if /// If the given mode should be ignored in case of an error, use
/// the internal beatmap was replaced, use [`mode_or_ignore`] instead. /// [`mode_or_ignore`] instead.
/// ///
/// [`mode_or_ignore`]: Self::mode_or_ignore /// [`mode_or_ignore`]: Self::mode_or_ignore
// Both variants have the same size // Both variants have the same size
@@ -380,33 +358,6 @@ impl<'map> Performance<'map> {
} }
} }
impl<A: AttributeProvider> From<A> for Performance<'_> {
fn from(attrs: A) -> Self {
Self::from_attributes(attrs)
}
}
macro_rules! impl_from_converted {
( $mode:ident ) => {
impl<'a> From<Converted<'a, $mode>> for Performance<'a> {
fn from(converted: Converted<'a, $mode>) -> Self {
Self::$mode(converted.into())
}
}
impl<'a, 'b: 'a> From<&'b Converted<'a, $mode>> for Performance<'a> {
fn from(converted: &'b Converted<'a, $mode>) -> Self {
Self::$mode(converted.as_owned().into())
}
}
};
}
impl_from_converted!(Osu);
impl_from_converted!(Taiko);
impl_from_converted!(Catch);
impl_from_converted!(Mania);
/// While generating remaining hitresults, decide how they should be distributed. /// While generating remaining hitresults, decide how they should be distributed.
#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum HitResultPriority { pub enum HitResultPriority {
@@ -425,3 +376,72 @@ impl Default for HitResultPriority {
Self::DEFAULT Self::DEFAULT
} }
} }
impl<'a, T: IntoPerformance<'a>> From<T> for Performance<'a> {
fn from(into: T) -> Self {
into.into_performance()
}
}
#[cfg(test)]
mod tests {
use crate::{
any::DifficultyAttributes,
catch::{CatchDifficultyAttributes, CatchPerformanceAttributes},
mania::{ManiaDifficultyAttributes, ManiaPerformanceAttributes},
osu::{OsuDifficultyAttributes, OsuPerformanceAttributes},
taiko::{Taiko, TaikoDifficultyAttributes, TaikoPerformanceAttributes},
Beatmap,
};
use super::*;
#[test]
fn create() {
let map = Beatmap::from_path("./resources/1028484.osu").unwrap();
let converted = map.unchecked_as_converted::<Taiko>();
let _ = Performance::new(&converted);
let _ = Performance::new(converted.as_owned());
let _ = Performance::new(&map);
let _ = Performance::new(map.clone());
let _ = Performance::new(OsuDifficultyAttributes::default());
let _ = Performance::new(TaikoDifficultyAttributes::default());
let _ = Performance::new(CatchDifficultyAttributes::default());
let _ = Performance::new(ManiaDifficultyAttributes::default());
let _ = Performance::new(OsuPerformanceAttributes::default());
let _ = Performance::new(TaikoPerformanceAttributes::default());
let _ = Performance::new(CatchPerformanceAttributes::default());
let _ = Performance::new(ManiaPerformanceAttributes::default());
let _ = Performance::new(DifficultyAttributes::Osu(OsuDifficultyAttributes::default()));
let _ = Performance::new(PerformanceAttributes::Taiko(
TaikoPerformanceAttributes::default(),
));
let _ = Performance::from(&converted);
let _ = Performance::from(converted);
let _ = Performance::from(&map);
let _ = Performance::from(map);
let _ = Performance::from(OsuDifficultyAttributes::default());
let _ = Performance::from(TaikoDifficultyAttributes::default());
let _ = Performance::from(CatchDifficultyAttributes::default());
let _ = Performance::from(ManiaDifficultyAttributes::default());
let _ = Performance::from(OsuPerformanceAttributes::default());
let _ = Performance::from(TaikoPerformanceAttributes::default());
let _ = Performance::from(CatchPerformanceAttributes::default());
let _ = Performance::from(ManiaPerformanceAttributes::default());
let _ = Performance::from(DifficultyAttributes::Osu(OsuDifficultyAttributes::default()));
let _ = Performance::from(PerformanceAttributes::Taiko(
TaikoPerformanceAttributes::default(),
));
let _ = DifficultyAttributes::Osu(OsuDifficultyAttributes::default()).performance();
let _ = PerformanceAttributes::Taiko(TaikoPerformanceAttributes::default()).performance();
}
}
+4 -4
View File
@@ -35,8 +35,8 @@ impl CatchDifficultyAttributes {
} }
/// Returns a builder for performance calculation. /// Returns a builder for performance calculation.
pub const fn performance<'a>(self) -> CatchPerformance<'a> { pub fn performance<'a>(self) -> CatchPerformance<'a> {
CatchPerformance::from_attributes(self) self.into()
} }
pub(crate) fn set_object_count(&mut self, count: &ObjectCount) { pub(crate) fn set_object_count(&mut self, count: &ObjectCount) {
@@ -89,8 +89,8 @@ impl CatchPerformanceAttributes {
} }
/// Returns a builder for performance calculation. /// Returns a builder for performance calculation.
pub const fn performance<'a>(self) -> CatchPerformance<'a> { pub fn performance<'a>(self) -> CatchPerformance<'a> {
CatchPerformance::from_attributes(self.difficulty) self.difficulty.into()
} }
} }
+1 -1
View File
@@ -58,7 +58,7 @@ impl IGameMode for Catch {
} }
fn performance(map: CatchBeatmap<'_>) -> Self::Performance<'_> { fn performance(map: CatchBeatmap<'_>) -> Self::Performance<'_> {
CatchPerformance::from_map(map) CatchPerformance::new(map)
} }
fn gradual_difficulty( fn gradual_difficulty(
+1 -1
View File
@@ -176,7 +176,7 @@ mod tests {
assert_eq!(next_gradual, next_gradual_3rd); assert_eq!(next_gradual, next_gradual_3rd);
} }
let regular_calc = CatchPerformance::from_map(converted.as_owned()) let regular_calc = CatchPerformance::new(converted.as_owned())
.difficulty(difficulty.clone()) .difficulty(difficulty.clone())
.passed_objects(i as u32) .passed_objects(i as u32)
.state(state.clone()); .state(state.clone());
+107 -73
View File
@@ -1,14 +1,14 @@
use std::cmp::{self, Ordering}; use std::cmp::{self, Ordering};
use crate::{ use crate::{
any::{AttributeProvider, Difficulty, DifficultyAttributes}, any::{Difficulty, IntoModePerformance, IntoPerformance},
osu::OsuPerformance, osu::OsuPerformance,
util::{map_or_attrs::MapOrAttrs, mods::Mods}, util::{map_or_attrs::MapOrAttrs, mods::Mods},
Performance,
}; };
use super::{ use super::{
attributes::{CatchDifficultyAttributes, CatchPerformanceAttributes}, attributes::{CatchDifficultyAttributes, CatchPerformanceAttributes},
convert::CatchBeatmap,
score_state::CatchScoreState, score_state::CatchScoreState,
Catch, Catch,
}; };
@@ -33,57 +33,38 @@ pub struct CatchPerformance<'map> {
impl<'map> CatchPerformance<'map> { impl<'map> CatchPerformance<'map> {
/// Create a new performance calculator for osu!catch maps. /// Create a new performance calculator for osu!catch maps.
/// ///
/// Note that creating [`CatchPerformance`] this way will require to /// The argument `map_or_attrs` must be either
/// perform the costly computation of [`CatchDifficultyAttributes`] /// - previously calculated attributes ([`CatchDifficultyAttributes`]
/// internally. If difficulty attributes for the current [`Difficulty`] /// or [`CatchPerformanceAttributes`])
/// settings are already available, consider using [`from_attributes`] or /// - a beatmap ([`CatchBeatmap<'map>`])
/// [`try_from_attributes`] instead.
/// ///
/// [`from_attributes`]: Self::from_attributes /// If a map is given, difficulty attributes will need to be calculated
/// [`try_from_attributes`]: Self::try_from_attributes /// internally which is a costly operation. Hence, passing attributes
pub const fn from_map(map: CatchBeatmap<'map>) -> Self { /// should be prefered.
Self { ///
map_or_attrs: MapOrAttrs::Map(map), /// However, when passing previously calculated attributes, make sure they
difficulty: Difficulty::new(), /// have been calculated for the same map and [`Difficulty`] settings.
acc: None, /// Otherwise, the final attributes will be incorrect.
combo: None, ///
fruits: None, /// [`CatchBeatmap<'map>`]: crate::catch::CatchBeatmap
droplets: None, pub fn new(map_or_attrs: impl IntoModePerformance<'map, Catch>) -> Self {
tiny_droplets: None, map_or_attrs.into_performance()
tiny_droplet_misses: None,
misses: None,
}
} }
/// Create a new performance calculator from difficulty attributes. /// Try to create a new performance calculator for osu!catch maps.
/// ///
/// Note that `attrs` must have been calculated for the same beatmap and /// Returns `None` if `map_or_attrs` does not belong to osu!catch e.g.
/// [`Difficulty`] settings, otherwise the final attributes will be /// a [`Converted`], [`DifficultyAttributes`], or [`PerformanceAttributes`]
/// incorrect. /// of a different mode.
pub const fn from_attributes(attrs: CatchDifficultyAttributes) -> Self {
Self {
map_or_attrs: MapOrAttrs::Attrs(attrs),
difficulty: Difficulty::new(),
acc: None,
combo: None,
fruits: None,
droplets: None,
tiny_droplets: None,
tiny_droplet_misses: None,
misses: None,
}
}
/// Try to create a new performance calculator from difficulty attributes.
/// ///
/// Note that `attrs` must have been calculated for the same beatmap and /// See [`CatchPerformance::new`] for more information.
/// [`Difficulty`] settings, otherwise the final attributes will be
/// incorrect.
/// ///
/// Returns `None` if `attrs` contained attributes of a different mode. /// [`Converted`]: crate::model::beatmap::Converted
pub fn try_from_attributes(attrs: impl AttributeProvider) -> Option<Self> { /// [`DifficultyAttributes`]: crate::any::DifficultyAttributes
if let DifficultyAttributes::Catch(attrs) = attrs.attributes() { /// [`PerformanceAttributes`]: crate::any::PerformanceAttributes
Some(Self::from_attributes(attrs)) pub fn try_new(map_or_attrs: impl IntoPerformance<'map>) -> Option<Self> {
if let Performance::Catch(calc) = map_or_attrs.into_performance() {
Some(calc)
} else { } else {
None None
} }
@@ -435,6 +416,20 @@ impl<'map> CatchPerformance<'map> {
inner.calculate() inner.calculate()
} }
pub(crate) const fn from_map_or_attrs(map_or_attrs: MapOrAttrs<'map, Catch>) -> Self {
Self {
map_or_attrs,
difficulty: Difficulty::new(),
acc: None,
combo: None,
fruits: None,
droplets: None,
tiny_droplets: None,
tiny_droplet_misses: None,
misses: None,
}
}
} }
impl<'map> TryFrom<OsuPerformance<'map>> for CatchPerformance<'map> { impl<'map> TryFrom<OsuPerformance<'map>> for CatchPerformance<'map> {
@@ -442,12 +437,9 @@ impl<'map> TryFrom<OsuPerformance<'map>> for CatchPerformance<'map> {
/// Try to create [`CatchPerformance`] through [`OsuPerformance`]. /// Try to create [`CatchPerformance`] through [`OsuPerformance`].
/// ///
/// Returns `None` if [`OsuPerformance`] already replaced its internal /// Returns `None` if [`OsuPerformance`] does not contain a beatmap, i.e.
/// beatmap with [`OsuDifficultyAttributes`], i.e. if /// if it was constructed through attributes or
/// [`OsuPerformance::from_attributes`] or [`OsuPerformance::generate_state`] /// [`OsuPerformance::generate_state`] was called.
/// was called.
///
/// [`OsuDifficultyAttributes`]: crate::osu::OsuDifficultyAttributes
fn try_from(mut osu: OsuPerformance<'map>) -> Result<Self, Self::Error> { fn try_from(mut osu: OsuPerformance<'map>) -> Result<Self, Self::Error> {
let MapOrAttrs::Map(converted) = osu.map_or_attrs else { let MapOrAttrs::Map(converted) = osu.map_or_attrs else {
return Err(osu); return Err(osu);
@@ -488,21 +480,9 @@ impl<'map> TryFrom<OsuPerformance<'map>> for CatchPerformance<'map> {
} }
} }
impl<'map> From<CatchBeatmap<'map>> for CatchPerformance<'map> { impl<'map, T: IntoModePerformance<'map, Catch>> From<T> for CatchPerformance<'map> {
fn from(map: CatchBeatmap<'map>) -> Self { fn from(into: T) -> Self {
Self::from_map(map) into.into_performance()
}
}
impl From<CatchDifficultyAttributes> for CatchPerformance<'_> {
fn from(attrs: CatchDifficultyAttributes) -> Self {
Self::from_attributes(attrs)
}
}
impl From<CatchPerformanceAttributes> for CatchPerformance<'_> {
fn from(attrs: CatchPerformanceAttributes) -> Self {
Self::from_attributes(attrs.difficulty)
} }
} }
@@ -606,8 +586,13 @@ mod test {
use std::sync::OnceLock; use std::sync::OnceLock;
use proptest::prelude::*; use proptest::prelude::*;
use rosu_map::section::general::GameMode;
use crate::Beatmap; use crate::{
any::{DifficultyAttributes, PerformanceAttributes},
osu::{Osu, OsuDifficultyAttributes, OsuPerformanceAttributes},
Beatmap,
};
use super::*; use super::*;
@@ -617,13 +602,14 @@ mod test {
const N_DROPLETS: u32 = 2; const N_DROPLETS: u32 = 2;
const N_TINY_DROPLETS: u32 = 291; const N_TINY_DROPLETS: u32 = 291;
fn beatmap() -> Beatmap {
Beatmap::from_path("./resources/2118524.osu").unwrap()
}
fn attrs() -> CatchDifficultyAttributes { fn attrs() -> CatchDifficultyAttributes {
ATTRS ATTRS
.get_or_init(|| { .get_or_init(|| {
let converted = Beatmap::from_path("./resources/2118524.osu") let converted = beatmap().unchecked_into_converted::<Catch>();
.unwrap()
.unchecked_into_converted::<Catch>();
let attrs = Difficulty::new().with_mode().calculate(&converted); let attrs = Difficulty::new().with_mode().calculate(&converted);
assert_eq!(N_FRUITS, attrs.n_fruits); assert_eq!(N_FRUITS, attrs.n_fruits);
@@ -815,4 +801,52 @@ mod test {
assert_eq!(state, expected); assert_eq!(state, expected);
} }
#[test]
fn create() {
let mut map = beatmap();
let converted = map.unchecked_as_converted();
let _ = CatchPerformance::new(CatchDifficultyAttributes::default());
let _ = CatchPerformance::new(CatchPerformanceAttributes::default());
let _ = CatchPerformance::new(&converted);
let _ = CatchPerformance::new(converted.as_owned());
let _ = CatchPerformance::try_new(CatchDifficultyAttributes::default()).unwrap();
let _ = CatchPerformance::try_new(CatchPerformanceAttributes::default()).unwrap();
let _ = CatchPerformance::try_new(DifficultyAttributes::Catch(
CatchDifficultyAttributes::default(),
))
.unwrap();
let _ = CatchPerformance::try_new(PerformanceAttributes::Catch(
CatchPerformanceAttributes::default(),
))
.unwrap();
let _ = CatchPerformance::try_new(&converted).unwrap();
let _ = CatchPerformance::try_new(converted.as_owned()).unwrap();
let _ = CatchPerformance::from(CatchDifficultyAttributes::default());
let _ = CatchPerformance::from(CatchPerformanceAttributes::default());
let _ = CatchPerformance::from(&converted);
let _ = CatchPerformance::from(converted);
let _ = CatchDifficultyAttributes::default().performance();
let _ = CatchPerformanceAttributes::default().performance();
map.mode = GameMode::Osu;
let converted = map.unchecked_as_converted::<Osu>();
assert!(CatchPerformance::try_new(OsuDifficultyAttributes::default()).is_none());
assert!(CatchPerformance::try_new(OsuPerformanceAttributes::default()).is_none());
assert!(CatchPerformance::try_new(DifficultyAttributes::Osu(
OsuDifficultyAttributes::default()
))
.is_none());
assert!(CatchPerformance::try_new(PerformanceAttributes::Osu(
OsuPerformanceAttributes::default()
))
.is_none());
assert!(CatchPerformance::try_new(&converted).is_none());
assert!(CatchPerformance::try_new(converted).is_none());
}
} }
+1 -1
View File
@@ -28,7 +28,7 @@
//! let stars = diff_attrs.stars(); //! let stars = diff_attrs.stars();
//! //!
//! // Calculate performance attributes //! // Calculate performance attributes
//! let perf_attrs = rosu_pp::Performance::from_attributes(diff_attrs) //! let perf_attrs = rosu_pp::Performance::new(diff_attrs)
//! // To speed up the calculation, we used the previous attributes. //! // To speed up the calculation, we used the previous attributes.
//! // **Note** that this should only be done if the map and all difficulty //! // **Note** that this should only be done if the map and all difficulty
//! // settings stay the same, otherwise the final attributes will be incorrect! //! // settings stay the same, otherwise the final attributes will be incorrect!
+4 -4
View File
@@ -36,8 +36,8 @@ impl ManiaDifficultyAttributes {
} }
/// Returns a builder for performance calculation. /// Returns a builder for performance calculation.
pub const fn performance<'a>(self) -> ManiaPerformance<'a> { pub fn performance<'a>(self) -> ManiaPerformance<'a> {
ManiaPerformance::from_attributes(self) self.into()
} }
} }
@@ -81,8 +81,8 @@ impl ManiaPerformanceAttributes {
} }
/// Returns a builder for performance calculation. /// Returns a builder for performance calculation.
pub const fn performance<'a>(self) -> ManiaPerformance<'a> { pub fn performance<'a>(self) -> ManiaPerformance<'a> {
ManiaPerformance::from_attributes(self.difficulty) self.difficulty.into()
} }
} }
+1 -1
View File
@@ -55,7 +55,7 @@ impl IGameMode for Mania {
} }
fn performance(map: ManiaBeatmap<'_>) -> Self::Performance<'_> { fn performance(map: ManiaBeatmap<'_>) -> Self::Performance<'_> {
ManiaPerformance::from_map(map) ManiaPerformance::new(map)
} }
fn gradual_difficulty( fn gradual_difficulty(
+1 -1
View File
@@ -163,7 +163,7 @@ mod tests {
assert_eq!(next_gradual, next_gradual_3rd); assert_eq!(next_gradual, next_gradual_3rd);
} }
let mut regular_calc = ManiaPerformance::from_map(converted.as_owned()) let mut regular_calc = ManiaPerformance::new(converted.as_owned())
.difficulty(difficulty.clone()) .difficulty(difficulty.clone())
.passed_objects(i as u32) .passed_objects(i as u32)
.state(state.clone()); .state(state.clone());
+108 -75
View File
@@ -1,14 +1,14 @@
use std::cmp; use std::cmp;
use crate::{ use crate::{
any::{AttributeProvider, Difficulty, DifficultyAttributes, HitResultPriority}, any::{Difficulty, HitResultPriority, IntoModePerformance, IntoPerformance},
osu::OsuPerformance, osu::OsuPerformance,
util::{map_or_attrs::MapOrAttrs, mods::Mods}, util::{map_or_attrs::MapOrAttrs, mods::Mods},
Performance,
}; };
use super::{ use super::{
attributes::{ManiaDifficultyAttributes, ManiaPerformanceAttributes}, attributes::{ManiaDifficultyAttributes, ManiaPerformanceAttributes},
convert::ManiaBeatmap,
score_state::ManiaScoreState, score_state::ManiaScoreState,
Mania, Mania,
}; };
@@ -34,59 +34,38 @@ pub struct ManiaPerformance<'map> {
impl<'map> ManiaPerformance<'map> { impl<'map> ManiaPerformance<'map> {
/// Create a new performance calculator for osu!mania maps. /// Create a new performance calculator for osu!mania maps.
/// ///
/// Note that creating [`ManiaPerformance`] this way will require to /// The argument `map_or_attrs` must be either
/// perform the costly computation of [`ManiaDifficultyAttributes`] /// - previously calculated attributes ([`ManiaDifficultyAttributes`]
/// internally. If difficulty attributes for the current [`Difficulty`] /// or [`ManiaPerformanceAttributes`])
/// settings are already available, consider using [`from_attributes`] or /// - a beatmap ([`ManiaBeatmap<'map>`])
/// [`try_from_attributes`] instead.
/// ///
/// [`from_attributes`]: Self::from_attributes /// If a map is given, difficulty attributes will need to be calculated
/// [`try_from_attributes`]: Self::try_from_attributes /// internally which is a costly operation. Hence, passing attributes
pub const fn from_map(map: ManiaBeatmap<'map>) -> Self { /// should be prefered.
Self { ///
map_or_attrs: MapOrAttrs::Map(map), /// However, when passing previously calculated attributes, make sure they
difficulty: Difficulty::new(), /// have been calculated for the same map and [`Difficulty`] settings.
n320: None, /// Otherwise, the final attributes will be incorrect.
n300: None, ///
n200: None, /// [`ManiaBeatmap<'map>`]: crate::mania::ManiaBeatmap
n100: None, pub fn new(map_or_attrs: impl IntoModePerformance<'map, Mania>) -> Self {
n50: None, map_or_attrs.into_performance()
misses: None,
acc: None,
hitresult_priority: HitResultPriority::DEFAULT,
}
} }
/// Create a new performance calculator from difficulty attributes. /// Try to create a new performance calculator for osu!mania maps.
/// ///
/// Note that `attrs` must have been calculated for the same beatmap and /// Returns `None` if `map_or_attrs` does not belong to osu!mania e.g.
/// [`Difficulty`] settings, otherwise the final attributes will be /// a [`Converted`], [`DifficultyAttributes`], or [`PerformanceAttributes`]
/// incorrect. /// of a different mode.
pub const fn from_attributes(attrs: ManiaDifficultyAttributes) -> Self {
Self {
map_or_attrs: MapOrAttrs::Attrs(attrs),
difficulty: Difficulty::new(),
n320: None,
n300: None,
n200: None,
n100: None,
n50: None,
misses: None,
acc: None,
hitresult_priority: HitResultPriority::DEFAULT,
}
}
/// Try to create a new performance calculator from difficulty attributes.
/// ///
/// Note that `attrs` must have been calculated for the same beatmap and /// See [`ManiaPerformance::new`] for more information.
/// [`Difficulty`] settings, otherwise the final attributes will be
/// incorrect.
/// ///
/// Returns `None` if `attrs` contained attributes of a different mode. /// [`Converted`]: crate::model::beatmap::Converted
pub fn try_from_attributes(attrs: impl AttributeProvider) -> Option<Self> { /// [`DifficultyAttributes`]: crate::any::DifficultyAttributes
if let DifficultyAttributes::Mania(attrs) = attrs.attributes() { /// [`PerformanceAttributes`]: crate::any::PerformanceAttributes
Some(Self::from_attributes(attrs)) pub fn try_new(map_or_attrs: impl IntoPerformance<'map>) -> Option<Self> {
if let Performance::Mania(calc) = map_or_attrs.into_performance() {
Some(calc)
} else { } else {
None None
} }
@@ -789,6 +768,21 @@ impl<'map> ManiaPerformance<'map> {
inner.calculate() inner.calculate()
} }
pub(crate) const fn from_map_or_attrs(map_or_attrs: MapOrAttrs<'map, Mania>) -> Self {
Self {
map_or_attrs,
difficulty: Difficulty::new(),
n320: None,
n300: None,
n200: None,
n100: None,
n50: None,
misses: None,
acc: None,
hitresult_priority: HitResultPriority::DEFAULT,
}
}
} }
impl<'map> TryFrom<OsuPerformance<'map>> for ManiaPerformance<'map> { impl<'map> TryFrom<OsuPerformance<'map>> for ManiaPerformance<'map> {
@@ -796,12 +790,9 @@ impl<'map> TryFrom<OsuPerformance<'map>> for ManiaPerformance<'map> {
/// Try to create [`ManiaPerformance`] through [`OsuPerformance`]. /// Try to create [`ManiaPerformance`] through [`OsuPerformance`].
/// ///
/// Returns `None` if [`OsuPerformance`] already replaced its internal /// Returns `None` if [`OsuPerformance`] does not contain a beatmap, i.e.
/// beatmap with [`OsuDifficultyAttributes`], i.e. if /// if it was constructed through attributes or
/// [`OsuPerformance::from_attributes`] or [`OsuPerformance::generate_state`] /// [`OsuPerformance::generate_state`] was called.
/// was called.
///
/// [`OsuDifficultyAttributes`]: crate::osu::OsuDifficultyAttributes
fn try_from(mut osu: OsuPerformance<'map>) -> Result<Self, Self::Error> { fn try_from(mut osu: OsuPerformance<'map>) -> Result<Self, Self::Error> {
let MapOrAttrs::Map(converted) = osu.map_or_attrs else { let MapOrAttrs::Map(converted) = osu.map_or_attrs else {
return Err(osu); return Err(osu);
@@ -843,21 +834,9 @@ impl<'map> TryFrom<OsuPerformance<'map>> for ManiaPerformance<'map> {
} }
} }
impl<'map> From<ManiaBeatmap<'map>> for ManiaPerformance<'map> { impl<'map, T: IntoModePerformance<'map, Mania>> From<T> for ManiaPerformance<'map> {
fn from(map: ManiaBeatmap<'map>) -> Self { fn from(into: T) -> Self {
Self::from_map(map) into.into_performance()
}
}
impl From<ManiaDifficultyAttributes> for ManiaPerformance<'_> {
fn from(attrs: ManiaDifficultyAttributes) -> Self {
Self::from_attributes(attrs)
}
}
impl From<ManiaPerformanceAttributes> for ManiaPerformance<'_> {
fn from(attrs: ManiaPerformanceAttributes) -> Self {
Self::from_attributes(attrs.difficulty)
} }
} }
@@ -943,8 +922,13 @@ mod tests {
use std::{cmp::Ordering, sync::OnceLock}; use std::{cmp::Ordering, sync::OnceLock};
use proptest::prelude::*; use proptest::prelude::*;
use rosu_map::section::general::GameMode;
use crate::Beatmap; use crate::{
any::{DifficultyAttributes, PerformanceAttributes},
osu::{Osu, OsuDifficultyAttributes, OsuPerformanceAttributes},
Beatmap,
};
use super::*; use super::*;
@@ -952,13 +936,14 @@ mod tests {
const N_OBJECTS: u32 = 594; const N_OBJECTS: u32 = 594;
fn beatmap() -> Beatmap {
Beatmap::from_path("./resources/1638954.osu").unwrap()
}
fn attrs() -> ManiaDifficultyAttributes { fn attrs() -> ManiaDifficultyAttributes {
ATTRS ATTRS
.get_or_init(|| { .get_or_init(|| {
let converted = Beatmap::from_path("./resources/1638954.osu") let converted = beatmap().unchecked_into_converted::<Mania>();
.unwrap()
.unchecked_into_converted::<Mania>();
let attrs = Difficulty::new().with_mode().calculate(&converted); let attrs = Difficulty::new().with_mode().calculate(&converted);
assert_eq!(N_OBJECTS, converted.hit_objects.len() as u32); assert_eq!(N_OBJECTS, converted.hit_objects.len() as u32);
@@ -1294,4 +1279,52 @@ mod tests {
assert_eq!(state, expected); assert_eq!(state, expected);
} }
#[test]
fn create() {
let mut map = beatmap();
let converted = map.unchecked_as_converted();
let _ = ManiaPerformance::new(ManiaDifficultyAttributes::default());
let _ = ManiaPerformance::new(ManiaPerformanceAttributes::default());
let _ = ManiaPerformance::new(&converted);
let _ = ManiaPerformance::new(converted.as_owned());
let _ = ManiaPerformance::try_new(ManiaDifficultyAttributes::default()).unwrap();
let _ = ManiaPerformance::try_new(ManiaPerformanceAttributes::default()).unwrap();
let _ = ManiaPerformance::try_new(DifficultyAttributes::Mania(
ManiaDifficultyAttributes::default(),
))
.unwrap();
let _ = ManiaPerformance::try_new(PerformanceAttributes::Mania(
ManiaPerformanceAttributes::default(),
))
.unwrap();
let _ = ManiaPerformance::try_new(&converted).unwrap();
let _ = ManiaPerformance::try_new(converted.as_owned()).unwrap();
let _ = ManiaPerformance::from(ManiaDifficultyAttributes::default());
let _ = ManiaPerformance::from(ManiaPerformanceAttributes::default());
let _ = ManiaPerformance::from(&converted);
let _ = ManiaPerformance::from(converted);
let _ = ManiaDifficultyAttributes::default().performance();
let _ = ManiaPerformanceAttributes::default().performance();
map.mode = GameMode::Osu;
let converted = map.unchecked_as_converted::<Osu>();
assert!(ManiaPerformance::try_new(OsuDifficultyAttributes::default()).is_none());
assert!(ManiaPerformance::try_new(OsuPerformanceAttributes::default()).is_none());
assert!(ManiaPerformance::try_new(DifficultyAttributes::Osu(
OsuDifficultyAttributes::default()
))
.is_none());
assert!(ManiaPerformance::try_new(PerformanceAttributes::Osu(
OsuPerformanceAttributes::default()
))
.is_none());
assert!(ManiaPerformance::try_new(&converted).is_none());
assert!(ManiaPerformance::try_new(converted).is_none());
}
} }
+2 -2
View File
@@ -88,8 +88,8 @@ impl Beatmap {
} }
/// Create a performance calculator for this [`Beatmap`]. /// Create a performance calculator for this [`Beatmap`].
pub const fn performance(&self) -> Performance<'_> { pub fn performance(&self) -> Performance<'_> {
Performance::from_map(self) Performance::new(self)
} }
/// Create a gradual difficulty calculator for this [`Beatmap`]. /// Create a gradual difficulty calculator for this [`Beatmap`].
+4 -4
View File
@@ -43,8 +43,8 @@ impl OsuDifficultyAttributes {
} }
/// Returns a builder for performance calculation. /// Returns a builder for performance calculation.
pub const fn performance<'a>(self) -> OsuPerformance<'a> { pub fn performance<'a>(self) -> OsuPerformance<'a> {
OsuPerformance::from_attributes(self) self.into()
} }
} }
@@ -88,8 +88,8 @@ impl OsuPerformanceAttributes {
} }
/// Returns a builder for performance calculation. /// Returns a builder for performance calculation.
pub const fn performance<'a>(self) -> OsuPerformance<'a> { pub fn performance<'a>(self) -> OsuPerformance<'a> {
OsuPerformance::from_attributes(self.difficulty) self.difficulty.into()
} }
} }
+1 -1
View File
@@ -59,7 +59,7 @@ impl IGameMode for Osu {
} }
fn performance(map: OsuBeatmap<'_>) -> Self::Performance<'_> { fn performance(map: OsuBeatmap<'_>) -> Self::Performance<'_> {
OsuPerformance::from_map(map) OsuPerformance::new(map)
} }
fn gradual_difficulty(difficulty: Difficulty, map: &OsuBeatmap<'_>) -> Self::GradualDifficulty { fn gradual_difficulty(difficulty: Difficulty, map: &OsuBeatmap<'_>) -> Self::GradualDifficulty {
+1 -1
View File
@@ -173,7 +173,7 @@ mod tests {
assert_eq!(next_gradual, next_gradual_3rd); assert_eq!(next_gradual, next_gradual_3rd);
} }
let mut regular_calc = OsuPerformance::from_map(converted.as_owned()) let mut regular_calc = OsuPerformance::new(converted.as_owned())
.difficulty(difficulty.clone()) .difficulty(difficulty.clone())
.passed_objects(i as u32) .passed_objects(i as u32)
.state(state); .state(state);
+104 -71
View File
@@ -3,7 +3,7 @@ use std::cmp;
use rosu_map::section::general::GameMode; use rosu_map::section::general::GameMode;
use crate::{ use crate::{
any::{AttributeProvider, Difficulty, DifficultyAttributes, HitResultPriority, Performance}, any::{Difficulty, HitResultPriority, IntoModePerformance, IntoPerformance, Performance},
catch::CatchPerformance, catch::CatchPerformance,
mania::ManiaPerformance, mania::ManiaPerformance,
taiko::TaikoPerformance, taiko::TaikoPerformance,
@@ -12,7 +12,6 @@ use crate::{
use super::{ use super::{
attributes::{OsuDifficultyAttributes, OsuPerformanceAttributes}, attributes::{OsuDifficultyAttributes, OsuPerformanceAttributes},
convert::OsuBeatmap,
score_state::OsuScoreState, score_state::OsuScoreState,
Osu, Osu,
}; };
@@ -35,59 +34,40 @@ pub struct OsuPerformance<'map> {
} }
impl<'map> OsuPerformance<'map> { impl<'map> OsuPerformance<'map> {
/// Create a new performance calculator for osu!standard maps. /// Create a new performance calculator for osu! maps.
/// ///
/// Note that creating [`OsuPerformance`] this way will require to /// The argument `map_or_attrs` must be either
/// perform the costly computation of [`OsuDifficultyAttributes`] /// - previously calculated attributes ([`OsuDifficultyAttributes`]
/// internally. If difficulty attributes for the current [`Difficulty`] /// or [`OsuPerformanceAttributes`])
/// settings are already available, consider using [`from_attributes`] or /// - a beatmap ([`OsuBeatmap<'map>`])
/// [`try_from_attributes`] instead.
/// ///
/// [`from_attributes`]: Self::from_attributes /// If a map is given, difficulty attributes will need to be calculated
/// [`try_from_attributes`]: Self::try_from_attributes /// internally which is a costly operation. Hence, passing attributes
pub const fn from_map(map: OsuBeatmap<'map>) -> Self { /// should be prefered.
Self { ///
map_or_attrs: MapOrAttrs::Map(map), /// However, when passing previously calculated attributes, make sure they
difficulty: Difficulty::new(), /// have been calculated for the same map and [`Difficulty`] settings.
acc: None, /// Otherwise, the final attributes will be incorrect.
combo: None, ///
n300: None, /// [`OsuBeatmap<'map>`]: crate::osu::OsuBeatmap
n100: None, pub fn new(map_or_attrs: impl IntoModePerformance<'map, Osu>) -> Self {
n50: None, map_or_attrs.into_performance()
misses: None,
hitresult_priority: HitResultPriority::DEFAULT,
}
} }
/// Create a new performance calculator from difficulty attributes. /// Try to create a new performance calculator for osu! maps.
/// ///
/// Note that `attrs` must have been calculated for the same beatmap and /// Returns `None` if `map_or_attrs` does not belong to osu! e.g.
/// [`Difficulty`] settings, otherwise the final attributes will be /// a [`Converted`], [`DifficultyAttributes`], or [`PerformanceAttributes`]
/// incorrect. /// of a different mode.
pub const fn from_attributes(attrs: OsuDifficultyAttributes) -> Self {
Self {
map_or_attrs: MapOrAttrs::Attrs(attrs),
difficulty: Difficulty::new(),
acc: None,
combo: None,
n300: None,
n100: None,
n50: None,
misses: None,
hitresult_priority: HitResultPriority::DEFAULT,
}
}
/// Try to create a new performance calculator from difficulty attributes.
/// ///
/// Note that `attrs` must have been calculated for the same beatmap and /// See [`OsuPerformance::new`] for more information.
/// [`Difficulty`] settings, otherwise the final attributes will be
/// incorrect.
/// ///
/// Returns `None` if `attrs` contained attributes of a different mode. /// [`Converted`]: crate::model::beatmap::Converted
pub fn try_from_attributes(attrs: impl AttributeProvider) -> Option<Self> { /// [`DifficultyAttributes`]: crate::any::DifficultyAttributes
if let DifficultyAttributes::Osu(attrs) = attrs.attributes() { /// [`PerformanceAttributes`]: crate::any::PerformanceAttributes
Some(Self::from_attributes(attrs)) pub fn try_new(map_or_attrs: impl IntoPerformance<'map>) -> Option<Self> {
if let Performance::Osu(calc) = map_or_attrs.into_performance() {
Some(calc)
} else { } else {
None None
} }
@@ -95,13 +75,12 @@ impl<'map> OsuPerformance<'map> {
/// Attempt to convert the map to the specified mode. /// Attempt to convert the map to the specified mode.
/// ///
/// Returns `Err(self)` if the internal beatmap was already replaced with /// Returns `Err(self)` if no beatmap is contained, i.e. if this
/// [`OsuDifficultyAttributes`], i.e. if /// [`OsuPerformance`] was created through attributes or
/// [`OsuPerformance::from_attributes`] or
/// [`OsuPerformance::generate_state`] was called. /// [`OsuPerformance::generate_state`] was called.
/// ///
/// If the given mode should be ignored in case the internal beatmap was /// If the given mode should be ignored in case of an error, use
/// replaced, use [`mode_or_ignore`] instead. /// [`mode_or_ignore`] instead.
/// ///
/// [`mode_or_ignore`]: Self::mode_or_ignore /// [`mode_or_ignore`]: Self::mode_or_ignore
// The `Ok`-variant is larger in size // The `Ok`-variant is larger in size
@@ -519,23 +498,25 @@ impl<'map> OsuPerformance<'map> {
inner.calculate() inner.calculate()
} }
}
impl<'map> From<OsuBeatmap<'map>> for OsuPerformance<'map> { pub(crate) const fn from_map_or_attrs(map_or_attrs: MapOrAttrs<'map, Osu>) -> Self {
fn from(map: OsuBeatmap<'map>) -> Self { Self {
Self::from_map(map) map_or_attrs,
difficulty: Difficulty::new(),
acc: None,
combo: None,
n300: None,
n100: None,
n50: None,
misses: None,
hitresult_priority: HitResultPriority::DEFAULT,
}
} }
} }
impl From<OsuDifficultyAttributes> for OsuPerformance<'_> { impl<'map, T: IntoModePerformance<'map, Osu>> From<T> for OsuPerformance<'map> {
fn from(attrs: OsuDifficultyAttributes) -> Self { fn from(into: T) -> Self {
Self::from_attributes(attrs) into.into_performance()
}
}
impl From<OsuPerformanceAttributes> for OsuPerformance<'_> {
fn from(attrs: OsuPerformanceAttributes) -> Self {
Self::from_attributes(attrs.difficulty)
} }
} }
@@ -878,7 +859,11 @@ mod test {
use proptest::prelude::*; use proptest::prelude::*;
use crate::Beatmap; use crate::{
any::{DifficultyAttributes, PerformanceAttributes},
taiko::{Taiko, TaikoDifficultyAttributes, TaikoPerformanceAttributes},
Beatmap,
};
use super::*; use super::*;
@@ -886,13 +871,14 @@ mod test {
const N_OBJECTS: u32 = 601; const N_OBJECTS: u32 = 601;
fn beatmap() -> Beatmap {
Beatmap::from_path("./resources/2785319.osu").unwrap()
}
fn attrs() -> OsuDifficultyAttributes { fn attrs() -> OsuDifficultyAttributes {
ATTRS ATTRS
.get_or_init(|| { .get_or_init(|| {
let converted = Beatmap::from_path("./resources/2785319.osu") let converted = beatmap().unchecked_into_converted::<Osu>();
.unwrap()
.unchecked_into_converted::<Osu>();
let attrs = Difficulty::new().with_mode().calculate(&converted); let attrs = Difficulty::new().with_mode().calculate(&converted);
assert_eq!( assert_eq!(
@@ -1139,4 +1125,51 @@ mod test {
assert_eq!(state, expected); assert_eq!(state, expected);
} }
#[test]
fn create() {
let mut map = beatmap();
let converted = map.unchecked_as_converted();
let _ = OsuPerformance::new(OsuDifficultyAttributes::default());
let _ = OsuPerformance::new(OsuPerformanceAttributes::default());
let _ = OsuPerformance::new(&converted);
let _ = OsuPerformance::new(converted.as_owned());
let _ = OsuPerformance::try_new(OsuDifficultyAttributes::default()).unwrap();
let _ = OsuPerformance::try_new(OsuPerformanceAttributes::default()).unwrap();
let _ =
OsuPerformance::try_new(DifficultyAttributes::Osu(OsuDifficultyAttributes::default()))
.unwrap();
let _ = OsuPerformance::try_new(PerformanceAttributes::Osu(
OsuPerformanceAttributes::default(),
))
.unwrap();
let _ = OsuPerformance::try_new(&converted).unwrap();
let _ = OsuPerformance::try_new(converted.as_owned()).unwrap();
let _ = OsuPerformance::from(OsuDifficultyAttributes::default());
let _ = OsuPerformance::from(OsuPerformanceAttributes::default());
let _ = OsuPerformance::from(&converted);
let _ = OsuPerformance::from(converted);
let _ = OsuDifficultyAttributes::default().performance();
let _ = OsuPerformanceAttributes::default().performance();
map.mode = GameMode::Taiko;
let converted = map.unchecked_as_converted::<Taiko>();
assert!(OsuPerformance::try_new(TaikoDifficultyAttributes::default()).is_none());
assert!(OsuPerformance::try_new(TaikoPerformanceAttributes::default()).is_none());
assert!(OsuPerformance::try_new(DifficultyAttributes::Taiko(
TaikoDifficultyAttributes::default()
))
.is_none());
assert!(OsuPerformance::try_new(PerformanceAttributes::Taiko(
TaikoPerformanceAttributes::default()
))
.is_none());
assert!(OsuPerformance::try_new(&converted).is_none());
assert!(OsuPerformance::try_new(converted).is_none());
}
} }
+4 -4
View File
@@ -37,8 +37,8 @@ impl TaikoDifficultyAttributes {
} }
/// Returns a builder for performance calculation. /// Returns a builder for performance calculation.
pub const fn performance<'a>(self) -> TaikoPerformance<'a> { pub fn performance<'a>(self) -> TaikoPerformance<'a> {
TaikoPerformance::from_attributes(self) self.into()
} }
} }
@@ -81,8 +81,8 @@ impl TaikoPerformanceAttributes {
} }
/// Returns a builder for performance calculation. /// Returns a builder for performance calculation.
pub const fn performance<'a>(self) -> TaikoPerformance<'a> { pub fn performance<'a>(self) -> TaikoPerformance<'a> {
TaikoPerformance::from_attributes(self.difficulty) self.difficulty.into()
} }
} }
+1 -1
View File
@@ -55,7 +55,7 @@ impl IGameMode for Taiko {
} }
fn performance(map: TaikoBeatmap<'_>) -> Self::Performance<'_> { fn performance(map: TaikoBeatmap<'_>) -> Self::Performance<'_> {
TaikoPerformance::from_map(map) TaikoPerformance::new(map)
} }
fn gradual_difficulty( fn gradual_difficulty(
+1 -1
View File
@@ -175,7 +175,7 @@ mod tests {
assert_eq!(next_gradual, next_gradual_3rd); assert_eq!(next_gradual, next_gradual_3rd);
} }
let mut regular_calc = TaikoPerformance::from_map(converted.as_owned()) let mut regular_calc = TaikoPerformance::new(converted.as_owned())
.difficulty(difficulty.clone()) .difficulty(difficulty.clone())
.passed_objects(i as u32) .passed_objects(i as u32)
.state(state); .state(state);
+106 -71
View File
@@ -1,14 +1,14 @@
use std::cmp; use std::cmp;
use crate::{ use crate::{
any::{AttributeProvider, Difficulty, DifficultyAttributes, HitResultPriority}, any::{Difficulty, HitResultPriority, IntoModePerformance, IntoPerformance},
osu::OsuPerformance, osu::OsuPerformance,
util::{map_or_attrs::MapOrAttrs, mods::Mods}, util::{map_or_attrs::MapOrAttrs, mods::Mods},
Performance,
}; };
use super::{ use super::{
attributes::{TaikoDifficultyAttributes, TaikoPerformanceAttributes}, attributes::{TaikoDifficultyAttributes, TaikoPerformanceAttributes},
convert::TaikoBeatmap,
score_state::TaikoScoreState, score_state::TaikoScoreState,
Taiko, Taiko,
}; };
@@ -32,55 +32,38 @@ pub struct TaikoPerformance<'map> {
impl<'map> TaikoPerformance<'map> { impl<'map> TaikoPerformance<'map> {
/// Create a new performance calculator for osu!taiko maps. /// Create a new performance calculator for osu!taiko maps.
/// ///
/// Note that creating [`TaikoPerformance`] this way will require to /// The argument `map_or_attrs` must be either
/// perform the costly computation of [`TaikoDifficultyAttributes`] /// - previously calculated attributes ([`TaikoDifficultyAttributes`]
/// internally. If difficulty attributes for the current [`Difficulty`] /// or [`TaikoPerformanceAttributes`])
/// settings are already available, consider using [`from_attributes`] or /// - a beatmap ([`TaikoBeatmap<'map>`])
/// [`try_from_attributes`] instead.
/// ///
/// [`from_attributes`]: Self::from_attributes /// If a map is given, difficulty attributes will need to be calculated
/// [`try_from_attributes`]: Self::try_from_attributes /// internally which is a costly operation. Hence, passing attributes
pub const fn from_map(map: TaikoBeatmap<'map>) -> Self { /// should be prefered.
Self { ///
map_or_attrs: MapOrAttrs::Map(map), /// However, when passing previously calculated attributes, make sure they
difficulty: Difficulty::new(), /// have been calculated for the same map and [`Difficulty`] settings.
combo: None, /// Otherwise, the final attributes will be incorrect.
acc: None, ///
misses: None, /// [`TaikoBeatmap<'map>`]: crate::taiko::TaikoBeatmap
n300: None, pub fn new(map_or_attrs: impl IntoModePerformance<'map, Taiko>) -> Self {
n100: None, map_or_attrs.into_performance()
hitresult_priority: HitResultPriority::DEFAULT,
}
} }
/// Create a new performance calculator from difficulty attributes. /// Try to create a new performance calculator for osu!taiko maps.
/// ///
/// Note that `attrs` must have been calculated for the same beatmap and /// Returns `None` if `map_or_attrs` does not belong to osu!taiko e.g.
/// [`Difficulty`] settings, otherwise the final attributes will be /// a [`Converted`], [`DifficultyAttributes`], or [`PerformanceAttributes`]
/// incorrect. /// of a different mode.
pub const fn from_attributes(attrs: TaikoDifficultyAttributes) -> Self {
Self {
map_or_attrs: MapOrAttrs::Attrs(attrs),
difficulty: Difficulty::new(),
combo: None,
acc: None,
misses: None,
n300: None,
n100: None,
hitresult_priority: HitResultPriority::DEFAULT,
}
}
/// Try to create a new performance calculator from difficulty attributes.
/// ///
/// Note that `attrs` must have been calculated for the same beatmap and /// See [`TaikoPerformance::new`] for more information.
/// [`Difficulty`] settings, otherwise the final attributes will be
/// incorrect.
/// ///
/// Returns `None` if `attrs` contained attributes of a different mode. /// [`Converted`]: crate::model::beatmap::Converted
pub fn try_from_attributes(attrs: impl AttributeProvider) -> Option<Self> { /// [`DifficultyAttributes`]: crate::any::DifficultyAttributes
if let DifficultyAttributes::Taiko(attrs) = attrs.attributes() { /// [`PerformanceAttributes`]: crate::any::PerformanceAttributes
Some(Self::from_attributes(attrs)) pub fn try_new(map_or_attrs: impl IntoPerformance<'map>) -> Option<Self> {
if let Performance::Taiko(calc) = map_or_attrs.into_performance() {
Some(calc)
} else { } else {
None None
} }
@@ -326,6 +309,19 @@ impl<'map> TaikoPerformance<'map> {
inner.calculate() inner.calculate()
} }
pub(crate) const fn from_map_or_attrs(map_or_attrs: MapOrAttrs<'map, Taiko>) -> Self {
Self {
map_or_attrs,
difficulty: Difficulty::new(),
combo: None,
acc: None,
misses: None,
n300: None,
n100: None,
hitresult_priority: HitResultPriority::DEFAULT,
}
}
} }
impl<'map> TryFrom<OsuPerformance<'map>> for TaikoPerformance<'map> { impl<'map> TryFrom<OsuPerformance<'map>> for TaikoPerformance<'map> {
@@ -333,12 +329,9 @@ impl<'map> TryFrom<OsuPerformance<'map>> for TaikoPerformance<'map> {
/// Try to create [`TaikoPerformance`] through [`OsuPerformance`]. /// Try to create [`TaikoPerformance`] through [`OsuPerformance`].
/// ///
/// Returns `None` if [`OsuPerformance`] already replaced its internal /// Returns `None` if [`OsuPerformance`] does not contain a beatmap, i.e.
/// beatmap with [`OsuDifficultyAttributes`], i.e. if /// if it was constructed through attributes or
/// [`OsuPerformance::from_attributes`] or [`OsuPerformance::generate_state`] /// [`OsuPerformance::generate_state`] was called.
/// was called.
///
/// [`OsuDifficultyAttributes`]: crate::osu::OsuDifficultyAttributes
fn try_from(mut osu: OsuPerformance<'map>) -> Result<Self, Self::Error> { fn try_from(mut osu: OsuPerformance<'map>) -> Result<Self, Self::Error> {
let MapOrAttrs::Map(converted) = osu.map_or_attrs else { let MapOrAttrs::Map(converted) = osu.map_or_attrs else {
return Err(osu); return Err(osu);
@@ -378,21 +371,9 @@ impl<'map> TryFrom<OsuPerformance<'map>> for TaikoPerformance<'map> {
} }
} }
impl<'map> From<TaikoBeatmap<'map>> for TaikoPerformance<'map> { impl<'map, T: IntoModePerformance<'map, Taiko>> From<T> for TaikoPerformance<'map> {
fn from(map: TaikoBeatmap<'map>) -> Self { fn from(into: T) -> Self {
Self::from_map(map) into.into_performance()
}
}
impl From<TaikoDifficultyAttributes> for TaikoPerformance<'_> {
fn from(attrs: TaikoDifficultyAttributes) -> Self {
Self::from_attributes(attrs)
}
}
impl From<TaikoPerformanceAttributes> for TaikoPerformance<'_> {
fn from(attrs: TaikoPerformanceAttributes) -> Self {
Self::from_attributes(attrs.difficulty)
} }
} }
@@ -528,8 +509,13 @@ mod test {
use std::sync::OnceLock; use std::sync::OnceLock;
use proptest::prelude::*; use proptest::prelude::*;
use rosu_map::section::general::GameMode;
use crate::Beatmap; use crate::{
any::{DifficultyAttributes, PerformanceAttributes},
osu::{Osu, OsuDifficultyAttributes, OsuPerformanceAttributes},
Beatmap,
};
use super::*; use super::*;
@@ -537,13 +523,14 @@ mod test {
const MAX_COMBO: u32 = 289; const MAX_COMBO: u32 = 289;
fn beatmap() -> Beatmap {
Beatmap::from_path("./resources/1028484.osu").unwrap()
}
fn attrs() -> TaikoDifficultyAttributes { fn attrs() -> TaikoDifficultyAttributes {
ATTRS ATTRS
.get_or_init(|| { .get_or_init(|| {
let converted = Beatmap::from_path("./resources/1028484.osu") let converted = beatmap().unchecked_into_converted::<Taiko>();
.unwrap()
.unchecked_into_converted::<Taiko>();
let attrs = Difficulty::new().with_mode().calculate(&converted); let attrs = Difficulty::new().with_mode().calculate(&converted);
assert_eq!(MAX_COMBO, attrs.max_combo); assert_eq!(MAX_COMBO, attrs.max_combo);
@@ -698,4 +685,52 @@ mod test {
assert_eq!(state, expected); assert_eq!(state, expected);
} }
#[test]
fn create() {
let mut map = beatmap();
let converted = map.unchecked_as_converted();
let _ = TaikoPerformance::new(TaikoDifficultyAttributes::default());
let _ = TaikoPerformance::new(TaikoPerformanceAttributes::default());
let _ = TaikoPerformance::new(&converted);
let _ = TaikoPerformance::new(converted.as_owned());
let _ = TaikoPerformance::try_new(TaikoDifficultyAttributes::default()).unwrap();
let _ = TaikoPerformance::try_new(TaikoPerformanceAttributes::default()).unwrap();
let _ = TaikoPerformance::try_new(DifficultyAttributes::Taiko(
TaikoDifficultyAttributes::default(),
))
.unwrap();
let _ = TaikoPerformance::try_new(PerformanceAttributes::Taiko(
TaikoPerformanceAttributes::default(),
))
.unwrap();
let _ = TaikoPerformance::try_new(&converted).unwrap();
let _ = TaikoPerformance::try_new(converted.as_owned()).unwrap();
let _ = TaikoPerformance::from(TaikoDifficultyAttributes::default());
let _ = TaikoPerformance::from(TaikoPerformanceAttributes::default());
let _ = TaikoPerformance::from(&converted);
let _ = TaikoPerformance::from(converted);
let _ = TaikoDifficultyAttributes::default().performance();
let _ = TaikoPerformanceAttributes::default().performance();
map.mode = GameMode::Osu;
let converted = map.unchecked_as_converted::<Osu>();
assert!(TaikoPerformance::try_new(OsuDifficultyAttributes::default()).is_none());
assert!(TaikoPerformance::try_new(OsuPerformanceAttributes::default()).is_none());
assert!(TaikoPerformance::try_new(DifficultyAttributes::Osu(
OsuDifficultyAttributes::default()
))
.is_none());
assert!(TaikoPerformance::try_new(PerformanceAttributes::Osu(
OsuPerformanceAttributes::default()
))
.is_none());
assert!(TaikoPerformance::try_new(&converted).is_none());
assert!(TaikoPerformance::try_new(converted).is_none());
}
} }
+54
View File
@@ -13,6 +13,7 @@ impl<'map, M: IGameMode> MapOrAttrs<'map, M> {
/// If `self` is of variant `Map`, store `attrs` in `self`, and return a /// If `self` is of variant `Map`, store `attrs` in `self`, and return a
/// mutable reference to it. /// mutable reference to it.
pub fn insert_attrs(&mut self, attrs: M::DifficultyAttributes) -> &mut M::DifficultyAttributes { pub fn insert_attrs(&mut self, attrs: M::DifficultyAttributes) -> &mut M::DifficultyAttributes {
// TODO: dont match, just overwrite
match self { match self {
MapOrAttrs::Map(_) => { MapOrAttrs::Map(_) => {
*self = Self::Attrs(attrs); *self = Self::Attrs(attrs);
@@ -67,3 +68,56 @@ where
} }
} }
} }
impl<'map, M: IGameMode> From<Converted<'map, M>> for MapOrAttrs<'map, M> {
fn from(converted: Converted<'map, M>) -> Self {
Self::Map(converted)
}
}
macro_rules! from_attrs {
(
$(
$module:ident {
$mode:ident, $diff:ident, $perf:ident
}
,)*
) => {
$(
impl From<crate::$module::$diff> for MapOrAttrs<'_, crate::$module::$mode> {
fn from(attrs: crate::$module::$diff) -> Self {
Self::Attrs(attrs)
}
}
impl From<crate::$module::$perf> for MapOrAttrs<'_, crate::$module::$mode> {
fn from(attrs: crate::$module::$perf) -> Self {
Self::Attrs(attrs.difficulty)
}
}
)*
};
}
from_attrs!(
osu {
Osu,
OsuDifficultyAttributes,
OsuPerformanceAttributes
},
taiko {
Taiko,
TaikoDifficultyAttributes,
TaikoPerformanceAttributes
},
catch {
Catch,
CatchDifficultyAttributes,
CatchPerformanceAttributes
},
mania {
Mania,
ManiaDifficultyAttributes,
ManiaPerformanceAttributes
},
);