Show player names (WIP)
authorGreg Burri <greg.burri@gmail.com>
Mon, 22 Mar 2021 10:01:11 +0000 (11:01 +0100)
committerGreg Burri <greg.burri@gmail.com>
Mon, 22 Mar 2021 10:01:11 +0000 (11:01 +0100)
backend/src/main.rs
backend/src/valheim_controller.rs
backend/templates/main.html

index 8ea0819..c186c3f 100644 (file)
@@ -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<Mutex<Config>>) -> 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() }
         }
     }
 }
index f11f7f4..0aa92de 100644 (file)
@@ -13,7 +13,7 @@ pub struct ValheimExe {
     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
@@ -37,8 +37,8 @@ impl ValheimExe {
         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
+        self.active_players.join(", ")\r
     }\r
 \r
     pub fn format_last_backup(&self) -> String {\r
@@ -73,36 +73,55 @@ fn format_byte_size(bytes: u64, precision: usize) -> String {
 const VALHEIM_PROCESS_NAME: &str = "valheim_server";\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 = -1;\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
+                        //"Got character ZDOID from {}"\r
+                        if let Some(pos) = mess.find("Got character ZDOID from") {\r
+                            let character_str = mess.get(pos+25..).unwrap();\r
+                            if let Some(pos_end) = character_str.find(" : ") {\r
+                                let player_name = String::from(character_str.get(0..pos_end));\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("Connections") {\r
                             let nb_of_connections_str = mess.get(pos+12..).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
@@ -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.,\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
index aef9992..7e7eff4 100644 (file)
@@ -11,7 +11,7 @@
         <div class="container">\r
             <h1>{{ text_status }}</h1>\r
             <table>\r
-                <tr><td>Number of active players</td><td>{{ nb_of_players }}</td>\r
+                <tr><td>Active players</td><td>{{ active_players }}</td>\r
                 <tr><td>World size</td><td>{{ world_size }}</td></tr>\r
                 <tr><td>Memory used</td><td>{{ memory }}</td></tr>\r
                 <tr><td>Load average (5 min)</td><td>{{ load_average }}</td></tr>\r