From 509bb6fe6e2c14cf07b56e20a588ffd67b594450 Mon Sep 17 00:00:00 2001 From: Tymon Date: Sat, 8 Apr 2023 01:38:04 +0200 Subject: [PATCH] add leaderboard command and use username in db --- src/commands/leaderboard.rs | 22 ++++++++++++++++++++++ src/commands/mod.rs | 5 ++++- src/game.rs | 11 +++++------ 3 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 src/commands/leaderboard.rs diff --git a/src/commands/leaderboard.rs b/src/commands/leaderboard.rs new file mode 100644 index 0000000..3531f21 --- /dev/null +++ b/src/commands/leaderboard.rs @@ -0,0 +1,22 @@ +use valence::prelude::*; + +use redis::{Commands, Connection}; + +pub fn run(client: &mut Client, connection: &mut Connection) { + let wins: redis::RedisResult> = connection.hgetall("wins"); + let Ok(mut wins) = wins else { + client.send_message("Leaderboards are empty!".color(Color::RED)); + return; + }; + + wins.sort_by_key(|(_, w)| w.clone()); + wins.reverse(); + + client.send_message("Top 10:".bold()); + + for i in 0..=(wins.len() - 1).clamp(0, 9) { + let (username, wins) = wins[i].clone(); + + client.send_message(format!("- {} with {} win(s)", username, wins).color(Color::GOLD)); + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 2d4d692..aa7b67b 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,8 +1,9 @@ mod game; +mod leaderboard; use valence::prelude::{event::CommandExecution, *}; -use crate::GameState; +use crate::{GameState, db::Connection}; pub struct Command { pub name: String, @@ -33,6 +34,7 @@ pub fn command_executor( mut events: EventReader, mut next_state: ResMut>, state: Res>, + mut connection: ResMut ) { for event in events.iter() { let cmd = Command::parse(event.command.to_string()); @@ -41,6 +43,7 @@ pub fn command_executor( match cmd.name.as_str() { "game" => game::run(cmd, &mut client, uuid.0, &mut next_state, &state), + "leaderboard" => leaderboard::run(&mut client, &mut connection.0), _ => client.send_message(format!("Command {} not found!", cmd.name)), } } diff --git a/src/game.rs b/src/game.rs index 03f91e3..30cce92 100644 --- a/src/game.rs +++ b/src/game.rs @@ -43,8 +43,7 @@ pub fn stop_game( &mut Position, &mut GameMode, &mut Alive, - &Username, - &UniqueId, + &Username )>, mut connection: ResMut, ) { @@ -52,9 +51,9 @@ pub fn stop_game( let mut alive_players = vec![]; - for (mut client, mut pos, mut gamemode, mut alive, username, uuid) in &mut clients { + for (mut client, mut pos, mut gamemode, mut alive, username) in &mut clients { if alive.0 { - alive_players.push((username, uuid)); + alive_players.push(username); } *gamemode = GameMode::Spectator; @@ -69,7 +68,7 @@ pub fn stop_game( // if statement to not panic if somehow no one was alive when the game ended if alive_players.len() >= 1 { // let's assume there's only one alive player - alive_players[0].0 .0.clone() + alive_players[0].0.clone() } else { "Somehow, no one".to_string() } @@ -80,7 +79,7 @@ pub fn stop_game( if alive_players.len() == 1 { let _: i32 = connection .0 - .hincr("wins".to_string(), alive_players[0].1 .0.to_string(), 1) + .hincr("wins".to_string(), alive_players[0].0.clone(), 1) .unwrap(); } }