Skip to content

Commit

Permalink
Final API setup for tauri
Browse files Browse the repository at this point in the history
  • Loading branch information
dadodasyra committed Mar 2, 2024
1 parent b36a9ca commit ded5fcf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
7 changes: 2 additions & 5 deletions frontend/src-tauri/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::time::Instant;
use serde::Serialize;

// Assuming these are your global variables in the `api` module
#[derive(Clone)]
Expand All @@ -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,
Expand All @@ -34,26 +35,22 @@ 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()
}

/**
* 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
Expand Down
38 changes: 34 additions & 4 deletions frontend/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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");
Expand All @@ -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<GameStatus, String> {
Ok(api.inner().get_game_status().await)
}

#[tauri::command]
async fn get_server_ip(api: State<'_, Api>) -> Result<String, String> {
Ok(api.inner().get_server_ip().await)
}

#[tauri::command]
async fn get_server_port(api: State<'_, Api>) -> Result<u16, String> {
Ok(api.inner().get_server_port().await)
}

#[tauri::command]
async fn get_last_updated_server_ip(api: State<'_, Api>) -> Result<u64, String> {
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())
}

0 comments on commit ded5fcf

Please sign in to comment.