X-Git-Url: http://git.euphorik.ch/index.cgi?a=blobdiff_plain;f=backend%2Fsrc%2Fvalheim_controller.rs;h=aa0ff319ba415f1c39f5f6a431b8fd434accfeda;hb=c6b54f676e513e2b43a9190f2afae5178641f978;hp=29f30f1b066d2cf2e43f4131e903a1ad1ecac776;hpb=0a5f926bbdb9350b3ba94da4744c0ef0aeb4caa4;p=valheim_web.git diff --git a/backend/src/valheim_controller.rs b/backend/src/valheim_controller.rs index 29f30f1..aa0ff31 100644 --- a/backend/src/valheim_controller.rs +++ b/backend/src/valheim_controller.rs @@ -1,7 +1,6 @@ -use sysinfo::{ ProcessExt, SystemExt }; use std::{ fs, time::SystemTime }; - +use sysinfo::{ ProcessExt, SystemExt }; use chrono::{ DateTime, offset::Local }; #[cfg(target_os = "linux")] @@ -38,11 +37,13 @@ impl ValheimExe { } 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,9 +76,14 @@ fn format_byte_size(bytes: u64, precision: usize) -> String { } const VALHEIM_PROCESS_NAME: &str = "valheim_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 = @@ -85,7 +91,7 @@ fn get_active_players() -> Vec { journal.seek_tail().unwrap(); - let mut number_of_connections = 0; + let mut number_of_connections = -1i32; let mut players : Vec = Vec::new(); loop { @@ -99,7 +105,7 @@ fn get_active_players() -> Vec { let player_name = String::from(character_str.get(0..pos_end).unwrap()); if !players.contains(&player_name) { players.push(player_name); - if players.len() == number_of_connections { + if players.len() as i32 == number_of_connections { return players; } } @@ -108,12 +114,13 @@ fn get_active_players() -> Vec { 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 n > number_of_connections { + 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() >= number_of_connections { - return players; + + if players.len() as i32 >= number_of_connections { + return players; + } } } } @@ -149,17 +156,16 @@ fn get_last_backup_datetime(backup_path: &str) -> Option { pub fn get_valheim_executable_information(world_path: &str, backup_path: &str) -> Option { let mut system = sysinfo::System::new_all(); system.refresh_system(); - let processes = system.get_process_by_name(VALHEIM_PROCESS_NAME); + let mut processes = system.processes_by_name(VALHEIM_PROCESS_NAME); - if processes.len() >= 1 { - let process = processes.first().unwrap(); + if let Some(process) = processes.next() { let world_size = match std::fs::metadata(world_path) { Ok(f) => f.len(), Err(_) => 0u64 }; Some( ValheimExe { 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.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(),