osu: fix 0-based indices

This commit is contained in:
Natsu Kagami 2024-10-12 17:22:33 +02:00
parent 6fbae89dfe
commit c5354e30ad
Signed by: nki
GPG key ID: 55A032EB38B49ADB

View file

@ -481,8 +481,12 @@ impl FromStr for Nth {
} else if !s.starts_with('#') { } else if !s.starts_with('#') {
Err(Error::msg("Not an order")) Err(Error::msg("Not an order"))
} else { } else {
let v = s.split_at("#".len()).1.parse()?; let v = s.split_at("#".len()).1.parse::<u8>()?;
Ok(Nth::Nth(v)) if v > 0 {
Ok(Nth::Nth(v - 1))
} else {
Err(Error::msg("number has to be at least 1"))
}
} }
} }
} }
@ -872,19 +876,17 @@ pub async fn top(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult
.await? .await?
.ok_or_else(|| Error::msg("User not found"))?; .ok_or_else(|| Error::msg("User not found"))?;
match nth { let plays = osu_client
Nth::Nth(nth) => { .user_best(UserID::ID(user.id), |f| f.mode(mode).limit(100))
let top_play = osu_client
.user_best(UserID::ID(user.id), |f| f.mode(mode).limit(nth))
.await?; .await?;
let rank = top_play.len() as u8; match nth {
Nth::Nth(nth) => {
let Some(play) = plays.get(nth as usize) else {
Err(Error::msg("no such play"))?
};
let top_play = top_play let beatmap = env.beatmaps.get_beatmap(play.beatmap_id, mode).await?;
.into_iter()
.last()
.ok_or_else(|| Error::msg("No such play"))?;
let beatmap = env.beatmaps.get_beatmap(top_play.beatmap_id, mode).await?;
let content = env.oppai.get_beatmap(beatmap.beatmap_id).await?; let content = env.oppai.get_beatmap(beatmap.beatmap_id).await?;
let beatmap = BeatmapWithMode(beatmap, mode); let beatmap = BeatmapWithMode(beatmap, mode);
@ -896,8 +898,8 @@ pub async fn top(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult
msg.author msg.author
)) ))
.embed( .embed(
score_embed(&top_play, &beatmap, &content, &user) score_embed(&play, &beatmap, &content, &user)
.top_record(rank) .top_record(nth + 1)
.build(), .build(),
) )
.components(vec![score_components(msg.guild_id)]) .components(vec![score_components(msg.guild_id)])
@ -908,9 +910,6 @@ pub async fn top(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult
cache::save_beatmap(&env, msg.channel_id, &beatmap).await?; cache::save_beatmap(&env, msg.channel_id, &beatmap).await?;
} }
Nth::All => { Nth::All => {
let plays = osu_client
.user_best(UserID::ID(user.id), |f| f.mode(mode).limit(100))
.await?;
let reply = msg let reply = msg
.reply( .reply(
&ctx, &ctx,