diff options
author | GeckoEidechse <40122905+GeckoEidechse@users.noreply.github.com> | 2023-01-13 17:00:31 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-13 17:00:31 +0100 |
commit | 5baa6a22a18970fc294710f42afcea2e5da2200d (patch) | |
tree | 3e09f5d54c2b4ba4233ba3eab4feb476449b70c0 | |
parent | eb6726fb016c468d39a674cf7707f2438093db1a (diff) | |
download | FlightCore-5baa6a22a18970fc294710f42afcea2e5da2200d.tar.gz FlightCore-5baa6a22a18970fc294710f42afcea2e5da2200d.zip |
feat: Add backend code for getting playercount (#135)
* feat: Initial backend code to get the playercount
from the Northstar master server together with servercount
* fix: Push correct backend code
* feat: Load playercount on application load
and show on PlayView
* refactor: Store global const in separate file
Moved user agent there for now
* refactor: User user agent from global const
* refactor: Move masterserver URL into global const
* refactor: Remove temporary variable
* fix: Do proper typing for playercount return value
* feat: Change text if unable to load playercount
So instead of showing some wrong value, we just say that we were unable
to load it.
* fix: Remove leftover print statement
* refactor: Move struct to library source file
* fix: Remove break element
* fix: Remove frontend display of playercount
This allows for separate PR for adding backend code early.
* refactor: serverlist endpoint var to global const
-rw-r--r-- | src-tauri/src/constants.rs | 6 | ||||
-rw-r--r-- | src-tauri/src/lib.rs | 6 | ||||
-rw-r--r-- | src-tauri/src/main.rs | 35 | ||||
-rw-r--r-- | src-vue/src/plugins/store.ts | 16 |
4 files changed, 62 insertions, 1 deletions
diff --git a/src-tauri/src/constants.rs b/src-tauri/src/constants.rs index 9d7e117c..4cc3c204 100644 --- a/src-tauri/src/constants.rs +++ b/src-tauri/src/constants.rs @@ -1,3 +1,9 @@ // This file stores various global constants values pub const APP_USER_AGENT: &str = "R2NorthstarTools/FlightCore"; + +// URL of the Northstar masterserver +pub const MASTER_SERVER_URL: &str = "https://northstar.tf"; + +// server list endpoint +pub const SERVER_BROWSER_ENDPOINT: &str = "/client/servers"; diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 2df1e243..67f42f85 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -39,6 +39,12 @@ pub struct NorthstarMod { pub directory: String, } +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct NorthstarServer { + #[serde(rename = "playerCount")] + pub player_count: i32, +} + /// Check version number of a mod pub fn check_mod_version_number(path_to_mod_folder: String) -> Result<String, anyhow::Error> { // println!("{}", format!("{}/mod.json", path_to_mod_folder)); diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 96be489f..b47a796e 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -9,7 +9,7 @@ use std::{ time::Duration, }; -use app::*; +use app::{*, constants::{APP_USER_AGENT, MASTER_SERVER_URL, SERVER_BROWSER_ENDPOINT}}; mod github; use github::release_notes::{ @@ -30,6 +30,7 @@ use mod_management::{ mod northstar; use northstar::get_northstar_version_number; +use serde::{Deserialize, Serialize}; use tauri::Manager; use tauri_plugin_store::PluginBuilder; use tokio::time::sleep; @@ -105,6 +106,7 @@ fn main() { clean_up_download_folder_caller, get_newest_flightcore_version, delete_northstar_mod, + get_server_player_count, delete_thunderstore_mod, ]) .run(tauri::generate_context!()) @@ -307,3 +309,34 @@ async fn clean_up_download_folder_caller( Err(err) => Err(err.to_string()), } } + + +/// Gets server and playercount from master server API +#[tauri::command] +async fn get_server_player_count() -> Result<(i32, usize), String> { + + 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 + .unwrap() + .text() + .await + .unwrap(); + + 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(); + + dbg!((total_player_count, server_count)); + + Ok((total_player_count, server_count)) +} diff --git a/src-vue/src/plugins/store.ts b/src-vue/src/plugins/store.ts index 57406dec..edeb13df 100644 --- a/src-vue/src/plugins/store.ts +++ b/src-vue/src/plugins/store.ts @@ -38,6 +38,9 @@ export interface FlightCoreStore { northstar_is_running: boolean, origin_is_running: boolean, + player_count: number, + server_count: number, + // user custom settings mods_per_page: number, } @@ -65,6 +68,9 @@ export const store = createStore<FlightCoreStore>({ northstar_is_running: false, origin_is_running: false, + player_count: -1, + server_count: -1, + mods_per_page: 20, } }, @@ -382,6 +388,16 @@ async function _initializeApp(state: any) { // Check installed Northstar version if found await _get_northstar_version_number(state); } + + await invoke<[number, number]>("get_server_player_count") + .then((message) => { + state.player_count = message[0]; + state.server_count = message[1]; + }) + .catch((error) => { + console.warn("Failed getting player/server count"); + console.warn(error); + }); } async function _checkForFlightCoreUpdates(state: FlightCoreStore) { |