Skip to content

Commit

Permalink
Playtime is now displayed in the launcher
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoFoxxo committed Nov 5, 2023
1 parent 94f1ad5 commit 37ddaf2
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
12 changes: 12 additions & 0 deletions src-tauri/src/commands/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LauncherConfig>>,
game_name: String,
) -> Result<u64, CommandError> {
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))),
}
}
12 changes: 12 additions & 0 deletions src-tauri/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,4 +590,16 @@ impl LauncherConfig {
self.save_config()?;
Ok(())
}

pub fn get_game_seconds_played(
&mut self,
game_name: &String,
) -> Result<u64, ConfigError> {
// 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))
}

}
1 change: 1 addition & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 8 additions & 17 deletions src/components/games/GameControls.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
Expand All @@ -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<string>("playtimeUpdated", (event) => {
getPlaytime().then((result) => {
playtime = result;
getPlaytime(activeGame).then((result) => {
playtime = formatPlaytime(result);
});
});
</script>
Expand Down
10 changes: 10 additions & 0 deletions src/lib/rpc/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,13 @@ export async function doesActiveToolingVersionSupportGame(
() => false,
);
}

export async function getPlaytime(
gameName: string,
): Promise<number> {
return await invoke_rpc(
"get_playtime",
{ gameName: gameName },
() => 0,
);
}

0 comments on commit 37ddaf2

Please sign in to comment.