diff --git a/frontend/src-tauri/src/api.rs b/frontend/src-tauri/src/api.rs index 267034bb..8effcd86 100644 --- a/frontend/src-tauri/src/api.rs +++ b/frontend/src-tauri/src/api.rs @@ -1,4 +1,5 @@ use std::time::Instant; +use serde::Serialize; // Assuming these are your global variables in the `api` module #[derive(Clone)] @@ -9,7 +10,7 @@ pub struct Api { pub last_updated_server_ip: Instant } -#[derive(PartialEq, Debug, Clone)] +#[derive(PartialEq, Debug, Clone, Serialize)] pub enum GameStatus { Closed, Started, @@ -34,7 +35,6 @@ impl Api { * It's updated AT LEAST every 5 seconds. (1-5 secs) * This information is prioritized over the others. */ - #[tauri::command] pub async fn get_game_status(&self) -> GameStatus { self.game_status.clone() } @@ -42,18 +42,15 @@ impl Api { /** * Server IP, may be updated */ - #[tauri::command] pub async fn get_server_ip(&self) -> String { self.server_ip.clone() } - #[tauri::command] pub async fn get_server_port(&self) -> u16 { self.server_port } - #[tauri::command] pub async fn get_last_updated_server_ip(&self) -> Instant { self.last_updated_server_ip diff --git a/frontend/src-tauri/src/main.rs b/frontend/src-tauri/src/main.rs index f29415f1..d9a51d63 100644 --- a/frontend/src-tauri/src/main.rs +++ b/frontend/src-tauri/src/main.rs @@ -1,6 +1,10 @@ // Prevents additional console window on Windows in release, DO NOT REMOVE!! #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] +use std::time::Instant; +use tauri::State; +use crate::api::{Api, GameStatus}; + mod fetch_informations; mod api; @@ -11,10 +15,10 @@ async fn main() { tauri::Builder::default() .manage(api) .invoke_handler(tauri::generate_handler![ - api::Api::get_game_status, - api::Api::get_server_ip, - api::Api::get_server_port, - api::Api::get_last_updated_server_ip + get_game_status, + get_server_ip, + get_server_port, + get_last_updated_server_ip ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); @@ -36,4 +40,30 @@ async fn main() { tokio::time::sleep(std::time::Duration::from_secs(5)).await; }*/ +} + +#[tauri::command] +async fn get_game_status(api: State<'_, Api>) -> Result { + Ok(api.inner().get_game_status().await) +} + +#[tauri::command] +async fn get_server_ip(api: State<'_, Api>) -> Result { + Ok(api.inner().get_server_ip().await) +} + +#[tauri::command] +async fn get_server_port(api: State<'_, Api>) -> Result { + Ok(api.inner().get_server_port().await) +} + +#[tauri::command] +async fn get_last_updated_server_ip(api: State<'_, Api>) -> Result { + let instant = api.inner().get_last_updated_server_ip().await; + let now = std::time::SystemTime::now(); + let epoch = std::time::UNIX_EPOCH; + let duration_since_epoch = now.duration_since(epoch).expect("Time went backwards"); + let instant_duration = instant.elapsed(); + let total_duration = duration_since_epoch.checked_sub(instant_duration).expect("Time went backwards"); + Ok(total_duration.as_secs()) } \ No newline at end of file