X-Git-Url: http://git.euphorik.ch/index.cgi?a=blobdiff_plain;f=backend%2Fsrc%2Fminecraft_controller.rs;h=451b0164961d5ca249d6a96cc2eb65e4eabc1f92;hb=2e6831744641c8d8ebeea0a47c933448e8499ddb;hp=d9a68c0107c3cb1488abf429cd94de55b0b0d100;hpb=5df9e614e940f47256773b7d0787e46f90082ff7;p=minecraft_web.git diff --git a/backend/src/minecraft_controller.rs b/backend/src/minecraft_controller.rs index d9a68c0..451b016 100644 --- a/backend/src/minecraft_controller.rs +++ b/backend/src/minecraft_controller.rs @@ -1,7 +1,5 @@ -use sysinfo::{ ProcessExt, SystemExt }; - use std::{ fs, time::SystemTime }; - +use sysinfo::{ ProcessExt, SystemExt }; use chrono::{ DateTime, offset::Local }; #[derive(Clone, Debug)] @@ -11,6 +9,7 @@ pub struct MinecraftExe { uptime: u64, // [s]. world_size: u64, // [B]. active_players: Vec, + version: String, last_backup: Option, } @@ -35,13 +34,15 @@ 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_version(&self) -> String { + self.version.clone() } pub fn format_last_backup(&self) -> String { @@ -75,37 +76,45 @@ 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) => + 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 { @@ -126,21 +135,22 @@ fn get_last_backup_datetime(backup_path: &str) -> Option { pub fn get_minecraft_executable_information(world_path: &str, backup_path: &str, rcon_password: &str) -> Option { let mut system = sysinfo::System::new_all(); system.refresh_system(); - let processes = system.get_process_by_name(MINECRAFT_PROCESS_NAME); + let mut processes = system.processes_by_name(MINECRAFT_PROCESS_NAME); // TODO: find the correct process by checking the correct jar name in parameters. - if processes.len() >= 1 { - let process = processes.first().unwrap(); + if let Some(process) = processes.next() { + let world_size = match fs_extra::dir::get_size(world_path) { Ok(l) => l, Err(_) => 0u64 }; - let world_size = match std::fs::metadata(world_path) { Ok(f) => f.len(), 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., + 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.load_average().five / system.cpus().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, + version: process.cmd()[process.cmd().len() - 2].clone(), // TODO: Extract the version from the .jar filename. last_backup: get_last_backup_datetime(backup_path) } )