X-Git-Url: http://git.euphorik.ch/index.cgi?a=blobdiff_plain;f=backend%2Fsrc%2Fvalheim_controller.rs;h=6fa6c7ff996bbf1397a6968b9c341cd0d1b577bf;hb=83993038d45c0d11c11417f9b47e19eb61e53928;hp=72eb60ef72daeebf3039f232a2dd0880d56f16d0;hpb=e4843b60da657b286c9a6fe56a117fb7a1c58ef3;p=valheim_web.git diff --git a/backend/src/valheim_controller.rs b/backend/src/valheim_controller.rs index 72eb60e..6fa6c7f 100644 --- a/backend/src/valheim_controller.rs +++ b/backend/src/valheim_controller.rs @@ -13,7 +13,7 @@ pub struct ValheimExe { load_average_5min: f64, // [%]. uptime: u64, // [s]. world_size: u64, // [B]. - nb_of_players: u32, + active_players: Vec, last_backup: Option, } @@ -30,15 +30,19 @@ impl ValheimExe { let mins = self.uptime / 60; let hours = mins / 60; let days = hours / 24; - format!("{}d{}h{}min", days, hours - 24 * days, mins - 60 * hours) + format!("{}d {}h {}min", days, hours - 24 * days, mins - 60 * hours) } pub fn format_world_size(&self) -> String { format_byte_size(self.world_size, 2) } - pub fn get_nb_of_player(&self) -> u32 { - self.nb_of_players + pub fn format_active_players(&self) -> String { + if self.active_players.len() == 0 { + String::from("") + } else { + self.active_players.join(", ") + } } pub fn format_last_backup(&self) -> String { @@ -71,38 +75,61 @@ fn format_byte_size(bytes: u64, precision: usize) -> String { } const VALHEIM_PROCESS_NAME: &str = "valheim_server"; +const STRING_BEFORE_CHARACTER_NAME: &str = "Got character ZDOID from"; +const STRING_BEFORE_NB_OF_CONNECTIONS: &str = "Connections"; #[cfg(target_os = "linux")] -fn get_number_of_players() -> u32 { +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 == "valheim.service" { - if let Some(pos) = mess.find("Connections") { - let nb_of_connections_str = mess.get(pos+12..).unwrap(); + 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() { - return n; + 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; + } + } } } } } } }, - _ => return 0 + _ => return players } } } #[cfg(target_os = "windows")] -fn get_number_of_players() -> u32 { - 0 +fn get_active_players() -> Vec { + Vec::new() } fn get_last_backup_datetime(backup_path: &str) -> Option { @@ -136,7 +163,7 @@ pub fn get_valheim_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, - nb_of_players: get_number_of_players(), + active_players: get_active_players(), last_backup: get_last_backup_datetime(backup_path) } )