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 {
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<Beatmap>,
infos: Vec<Option<Option<BeatmapInfo>>>,
infos: Vec<Option<Option<BeatmapInfoWithPP>>>,
mode: Option<Mode>,
mods: Mods,
message: String,
}
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 cache = data.get::<BeatmapCache>().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
}

View file

@ -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<BeatmapInfo>,
info: Option<BeatmapInfoWithPP>,
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,
)

View file

@ -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<BeatmapInfo>, Mods),
Beatmap(Beatmap, Option<BeatmapInfoWithPP>, Mods),
Beatmapset(Vec<Beatmap>),
}
@ -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<BeatmapInfo>,
info: Option<BeatmapInfoWithPP>,
link: &'_ str,
mode: Option<Mode>,
mods: Mods,

View file

@ -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| {

View file

@ -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<oppai_rs::Mode>,
mods: impl Into<oppai_rs::Mods>,
_mods: impl Into<oppai_rs::Mods>,
) -> 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[..])?;
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))
}
}