From ca3b20704ac1720d9ddacdfe7c004ef91cf1f736 Mon Sep 17 00:00:00 2001 From: ErrorNoInternet Date: Fri, 13 Jan 2023 18:40:30 +0800 Subject: [PATCH] Add join time sorter --- src/bot.rs | 23 +++++++++++++++++++++-- src/main.rs | 6 +++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index 5170395..cc29bf8 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -1,4 +1,4 @@ -use crate::{logging::log_error, State}; +use crate::{logging::log_error, PlayerTimeData, State}; use async_recursion::async_recursion; use azalea::{ pathfinder::BlockPosGoal, prelude::*, BlockPos, SprintDirection, Vec3, WalkDirection, @@ -210,7 +210,26 @@ pub async fn process_command( } Command::LastOnline => { if segments.len() < 1 { - return "Please tell me the name of the player!".to_string(); + let mut sorted_player_time_data: Vec = state + .player_timestamps + .lock() + .unwrap() + .values() + .map(|item| item.to_owned()) + .collect(); + sorted_player_time_data.sort_by(|a, b| b.join_time.cmp(&a.join_time)); + let mut player_timestamps = state.player_timestamps.lock().unwrap(); + let mut players = Vec::new(); + for player_time_data in sorted_player_time_data { + for (player, original_player_time_data) in player_timestamps.clone().iter() { + if player_time_data == original_player_time_data.to_owned() { + players.push(player.to_owned()); + player_timestamps.remove(player); + break; + } + } + } + return format!("Sorted by join time: {}", players.join(", ")); } for (player, player_time_data) in state.player_timestamps.lock().unwrap().iter() { diff --git a/src/main.rs b/src/main.rs index d9707de..a8c43e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -129,14 +129,14 @@ async fn main() { } } -#[derive(Eq, Hash, PartialEq, PartialOrd, Debug, Clone)] +#[derive(Eq, Hash, PartialEq, PartialOrd, Default, Debug, Clone)] pub struct Player { uuid: String, entity_id: u32, username: String, } -#[derive(Eq, Hash, PartialEq, PartialOrd, Debug, Clone)] +#[derive(Eq, Hash, PartialEq, PartialOrd, Default, Debug, Clone)] pub struct Entity { id: u32, uuid: String, @@ -149,7 +149,7 @@ pub struct PositionTimeData { time: u64, } -#[derive(Default, Debug, Clone)] +#[derive(Eq, Hash, PartialEq, PartialOrd, Default, Debug, Clone)] pub struct PlayerTimeData { join_time: u64, chat_message_time: u64,