Avoid data duplication.
authorGreg Burri <greg.burri@gmail.com>
Mon, 22 Mar 2021 10:25:06 +0000 (11:25 +0100)
committerGreg Burri <greg.burri@gmail.com>
Mon, 22 Mar 2021 10:25:06 +0000 (11:25 +0100)
backend/src/valheim_controller.rs

index a89c442..aaf82f0 100644 (file)
@@ -75,6 +75,8 @@ fn format_byte_size(bytes: u64, precision: usize) -> String {
 }\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_active_players() -> Vec<String> {\r
@@ -91,9 +93,8 @@ fn get_active_players() -> Vec<String> {
             Ok(Some(entry)) => {\r
                 if let (Some(unit), Some(mess)) = (entry.get("_SYSTEMD_UNIT"), entry.get("MESSAGE")) {\r
                     if unit == "valheim.service" {\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) = 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
@@ -104,8 +105,8 @@ fn get_active_players() -> Vec<String> {
                                 }\r
                             }\r
                         }\r
-                        else if let Some(pos) = mess.find("Connections") {\r
-                            let nb_of_connections_str = mess.get(pos+12..).unwrap();\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
                                     number_of_connections = n;\r