X-Git-Url: http://git.euphorik.ch/index.cgi?a=blobdiff_plain;f=backend%2Fsrc%2Fvalheim_controller.rs;h=457abc114db2b55ef238ba3b88f53fb11794a076;hb=2f8ea6c56e1d0f6d481c31766fb18c883c673068;hp=a483756ffa0bd17f9cfbc6d13cde624999aa453c;hpb=c4620d6f981e84059c464604969a55ff085152f6;p=valheim_web.git diff --git a/backend/src/valheim_controller.rs b/backend/src/valheim_controller.rs index a483756..457abc1 100644 --- a/backend/src/valheim_controller.rs +++ b/backend/src/valheim_controller.rs @@ -1,4 +1,5 @@ -use sysinfo::{ProcessExt, SystemExt}; +use sysinfo::{ ProcessExt, SystemExt }; +use systemd::journal; #[derive(Debug)] pub struct ValheimExe { @@ -6,24 +7,32 @@ pub struct ValheimExe { load_average_5min: f64, // [%]. uptime: u64, // [s]. world_size: u64, // [B]. + nb_of_players: u32, } impl ValheimExe { pub fn format_memory(&self) -> String { format_byte_size(self.memory * 1024, 2) } + pub fn format_load_average(&self) -> String { format!("{:.2} %", self.load_average_5min) } + pub fn format_uptime(&self) -> String { let mins = self.uptime / 60; let hours = mins / 60; let days = hours / 24; 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 + } } const BINARY_PREFIXES: [&str; 8] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB"]; @@ -46,6 +55,33 @@ fn format_byte_size(bytes: u64, precision: usize) -> String { const VALHEIM_PROCESS_NAME: &str = "valheim_server"; +fn get_number_of_players() -> u32 { + let mut journal = + journal::OpenOptions::default().current_user(true).open().unwrap(); + + journal.seek_tail().unwrap(); + + 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_end) = nb_of_connections_str.find(' ') { + if let Ok(n) = nb_of_connections_str.get(0..pos_end).unwrap().parse() { + return n; + } + } + } + } + } + }, + _ => return 0 + } + } +} + pub fn get_valheim_executable_information(world_path : &str) -> Option { let mut system = sysinfo::System::new_all(); system.refresh_system(); @@ -61,7 +97,8 @@ pub fn get_valheim_executable_information(world_path : &str) -> Option