pikadick/commands/tic_tac_toe/
scoreboard.rs1use crate::{
2 checks::ENABLED_CHECK,
3 util::AsciiTable,
4 ClientDataKey,
5};
6use anyhow::Context as _;
7use serenity::{
8 framework::standard::{
9 macros::command,
10 Args,
11 CommandResult,
12 },
13 model::prelude::*,
14 prelude::*,
15};
16use tracing::error;
17
18#[command]
19#[description("Get the top stats for Tic-Tac-Toe in this server")]
20#[checks(Enabled)]
21#[bucket("default")]
22pub async fn scoreboard(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
23 let data_lock = ctx.data.read().await;
24 let client_data = data_lock
25 .get::<ClientDataKey>()
26 .expect("missing client data");
27 let db = client_data.db.clone();
28 drop(data_lock);
29
30 let scores = match db
31 .get_top_tic_tac_toe_scores(msg.guild_id.into())
32 .await
33 .context("failed to get top tic-tac-toe stats")
34 {
35 Ok(scores) => scores,
36 Err(e) => {
37 error!("{:?}", e);
38 msg.channel_id.say(&ctx.http, format!("{:?}", e)).await?;
39 return Ok(());
40 }
41 };
42
43 let mut table = AsciiTable::new(7, scores.len() + 1);
44 table.set_padding(2);
45
46 table.set_cell(0, 0, "Position");
47 table.set_cell(1, 0, "Name");
48 table.set_cell(2, 0, "Score");
49 table.set_cell(3, 0, "Wins");
50 table.set_cell(4, 0, "Losses");
51 table.set_cell(5, 0, "Ties");
52 table.set_cell(6, 0, "Concedes");
53
54 for (i, score) in scores.iter().enumerate() {
55 let username = score
56 .player
57 .to_user(&ctx)
58 .await
59 .context("failed to get user name")?
60 .name;
61
62 table.set_cell(0, i + 1, format!("{}", i + 1));
63 table.set_cell(1, i + 1, username);
64 table.set_cell(2, i + 1, score.score.to_string());
65 table.set_cell(3, i + 1, score.wins.to_string());
66 table.set_cell(4, i + 1, score.losses.to_string());
67 table.set_cell(5, i + 1, score.ties.to_string());
68 table.set_cell(6, i + 1, score.concedes.to_string());
69 }
70
71 msg.channel_id
72 .say(
73 &ctx.http,
74 format!("```\nTop Tic-Tac-Toe Stats\n{}\n```", table),
75 )
76 .await?;
77 Ok(())
78}