Properly format ranks table

This commit is contained in:
Natsu Kagami 2024-08-04 21:35:49 +02:00 committed by Natsu Kagami
parent 5c523009e1
commit 4909f6ea27

View file

@ -83,12 +83,12 @@ impl FromStr for RankQuery {
#[command("ranks")] #[command("ranks")]
#[description = "See the server's ranks"] #[description = "See the server's ranks"]
#[usage = "[mode (Std, Taiko, Catch, Mania) = Std]"] #[usage = "[mode (Std, Taiko, Catch, Mania) = Std]"]
#[max_args(1)] #[max_args(2)]
#[only_in(guilds)] #[only_in(guilds)]
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 env = ctx.data.read().await.get::<OsuEnv>().unwrap().clone(); let env = ctx.data.read().await.get::<OsuEnv>().unwrap().clone();
let mode = args.find::<ModeArg>().map(|v| v.0).unwrap_or(Mode::Std); let mode = args.find::<ModeArg>().map(|v| v.0).unwrap_or(Mode::Std);
let query = args.single::<RankQuery>().unwrap_or_default(); let query = args.find::<RankQuery>().unwrap_or_default();
let guild = m.guild_id.expect("Guild-only command"); let guild = m.guild_id.expect("Guild-only command");
let mut users = env let mut users = env
@ -124,6 +124,7 @@ pub async fn server_rank(ctx: &Context, m: &Message, mut args: Args) -> CommandR
.map(|v| v.pp) .map(|v| v.pp)
.partial_cmp(&b.modes.get(&mode).map(|v| v.pp)) .partial_cmp(&b.modes.get(&mode).map(|v| v.pp))
.unwrap() .unwrap()
.reverse()
}), }),
RankQuery::TotalPP => Box::new(|(_, a), (_, b)| { RankQuery::TotalPP => Box::new(|(_, a), (_, b)| {
a.modes a.modes
@ -132,6 +133,7 @@ pub async fn server_rank(ctx: &Context, m: &Message, mut args: Args) -> CommandR
.sum::<f64>() .sum::<f64>()
.partial_cmp(&b.modes.values().map(|v| v.pp).sum()) .partial_cmp(&b.modes.values().map(|v| v.pp).sum())
.unwrap() .unwrap()
.reverse()
}), }),
RankQuery::MapLength => Box::new(|(_, a), (_, b)| { RankQuery::MapLength => Box::new(|(_, a), (_, b)| {
a.modes a.modes
@ -139,6 +141,7 @@ pub async fn server_rank(ctx: &Context, m: &Message, mut args: Args) -> CommandR
.map(|v| v.map_length) .map(|v| v.map_length)
.partial_cmp(&b.modes.get(&mode).map(|v| v.map_length)) .partial_cmp(&b.modes.get(&mode).map(|v| v.map_length))
.unwrap() .unwrap()
.reverse()
}), }),
}; };
users.sort_unstable_by(sort_fn); users.sort_unstable_by(sort_fn);
@ -165,9 +168,21 @@ pub async fn server_rank(ctx: &Context, m: &Message, mut args: Args) -> CommandR
return Ok(false); return Ok(false);
} }
let users = &users[start..end]; let users = &users[start..end];
let table = { let table = match query {
const HEADERS: [&'static str; 5] = RankQuery::PP | RankQuery::MapLength => {
["#", "pp", "Map length", "Username", "Member"]; let (headers, first_col, second_col) = if query == RankQuery::PP {
(
["#", "pp", "Map length", "Username", "Member"],
RankQuery::PP,
RankQuery::MapLength,
)
} else {
(
["#", "Map length", "pp", "Username", "Member"],
RankQuery::MapLength,
RankQuery::PP,
)
};
const ALIGNS: [Align; 5] = [Right, Right, Right, Left, Left]; const ALIGNS: [Align; 5] = [Right, Right, Right, Left, Left];
let table = users let table = users
@ -176,14 +191,33 @@ pub async fn server_rank(ctx: &Context, m: &Message, mut args: Args) -> CommandR
.map(|(i, (mem, ou))| { .map(|(i, (mem, ou))| {
[ [
format!("{}", 1 + i + start), format!("{}", 1 + i + start),
RankQuery::PP.extract_row(mode, ou).to_string(), first_col.extract_row(mode, ou).to_string(),
RankQuery::MapLength.extract_row(mode, ou).to_string(), second_col.extract_row(mode, ou).to_string(),
ou.username.to_string(),
mem.distinct(),
]
})
.collect::<Vec<_>>();
table_formatting(&headers, &ALIGNS, table)
}
RankQuery::TotalPP => {
const HEADERS: [&'static str; 4] = ["#", "Total pp", "Username", "Member"];
const ALIGNS: [Align; 4] = [Right, Right, Left, Left];
let table = users
.iter()
.enumerate()
.map(|(i, (mem, ou))| {
[
format!("{}", 1 + i + start),
query.extract_row(mode, ou).to_string(),
ou.username.to_string(), ou.username.to_string(),
mem.distinct(), mem.distinct(),
] ]
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
table_formatting(&HEADERS, &ALIGNS, table) table_formatting(&HEADERS, &ALIGNS, table)
}
}; };
let content = MessageBuilder::new() let content = MessageBuilder::new()
.push_line(table) .push_line(table)