feat: added convenience methods

This commit is contained in:
MaxOhn
2023-12-29 11:31:35 +01:00
parent f2854dbde5
commit f3c39c7891
10 changed files with 164 additions and 57 deletions
+6
View File
@@ -274,6 +274,12 @@ impl CatchDifficultyAttributes {
pub fn max_combo(&self) -> usize {
self.n_fruits + self.n_droplets
}
/// Returns a builder for performance calculation.
#[inline]
pub fn pp(self) -> CatchPP<'static> {
CatchPP::from(self)
}
}
/// The result of a performance calculation on an osu!catch map.
+26 -14
View File
@@ -418,20 +418,7 @@ impl<'map> CatchPP<'map> {
/// attributes for catch e.g. if it's [`DifficultyAttributes::Taiko`].
#[inline]
pub fn try_from_attributes(attributes: impl CatchAttributeProvider) -> Option<Self> {
attributes.attributes().map(|attrs| Self {
map_or_attrs: MapOrElse::Else(attrs),
mods: 0,
acc: None,
combo: None,
n_fruits: None,
n_droplets: None,
n_tiny_droplets: None,
n_tiny_droplet_misses: None,
n_misses: None,
passed_objects: None,
clock_rate: None,
})
attributes.attributes().map(Self::from)
}
}
@@ -529,6 +516,31 @@ fn accuracy(
numerator as f64 / denominator as f64
}
impl From<CatchDifficultyAttributes> for CatchPP<'_> {
fn from(attrs: CatchDifficultyAttributes) -> Self {
Self {
map_or_attrs: MapOrElse::Else(attrs),
mods: 0,
acc: None,
combo: None,
n_fruits: None,
n_droplets: None,
n_tiny_droplets: None,
n_tiny_droplet_misses: None,
n_misses: None,
passed_objects: None,
clock_rate: None,
}
}
}
impl From<CatchPerformanceAttributes> for CatchPP<'_> {
fn from(attrs: CatchPerformanceAttributes) -> Self {
attrs.difficulty.into()
}
}
/// Abstract type to provide flexibility when passing difficulty attributes to a performance calculation.
pub trait CatchAttributeProvider {
/// Provide the actual difficulty attributes.
+6
View File
@@ -306,6 +306,12 @@ impl DifficultyAttributes {
Self::Mania(attrs) => attrs.max_combo,
}
}
/// Returns a builder for performance calculation.
#[inline]
pub fn pp(self) -> AnyPP<'static> {
AnyPP::from(self)
}
}
impl From<osu::OsuDifficultyAttributes> for DifficultyAttributes {
+6
View File
@@ -262,6 +262,12 @@ impl ManiaDifficultyAttributes {
pub fn is_convert(&self) -> bool {
self.is_convert
}
/// Returns a builder for performance calculation.
#[inline]
pub fn pp(self) -> ManiaPP<'static> {
ManiaPP::from(self)
}
}
/// The result of a performance calculation on an osu!mania map.
+27 -15
View File
@@ -803,21 +803,7 @@ impl<'map> ManiaPP<'map> {
/// attributes for mania e.g. if it's [`DifficultyAttributes::Taiko`].
#[inline]
pub fn try_from_attributes(attributes: impl ManiaAttributeProvider) -> Option<Self> {
attributes.attributes().map(|attrs| Self {
map_or_attrs: MapOrElse::Else(attrs),
is_convert_overwrite: None,
mods: 0,
passed_objects: None,
clock_rate: None,
n320: None,
n300: None,
n200: None,
n100: None,
n50: None,
n_misses: None,
acc: None,
hitresult_priority: None,
})
attributes.attributes().map(Self::from)
}
}
@@ -913,6 +899,32 @@ fn accuracy(
numerator as f64 / denominator as f64
}
impl From<ManiaDifficultyAttributes> for ManiaPP<'_> {
fn from(attrs: ManiaDifficultyAttributes) -> Self {
Self {
map_or_attrs: MapOrElse::Else(attrs),
is_convert_overwrite: None,
mods: 0,
passed_objects: None,
clock_rate: None,
n320: None,
n300: None,
n200: None,
n100: None,
n50: None,
n_misses: None,
acc: None,
hitresult_priority: None,
}
}
}
impl From<ManiaPerformanceAttributes> for ManiaPP<'_> {
fn from(attrs: ManiaPerformanceAttributes) -> Self {
attrs.difficulty.into()
}
}
/// Abstract type to provide flexibility when passing difficulty attributes to a performance calculation.
pub trait ManiaAttributeProvider {
/// Provide the actual difficulty attributes.
+6
View File
@@ -595,6 +595,12 @@ impl OsuDifficultyAttributes {
pub fn n_objects(&self) -> usize {
self.n_circles + self.n_sliders + self.n_spinners
}
/// Returns a builder for performance calculation.
#[inline]
pub fn pp(self) -> OsuPP<'static> {
OsuPP::from(self)
}
}
/// The result of a performance calculation on an osu!standard map.
+28 -14
View File
@@ -437,20 +437,7 @@ impl<'map> OsuPP<'map> {
/// attributes for catch e.g. if it's [`DifficultyAttributes::Taiko`].
#[inline]
pub fn try_from_attributes(attributes: impl OsuAttributeProvider) -> Option<Self> {
attributes.attributes().map(|attrs| Self {
map_or_attrs: MapOrElse::Else(attrs),
mods: 0,
acc: None,
combo: None,
n300: None,
n100: None,
n50: None,
n_misses: None,
passed_objects: None,
clock_rate: None,
hitresult_priority: None,
})
attributes.attributes().map(Self::from)
}
}
@@ -782,6 +769,33 @@ fn accuracy(n300: usize, n100: usize, n50: usize, n_misses: usize) -> f64 {
numerator as f64 / denominator as f64
}
impl From<OsuDifficultyAttributes> for OsuPP<'_> {
#[inline]
fn from(attrs: OsuDifficultyAttributes) -> Self {
Self {
map_or_attrs: MapOrElse::Else(attrs),
mods: 0,
acc: None,
combo: None,
n300: None,
n100: None,
n50: None,
n_misses: None,
passed_objects: None,
clock_rate: None,
hitresult_priority: None,
}
}
}
impl From<OsuPerformanceAttributes> for OsuPP<'_> {
#[inline]
fn from(attrs: OsuPerformanceAttributes) -> Self {
attrs.difficulty.into()
}
}
/// Abstract type to provide flexibility when passing difficulty attributes to a performance calculation.
pub trait OsuAttributeProvider {
/// Provide the actual difficulty attributes.
+28 -1
View File
@@ -60,7 +60,15 @@ impl<'map> AnyPP<'map> {
}
}
// TODO: from_attributes method
/// Create a new performance calculator through previously calculated
/// attributes.
///
/// Note that the map, mods, and passed object count should be the same
/// as when the attributes were calculated.
#[inline]
pub fn from_attributes(attributes: impl AttributeProvider) -> Self {
Self::from(attributes.attributes())
}
/// Consume the performance calculator and calculate
/// performance attributes for the given parameters.
@@ -285,6 +293,25 @@ impl<'map> AnyPP<'map> {
}
}
impl From<DifficultyAttributes> for AnyPP<'_> {
#[inline]
fn from(attrs: DifficultyAttributes) -> Self {
match attrs {
DifficultyAttributes::Osu(attrs) => Self::Osu(attrs.pp()),
DifficultyAttributes::Taiko(attrs) => Self::Taiko(attrs.pp()),
DifficultyAttributes::Catch(attrs) => Self::Catch(attrs.pp()),
DifficultyAttributes::Mania(attrs) => Self::Mania(attrs.pp()),
}
}
}
impl From<PerformanceAttributes> for AnyPP<'_> {
#[inline]
fn from(attrs: PerformanceAttributes) -> Self {
attrs.difficulty_attributes().into()
}
}
/// While generating remaining hitresults, decide how they should be distributed.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum HitResultPriority {
+6
View File
@@ -326,6 +326,12 @@ impl TaikoDifficultyAttributes {
pub fn is_convert(&self) -> bool {
self.is_convert
}
/// Returns a builder for performance calculation.
#[inline]
pub fn pp(self) -> TaikoPP<'static> {
TaikoPP::from(self)
}
}
/// The result of a performance calculation on an osu!taiko map.
+25 -13
View File
@@ -381,19 +381,7 @@ impl<'map> TaikoPP<'map> {
/// attributes for catch e.g. if it's [`DifficultyAttributes::Mania`].
#[inline]
pub fn try_from_attributes(attributes: impl TaikoAttributeProvider) -> Option<Self> {
attributes.attributes().map(|attrs| Self {
map_or_attrs: MapOrElse::Else(attrs),
is_convert_overwrite: None,
mods: 0,
combo: None,
acc: None,
n_misses: None,
passed_objects: None,
clock_rate: None,
n300: None,
n100: None,
hitresult_priority: None,
})
attributes.attributes().map(Self::from)
}
}
@@ -525,6 +513,30 @@ fn accuracy(n300: usize, n100: usize, n_misses: usize) -> f64 {
numerator as f64 / denominator as f64
}
impl From<TaikoDifficultyAttributes> for TaikoPP<'_> {
fn from(attrs: TaikoDifficultyAttributes) -> Self {
Self {
map_or_attrs: MapOrElse::Else(attrs),
is_convert_overwrite: None,
mods: 0,
combo: None,
acc: None,
n_misses: None,
passed_objects: None,
clock_rate: None,
n300: None,
n100: None,
hitresult_priority: None,
}
}
}
impl From<TaikoPerformanceAttributes> for TaikoPP<'_> {
fn from(attrs: TaikoPerformanceAttributes) -> Self {
attrs.difficulty.into()
}
}
/// Abstract type to provide flexibility when passing difficulty attributes to a performance calculation.
pub trait TaikoAttributeProvider {
/// Provide the actual difficulty attributes.