Save on not calculating pp in beatmap info

This commit is contained in:
Natsu Kagami 2021-02-17 05:07:25 +09:00
parent 6af2bf967f
commit 96fd0427f5
Signed by: nki
GPG key ID: 7306B3D3C3AD6E51
5 changed files with 39 additions and 19 deletions

View file

@ -2,7 +2,9 @@ pub use beatmapset::display_beatmapset;
mod beatmapset { mod beatmapset {
use crate::{ use crate::{
discord::{cache::save_beatmap, oppai_cache::BeatmapInfo, BeatmapCache, BeatmapWithMode}, discord::{
cache::save_beatmap, oppai_cache::BeatmapInfoWithPP, BeatmapCache, BeatmapWithMode,
},
models::{Beatmap, Mode, Mods}, models::{Beatmap, Mode, Mods},
}; };
use serenity::{ use serenity::{
@ -46,14 +48,14 @@ mod beatmapset {
struct Paginate { struct Paginate {
maps: Vec<Beatmap>, maps: Vec<Beatmap>,
infos: Vec<Option<Option<BeatmapInfo>>>, infos: Vec<Option<Option<BeatmapInfoWithPP>>>,
mode: Option<Mode>, mode: Option<Mode>,
mods: Mods, mods: Mods,
message: String, message: String,
} }
impl Paginate { impl Paginate {
async fn get_beatmap_info(&self, ctx: &Context, b: &Beatmap) -> Option<BeatmapInfo> { async fn get_beatmap_info(&self, ctx: &Context, b: &Beatmap) -> Option<BeatmapInfoWithPP> {
let data = ctx.data.read().await; let data = ctx.data.read().await;
let cache = data.get::<BeatmapCache>().unwrap(); let cache = data.get::<BeatmapCache>().unwrap();
let mode = self.mode.unwrap_or(b.mode).to_oppai_mode(); let mode = self.mode.unwrap_or(b.mode).to_oppai_mode();
@ -61,7 +63,7 @@ mod beatmapset {
.get_beatmap(b.beatmap_id) .get_beatmap(b.beatmap_id)
.map(move |v| { .map(move |v| {
v.ok() 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 .await
} }

View file

@ -1,6 +1,6 @@
use super::BeatmapWithMode; use super::BeatmapWithMode;
use crate::{ use crate::{
discord::oppai_cache::{BeatmapContent, BeatmapInfo, OppaiAccuracy}, discord::oppai_cache::{BeatmapContent, BeatmapInfo, BeatmapInfoWithPP, OppaiAccuracy},
models::{Beatmap, Mode, Mods, Rank, Score, User}, models::{Beatmap, Mode, Mods, Rank, Score, User},
}; };
use chrono::Utc; use chrono::Utc;
@ -60,7 +60,7 @@ pub fn beatmap_embed<'a>(
b: &'_ Beatmap, b: &'_ Beatmap,
m: Mode, m: Mode,
mods: Mods, mods: Mods,
info: Option<BeatmapInfo>, info: Option<BeatmapInfoWithPP>,
c: &'a mut CreateEmbed, c: &'a mut CreateEmbed,
) -> &'a mut CreateEmbed { ) -> &'a mut CreateEmbed {
let mod_str = if mods == Mods::NOMOD { let mod_str = if mods == Mods::NOMOD {
@ -68,7 +68,9 @@ pub fn beatmap_embed<'a>(
} else { } else {
format!(" {}", mods) 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( c.title(
MessageBuilder::new() MessageBuilder::new()
.push_bold_safe(&b.artist) .push_bold_safe(&b.artist)
@ -88,12 +90,12 @@ pub fn beatmap_embed<'a>(
.url(b.link()) .url(b.link())
.image(b.cover_url()) .image(b.cover_url())
.color(0xffb6c1) .color(0xffb6c1)
.fields(info.map(|info| { .fields(info.map(|(_, pp)| {
( (
"Calculated pp", "Calculated pp",
format!( format!(
"95%: **{:.2}**pp, 98%: **{:.2}**pp, 99%: **{:.2}**pp, 100%: **{:.2}**pp", "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, false,
) )

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
discord::beatmap_cache::BeatmapMetaCache, discord::beatmap_cache::BeatmapMetaCache,
discord::oppai_cache::{BeatmapCache, BeatmapInfo}, discord::oppai_cache::{BeatmapCache, BeatmapInfoWithPP},
models::{Beatmap, Mode, Mods}, models::{Beatmap, Mode, Mods},
}; };
use lazy_static::lazy_static; use lazy_static::lazy_static;
@ -67,7 +67,7 @@ pub fn hook<'a>(
} }
enum EmbedType { enum EmbedType {
Beatmap(Beatmap, Option<BeatmapInfo>, Mods), Beatmap(Beatmap, Option<BeatmapInfoWithPP>, Mods),
Beatmapset(Vec<Beatmap>), Beatmapset(Vec<Beatmap>),
} }
@ -125,7 +125,7 @@ fn handle_old_links<'a>(
Some(mode) => cache Some(mode) => cache
.get_beatmap(b.beatmap_id) .get_beatmap(b.beatmap_id)
.await .await
.and_then(|b| b.get_info_with(Some(mode), mods)) .and_then(|b| b.get_possible_pp_with(Some(mode), mods))
.pls_ok(), .pls_ok(),
None => None, None => None,
}; };
@ -195,7 +195,7 @@ fn handle_new_links<'a>(
Some(mode) => cache Some(mode) => cache
.get_beatmap(beatmap.beatmap_id) .get_beatmap(beatmap.beatmap_id)
.await .await
.and_then(|b| b.get_info_with(Some(mode), mods)) .and_then(|b| b.get_possible_pp_with(Some(mode), mods))
.pls_ok(), .pls_ok(),
None => None, None => None,
}; };
@ -261,7 +261,7 @@ fn handle_short_links<'a>(
Some(mode) => cache Some(mode) => cache
.get_beatmap(beatmap.beatmap_id) .get_beatmap(beatmap.beatmap_id)
.await .await
.and_then(|b| b.get_info_with(Some(mode), mods)) .and_then(|b| b.get_possible_pp_with(Some(mode), mods))
.pls_ok(), .pls_ok(),
None => None, None => None,
}; };
@ -287,7 +287,7 @@ fn handle_short_links<'a>(
async fn handle_beatmap<'a, 'b>( async fn handle_beatmap<'a, 'b>(
ctx: &Context, ctx: &Context,
beatmap: &Beatmap, beatmap: &Beatmap,
info: Option<BeatmapInfo>, info: Option<BeatmapInfoWithPP>,
link: &'_ str, link: &'_ str,
mode: Option<Mode>, mode: Option<Mode>,
mods: Mods, mods: Mods,

View file

@ -610,7 +610,7 @@ pub async fn last(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult
.unwrap() .unwrap()
.get_beatmap(b.beatmap_id) .get_beatmap(b.beatmap_id)
.await? .await?
.get_info_with(m.to_oppai_mode(), mods) .get_possible_pp_with(m.to_oppai_mode(), mods)
.ok(); .ok();
msg.channel_id msg.channel_id
.send_message(&ctx, |f| { .send_message(&ctx, |f| {

View file

@ -15,9 +15,11 @@ pub struct BeatmapContent {
pub struct BeatmapInfo { pub struct BeatmapInfo {
pub objects: u32, pub objects: u32,
pub stars: f32, 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 { impl BeatmapContent {
/// Get pp given the combo and accuracy. /// Get pp given the combo and accuracy.
pub fn get_pp_from( pub fn get_pp_from(
@ -39,8 +41,22 @@ impl BeatmapContent {
pub fn get_info_with( pub fn get_info_with(
&self, &self,
mode: Option<oppai_rs::Mode>, mode: Option<oppai_rs::Mode>,
mods: impl Into<oppai_rs::Mods>, _mods: impl Into<oppai_rs::Mods>,
) -> Result<BeatmapInfo> { ) -> Result<BeatmapInfo> {
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<oppai_rs::Mode>,
mods: impl Into<oppai_rs::Mods>,
) -> Result<BeatmapInfoWithPP> {
let mut oppai = oppai_rs::Oppai::new_from_content(&self.content[..])?; let mut oppai = oppai_rs::Oppai::new_from_content(&self.content[..])?;
if let Some(mode) = mode { if let Some(mode) = mode {
oppai.mode(mode)?; oppai.mode(mode)?;
@ -54,7 +70,7 @@ impl BeatmapContent {
]; ];
let objects = oppai.num_objects(); let objects = oppai.num_objects();
let stars = oppai.stars(); let stars = oppai.stars();
Ok(BeatmapInfo { stars, pp, objects }) Ok((BeatmapInfo { stars, objects }, pp))
} }
} }