From 59a054a524604e924aa9e54f837c8bd4d090c0f2 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Mon, 22 Mar 2021 11:01:11 +0100 Subject: [PATCH] Show player names (WIP) --- backend/src/main.rs | 13 ++++++++--- backend/src/valheim_controller.rs | 39 +++++++++++++++++++++++-------- backend/templates/main.html | 2 +- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/backend/src/main.rs b/backend/src/main.rs index 8ea0819..c186c3f 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -21,7 +21,7 @@ struct MainTemplate { load_average: String, uptime: String, world_size: String, - nb_of_players: u32, + active_players: String, last_backup: String, } @@ -44,12 +44,19 @@ async fn main_page(config_shared: web::Data>) -> impl Responder { load_average: info.format_load_average(), uptime: info.format_uptime(), world_size: info.format_world_size(), - nb_of_players: info.get_nb_of_player(), + active_players: info.format_active_players(), last_backup: info.format_last_backup() }, None => { let value_unknown = String::from(VALUE_UNKNOWN); - MainTemplate { text_status: String::from("Valheim server is down :("), memory: value_unknown.clone(), load_average: value_unknown.clone(), uptime: value_unknown.clone(), world_size: value_unknown.clone(), nb_of_players: 0, last_backup: value_unknown.clone() } + MainTemplate { + text_status: String::from("Valheim server is down :("), + memory: value_unknown.clone(), + load_average: value_unknown.clone(), + uptime: value_unknown.clone(), + world_size: value_unknown.clone(), + active_players: value_unknown.clone(), + last_backup: value_unknown.clone() } } } } diff --git a/backend/src/valheim_controller.rs b/backend/src/valheim_controller.rs index f11f7f4..0aa92de 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, } @@ -37,8 +37,8 @@ impl ValheimExe { 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 { + self.active_players.join(", ") } pub fn format_last_backup(&self) -> String { @@ -73,36 +73,55 @@ fn format_byte_size(bytes: u64, precision: usize) -> String { const VALHEIM_PROCESS_NAME: &str = "valheim_server"; #[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 = -1; + 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") { + //"Got character ZDOID from {}" + if let Some(pos) = mess.find("Got character ZDOID from") { + let character_str = mess.get(pos+25..).unwrap(); + if let Some(pos_end) = character_str.find(" : ") { + let player_name = String::from(character_str.get(0..pos_end)); + if !players.contains(player_name) { + players.push(player_name); + if players.len() == number_of_connections { + return players; + } + } + } + } + else if let Some(pos) = mess.find("Connections") { let nb_of_connections_str = mess.get(pos+12..).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; + number_of_connections = n; + if players.len() >= 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 +155,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) } ) diff --git a/backend/templates/main.html b/backend/templates/main.html index aef9992..7e7eff4 100644 --- a/backend/templates/main.html +++ b/backend/templates/main.html @@ -11,7 +11,7 @@

{{ text_status }}

- + -- 2.45.2
Number of active players{{ nb_of_players }}
Active players{{ active_players }}
World size{{ world_size }}
Memory used{{ memory }}
Load average (5 min){{ load_average }}