X-Git-Url: http://git.euphorik.ch/index.cgi?p=minecraft_web.git;a=blobdiff_plain;f=backend%2Fsrc%2Fminecraft_controller.rs;fp=backend%2Fsrc%2Fminecraft_controller.rs;h=260a6e410fc2d0975539548aa3c953a95065fde8;hp=05ffeb3d0c3aded83153154bf7e7da2deb10cb3d;hb=16f93d7c5d110a92f90a4596e4fb3b994ae12932;hpb=9b09a05def141a8f5b08c822d886e9e9f9ba3050 diff --git a/backend/src/minecraft_controller.rs b/backend/src/minecraft_controller.rs index 05ffeb3..260a6e4 100644 --- a/backend/src/minecraft_controller.rs +++ b/backend/src/minecraft_controller.rs @@ -76,7 +76,7 @@ fn format_byte_size(bytes: u64, precision: usize) -> String { String::from("") } -const MINECRAFT_PROCESS_NAME: &str = "minecraft_server"; +const MINECRAFT_PROCESS_NAME: &str = "java"; #[cfg(target_os = "linux")] const STRING_BEFORE_CHARACTER_NAME: &str = "Got character ZDOID from"; @@ -85,58 +85,32 @@ const STRING_BEFORE_CHARACTER_NAME: &str = "Got character ZDOID from"; 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; - } - } - } - } - } - } +fn get_active_players(rcon_password: &str) -> Vec { + let mut client = minecraft_client_rs::Client::new("127.0.0.1:25575".to_string()).unwrap(); + + let players = + 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() + }, } }, - _ => return players - } - } -} + Err(_e) => { + println!("Authentication error"); + Vec::new() + }, + }; + + client.close().unwrap(); -#[cfg(target_os = "windows")] -fn get_active_players() -> Vec { - Vec::new() + players } fn get_last_backup_datetime(backup_path: &str) -> Option { @@ -154,11 +128,12 @@ 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); + // TODO: find the correct process by checking the correct jar name in parameters. if processes.len() >= 1 { let process = processes.first().unwrap(); @@ -170,7 +145,7 @@ pub fn get_minecraft_executable_information(world_path: &str, backup_path: &str) 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(), + active_players: get_active_players(rcon_password), last_backup: get_last_backup_datetime(backup_path) } )