From 7565a6e5c572063c73c0dfa7627c0a517d52fe5c Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Mon, 5 Aug 2024 16:04:54 +0200 Subject: [PATCH] Properly scale map age to 0.975 --- youmubot-db-sql/src/lib.rs | 3 ++- youmubot-osu/src/discord/mod.rs | 42 ++++++++++++++++++++------------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/youmubot-db-sql/src/lib.rs b/youmubot-db-sql/src/lib.rs index 0662619..fd0c3b0 100644 --- a/youmubot-db-sql/src/lib.rs +++ b/youmubot-db-sql/src/lib.rs @@ -18,7 +18,8 @@ pub async fn connect(path: impl AsRef) -> Result { .filename(path) .foreign_keys(true) .create_if_missing(true) - .journal_mode(sqlite::SqliteJournalMode::Wal), + .journal_mode(sqlite::SqliteJournalMode::Wal) + .synchronous(sqlite::SqliteSynchronous::Normal), ) .await?; diff --git a/youmubot-osu/src/discord/mod.rs b/youmubot-osu/src/discord/mod.rs index f5292d2..8c2a624 100644 --- a/youmubot-osu/src/discord/mod.rs +++ b/youmubot-osu/src/discord/mod.rs @@ -22,7 +22,7 @@ pub use hook::{dot_osu_hook, hook, score_hook}; use server_rank::{SERVER_RANK_COMMAND, SHOW_LEADERBOARD_COMMAND}; use stream::FuturesOrdered; use youmubot_prelude::announcer::AnnouncerHandler; -use youmubot_prelude::{stream::FuturesUnordered, *}; +use youmubot_prelude::*; use crate::{ discord::beatmap_cache::BeatmapMetaCache, @@ -880,27 +880,41 @@ async fn get_user( Ok(()) } +const SCALING_FACTOR: f64 = 0.975; +static SCALES: std::sync::OnceLock> = std::sync::OnceLock::new(); +fn scales() -> &'static [f64] { + SCALES.get_or_init(|| { + (0..256) + .map(|r| SCALING_FACTOR.powi(r)) + // .scan(1.0, |a, _| { + // let old = *a; + // *a *= SCALING_FACTOR; + // Some(old) + // }) + .collect::>() + .into_boxed_slice() + }) +} + pub(in crate::discord) async fn calculate_weighted_map_length( from_scores: impl IntoIterator, cache: &BeatmapMetaCache, mode: Mode, ) -> Result { - from_scores + let scores = from_scores .into_iter() - .enumerate() - .map(|(i, s)| async move { + .map(|s| async move { let beatmap = cache.get_beatmap(s.beatmap_id, mode).await?; - const SCALING_FACTOR: f64 = 0.975; Ok(beatmap .difficulty .apply_mods(s.mods, 0.0 /* dont care */) .drain_length - .as_secs_f64() - * (SCALING_FACTOR.powi(i as i32))) + .as_secs_f64()) as Result<_> }) - .collect::>() - .try_fold(0.0, |a, b| future::ready(Ok(a + b))) - .await + .collect::>() + .try_collect::>() + .await?; + Ok(scores.into_iter().zip(scales()).map(|(a, b)| a * b).sum()) } pub(in crate::discord) async fn calculate_weighted_map_age( @@ -908,10 +922,6 @@ pub(in crate::discord) async fn calculate_weighted_map_age( cache: &BeatmapMetaCache, mode: Mode, ) -> Result { - const SCALING_FACTOR: f64 = 0.95; - let scales = (0..100) - .scan(1.0, |a, _| Some(*a * SCALING_FACTOR)) - .collect::>(); let scores = from_scores .into_iter() .map(|s| async move { @@ -929,9 +939,9 @@ pub(in crate::discord) async fn calculate_weighted_map_age( .await?; Ok((scores .iter() - .zip(scales.iter()) + .zip(scales().iter()) .map(|(a, b)| a * b) .sum::() - / scales.iter().take(scores.len()).sum::()) + / scales().iter().take(scores.len()).sum::()) .floor() as i64) }