Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: convert overhaul #44

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 0 additions & 114 deletions src/any/difficulty/converted.rs

This file was deleted.

54 changes: 20 additions & 34 deletions src/any/difficulty/gradual.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use std::borrow::Cow;

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

use crate::{
any::DifficultyAttributes,
catch::{Catch, CatchBeatmap, CatchGradualDifficulty},
mania::{Mania, ManiaBeatmap, ManiaGradualDifficulty},
model::mode::IGameMode,
osu::{Osu, OsuBeatmap, OsuGradualDifficulty},
taiko::{Taiko, TaikoBeatmap, TaikoGradualDifficulty},
Beatmap, Converted, Difficulty,
catch::{Catch, CatchGradualDifficulty},
mania::{Mania, ManiaGradualDifficulty},
model::mode::{ConvertError, IGameMode},
osu::{Osu, OsuGradualDifficulty},
taiko::{Taiko, TaikoGradualDifficulty},
Beatmap, Difficulty,
};

/// Gradually calculate the difficulty attributes on maps of any mode.
Expand Down Expand Up @@ -51,38 +49,26 @@ pub enum GradualDifficulty {
Mania(ManiaGradualDifficulty),
}

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

impl GradualDifficulty {
/// Create a [`GradualDifficulty`] for a map of any mode.
#[allow(clippy::missing_panics_doc)]
pub fn new(difficulty: Difficulty, map: &Beatmap) -> Self {
let map = Cow::Borrowed(map);
Self::new_with_mode(difficulty, map, map.mode).expect("no conversion required")
}

match map.mode {
GameMode::Osu => Self::Osu(Osu::gradual_difficulty(difficulty, &Converted::new(map))),
GameMode::Taiko => {
Self::Taiko(Taiko::gradual_difficulty(difficulty, &Converted::new(map)))
}
GameMode::Catch => {
Self::Catch(Catch::gradual_difficulty(difficulty, &Converted::new(map)))
}
GameMode::Mania => {
Self::Mania(Mania::gradual_difficulty(difficulty, &Converted::new(map)))
}
/// Create a [`GradualDifficulty`] for a [`Beatmap`] on a specific [`GameMode`].
pub fn new_with_mode(
difficulty: Difficulty,
map: &Beatmap,
mode: GameMode,
) -> Result<Self, ConvertError> {
match mode {
GameMode::Osu => Osu::gradual_difficulty(difficulty, map).map(Self::Osu),
GameMode::Taiko => Taiko::gradual_difficulty(difficulty, map).map(Self::Taiko),
GameMode::Catch => Catch::gradual_difficulty(difficulty, map).map(Self::Catch),
GameMode::Mania => Mania::gradual_difficulty(difficulty, map).map(Self::Mania),
}
}

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);
}

impl Iterator for GradualDifficulty {
Expand Down
91 changes: 57 additions & 34 deletions src/any/difficulty/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{
borrow::Cow,
fmt::{Debug, Formatter, Result as FmtResult},
num::NonZeroU64,
};
Expand All @@ -9,20 +8,14 @@ use rosu_map::section::general::GameMode;
use crate::{
catch::Catch,
mania::Mania,
model::{
beatmap::{Beatmap, Converted},
mods::GameMods,
},
model::{beatmap::Beatmap, mode::ConvertError, mods::GameMods},
osu::Osu,
taiko::Taiko,
GradualDifficulty, GradualPerformance,
};

use self::converted::ConvertedDifficulty;

use super::{attributes::DifficultyAttributes, InspectDifficulty, Strains};

pub mod converted;
pub mod gradual;
pub mod inspect;
pub mod object;
Expand Down Expand Up @@ -100,14 +93,6 @@ impl Difficulty {
}
}

/// Use this [`Difficulty`] as a calculator for a specific [`IGameMode`].
///
/// Note that [`ConvertedDifficulty`] won't allow to further customize
/// fields so be sure they're all set before converting to it.
pub const fn with_mode<M: IGameMode>(&self) -> ConvertedDifficulty<'_, M> {
ConvertedDifficulty::new(self)
}

/// Turn this [`Difficulty`] into a [`InspectDifficulty`] to inspect its
/// configured values.
pub fn inspect(self) -> InspectDifficulty {
Expand Down Expand Up @@ -281,48 +266,86 @@ impl Difficulty {
}

/// Perform the difficulty calculation.
#[allow(clippy::missing_panics_doc)]
pub fn calculate(&self, map: &Beatmap) -> DifficultyAttributes {
let map = Cow::Borrowed(map);

match map.mode {
GameMode::Osu => DifficultyAttributes::Osu(Osu::difficulty(self, &Converted::new(map))),
GameMode::Taiko => {
DifficultyAttributes::Taiko(Taiko::difficulty(self, &Converted::new(map)))
}
GameMode::Catch => {
DifficultyAttributes::Catch(Catch::difficulty(self, &Converted::new(map)))
}
GameMode::Mania => {
DifficultyAttributes::Mania(Mania::difficulty(self, &Converted::new(map)))
}
GameMode::Osu => DifficultyAttributes::Osu(
Osu::difficulty(self, map).expect("no conversion required"),
),
GameMode::Taiko => DifficultyAttributes::Taiko(
Taiko::difficulty(self, map).expect("no conversion required"),
),
GameMode::Catch => DifficultyAttributes::Catch(
Catch::difficulty(self, map).expect("no conversion required"),
),
GameMode::Mania => DifficultyAttributes::Mania(
Mania::difficulty(self, map).expect("no conversion required"),
),
}
}

/// Perform the difficulty calculation for a specific [`IGameMode`].
pub fn calculate_for_mode<M: IGameMode>(
&self,
map: &Beatmap,
) -> Result<M::DifficultyAttributes, ConvertError> {
M::difficulty(self, map)
}

/// Perform the difficulty calculation but instead of evaluating the skill
/// strains, return them as is.
///
/// Suitable to plot the difficulty of a map over time.
#[allow(clippy::missing_panics_doc)]
pub fn strains(&self, map: &Beatmap) -> Strains {
let map = Cow::Borrowed(map);

match map.mode {
GameMode::Osu => Strains::Osu(Osu::strains(self, &Converted::new(map))),
GameMode::Taiko => Strains::Taiko(Taiko::strains(self, &Converted::new(map))),
GameMode::Catch => Strains::Catch(Catch::strains(self, &Converted::new(map))),
GameMode::Mania => Strains::Mania(Mania::strains(self, &Converted::new(map))),
GameMode::Osu => Strains::Osu(Osu::strains(self, map).expect("no conversion required")),
GameMode::Taiko => {
Strains::Taiko(Taiko::strains(self, map).expect("no conversion required"))
}
GameMode::Catch => {
Strains::Catch(Catch::strains(self, map).expect("no conversion required"))
}
GameMode::Mania => {
Strains::Mania(Mania::strains(self, map).expect("no conversion required"))
}
}
}

/// Perform the strain calculation for a specific [`IGameMode`].
pub fn strains_for_mode<M: IGameMode>(
&self,
map: &Beatmap,
) -> Result<M::Strains, ConvertError> {
M::strains(self, map)
}

/// Create a gradual difficulty calculator for a [`Beatmap`].
pub fn gradual_difficulty(self, map: &Beatmap) -> GradualDifficulty {
GradualDifficulty::new(self, map)
}

/// Create a gradual difficulty calculator for a [`Beatmap`] on a specific [`IGameMode`].
pub fn gradual_difficulty_for_mode<M: IGameMode>(
self,
map: &Beatmap,
) -> Result<M::GradualDifficulty, ConvertError> {
M::gradual_difficulty(self, map)
}

/// Create a gradual performance calculator for a [`Beatmap`].
pub fn gradual_performance(self, map: &Beatmap) -> GradualPerformance {
GradualPerformance::new(self, map)
}

/// Create a gradual performance calculator for a [`Beatmap`] on a specific [`IGameMode`].
pub fn gradual_performance_for_mode<M: IGameMode>(
self,
map: &Beatmap,
) -> Result<M::GradualPerformance, ConvertError> {
M::gradual_performance(self, map)
}

pub(crate) const fn get_mods(&self) -> &GameMods {
&self.mods
}
Expand Down
3 changes: 1 addition & 2 deletions src/any/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
pub use self::{
attributes::{DifficultyAttributes, PerformanceAttributes},
difficulty::{
converted::ConvertedDifficulty, gradual::GradualDifficulty, inspect::InspectDifficulty,
Difficulty, ModsDependent,
gradual::GradualDifficulty, inspect::InspectDifficulty, Difficulty, ModsDependent,
},
performance::{
gradual::GradualPerformance,
Expand Down
Loading