From 95461b556df2f7f36fa773317b2fd95fba74ac46 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Mon, 22 Mar 2021 11:25:06 +0100 Subject: [PATCH] Avoid data duplication. --- backend/src/valheim_controller.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/backend/src/valheim_controller.rs b/backend/src/valheim_controller.rs index a89c442..aaf82f0 100644 --- a/backend/src/valheim_controller.rs +++ b/backend/src/valheim_controller.rs @@ -75,6 +75,8 @@ fn format_byte_size(bytes: u64, precision: usize) -> String { } const VALHEIM_PROCESS_NAME: &str = "valheim_server"; +const STRING_BEFORE_CHARACTER_NAME: &str = "Got character ZDOID from"; +const STRING_BEFORE_NB_OF_CONNECTIONS: &str = "Connections"; #[cfg(target_os = "linux")] fn get_active_players() -> Vec { @@ -91,9 +93,8 @@ fn get_active_players() -> Vec { Ok(Some(entry)) => { if let (Some(unit), Some(mess)) = (entry.get("_SYSTEMD_UNIT"), entry.get("MESSAGE")) { if unit == "valheim.service" { - //"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) = mess.find(STRING_BEFORE_CHARACTER_NAME) { + let character_str = mess.get(pos+STRING_BEFORE_CHARACTER_NAME.len()+1..).unwrap(); if let Some(pos_end) = character_str.find(" : ") { let player_name = String::from(character_str.get(0..pos_end).unwrap()); if !players.contains(&player_name) { @@ -104,8 +105,8 @@ fn get_active_players() -> Vec { } } } - else if let Some(pos) = mess.find("Connections") { - let nb_of_connections_str = mess.get(pos+12..).unwrap(); + else if let Some(pos) = mess.find(STRING_BEFORE_NB_OF_CONNECTIONS) { + let nb_of_connections_str = mess.get(pos+STRING_BEFORE_NB_OF_CONNECTIONS.len()+1..).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() { number_of_connections = n; -- 2.45.2