Use 'rcon' to get the player list
[minecraft_web.git] / backend / src / minecraft_controller.rs
1 use sysinfo::{ ProcessExt, SystemExt };
2
3 use std::{ fs, time::SystemTime };
4
5 use chrono::{ DateTime, offset::Local };
6
7 #[cfg(target_os = "linux")]
8 use systemd::journal;
9
10 #[derive(Clone, Debug)]
11 pub struct MinecraftExe {
12 memory: u64, // [kB].
13 load_average_5min: f64, // [%].
14 uptime: u64, // [s].
15 world_size: u64, // [B].
16 active_players: Vec<String>,
17 last_backup: Option<SystemTime>,
18 }
19
20 impl MinecraftExe {
21 pub fn format_memory(&self) -> String {
22 format_byte_size(self.memory * 1024, 2)
23 }
24
25 pub fn format_load_average(&self) -> String {
26 format!("{:.2} %", self.load_average_5min)
27 }
28
29 pub fn format_uptime(&self) -> String {
30 let mins = self.uptime / 60;
31 let hours = mins / 60;
32 let days = hours / 24;
33 format!("{}d {}h {}min", days, hours - 24 * days, mins - 60 * hours)
34 }
35
36 pub fn format_world_size(&self) -> String {
37 format_byte_size(self.world_size, 2)
38 }
39
40 pub fn format_active_players(&self) -> String {
41 /* Commented because the player list isn't correct (the number is).
42 if self.active_players.len() == 0 {
43 String::from("<none>")
44 } else {
45 self.active_players.join(", ")
46 }*/
47 self.active_players.len().to_string()
48 }
49
50 pub fn format_last_backup(&self) -> String {
51 match self.last_backup {
52 Some(t) => {
53 let datetime: DateTime<Local> = t.into();
54 datetime.format("%d/%m/%Y %T").to_string()
55 },
56 None => String::from("?")
57 }
58 }
59 }
60
61 const BINARY_PREFIXES: [&str; 8] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB"];
62
63 fn format_byte_size(bytes: u64, precision: usize) -> String {
64 for i in 0 .. 8 {
65 let mut size: u64 = 1;
66 size *= 1024u64.pow(i as u32);
67
68 if bytes < 1024 {
69 return format!("{} {}", std::cmp::max(0u64, bytes), BINARY_PREFIXES[i]);
70 }
71 else if bytes < 1024 * size {
72 return format!("{:.prec$} {}", bytes as f64 / size as f64, BINARY_PREFIXES[i], prec = precision);
73 }
74 }
75
76 String::from("")
77 }
78
79 const MINECRAFT_PROCESS_NAME: &str = "java";
80
81 #[cfg(target_os = "linux")]
82 const STRING_BEFORE_CHARACTER_NAME: &str = "Got character ZDOID from";
83
84 #[cfg(target_os = "linux")]
85 const STRING_BEFORE_NB_OF_CONNECTIONS: &str = "Connections";
86
87 // It doesn't work for the moment, it only scan the connection event and do not treat disconnections.
88 fn get_active_players(rcon_password: &str) -> Vec<String> {
89 let mut client = minecraft_client_rs::Client::new("127.0.0.1:25575".to_string()).unwrap();
90
91 let players =
92 match client.authenticate(rcon_password.to_string()) {
93 Ok(_) => {
94 match client.send_command("list".to_string()) {
95 Ok(resp) => {
96 println!("{}", resp.body);
97 Vec::new()
98 },
99 Err(_e) => {
100 println!("Error asking seed");
101 Vec::new()
102 },
103 }
104 },
105 Err(_e) => {
106 println!("Authentication error");
107 Vec::new()
108 },
109 };
110
111 client.close().unwrap();
112
113 players
114 }
115
116 fn get_last_backup_datetime(backup_path: &str) -> Option<SystemTime> {
117 let mut times =
118 fs::read_dir(backup_path).ok()?.filter_map(
119 |e| {
120 let dir = e.ok()?;
121 if dir.path().is_file() { Some(dir.metadata().ok()?.modified().ok()?) } else { None }
122 }
123 )
124 .collect::<Vec<SystemTime>>();
125
126 times.sort();
127
128 Some(times.last()?.clone())
129 }
130
131 pub fn get_minecraft_executable_information(world_path: &str, backup_path: &str, rcon_password: &str) -> Option<MinecraftExe> {
132 let mut system = sysinfo::System::new_all();
133 system.refresh_system();
134 let processes = system.get_process_by_name(MINECRAFT_PROCESS_NAME);
135
136 // TODO: find the correct process by checking the correct jar name in parameters.
137 if processes.len() >= 1 {
138 let process = processes.first().unwrap();
139
140 let world_size = match std::fs::metadata(world_path) { Ok(f) => f.len(), Err(_) => 0u64 };
141
142 Some(
143 MinecraftExe {
144 memory: process.memory(),
145 load_average_5min: system.get_load_average().five / system.get_processors().len() as f64 * 100.,
146 uptime: std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() - process.start_time(),
147 world_size,
148 active_players: get_active_players(rcon_password),
149 last_backup: get_last_backup_datetime(backup_path)
150 }
151 )
152 } else {
153 None
154 }
155 }