Prepare for more information to be get from rcon
authorGreg Burri <greg.burri@gmail.com>
Thu, 24 Jun 2021 14:27:38 +0000 (16:27 +0200)
committerGreg Burri <greg.burri@gmail.com>
Thu, 24 Jun 2021 14:27:38 +0000 (16:27 +0200)
backend/src/minecraft_controller.rs

index 13a3e3f..163008a 100644 (file)
@@ -73,37 +73,43 @@ fn format_byte_size(bytes: u64, precision: usize) -> String {
 \r
 const MINECRAFT_PROCESS_NAME: &str = "java";\r
 \r
-// It doesn't work for the moment, it only scan the connection event and do not treat disconnections.\r
-fn get_active_players(rcon_password: &str) -> Vec<String> {\r
+struct StatusFromRcon {\r
+    players: Vec<String>,\r
+}\r
+\r
+fn get_status_from_rcon(rcon_password: &str) -> StatusFromRcon {\r
     let mut client = minecraft_client_rs::Client::new("127.0.0.1:25575".to_string()).unwrap();\r
 \r
-    let players =\r
+    let status =\r
         match client.authenticate(rcon_password.to_string()) {\r
             Ok(_) => {\r
-                match client.send_command("list".to_string()) {\r
-                    Ok(resp) => {\r
-                        resp.body\r
-                            .split('\n')\r
-                            .skip(1)\r
-                            .filter(|n| !n.is_empty())\r
-                            .map(|n| n.to_string())\r
-                            .collect()\r
-                    },\r
-                    Err(_e) => {\r
-                        println!("Error from 'list' command");\r
-                        Vec::new()\r
-                    },\r
+                StatusFromRcon {\r
+                    players:\r
+                        match client.send_command("list".to_string()) {\r
+                            Ok(resp) => {\r
+                                resp.body\r
+                                    .split('\n')\r
+                                    .skip(1)\r
+                                    .filter(|n| !n.is_empty())\r
+                                    .map(|n| n.to_string())\r
+                                    .collect()\r
+                            },\r
+                            Err(_e) => {\r
+                                println!("Error from 'list' command");\r
+                                Vec::new()\r
+                            },\r
+                        }\r
                 }\r
             },\r
             Err(_e) => {\r
                 println!("Authentication error");\r
-                Vec::new()\r
+                StatusFromRcon { players: Vec::new() }\r
             },\r
         };\r
 \r
     client.close().unwrap();\r
 \r
-    players\r
+    status\r
 }\r
 \r
 fn get_last_backup_datetime(backup_path: &str) -> Option<SystemTime> {\r
@@ -132,13 +138,15 @@ pub fn get_minecraft_executable_information(world_path: &str, backup_path: &str,
 \r
         let world_size = match fs_extra::dir::get_size(world_path) { Ok(l) => l, Err(_) => 0u64 };\r
 \r
+        let status_from_rcon = get_status_from_rcon(rcon_password);\r
+\r
         Some(\r
             MinecraftExe {\r
                 memory: process.memory(),\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
-                active_players: get_active_players(rcon_password),\r
+                active_players: status_from_rcon.players,\r
                 last_backup: get_last_backup_datetime(backup_path)\r
             }\r
         )\r