feat(discord): Added /scoreboard command

This commit is contained in:
Alexey 2025-12-11 15:57:00 +03:00
commit 213ecca8b9
3 changed files with 73 additions and 0 deletions

View file

@ -0,0 +1,71 @@
use poise::serenity_prelude::{Mentionable, UserId};
use squad_quest::{SquadObject, account::Account, map::Map};
use crate::{Context, Error};
fn account_balance_string(account: &Account, map: &Map) -> String {
let rooms_value = account_rooms_value(account, map);
let full_balance = account_full_balance(account, map);
format!("{account}: **{full_balance}** points (**{balance}** on balance \
+ **{rooms_value}** unlocked rooms networth)",
account = account_user_id(&account).mention(),
balance = account.balance,
)
}
fn account_rooms_value(account: &Account, map: &Map) -> u32 {
map.room.iter().filter_map(|r| {
if account.rooms_unlocked.contains(&r.id) {
Some(r.value)
} else {
None
}
})
.sum()
}
fn account_full_balance(account: &Account, map: &Map) -> u32 {
let rooms_value = account_rooms_value(account, map);
account.balance + rooms_value
}
fn account_user_id(account: &Account) -> UserId {
UserId::new(account.id.clone().parse::<u64>().expect("automatically inserted"))
}
#[poise::command(
prefix_command,
slash_command,
guild_only,
)]
pub async fn scoreboard(
ctx: Context<'_>,
) -> Result<(), Error> {
let map_path = ctx.data().config.full_map_path();
let map = Map::load(map_path).expect("map.toml should exist");
let mut accounts = ctx.data().config.load_accounts();
accounts.sort_by(|a,b| {
let a_balance = account_full_balance(a, &map);
let b_balance = account_full_balance(b, &map);
a_balance.cmp(&b_balance)
});
let this_user = ctx.author().id;
let mut reply_string = String::new();
for account in accounts {
let user_id = account_user_id(&account);
let mut str = account_balance_string(&account, &map);
if user_id == this_user {
str = format!("__{str}__ << You");
}
reply_string.push_str(&str);
}
ctx.reply(reply_string).await?;
Ok(())
}

View file

@ -6,6 +6,7 @@ pub mod quest;
pub mod init;
pub mod answer;
pub mod social;
pub mod account;
pub const ERROR_MSG: &str = "Server error :(";

View file

@ -41,6 +41,7 @@ async fn main() {
commands::init::init(),
commands::answer::answer(),
commands::social::social(),
commands::account::scoreboard(),
],
..Default::default()
})