diff options
-rw-r--r-- | src-tauri/src/main.rs | 53 | ||||
-rw-r--r-- | src-tauri/src/util.rs | 48 |
2 files changed, 54 insertions, 47 deletions
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 6b90093e..d3aa3e04 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -14,7 +14,7 @@ use std::ptr::null_mut; #[cfg(target_os = "windows")] use winapi::um::winuser::{MessageBoxW, MB_ICONERROR, MB_OK, MB_USERICON}; -use crate::constants::{APP_USER_AGENT, MASTER_SERVER_URL, REFRESH_DELAY, SERVER_BROWSER_ENDPOINT}; +use crate::constants::REFRESH_DELAY; mod github; use github::release_notes::check_is_flightcore_outdated; @@ -112,7 +112,10 @@ fn main() { loop { sleep(REFRESH_DELAY).await; app_handle - .emit_all("northstar-statistics", get_server_player_count().await) + .emit_all( + "northstar-statistics", + util::get_server_player_count().await, + ) .unwrap(); } }); @@ -145,7 +148,7 @@ fn main() { clean_up_download_folder_caller, github::release_notes::get_newest_flightcore_version, mod_management::delete_northstar_mod, - get_server_player_count, + util::get_server_player_count, mod_management::delete_thunderstore_mod, open_repair_window, query_thunderstore_packages_api, @@ -421,44 +424,6 @@ async fn clean_up_download_folder_caller( } } -/// Fetches `/client/servers` endpoint from master server -async fn fetch_server_list() -> Result<String, anyhow::Error> { - let url = format!("{MASTER_SERVER_URL}{SERVER_BROWSER_ENDPOINT}"); - let client = reqwest::Client::new(); - let res = client - .get(url) - .header(reqwest::header::USER_AGENT, APP_USER_AGENT) - .send() - .await? - .text() - .await?; - - Ok(res) -} - -/// Gets server and playercount from master server API -#[tauri::command] -async fn get_server_player_count() -> Result<(i32, usize), String> { - let res = match fetch_server_list().await { - Ok(res) => res, - Err(err) => return Err(err.to_string()), - }; - - let ns_servers: Vec<NorthstarServer> = - serde_json::from_str(&res).expect("JSON was not well-formatted"); - - // Get server count - let server_count = ns_servers.len(); - - // Sum up player count - let total_player_count: i32 = ns_servers.iter().map(|server| server.player_count).sum(); - - log::info!("total_player_count: {}", total_player_count); - log::info!("server_count: {}", server_count); - - Ok((total_player_count, server_count)) -} - /// Spawns repair window #[tauri::command] async fn open_repair_window(handle: tauri::AppHandle) -> Result<(), String> { @@ -572,12 +537,6 @@ pub struct NorthstarMod { pub directory: String, } -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct NorthstarServer { - #[serde(rename = "playerCount")] - pub player_count: i32, -} - // I intend to add more linux related stuff to check here, so making a func // for now tho it only checks `ldd --version` // - salmon diff --git a/src-tauri/src/util.rs b/src-tauri/src/util.rs index 0c2c5da4..0802d68c 100644 --- a/src-tauri/src/util.rs +++ b/src-tauri/src/util.rs @@ -1,5 +1,15 @@ //! This module contains various utility/helper functions that do not fit into any other module +use serde::{Deserialize, Serialize}; + +use crate::constants::{APP_USER_AGENT, MASTER_SERVER_URL, SERVER_BROWSER_ENDPOINT}; + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct NorthstarServer { + #[serde(rename = "playerCount")] + pub player_count: i32, +} + /// This function's only use is to force a `panic!()` // This must NOT be async to ensure crashing whole application. #[tauri::command] @@ -12,3 +22,41 @@ pub fn force_panic() { pub async fn is_debug_mode() -> bool { cfg!(debug_assertions) } + +/// Fetches `/client/servers` endpoint from master server +async fn fetch_server_list() -> Result<String, anyhow::Error> { + let url = format!("{MASTER_SERVER_URL}{SERVER_BROWSER_ENDPOINT}"); + let client = reqwest::Client::new(); + let res = client + .get(url) + .header(reqwest::header::USER_AGENT, APP_USER_AGENT) + .send() + .await? + .text() + .await?; + + Ok(res) +} + +/// Gets server and playercount from master server API +#[tauri::command] +pub async fn get_server_player_count() -> Result<(i32, usize), String> { + let res = match fetch_server_list().await { + Ok(res) => res, + Err(err) => return Err(err.to_string()), + }; + + let ns_servers: Vec<NorthstarServer> = + serde_json::from_str(&res).expect("JSON was not well-formatted"); + + // Get server count + let server_count = ns_servers.len(); + + // Sum up player count + let total_player_count: i32 = ns_servers.iter().map(|server| server.player_count).sum(); + + log::info!("total_player_count: {}", total_player_count); + log::info!("server_count: {}", server_count); + + Ok((total_player_count, server_count)) +} |