From 96fd0427f5f3e88ba3f5c677c18889a5a271e8f2 Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Wed, 17 Feb 2021 05:07:25 +0900 Subject: [PATCH] Save on not calculating pp in beatmap info --- youmubot-osu/src/discord/display.rs | 10 ++++++---- youmubot-osu/src/discord/embeds.rs | 12 +++++++----- youmubot-osu/src/discord/hook.rs | 12 ++++++------ youmubot-osu/src/discord/mod.rs | 2 +- youmubot-osu/src/discord/oppai_cache.rs | 22 +++++++++++++++++++--- 5 files changed, 39 insertions(+), 19 deletions(-) diff --git a/youmubot-osu/src/discord/display.rs b/youmubot-osu/src/discord/display.rs index 47e0760..93c8963 100644 --- a/youmubot-osu/src/discord/display.rs +++ b/youmubot-osu/src/discord/display.rs @@ -2,7 +2,9 @@ pub use beatmapset::display_beatmapset; mod beatmapset { use crate::{ - discord::{cache::save_beatmap, oppai_cache::BeatmapInfo, BeatmapCache, BeatmapWithMode}, + discord::{ + cache::save_beatmap, oppai_cache::BeatmapInfoWithPP, BeatmapCache, BeatmapWithMode, + }, models::{Beatmap, Mode, Mods}, }; use serenity::{ @@ -46,14 +48,14 @@ mod beatmapset { struct Paginate { maps: Vec, - infos: Vec>>, + infos: Vec>>, mode: Option, mods: Mods, message: String, } impl Paginate { - async fn get_beatmap_info(&self, ctx: &Context, b: &Beatmap) -> Option { + async fn get_beatmap_info(&self, ctx: &Context, b: &Beatmap) -> Option { let data = ctx.data.read().await; let cache = data.get::().unwrap(); let mode = self.mode.unwrap_or(b.mode).to_oppai_mode(); @@ -61,7 +63,7 @@ mod beatmapset { .get_beatmap(b.beatmap_id) .map(move |v| { v.ok() - .and_then(move |v| v.get_info_with(Some(mode?), self.mods).ok()) + .and_then(move |v| v.get_possible_pp_with(Some(mode?), self.mods).ok()) }) .await } diff --git a/youmubot-osu/src/discord/embeds.rs b/youmubot-osu/src/discord/embeds.rs index 2491f7e..cc954a0 100644 --- a/youmubot-osu/src/discord/embeds.rs +++ b/youmubot-osu/src/discord/embeds.rs @@ -1,6 +1,6 @@ use super::BeatmapWithMode; use crate::{ - discord::oppai_cache::{BeatmapContent, BeatmapInfo, OppaiAccuracy}, + discord::oppai_cache::{BeatmapContent, BeatmapInfo, BeatmapInfoWithPP, OppaiAccuracy}, models::{Beatmap, Mode, Mods, Rank, Score, User}, }; use chrono::Utc; @@ -60,7 +60,7 @@ pub fn beatmap_embed<'a>( b: &'_ Beatmap, m: Mode, mods: Mods, - info: Option, + info: Option, c: &'a mut CreateEmbed, ) -> &'a mut CreateEmbed { let mod_str = if mods == Mods::NOMOD { @@ -68,7 +68,9 @@ pub fn beatmap_embed<'a>( } else { format!(" {}", mods) }; - let diff = b.difficulty.apply_mods(mods, info.map(|v| v.stars as f64)); + let diff = b + .difficulty + .apply_mods(mods, info.map(|(v, _)| v.stars as f64)); c.title( MessageBuilder::new() .push_bold_safe(&b.artist) @@ -88,12 +90,12 @@ pub fn beatmap_embed<'a>( .url(b.link()) .image(b.cover_url()) .color(0xffb6c1) - .fields(info.map(|info| { + .fields(info.map(|(_, pp)| { ( "Calculated pp", format!( "95%: **{:.2}**pp, 98%: **{:.2}**pp, 99%: **{:.2}**pp, 100%: **{:.2}**pp", - info.pp[0], info.pp[1], info.pp[2], info.pp[3] + pp[0], pp[1], pp[2], pp[3] ), false, ) diff --git a/youmubot-osu/src/discord/hook.rs b/youmubot-osu/src/discord/hook.rs index 6c33f9e..d5429a9 100644 --- a/youmubot-osu/src/discord/hook.rs +++ b/youmubot-osu/src/discord/hook.rs @@ -1,6 +1,6 @@ use crate::{ discord::beatmap_cache::BeatmapMetaCache, - discord::oppai_cache::{BeatmapCache, BeatmapInfo}, + discord::oppai_cache::{BeatmapCache, BeatmapInfoWithPP}, models::{Beatmap, Mode, Mods}, }; use lazy_static::lazy_static; @@ -67,7 +67,7 @@ pub fn hook<'a>( } enum EmbedType { - Beatmap(Beatmap, Option, Mods), + Beatmap(Beatmap, Option, Mods), Beatmapset(Vec), } @@ -125,7 +125,7 @@ fn handle_old_links<'a>( Some(mode) => cache .get_beatmap(b.beatmap_id) .await - .and_then(|b| b.get_info_with(Some(mode), mods)) + .and_then(|b| b.get_possible_pp_with(Some(mode), mods)) .pls_ok(), None => None, }; @@ -195,7 +195,7 @@ fn handle_new_links<'a>( Some(mode) => cache .get_beatmap(beatmap.beatmap_id) .await - .and_then(|b| b.get_info_with(Some(mode), mods)) + .and_then(|b| b.get_possible_pp_with(Some(mode), mods)) .pls_ok(), None => None, }; @@ -261,7 +261,7 @@ fn handle_short_links<'a>( Some(mode) => cache .get_beatmap(beatmap.beatmap_id) .await - .and_then(|b| b.get_info_with(Some(mode), mods)) + .and_then(|b| b.get_possible_pp_with(Some(mode), mods)) .pls_ok(), None => None, }; @@ -287,7 +287,7 @@ fn handle_short_links<'a>( async fn handle_beatmap<'a, 'b>( ctx: &Context, beatmap: &Beatmap, - info: Option, + info: Option, link: &'_ str, mode: Option, mods: Mods, diff --git a/youmubot-osu/src/discord/mod.rs b/youmubot-osu/src/discord/mod.rs index db42722..239965f 100644 --- a/youmubot-osu/src/discord/mod.rs +++ b/youmubot-osu/src/discord/mod.rs @@ -610,7 +610,7 @@ pub async fn last(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult .unwrap() .get_beatmap(b.beatmap_id) .await? - .get_info_with(m.to_oppai_mode(), mods) + .get_possible_pp_with(m.to_oppai_mode(), mods) .ok(); msg.channel_id .send_message(&ctx, |f| { diff --git a/youmubot-osu/src/discord/oppai_cache.rs b/youmubot-osu/src/discord/oppai_cache.rs index 47c1fb3..79ba898 100644 --- a/youmubot-osu/src/discord/oppai_cache.rs +++ b/youmubot-osu/src/discord/oppai_cache.rs @@ -15,9 +15,11 @@ pub struct BeatmapContent { pub struct BeatmapInfo { pub objects: u32, pub stars: f32, - pub pp: [f32; 4], // 95, 98, 99, 100 } +/// Beatmap Info with attached 95/98/99/100% FC pp. +pub type BeatmapInfoWithPP = (BeatmapInfo, [f32; 4]); + impl BeatmapContent { /// Get pp given the combo and accuracy. pub fn get_pp_from( @@ -39,8 +41,22 @@ impl BeatmapContent { pub fn get_info_with( &self, mode: Option, - mods: impl Into, + _mods: impl Into, ) -> Result { + let mut oppai = oppai_rs::Oppai::new_from_content(&self.content[..])?; + if let Some(mode) = mode { + oppai.mode(mode)?; + } + let objects = oppai.num_objects(); + let stars = oppai.stars(); + Ok(BeatmapInfo { stars, objects }) + } + + pub fn get_possible_pp_with( + &self, + mode: Option, + mods: impl Into, + ) -> Result { let mut oppai = oppai_rs::Oppai::new_from_content(&self.content[..])?; if let Some(mode) = mode { oppai.mode(mode)?; @@ -54,7 +70,7 @@ impl BeatmapContent { ]; let objects = oppai.num_objects(); let stars = oppai.stars(); - Ok(BeatmapInfo { stars, pp, objects }) + Ok((BeatmapInfo { stars, objects }, pp)) } }