From 37ddaf2a1fd61ff39ee97309f9c1ccdba260550e Mon Sep 17 00:00:00 2001 From: Hamish Date: Sun, 5 Nov 2023 15:15:51 +1000 Subject: [PATCH] Playtime is now displayed in the launcher --- src-tauri/src/commands/config.rs | 12 ++++++++++++ src-tauri/src/config.rs | 12 ++++++++++++ src-tauri/src/main.rs | 1 + src/components/games/GameControls.svelte | 25 ++++++++---------------- src/lib/rpc/config.ts | 10 ++++++++++ 5 files changed, 43 insertions(+), 17 deletions(-) diff --git a/src-tauri/src/commands/config.rs b/src-tauri/src/commands/config.rs index b4c2167f..c2b22cf8 100644 --- a/src-tauri/src/commands/config.rs +++ b/src-tauri/src/commands/config.rs @@ -414,3 +414,15 @@ pub async fn does_active_tooling_version_support_game( _ => Ok(false), } } + +#[tauri::command] +pub async fn get_playtime( + config: tauri::State<'_, tokio::sync::Mutex>, + game_name: String, +) -> Result { + let mut config_lock = config.lock().await; + match config_lock.get_game_seconds_played(&game_name) { + Ok(playtime) => Ok(playtime), + Err(err) => Err(CommandError::Configuration(format!("Error occurred when getting game playtime: {}", err))), + } +} \ No newline at end of file diff --git a/src-tauri/src/config.rs b/src-tauri/src/config.rs index 2ae44868..0b6a997e 100644 --- a/src-tauri/src/config.rs +++ b/src-tauri/src/config.rs @@ -590,4 +590,16 @@ impl LauncherConfig { self.save_config()?; Ok(()) } + + pub fn get_game_seconds_played( + &mut self, + game_name: &String, + ) -> Result { + // make lowercase and remove the space so that the game is recognised + let formatted_game = game_name.to_lowercase().replace(" ", ""); + + let game_config = self.get_supported_game_config_mut(&formatted_game)?; + Ok(game_config.seconds_played.unwrap_or(0)) + } + } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 9efc0557..3eb5a1ca 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -146,6 +146,7 @@ fn main() { commands::config::cleanup_enabled_texture_packs, commands::config::delete_old_data_directory, commands::config::does_active_tooling_version_support_game, + commands::config::get_playtime, commands::config::finalize_installation, commands::config::get_active_tooling_version_folder, commands::config::get_active_tooling_version, diff --git a/src/components/games/GameControls.svelte b/src/components/games/GameControls.svelte index 5f99e7f5..f84b4022 100644 --- a/src/components/games/GameControls.svelte +++ b/src/components/games/GameControls.svelte @@ -16,9 +16,9 @@ import { resetGameSettings, uninstallGame } from "$lib/rpc/game"; import { platform } from "@tauri-apps/api/os"; import { getLaunchGameString, launchGame, openREPL } from "$lib/rpc/binaries"; + import { getPlaytime } from "$lib/rpc/config"; import { _ } from "svelte-i18n"; import { navigate } from "svelte-navigator"; - import { readTextFile, BaseDirectory } from "@tauri-apps/api/fs"; import { listen } from "@tauri-apps/api/event"; import { toastStore } from "$lib/stores/ToastStore"; @@ -45,18 +45,9 @@ ); }); - // get the playtime from the txt file in the launcher config folder - async function getPlaytime() { - const playtimeRaw = await readTextFile("playtime.txt", { - dir: BaseDirectory.App, - }); - const playtime = formatPlaytime(parseInt(playtimeRaw)); - return playtime; - } - - // format the time from the playtime.txt file which is stored as seconds + // format the time from the settings file which is stored as seconds function formatPlaytime(playtimeRaw: number) { - // calculate the number of hours, minutes, and seconds + // calculate the number of hours and minutes const hours = Math.floor(playtimeRaw / 3600); const minutes = Math.floor((playtimeRaw % 3600) / 60); @@ -83,15 +74,15 @@ let playtime = ""; - // run the function and assign the result to the playtime variable when the page first loads - getPlaytime().then((result) => { - playtime = result; + // get the playtime from the backend, format it, and assign it to the playtime variable when the page first loads + getPlaytime(activeGame).then((result) => { + playtime = formatPlaytime(result); }); // listen for the custom playtiemUpdated event from the backend and then refresh the playtime on screen listen("playtimeUpdated", (event) => { - getPlaytime().then((result) => { - playtime = result; + getPlaytime(activeGame).then((result) => { + playtime = formatPlaytime(result); }); }); diff --git a/src/lib/rpc/config.ts b/src/lib/rpc/config.ts index abeec6ce..d9d6941d 100644 --- a/src/lib/rpc/config.ts +++ b/src/lib/rpc/config.ts @@ -256,3 +256,13 @@ export async function doesActiveToolingVersionSupportGame( () => false, ); } + +export async function getPlaytime( + gameName: string, +): Promise { + return await invoke_rpc( + "get_playtime", + { gameName: gameName }, + () => 0, + ); +} \ No newline at end of file