mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-04-19 16:58:55 +00:00
Allow server-ranks by map length
This commit is contained in:
parent
e1671ca510
commit
0f91c3a22a
1 changed files with 22 additions and 12 deletions
|
@ -19,19 +19,20 @@ use serenity::{
|
||||||
use youmubot_prelude::{stream::FuturesUnordered, *};
|
use youmubot_prelude::{stream::FuturesUnordered, *};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
enum ModeOrTotal {
|
enum RankQuery {
|
||||||
Total,
|
Total,
|
||||||
|
MapLength,
|
||||||
Mode(Mode),
|
Mode(Mode),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for ModeOrTotal {
|
impl FromStr for RankQuery {
|
||||||
type Err = <ModeArg as FromStr>::Err;
|
type Err = <ModeArg as FromStr>::Err;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
if s == "total" {
|
match s {
|
||||||
Ok(ModeOrTotal::Total)
|
"total" => Ok(RankQuery::Total),
|
||||||
} else {
|
"map-length" => Ok(RankQuery::MapLength),
|
||||||
ModeArg::from_str(s).map(|ModeArg(m)| ModeOrTotal::Mode(m))
|
_ => ModeArg::from_str(s).map(|ModeArg(m)| RankQuery::Mode(m)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -113,8 +114,8 @@ fn table_formatting<const N: usize, S: AsRef<str> + std::fmt::Debug, Ts: AsRef<[
|
||||||
pub async fn server_rank(ctx: &Context, m: &Message, mut args: Args) -> CommandResult {
|
pub async fn server_rank(ctx: &Context, m: &Message, mut args: Args) -> CommandResult {
|
||||||
let data = ctx.data.read().await;
|
let data = ctx.data.read().await;
|
||||||
let mode = args
|
let mode = args
|
||||||
.single::<ModeOrTotal>()
|
.single::<RankQuery>()
|
||||||
.unwrap_or(ModeOrTotal::Mode(Mode::Std));
|
.unwrap_or(RankQuery::Mode(Mode::Std));
|
||||||
let guild = m.guild_id.expect("Guild-only command");
|
let guild = m.guild_id.expect("Guild-only command");
|
||||||
let member_cache = data.get::<MemberCache>().unwrap();
|
let member_cache = data.get::<MemberCache>().unwrap();
|
||||||
let osu_users = data
|
let osu_users = data
|
||||||
|
@ -132,10 +133,11 @@ pub async fn server_rank(ctx: &Context, m: &Message, mut args: Args) -> CommandR
|
||||||
.filter_map(|m| osu_users.get(&m.user.id).map(|ou| (m, ou)))
|
.filter_map(|m| osu_users.get(&m.user.id).map(|ou| (m, ou)))
|
||||||
.filter_map(|(member, osu_user)| {
|
.filter_map(|(member, osu_user)| {
|
||||||
let pp = match mode {
|
let pp = match mode {
|
||||||
ModeOrTotal::Total if osu_user.pp.iter().any(|v| v.is_some_and(|v| v > 0.0)) => {
|
RankQuery::Total if osu_user.pp.iter().any(|v| v.is_some_and(|v| v > 0.0)) => {
|
||||||
Some(osu_user.pp.iter().map(|v| v.unwrap_or(0.0)).sum())
|
Some(osu_user.pp.iter().map(|v| v.unwrap_or(0.0)).sum())
|
||||||
}
|
}
|
||||||
ModeOrTotal::Mode(m) => osu_user.pp.get(m as usize).and_then(|v| *v),
|
RankQuery::MapLength => osu_user.pp.get(Mode::Std as usize).and_then(|v| *v),
|
||||||
|
RankQuery::Mode(m) => osu_user.pp.get(m as usize).and_then(|v| *v),
|
||||||
_ => None,
|
_ => None,
|
||||||
}?;
|
}?;
|
||||||
Some((pp, member.user.name.clone(), osu_user))
|
Some((pp, member.user.name.clone(), osu_user))
|
||||||
|
@ -146,7 +148,15 @@ pub async fn server_rank(ctx: &Context, m: &Message, mut args: Args) -> CommandR
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(a, b, u)| (a, (b, u.clone())))
|
.map(|(a, b, u)| (a, (b, u.clone())))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
if matches!(mode, RankQuery::MapLength) {
|
||||||
|
users.sort_by(|(_, (_, a)), (_, (_, b))| {
|
||||||
|
(b.std_weighted_map_length)
|
||||||
|
.partial_cmp(&a.std_weighted_map_length)
|
||||||
|
.unwrap_or(std::cmp::Ordering::Equal)
|
||||||
|
});
|
||||||
|
} else {
|
||||||
users.sort_by(|(a, _), (b, _)| (*b).partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));
|
users.sort_by(|(a, _), (b, _)| (*b).partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));
|
||||||
|
}
|
||||||
|
|
||||||
if users.is_empty() {
|
if users.is_empty() {
|
||||||
m.reply(&ctx, "No saved users in the current server...")
|
m.reply(&ctx, "No saved users in the current server...")
|
||||||
|
@ -169,7 +179,7 @@ pub async fn server_rank(ctx: &Context, m: &Message, mut args: Args) -> CommandR
|
||||||
}
|
}
|
||||||
let total_len = users.len();
|
let total_len = users.len();
|
||||||
let users = &users[start..end];
|
let users = &users[start..end];
|
||||||
let table = if matches!(mode, ModeOrTotal::Mode(Mode::Std)) {
|
let table = if matches!(mode, RankQuery::Mode(Mode::Std) | RankQuery::MapLength) {
|
||||||
const HEADERS: [&'static str; 5] =
|
const HEADERS: [&'static str; 5] =
|
||||||
["#", "pp", "Map length", "Username", "Member"];
|
["#", "pp", "Map length", "Username", "Member"];
|
||||||
const ALIGNS: [Align; 5] = [Right, Right, Right, Left, Left];
|
const ALIGNS: [Align; 5] = [Right, Right, Right, Left, Left];
|
||||||
|
|
Loading…
Add table
Reference in a new issue