add leaderboard command and use username in db

master
Tymon 1 year ago
parent e6a9232661
commit 509bb6fe6e

@ -0,0 +1,22 @@
use valence::prelude::*;
use redis::{Commands, Connection};
pub fn run(client: &mut Client, connection: &mut Connection) {
let wins: redis::RedisResult<Vec<(String, usize)>> = 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));
}
}

@ -1,8 +1,9 @@
mod game; mod game;
mod leaderboard;
use valence::prelude::{event::CommandExecution, *}; use valence::prelude::{event::CommandExecution, *};
use crate::GameState; use crate::{GameState, db::Connection};
pub struct Command { pub struct Command {
pub name: String, pub name: String,
@ -33,6 +34,7 @@ pub fn command_executor(
mut events: EventReader<CommandExecution>, mut events: EventReader<CommandExecution>,
mut next_state: ResMut<NextState<GameState>>, mut next_state: ResMut<NextState<GameState>>,
state: Res<State<GameState>>, state: Res<State<GameState>>,
mut connection: ResMut<Connection>
) { ) {
for event in events.iter() { for event in events.iter() {
let cmd = Command::parse(event.command.to_string()); let cmd = Command::parse(event.command.to_string());
@ -41,6 +43,7 @@ pub fn command_executor(
match cmd.name.as_str() { match cmd.name.as_str() {
"game" => game::run(cmd, &mut client, uuid.0, &mut next_state, &state), "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)), _ => client.send_message(format!("Command {} not found!", cmd.name)),
} }
} }

@ -43,8 +43,7 @@ pub fn stop_game(
&mut Position, &mut Position,
&mut GameMode, &mut GameMode,
&mut Alive, &mut Alive,
&Username, &Username
&UniqueId,
)>, )>,
mut connection: ResMut<Connection>, mut connection: ResMut<Connection>,
) { ) {
@ -52,9 +51,9 @@ pub fn stop_game(
let mut alive_players = vec![]; 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 { if alive.0 {
alive_players.push((username, uuid)); alive_players.push(username);
} }
*gamemode = GameMode::Spectator; *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 statement to not panic if somehow no one was alive when the game ended
if alive_players.len() >= 1 { if alive_players.len() >= 1 {
// let's assume there's only one alive player // let's assume there's only one alive player
alive_players[0].0 .0.clone() alive_players[0].0.clone()
} else { } else {
"Somehow, no one".to_string() "Somehow, no one".to_string()
} }
@ -80,7 +79,7 @@ pub fn stop_game(
if alive_players.len() == 1 { if alive_players.len() == 1 {
let _: i32 = connection let _: i32 = connection
.0 .0
.hincr("wins".to_string(), alive_players[0].1 .0.to_string(), 1) .hincr("wins".to_string(), alive_players[0].0.clone(), 1)
.unwrap(); .unwrap();
} }
} }

Loading…
Cancel
Save