From 0e8b1a11d27d401c3468ad647de973870e6585f3 Mon Sep 17 00:00:00 2001 From: Badewanne3 <41148446+MaxOhn@users.noreply.github.com> Date: Fri, 2 Feb 2024 13:29:20 +0100 Subject: [PATCH] fix: consider clock rate in map info (#618) * fix: consider clock rate in map info * fix: fixed feature-gated code --- .../impls/edit_on_timeout/recent_score.rs | 2 +- .../active/impls/edit_on_timeout/top_score.rs | 2 +- bathbot/src/active/impls/simulate/mod.rs | 18 ++++++-------- bathbot/src/commands/osu/leaderboard.rs | 2 +- .../src/commands/osu/recent/leaderboard.rs | 4 ++-- bathbot/src/embeds/tracking/notification.rs | 2 +- bathbot/src/manager/pp.rs | 6 +++-- bathbot/src/util/osu.rs | 24 +++++++------------ 8 files changed, 25 insertions(+), 35 deletions(-) diff --git a/bathbot/src/active/impls/edit_on_timeout/recent_score.rs b/bathbot/src/active/impls/edit_on_timeout/recent_score.rs index a7670669..42a57f6b 100644 --- a/bathbot/src/active/impls/edit_on_timeout/recent_score.rs +++ b/bathbot/src/active/impls/edit_on_timeout/recent_score.rs @@ -389,7 +389,7 @@ impl RecentScoreEdit { }]; } - let map_info = MapInfo::new(map, stars).mods(score.mods.bits()).to_string(); + let map_info = MapInfo::new(map, stars).mods(&score.mods).to_string(); fields![fields { "Map Info".to_owned(), map_info, false }]; #[cfg(feature = "twitch")] diff --git a/bathbot/src/active/impls/edit_on_timeout/top_score.rs b/bathbot/src/active/impls/edit_on_timeout/top_score.rs index 6aa36f0a..f26d7c26 100644 --- a/bathbot/src/active/impls/edit_on_timeout/top_score.rs +++ b/bathbot/src/active/impls/edit_on_timeout/top_score.rs @@ -331,7 +331,7 @@ impl TopScoreEdit { }]; } - let map_info = MapInfo::new(map, stars).mods(score.mods.bits()).to_string(); + let map_info = MapInfo::new(map, stars).mods(&score.mods).to_string(); fields![fields { "Map Info", map_info, false }]; EmbedBuilder::new() diff --git a/bathbot/src/active/impls/simulate/mod.rs b/bathbot/src/active/impls/simulate/mod.rs index 6e0efcb2..366a1b85 100644 --- a/bathbot/src/active/impls/simulate/mod.rs +++ b/bathbot/src/active/impls/simulate/mod.rs @@ -30,7 +30,7 @@ use crate::{ commands::osu::parsed_map::AttachedSimulateMap, core::Context, embeds::{ComboFormatter, HitResultFormatter, KeyFormatter, PpFormatter}, - manager::OsuMap, + manager::{Mods, OsuMap}, util::{ interaction::{InteractionComponent, InteractionModal}, osu::{grade_completion_mods_raw, MapInfo}, @@ -224,7 +224,7 @@ impl IActiveMessage for SimulateComponents { fields.push(hits); } - let map_info = self.map.map_info(clock_rate, stars, mods.bits()); + let map_info = self.map.map_info(stars, Mods::from(mods.as_ref())); fields![fields { "Map Info", map_info, false; }]; let mut embed = EmbedBuilder::new() @@ -649,15 +649,11 @@ impl SimulateMap { } } - pub fn map_info(&self, clock_rate: Option, stars: f32, mods: u32) -> String { + pub fn map_info(&self, stars: f32, mods: Mods) -> String { match self { Self::Full(map) => { let mut map_info = MapInfo::new(map, stars); - if let Some(clock_rate) = clock_rate { - map_info.clock_rate(clock_rate); - } - map_info.mods(mods).to_string() } Self::Attached(map) => { @@ -665,11 +661,11 @@ impl SimulateMap { let mut builder = BeatmapAttributesBuilder::new(map); - if let Some(clock_rate) = clock_rate { - builder.clock_rate(clock_rate as f64); + if let Some(clock_rate) = mods.clock_rate { + builder.clock_rate(f64::from(clock_rate)); } - let attrs = builder.mode(map.mode).mods(mods).build(); + let attrs = builder.mode(map.mode).mods(mods.bits).build(); let clock_rate = attrs.clock_rate; @@ -695,7 +691,7 @@ impl SimulateMap { } let (cs_key, cs_value) = if map.mode == Mode::Mania { - ("Keys", MapInfo::keys(mods, attrs.cs as f32)) + ("Keys", MapInfo::keys(mods.bits, attrs.cs as f32)) } else { ("CS", round(attrs.cs as f32)) }; diff --git a/bathbot/src/commands/osu/leaderboard.rs b/bathbot/src/commands/osu/leaderboard.rs index 766a48da..9ff10a8d 100644 --- a/bathbot/src/commands/osu/leaderboard.rs +++ b/bathbot/src/commands/osu/leaderboard.rs @@ -310,7 +310,7 @@ async fn leaderboard( let mods_bits = specify_mods.as_ref().map_or(0, GameModsIntermode::bits); - let mut calc = ctx.pp(&map).mode(map.mode()).mods(mods_bits); + let mut calc = ctx.pp(&map).mode(map.mode()).mods(Mods::new(mods_bits)); let attrs_fut = calc.performance(); let scores_fut = diff --git a/bathbot/src/commands/osu/recent/leaderboard.rs b/bathbot/src/commands/osu/recent/leaderboard.rs index ed8d08e8..8b67b220 100644 --- a/bathbot/src/commands/osu/recent/leaderboard.rs +++ b/bathbot/src/commands/osu/recent/leaderboard.rs @@ -24,7 +24,7 @@ use crate::{ GameModeOption, }, core::commands::{prefix::Args, CommandOrigin}, - manager::redis::osu::UserArgs, + manager::{redis::osu::UserArgs, Mods}, Context, }; @@ -327,7 +327,7 @@ pub(super) async fn leaderboard( let mods_bits = specify_mods.as_ref().map_or(0, GameModsIntermode::bits); - let mut calc = ctx.pp(&map).mode(map.mode()).mods(mods_bits); + let mut calc = ctx.pp(&map).mode(map.mode()).mods(Mods::new(mods_bits)); let attrs = calc.performance().await; if let Some(ModSelection::Exclude(ref mods)) = mods { diff --git a/bathbot/src/embeds/tracking/notification.rs b/bathbot/src/embeds/tracking/notification.rs index b5c35bb8..b847e9bc 100644 --- a/bathbot/src/embeds/tracking/notification.rs +++ b/bathbot/src/embeds/tracking/notification.rs @@ -43,7 +43,7 @@ impl TrackNotificationEmbed { let attrs = ctx .pp(map) .mode(score.mode) - .mods(score.mods.bits()) + .mods(&score.mods) .performance() .await; diff --git a/bathbot/src/manager/pp.rs b/bathbot/src/manager/pp.rs index 20942375..845d0a79 100644 --- a/bathbot/src/manager/pp.rs +++ b/bathbot/src/manager/pp.rs @@ -287,14 +287,16 @@ impl<'s> From<&'s LeaderboardScore> for ScoreData { } } +/// Mods with an optional custom clock rate. #[derive(Copy, Clone, Default, PartialEq)] pub struct Mods { pub bits: u32, pub clock_rate: Option, } -impl From for Mods { - fn from(bits: u32) -> Self { +impl Mods { + /// Create new [`Mods`] without a custom clock rate. + pub fn new(bits: u32) -> Self { Self { bits, clock_rate: None, diff --git a/bathbot/src/util/osu.rs b/bathbot/src/util/osu.rs index 09ab87ac..d0653bb5 100644 --- a/bathbot/src/util/osu.rs +++ b/bathbot/src/util/osu.rs @@ -32,7 +32,7 @@ use time::OffsetDateTime; use crate::{ core::{BotConfig, Context}, embeds::HitResultFormatter, - manager::{redis::RedisData, OsuMap}, + manager::{redis::RedisData, Mods, OsuMap}, }; pub fn grade_emote(grade: Grade) -> &'static str { @@ -575,8 +575,7 @@ pub async fn get_combined_thumbnail<'s>( pub struct MapInfo<'map> { map: &'map OsuMap, stars: f32, - mods: Option, - clock_rate: Option, + mods: Mods, } impl<'map> MapInfo<'map> { @@ -584,19 +583,12 @@ impl<'map> MapInfo<'map> { Self { map, stars, - mods: None, - clock_rate: None, + mods: Mods::default(), } } - pub fn mods(&mut self, mods: u32) -> &mut Self { - self.mods = Some(mods); - - self - } - - pub fn clock_rate(&mut self, clock_rate: f32) -> &mut Self { - self.clock_rate = Some(clock_rate); + pub fn mods(&mut self, mods: impl Into) -> &mut Self { + self.mods = mods.into(); self } @@ -635,12 +627,12 @@ impl Display for MapInfo<'_> { GameMode::Mania => Mode::Mania, }; - let mods = self.mods.unwrap_or(0); + let mods = self.mods.bits; let mut builder = BeatmapAttributesBuilder::new(&self.map.pp_map); - if let Some(clock_rate) = self.clock_rate { - builder.clock_rate(clock_rate as f64); + if let Some(clock_rate) = self.mods.clock_rate { + builder.clock_rate(f64::from(clock_rate)); } let attrs = builder.mode(mode).mods(mods).build();