X-Git-Url: http://git.euphorik.ch/index.cgi?a=blobdiff_plain;f=backend%2Fsrc%2Fminecraft_controller.rs;h=8f67d200111f1de90e971bdf3ff524488ff23e2e;hb=34c1bf4bcb51473466ee9398cb63f0049ddc06a0;hp=05ffeb3d0c3aded83153154bf7e7da2deb10cb3d;hpb=9b09a05def141a8f5b08c822d886e9e9f9ba3050;p=minecraft_web.git diff --git a/backend/src/minecraft_controller.rs b/backend/src/minecraft_controller.rs index 05ffeb3..8f67d20 100644 --- a/backend/src/minecraft_controller.rs +++ b/backend/src/minecraft_controller.rs @@ -4,9 +4,6 @@ use std::{ fs, time::SystemTime }; use chrono::{ DateTime, offset::Local }; -#[cfg(target_os = "linux")] -use systemd::journal; - #[derive(Clone, Debug)] pub struct MinecraftExe { memory: u64, // [kB]. @@ -38,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 { @@ -76,67 +71,47 @@ fn format_byte_size(bytes: u64, precision: usize) -> String { String::from("") } -const MINECRAFT_PROCESS_NAME: &str = "minecraft_server"; - -#[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"; - -// It doesn't work for the moment, it only scan the connection event and do not treat disconnections. -#[cfg(target_os = "linux")] -fn get_active_players() -> Vec { - let mut journal = - journal::OpenOptions::default().current_user(true).open().unwrap(); - - journal.seek_tail().unwrap(); - - let mut number_of_connections = -1i32; - let mut players : Vec = Vec::new(); - - loop { - match journal.previous_entry() { - Ok(Some(entry)) => { - if let (Some(unit), Some(mess)) = (entry.get("_SYSTEMD_UNIT"), entry.get("MESSAGE")) { - if unit == "minecraft.service" { - if let Some(pos) = mess.find(STRING_BEFORE_CHARACTER_NAME) { - let character_str = mess.get(pos+STRING_BEFORE_CHARACTER_NAME.len()+1..).unwrap(); - if let Some(pos_end) = character_str.find(" : ") { - let player_name = String::from(character_str.get(0..pos_end).unwrap()); - if !players.contains(&player_name) { - players.push(player_name); - if players.len() as i32 == number_of_connections { - return players; - } - } - } - } - else if let Some(pos) = mess.find(STRING_BEFORE_NB_OF_CONNECTIONS) { - let nb_of_connections_str = mess.get(pos+STRING_BEFORE_NB_OF_CONNECTIONS.len()+1..).unwrap(); - if let Some(pos_end) = nb_of_connections_str.find(' ') { - if let Ok(n) = nb_of_connections_str.get(0..pos_end).unwrap().parse::() { - if number_of_connections == -1 { - number_of_connections = n; - - if players.len() as i32 >= number_of_connections { - return players; - } - } - } - } +const MINECRAFT_PROCESS_NAME: &str = "java"; + +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 status = + match client.authenticate(rcon_password.to_string()) { + Ok(_) => { + 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() + }, } - } } }, - _ => return players - } - } -} + Err(_e) => { + println!("Authentication error"); + StatusFromRcon { players: Vec::new() } + }, + }; + + client.close().unwrap(); -#[cfg(target_os = "windows")] -fn get_active_players() -> Vec { - Vec::new() + status } fn get_last_backup_datetime(backup_path: &str) -> Option { @@ -154,23 +129,24 @@ fn get_last_backup_datetime(backup_path: &str) -> Option { Some(times.last()?.clone()) } -pub fn get_minecraft_executable_information(world_path: &str, 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); - if processes.len() >= 1 { - let process = processes.first().unwrap(); + // TODO: find the correct process by checking the correct jar name in parameters. + 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., + load_average_5min: system.load_average().five / system.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(), + active_players: status_from_rcon.players, last_backup: get_last_backup_datetime(backup_path) } )