Use the user's own top scores for saving checks

This commit is contained in:
Natsu Kagami 2024-02-05 01:43:42 +01:00
parent d20dbad1b8
commit 30f186c604
Signed by: nki
GPG key ID: 55A032EB38B49ADB
4 changed files with 93 additions and 91 deletions

1
Cargo.lock generated
View file

@ -2992,6 +2992,7 @@ dependencies = [
"dashmap",
"lazy_static",
"osuparse",
"rand",
"regex",
"reqwest",
"rosu-pp",

View file

@ -19,6 +19,7 @@ rosu-pp = "0.9.1"
serde = { version = "1.0.137", features = ["derive"] }
serenity = "0.11.2"
zip = "0.6.2"
rand = "0.8"
youmubot-db = { path = "../youmubot-db" }
youmubot-db-sql = { path = "../youmubot-db-sql" }

View file

@ -6,6 +6,7 @@ use crate::{
request::{BeatmapRequestKind, UserID},
Client as OsuHttpClient,
};
use rand::seq::IteratorRandom;
use serenity::{
collector::{CollectReaction, ReactionAction},
framework::standard::{
@ -26,11 +27,9 @@ pub(crate) mod display;
pub(crate) mod embeds;
mod hook;
pub(crate) mod oppai_cache;
mod register_user;
mod server_rank;
use db::OsuUser;
use db::{OsuLastBeatmap, OsuSavedUsers, OsuUserBests};
use db::{OsuLastBeatmap, OsuSavedUsers, OsuUser, OsuUserBests};
use embeds::{beatmap_embed, score_embed, user_embed};
use hook::SHORT_LINK_REGEX;
pub use hook::{dot_osu_hook, hook};
@ -176,27 +175,52 @@ pub async fn save(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult
let data = ctx.data.read().await;
let osu = data.get::<OsuClient>().unwrap();
async fn check(client: &OsuHttpClient, u: &User) -> Result<bool> {
let check_beatmap_id = register_user::user_register_beatmap_id(&u);
let user = args.single::<String>()?;
let u = match osu.user(UserID::Auto(user), |f| f).await? {
Some(u) => u,
None => {
msg.reply(&ctx, "user not found...").await?;
return Ok(());
}
};
async fn find_score(client: &OsuHttpClient, u: &User) -> Result<Option<(models::Score, Mode)>> {
for mode in &[Mode::Std, Mode::Taiko, Mode::Catch, Mode::Mania] {
let scores = client
.user_best(UserID::ID(u.id), |f| f.mode(*mode))
.await?;
match scores.into_iter().choose(&mut rand::thread_rng()) {
Some(v) => return Ok(Some((v, *mode))),
None => (),
};
}
Ok(None)
}
let (score, mode) = match find_score(&osu, &u).await? {
Some(v) => v,
None => {
msg.reply(
&ctx,
"No plays found in this account! Play something first...!",
)
.await?;
return Ok(());
}
};
async fn check(client: &OsuHttpClient, u: &User, map_id: u64) -> Result<bool> {
Ok(client
.user_recent(UserID::ID(u.id), |f| f.mode(Mode::Std).limit(1))
.await?
.into_iter()
.take(1)
.any(|s| s.beatmap_id == check_beatmap_id))
.any(|s| s.beatmap_id == map_id))
}
let user = args.single::<String>()?;
let user: Option<User> = osu.user(UserID::Auto(user), |f| f).await?;
match user {
Some(u) => {
if !check(&osu, &u).await? {
let check_beatmap_id = register_user::user_register_beatmap_id(&u);
let reply = msg.reply(&ctx, format!("To set your osu username, please make your most recent play be the following map: `/b/{}` in **osu! standard** mode! It does **not** have to be a pass, and **NF** can be used! React to this message with 👌 within 5 minutes when you're done!", check_beatmap_id));
let reply = msg.reply(&ctx, format!("To set your osu username, please make your most recent play be the following map: `/b/{}` in **{}** mode! It does **not** have to be a pass, and **NF** can be used! React to this message with 👌 within 5 minutes when you're done!", score.beatmap_id, mode.as_str_new_site()));
let beatmap = osu
.beatmaps(
crate::request::BeatmapRequestKind::Beatmap(check_beatmap_id),
|f| f,
crate::request::BeatmapRequestKind::Beatmap(score.beatmap_id),
|f| f.mode(mode, true),
)
.await?
.into_iter()
@ -207,11 +231,11 @@ pub async fn save(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult
.unwrap()
.get_beatmap(beatmap.beatmap_id)
.await?
.get_possible_pp_with(Mode::Std, Mods::NOMOD)?;
.get_possible_pp_with(mode, Mods::NOMOD)?;
let mut reply = reply.await?;
reply
.edit(&ctx, |f| {
f.embed(|e| beatmap_embed(&beatmap, Mode::Std, Mods::NOMOD, info, e))
f.embed(|e| beatmap_embed(&beatmap, mode, Mods::NOMOD, info, e))
})
.await?;
let reaction = reply.react(&ctx, '👌').await?;
@ -225,7 +249,7 @@ pub async fn save(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult
.collect_limit(1)
.await;
if let Some(ur) = user_reaction {
if check(&osu, &u).await? {
if check(&osu, &u, score.beatmap_id).await? {
break true;
}
if let ReactionAction::Added(ur) = &*ur {
@ -239,7 +263,7 @@ pub async fn save(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult
reaction.delete(&ctx).await?;
return Ok(());
}
}
let username = u.username.clone();
add_user(msg.author.id, u, &data).await?;
msg.reply(
@ -250,11 +274,6 @@ pub async fn save(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult
.build(),
)
.await?;
}
None => {
msg.reply(&ctx, "user not found...").await?;
}
}
Ok(())
}

View file

@ -1,19 +0,0 @@
use crate::models::User;
const BEATMAP_IDS: [u64; 100] = [
2469345, 2084862, 2486881, 2330357, 2546607, 1655981, 1626537, 888015, 1062394, 1319547,
1852572, 1944926, 2129143, 1057509, 2022718, 1097543, 1736329, 1056207, 930249, 1936782,
1919312, 1570203, 2201460, 1495498, 965549, 2428358, 2118444, 1849433, 820619, 999944, 1571309,
1055147, 1619555, 338682, 1438917, 954692, 824891, 2026320, 764014, 2237466, 2058788, 1969946,
1892257, 1473301, 2336704, 774965, 657509, 1031604, 898576, 714001, 1872396, 831705, 1917082,
978326, 795232, 1814494, 713867, 2077126, 1612329, 1314214, 1849273, 1829925, 1640362, 801158,
431957, 1054501, 1627148, 816600, 1857519, 1080094, 1642274, 1232440, 1843653, 953586, 2044362,
1489536, 951053, 1069111, 2154507, 1007699, 1099936, 1077323, 1874119, 909032, 760466, 1911308,
1820921, 1231520, 954254, 425779, 1586059, 2198684, 1040044, 799913, 994933, 969681, 888016,
1100327, 1063410, 2078961,
];
pub fn user_register_beatmap_id(u: &User) -> u64 {
let now = chrono::Utc::now();
BEATMAP_IDS[(u.id + (now.timestamp() / 3600) as u64) as usize % BEATMAP_IDS.len()]
}