pikadick/commands/tic_tac_toe/
stats.rs

1use 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 personal stats for Tic-Tac-Toe")]
20#[checks(Enabled)]
21#[bucket("default")]
22pub async fn stats(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_tic_tac_toe_score(msg.guild_id.into(), msg.author.id)
32        .await
33        .context("failed to get 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(4, 2);
44    table.set_padding(2);
45
46    let mut wins_buffer = itoa::Buffer::new();
47    let mut losses_buffer = itoa::Buffer::new();
48    let mut ties_buffer = itoa::Buffer::new();
49    let mut concedes_buffer = itoa::Buffer::new();
50
51    table.set_cell(0, 0, "Wins");
52    table.set_cell(1, 0, "Losses");
53    table.set_cell(2, 0, "Ties");
54    table.set_cell(3, 0, "Concedes");
55
56    table.set_cell(0, 1, wins_buffer.format(scores.wins));
57    table.set_cell(1, 1, losses_buffer.format(scores.losses));
58    table.set_cell(2, 1, ties_buffer.format(scores.ties));
59    table.set_cell(3, 1, concedes_buffer.format(scores.concedes));
60
61    msg.channel_id
62        .say(
63            &ctx.http,
64            format!(
65                "```\n{}'s Tic-Tac-Toe Stats\n{}\n```",
66                msg.author.name, table
67            ),
68        )
69        .await?;
70    Ok(())
71}