load_average_5min: f64, // [%].\r
uptime: u64, // [s].\r
world_size: u64, // [B].\r
- nb_of_players: u32,\r
+ active_players: Vec<String>,\r
last_backup: Option<SystemTime>,\r
}\r
\r
let mins = self.uptime / 60;\r
let hours = mins / 60;\r
let days = hours / 24;\r
- format!("{}d{}h{}min", days, hours - 24 * days, mins - 60 * hours)\r
+ format!("{}d {}h {}min", days, hours - 24 * days, mins - 60 * hours)\r
}\r
\r
pub fn format_world_size(&self) -> String {\r
format_byte_size(self.world_size, 2)\r
}\r
\r
- pub fn get_nb_of_player(&self) -> u32 {\r
- self.nb_of_players\r
+ pub fn format_active_players(&self) -> String {\r
+ if self.active_players.len() == 0 {\r
+ String::from("<none>")\r
+ } else {\r
+ self.active_players.join(", ")\r
+ }\r
}\r
\r
pub fn format_last_backup(&self) -> String {\r
}\r
\r
const VALHEIM_PROCESS_NAME: &str = "valheim_server";\r
+const STRING_BEFORE_CHARACTER_NAME: &str = "Got character ZDOID from";\r
+const STRING_BEFORE_NB_OF_CONNECTIONS: &str = "Connections";\r
\r
#[cfg(target_os = "linux")]\r
-fn get_number_of_players() -> u32 {\r
+fn get_active_players() -> Vec<String> {\r
let mut journal =\r
journal::OpenOptions::default().current_user(true).open().unwrap();\r
\r
journal.seek_tail().unwrap();\r
\r
+ let mut number_of_connections = 0;\r
+ let mut players : Vec<String> = Vec::new();\r
+\r
loop {\r
match journal.previous_entry() {\r
Ok(Some(entry)) => {\r
if let (Some(unit), Some(mess)) = (entry.get("_SYSTEMD_UNIT"), entry.get("MESSAGE")) {\r
if unit == "valheim.service" {\r
- if let Some(pos) = mess.find("Connections") {\r
- let nb_of_connections_str = mess.get(pos+12..).unwrap();\r
+ if let Some(pos) = mess.find(STRING_BEFORE_CHARACTER_NAME) {\r
+ let character_str = mess.get(pos+STRING_BEFORE_CHARACTER_NAME.len()+1..).unwrap();\r
+ if let Some(pos_end) = character_str.find(" : ") {\r
+ let player_name = String::from(character_str.get(0..pos_end).unwrap());\r
+ if !players.contains(&player_name) {\r
+ players.push(player_name);\r
+ if players.len() == number_of_connections {\r
+ return players;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ else if let Some(pos) = mess.find(STRING_BEFORE_NB_OF_CONNECTIONS) {\r
+ let nb_of_connections_str = mess.get(pos+STRING_BEFORE_NB_OF_CONNECTIONS.len()+1..).unwrap();\r
if let Some(pos_end) = nb_of_connections_str.find(' ') {\r
if let Ok(n) = nb_of_connections_str.get(0..pos_end).unwrap().parse() {\r
- return n;\r
+ number_of_connections = n;\r
+ if players.len() >= number_of_connections {\r
+ return players;\r
+ }\r
}\r
}\r
}\r
}\r
}\r
},\r
- _ => return 0\r
+ _ => return players\r
}\r
}\r
}\r
\r
#[cfg(target_os = "windows")]\r
-fn get_number_of_players() -> u32 {\r
- 0\r
+fn get_active_players() -> Vec<String> {\r
+ Vec::new()\r
}\r
\r
fn get_last_backup_datetime(backup_path: &str) -> Option<SystemTime> {\r
load_average_5min: system.get_load_average().five / system.get_processors().len() as f64 * 100.,\r
uptime: std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() - process.start_time(),\r
world_size,\r
- nb_of_players: get_number_of_players(),\r
+ active_players: get_active_players(),\r
last_backup: get_last_backup_datetime(backup_path)\r
}\r
)\r