X-Git-Url: http://git.euphorik.ch/index.cgi?a=blobdiff_plain;f=backend%2Fsrc%2Fminecraft_controller.rs;h=7af13dd9a4b02b73a383f813180fde1ffa6ef406;hb=56bda46ddd9202d7d5485792fc2062fc69b6ed07;hp=9bcc7d33f3cca961b54465acfcbc6f4cbe83be86;hpb=1b24eca7c07076d8706aca658778256230fd73b9;p=minecraft_web.git diff --git a/backend/src/minecraft_controller.rs b/backend/src/minecraft_controller.rs index 9bcc7d3..7af13dd 100644 --- a/backend/src/minecraft_controller.rs +++ b/backend/src/minecraft_controller.rs @@ -35,13 +35,11 @@ impl MinecraftExe { } pub fn format_active_players(&self) -> String { - /* Commented because the player list isn't correct (the number is). if self.active_players.len() == 0 { String::from("") } else { self.active_players.join(", ") - }*/ - self.active_players.len().to_string() + } } pub fn format_last_backup(&self) -> String { @@ -75,39 +73,45 @@ fn format_byte_size(bytes: u64, precision: usize) -> String { const MINECRAFT_PROCESS_NAME: &str = "java"; -#[cfg(target_os = "linux")] -const STRING_BEFORE_CHARACTER_NAME: &str = "Got character ZDOID from"; - -#[cfg(target_os = "linux")] -const STRING_BEFORE_NB_OF_CONNECTIONS: &str = "Connections"; +struct StatusFromRcon { + players: Vec, +} -// 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 { +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) => { - println!("{}", resp.body); - Vec::new() - }, - Err(_e) => { - println!("Error asking seed"); - Vec::new() - }, + StatusFromRcon { + players: + match client.send_command("list".to_string()) { + Ok(resp) => + match resp.body.find(':') { + Some(i) if i < resp.body.len() -1 => + resp.body[i + 1..resp.body.len()] + .split(',') + .map(|nick| nick.trim().to_string()) + .filter(|nick| !nick.is_empty()) + .collect(), + _ => Vec::new() + }, + 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 { @@ -134,15 +138,17 @@ pub fn get_minecraft_executable_information(world_path: &str, backup_path: &str, if processes.len() >= 1 { let process = processes.first().unwrap(); - let world_size = match std::fs::metadata(world_path) { Ok(f) => f.len(), Err(_) => 0u64 }; + 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(), + memory: process.memory() / 3, // Because the Java garbage collector ZGC reports three times more the real memory usage: https://stackoverflow.com/a/62934057/212675 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) } )