Skip to content

Commit

Permalink
lint: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
xTVaser committed Nov 4, 2023
1 parent 2796692 commit 1722042
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 39 deletions.
28 changes: 14 additions & 14 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 20 additions & 12 deletions src-tauri/src/commands/binaries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
use std::os::windows::process::CommandExt;
use std::{
collections::HashMap,
fs::File,
io::prelude::*,
path::{Path, PathBuf},
process::Command,
thread,
time::Instant,
fs::File,
io::prelude::*, thread,
};

use log::{info, warn};
Expand Down Expand Up @@ -753,22 +754,19 @@ pub async fn launch_game(
let start_time = Instant::now(); // get the start time of the game

if let Ok(mut child) = command.spawn() {

// move all playtime tracking to a separate thread
thread::spawn(move || {

// start waiting for the game to exit
if let Err(err) = child.wait() {
log::error!("Error occured when waiting for game to exit: {}", err);
return;
}

// once the game exits pass the time the game started to the track_playtine function
if let Err(err) = track_playtime(start_time, app_handle) {
log::error!("Error occured when tracking playtime: {}", err);
return;
return;
}

});
}
Ok(())
Expand All @@ -778,14 +776,15 @@ fn track_playtime(
start_time: std::time::Instant,
app_handle: tauri::AppHandle,
) -> Result<Option<bool>, CommandError> {

// get the playtime of the session
let mut elapsed_time = start_time.elapsed().as_secs();

let config_dir = match path::config_dir() {
None => {
log::error!("Couldn't determine application config directory");
return Err(CommandError::BinaryExecution("Couldn't determine application config directory".to_owned()))
return Err(CommandError::BinaryExecution(
"Couldn't determine application config directory".to_owned(),
));
}
Some(path) => path,
};
Expand All @@ -802,7 +801,10 @@ fn track_playtime(

if let Err(err) = file.read_to_string(&mut contents) {
log::error!("Could not read playtime.txt: {}", err);
return Err(CommandError::BinaryExecution(format!("Could not read playtime.txt: {}", err)));
return Err(CommandError::BinaryExecution(format!(
"Could not read playtime.txt: {}",
err
)));
}

// if there is a int parse error and set the existing playtime to 0
Expand All @@ -821,13 +823,19 @@ fn track_playtime(
// add the new value to the playtime file
if let Err(err) = file.write_all(elapsed_time.to_string().as_bytes()) {
log::error!("Could not write playtime to file: {}", err);
return Err(CommandError::BinaryExecution(format!("Could not write playtime to file: {}", err)));
return Err(CommandError::BinaryExecution(format!(
"Could not write playtime to file: {}",
err
)));
}

// send an event to the front end so that it can refresh the playtime on screen
if let Err(err) = app_handle.emit_all("playtimeUpdated", ()) {
log::error!("Failed to emit playtimeUpdated event: {}", err);
return Err(CommandError::BinaryExecution(format!("Failed to emit playtimeUpdated event: {}", err)));
return Err(CommandError::BinaryExecution(format!(
"Failed to emit playtimeUpdated event: {}",
err
)));
}

Ok(None)
Expand Down
27 changes: 15 additions & 12 deletions src/components/games/GameControls.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import { getLaunchGameString, launchGame, openREPL } from "$lib/rpc/binaries";
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 { readTextFile, BaseDirectory } from "@tauri-apps/api/fs";
import { listen } from "@tauri-apps/api/event";
import { toastStore } from "$lib/stores/ToastStore";
export let activeGame: SupportedGame;
Expand All @@ -44,10 +44,12 @@
"saves",
);
});
// 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 playtimeRaw = await readTextFile("playtime.txt", {
dir: BaseDirectory.App,
});
const playtime = formatPlaytime(parseInt(playtimeRaw));
return playtime;
}
Expand All @@ -57,29 +59,29 @@
// calculate the number of hours, minutes, and seconds
const hours = Math.floor(playtimeRaw / 3600);
const minutes = Math.floor((playtimeRaw % 3600) / 60);
// initialize the formatted playtime string
let formattedPlaytime = '';
let formattedPlaytime = "";
// add the hours to the formatted playtime string
if (hours > 0) {
formattedPlaytime += `${hours} hour${hours > 1 ? 's' : ''}`;
formattedPlaytime += `${hours} hour${hours > 1 ? "s" : ""}`;
}
// add the minutes to the formatted playtime string
if (minutes > 0) {
// add a comma if there are already hours in the formatted playtime string
if (formattedPlaytime.length > 0) {
formattedPlaytime += ', ';
formattedPlaytime += ", ";
}
formattedPlaytime += `${minutes} minute${minutes > 1 ? 's' : ''}`;
formattedPlaytime += `${minutes} minute${minutes > 1 ? "s" : ""}`;
}
// return the formatted playtime string
return formattedPlaytime;
}
let playtime = '';
let playtime = "";
// run the function and assign the result to the playtime variable when the page first loads
getPlaytime().then((result) => {
Expand All @@ -92,7 +94,6 @@
playtime = result;
});
});
</script>

<div class="flex flex-col justify-end items-end mt-auto">
Expand All @@ -102,7 +103,9 @@
{$_(`gameName_${getInternalName(activeGame)}`)}
</h1>
{#if playtime}
<h1 class="pb-4 text-xl text-outline tracking-tighter font-extrabold">{`Played For ${playtime}`}</h1>
<h1 class="pb-4 text-xl text-outline tracking-tighter font-extrabold">
{`Played For ${playtime}`}
</h1>
{/if}
<div class="flex flex-row gap-2">
<Button
Expand Down
1 change: 0 additions & 1 deletion src/routes/GameFeature.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
texturePacksToDelete = event.detail.packsToDelete;
}
async function gameJobFinished() {
gameJobToRun = undefined;
}
Expand Down

0 comments on commit 1722042

Please sign in to comment.