osu: Server leaderboard command (#6)

* New db stores user best by beatmap id

* `check` command saves into the database

* Add leaderboard command

* Update youmubot-osu/src/discord/server_rank.rs

Swap ordering
This commit is contained in:
Natsu Kagami 2020-09-13 04:55:36 +00:00 committed by GitHub
parent bfd9d1c68d
commit ce0349859c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 220 additions and 15 deletions

View file

@ -27,10 +27,10 @@ pub(crate) mod oppai_cache;
mod server_rank;
use db::OsuUser;
use db::{OsuLastBeatmap, OsuSavedUsers};
use db::{OsuLastBeatmap, OsuSavedUsers, OsuUserBests};
use embeds::{beatmap_embed, score_embed, user_embed};
pub use hook::hook;
use server_rank::SERVER_RANK_COMMAND;
use server_rank::{LEADERBOARD_COMMAND, SERVER_RANK_COMMAND};
/// The osu! client.
pub(crate) struct OsuClient;
@ -58,6 +58,7 @@ pub fn setup(
// Databases
OsuSavedUsers::insert_into(&mut *data, &path.join("osu_saved_users.yaml"))?;
OsuLastBeatmap::insert_into(&mut *data, &path.join("last_beatmaps.yaml"))?;
OsuUserBests::insert_into(&mut *data, &path.join("osu_user_bests.yaml"))?;
// API client
let http_client = data.get_cloned::<HTTPClient>();
@ -79,7 +80,19 @@ pub fn setup(
#[group]
#[prefix = "osu"]
#[description = "osu! related commands."]
#[commands(std, taiko, catch, mania, save, recent, last, check, top, server_rank)]
#[commands(
std,
taiko,
catch,
mania,
save,
recent,
last,
check,
top,
server_rank,
leaderboard
)]
#[default_command(std)]
struct Osu;
@ -249,7 +262,7 @@ fn list_plays(plays: Vec<Score>, mode: Mode, ctx: Context, m: &Message) -> Comma
}
let plays = &plays[start..end];
let beatmaps = {
let beatmaps: Vec<&mut String> = {
let b = &mut beatmaps[start..end];
b.par_iter_mut()
.enumerate()
@ -452,7 +465,8 @@ pub fn last(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
#[command]
#[aliases("c", "chk")]
#[description = "Check your own or someone else's best record on the last beatmap."]
#[usage = "[username or tag = yourself]"]
#[description = "Check your own or someone else's best record on the last beatmap. Also stores the result if possible."]
#[max_args(1)]
pub fn check(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {
let bm = cache::get_beatmap(&*ctx.data.read(), msg.channel_id)?;
@ -464,7 +478,13 @@ pub fn check(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult
Some(bm) => {
let b = &bm.0;
let m = bm.1;
let user = to_user_id_query(args.single::<UsernameArg>().ok(), &*ctx.data.read(), msg)?;
let username_arg = args.single::<UsernameArg>().ok();
let user_id = match username_arg.as_ref() {
Some(UsernameArg::Tagged(v)) => Some(v.clone()),
None => Some(msg.author.id),
_ => None,
};
let user = to_user_id_query(username_arg, &*ctx.data.read(), msg)?;
let osu = ctx.data.get_cloned::<OsuClient>();
let oppai = ctx.data.get_cloned::<BeatmapCache>();
@ -480,11 +500,20 @@ pub fn check(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult
msg.reply(&ctx, "No scores found")?;
}
for score in scores.into_iter() {
for score in scores.iter() {
msg.channel_id.send_message(&ctx, |c| {
c.embed(|m| score_embed(&score, &bm, &content, &user, None, m))
c.embed(|m| score_embed(score, &bm, &content, &user, None, m))
})?;
}
if let Some(user_id) = user_id {
// Save to database
OsuUserBests::open(&*ctx.data.read())
.borrow_mut()?
.entry((bm.0.beatmap_id, bm.1))
.or_default()
.insert(user_id, scores);
}
}
}
@ -493,7 +522,7 @@ pub fn check(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult
#[command]
#[description = "Get the n-th top record of an user."]
#[usage = "#[n-th = --all] / [mode (std, taiko, catch, mania) = std / [username or user_id = your saved user id]"]
#[usage = "#[n-th = --all] / [mode (std, taiko, catch, mania)] = std / [username or user_id = your saved user id]"]
#[example = "#2 / taiko / natsukagami"]
#[max_args(3)]
pub fn top(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult {