From: Greg Burri Date: Thu, 24 Jun 2021 14:27:38 +0000 (+0200) Subject: Prepare for more information to be get from rcon X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=7d3366c4902d5c3e91551668e92d4572f9634511;p=minecraft_web.git Prepare for more information to be get from rcon --- diff --git a/backend/src/minecraft_controller.rs b/backend/src/minecraft_controller.rs index 13a3e3f..163008a 100644 --- a/backend/src/minecraft_controller.rs +++ b/backend/src/minecraft_controller.rs @@ -73,37 +73,43 @@ fn format_byte_size(bytes: u64, precision: usize) -> String { const MINECRAFT_PROCESS_NAME: &str = "java"; -// It doesn't work for the moment, it only scan the connection event and do not treat disconnections. -fn get_active_players(rcon_password: &str) -> Vec { +struct StatusFromRcon { + players: Vec, +} + +fn get_status_from_rcon(rcon_password: &str) -> StatusFromRcon { let mut client = minecraft_client_rs::Client::new("127.0.0.1:25575".to_string()).unwrap(); - let players = + let status = match client.authenticate(rcon_password.to_string()) { Ok(_) => { - match client.send_command("list".to_string()) { - Ok(resp) => { - resp.body - .split('\n') - .skip(1) - .filter(|n| !n.is_empty()) - .map(|n| n.to_string()) - .collect() - }, - Err(_e) => { - println!("Error from 'list' command"); - Vec::new() - }, + StatusFromRcon { + players: + match client.send_command("list".to_string()) { + Ok(resp) => { + resp.body + .split('\n') + .skip(1) + .filter(|n| !n.is_empty()) + .map(|n| n.to_string()) + .collect() + }, + Err(_e) => { + println!("Error from 'list' command"); + Vec::new() + }, + } } }, Err(_e) => { println!("Authentication error"); - Vec::new() + StatusFromRcon { players: Vec::new() } }, }; client.close().unwrap(); - players + status } fn get_last_backup_datetime(backup_path: &str) -> Option { @@ -132,13 +138,15 @@ pub fn get_minecraft_executable_information(world_path: &str, backup_path: &str, let world_size = match fs_extra::dir::get_size(world_path) { Ok(l) => l, Err(_) => 0u64 }; + let status_from_rcon = get_status_from_rcon(rcon_password); + Some( MinecraftExe { memory: process.memory(), load_average_5min: system.get_load_average().five / system.get_processors().len() as f64 * 100., uptime: std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() - process.start_time(), world_size, - active_players: get_active_players(rcon_password), + active_players: status_from_rcon.players, last_backup: get_last_backup_datetime(backup_path) } )