From f9d5cfce73bcb367ede6c4e15b0e069524ede6cc Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Thu, 22 May 2025 13:08:24 -0300 Subject: [PATCH 1/4] fix: change ticks so it syncs playtime each 2 minutes --- src/main/services/process-watcher.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/services/process-watcher.ts b/src/main/services/process-watcher.ts index 1bd94e2b..dd051760 100644 --- a/src/main/services/process-watcher.ts +++ b/src/main/services/process-watcher.ts @@ -28,7 +28,7 @@ interface GameExecutables { [key: string]: ExecutableInfo[]; } -const TICKS_TO_UPDATE_API = 120; +const TICKS_TO_UPDATE_API = 80; let currentTick = 1; const isWindowsPlatform = process.platform === "win32"; From 772aea69a965928b3bc6153393b8dddc67c64ce9 Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Thu, 22 May 2025 13:11:32 -0300 Subject: [PATCH 2/4] feat: persist playtime that failed to sync on leveldb --- src/main/constants.ts | 12 ++--- src/main/index.ts | 2 +- src/main/services/process-watcher.ts | 66 +++++++++++++++++++++------- src/types/level.types.ts | 1 + 4 files changed, 59 insertions(+), 22 deletions(-) diff --git a/src/main/constants.ts b/src/main/constants.ts index 243891e7..5625a53e 100644 --- a/src/main/constants.ts +++ b/src/main/constants.ts @@ -4,7 +4,8 @@ import { SystemPath } from "./services/system-path"; export const defaultDownloadsPath = SystemPath.getPath("downloads"); -export const isStaging = import.meta.env.MAIN_VITE_API_URL.includes("staging"); +export const isStaging = + true || import.meta.env.MAIN_VITE_API_URL.includes("staging"); export const windowsStartMenuPath = path.join( SystemPath.getPath("appData"), @@ -26,11 +27,10 @@ export const commonRedistPath = path.join( "CommonRedist" ); -export const logsPath = path.join(SystemPath.getPath("userData"), "logs"); - -export const seedsPath = app.isPackaged - ? path.join(process.resourcesPath, "seeds") - : path.join(__dirname, "..", "..", "seeds"); +export const logsPath = path.join( + SystemPath.getPath("userData"), + `logs${isStaging ? "-staging" : ""}` +); export const achievementSoundPath = app.isPackaged ? path.join(process.resourcesPath, "achievement.wav") diff --git a/src/main/index.ts b/src/main/index.ts index 5151b956..abe3b17a 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -4,7 +4,7 @@ import i18n from "i18next"; import path from "node:path"; import url from "node:url"; import { electronApp, optimizer } from "@electron-toolkit/utils"; -import { logger, WindowManager } from "@main/services"; +import { logger, clearGamesPlaytime, WindowManager } from "@main/services"; import resources from "@locales"; import { PythonRPC } from "./services/python-rpc"; import { db, levelKeys } from "./level"; diff --git a/src/main/services/process-watcher.ts b/src/main/services/process-watcher.ts index dd051760..cacc6734 100644 --- a/src/main/services/process-watcher.ts +++ b/src/main/services/process-watcher.ts @@ -225,7 +225,18 @@ function onOpenGame(game: Game) { }); if (game.remoteId) { - updateGamePlaytime(game, 0, new Date()).catch(() => {}); + updateGamePlaytime( + game, + game.unsyncedDeltaPlayTimeInMilliseconds ?? 0, + new Date() + ) + .then(() => { + gamesSublevel.put(levelKeys.game(game.shop, game.objectId), { + ...game, + unsyncedDeltaPlayTimeInMilliseconds: 0, + }); + }) + .catch(() => {}); if (game.automaticCloudSync) { CloudSync.uploadSaveGame( @@ -260,22 +271,34 @@ function onTickGame(game: Game) { }); if (currentTick % TICKS_TO_UPDATE_API === 0) { + const deltaToSync = + now - + gamePlaytime.lastSyncTick + + (game.unsyncedDeltaPlayTimeInMilliseconds ?? 0); + const gamePromise = game.remoteId - ? updateGamePlaytime( - game, - now - gamePlaytime.lastSyncTick, - game.lastTimePlayed! - ) + ? updateGamePlaytime(game, deltaToSync, game.lastTimePlayed!) : createGame(game); gamePromise .then(() => { + gamesSublevel.put(levelKeys.game(game.shop, game.objectId), { + ...game, + unsyncedDeltaPlayTimeInMilliseconds: 0, + }); + }) + .catch(() => { + gamesSublevel.put(levelKeys.game(game.shop, game.objectId), { + ...game, + unsyncedDeltaPlayTimeInMilliseconds: deltaToSync, + }); + }) + .finally(() => { gamesPlaytime.set(levelKeys.game(game.shop, game.objectId), { ...gamePlaytime, lastSyncTick: now, }); - }) - .catch(() => {}); + }); } } @@ -286,12 +309,6 @@ const onCloseGame = (game: Game) => { gamesPlaytime.delete(levelKeys.game(game.shop, game.objectId)); if (game.remoteId) { - updateGamePlaytime( - game, - performance.now() - gamePlaytime.lastSyncTick, - game.lastTimePlayed! - ).catch(() => {}); - if (game.automaticCloudSync) { CloudSync.uploadSaveGame( game.objectId, @@ -300,7 +317,26 @@ const onCloseGame = (game: Game) => { CloudSync.getBackupLabel(true) ); } + + const deltaToSync = + performance.now() - + gamePlaytime.lastSyncTick + + (game.unsyncedDeltaPlayTimeInMilliseconds ?? 0); + + return updateGamePlaytime(game, deltaToSync, game.lastTimePlayed!) + .then(() => { + return gamesSublevel.put(levelKeys.game(game.shop, game.objectId), { + ...game, + unsyncedDeltaPlayTimeInMilliseconds: 0, + }); + }) + .catch(() => { + return gamesSublevel.put(levelKeys.game(game.shop, game.objectId), { + ...game, + unsyncedDeltaPlayTimeInMilliseconds: deltaToSync, + }); + }); } else { - createGame(game).catch(() => {}); + return createGame(game).catch(() => {}); } }; diff --git a/src/types/level.types.ts b/src/types/level.types.ts index fd904ca5..6a729cc5 100644 --- a/src/types/level.types.ts +++ b/src/types/level.types.ts @@ -34,6 +34,7 @@ export interface Game { title: string; iconUrl: string | null; playTimeInMilliseconds: number; + unsyncedDeltaPlayTimeInMilliseconds?: number; lastTimePlayed: Date | null; objectId: string; shop: GameShop; From eab9f92b3e49a316a878adc38cc9b71efc42af8f Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Thu, 22 May 2025 13:12:45 -0300 Subject: [PATCH 3/4] feat: ensure playtime is not lost when hydra closes --- src/main/index.ts | 14 +++++++++++--- src/main/services/process-watcher.ts | 12 ++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index abe3b17a..17eacd97 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -143,9 +143,17 @@ app.on("window-all-closed", () => { WindowManager.mainWindow = null; }); -app.on("before-quit", () => { - /* Disconnects libtorrent */ - PythonRPC.kill(); +let canAppBeClose = false; + +app.on("before-quit", async (e) => { + if (!canAppBeClose) { + e.preventDefault(); + /* Disconnects libtorrent */ + PythonRPC.kill(); + await clearGamesPlaytime(); + canAppBeClose = true; + app.quit(); + } }); app.on("activate", () => { diff --git a/src/main/services/process-watcher.ts b/src/main/services/process-watcher.ts index cacc6734..deb93f4e 100644 --- a/src/main/services/process-watcher.ts +++ b/src/main/services/process-watcher.ts @@ -340,3 +340,15 @@ const onCloseGame = (game: Game) => { return createGame(game).catch(() => {}); } }; + +export const clearGamesPlaytime = async () => { + for (const game of gamesPlaytime.keys()) { + const gameData = await gamesSublevel.get(game); + + if (gameData) { + await onCloseGame(gameData); + } + } + + gamesPlaytime.clear(); +}; From e734b6937ac2ef4e69543cc10ba419bbcf828f76 Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Thu, 22 May 2025 14:04:18 -0300 Subject: [PATCH 4/4] fix: staging hard coded true --- src/main/constants.ts | 3 +-- src/main/index.ts | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/constants.ts b/src/main/constants.ts index 5625a53e..16642d50 100644 --- a/src/main/constants.ts +++ b/src/main/constants.ts @@ -4,8 +4,7 @@ import { SystemPath } from "./services/system-path"; export const defaultDownloadsPath = SystemPath.getPath("downloads"); -export const isStaging = - true || import.meta.env.MAIN_VITE_API_URL.includes("staging"); +export const isStaging = import.meta.env.MAIN_VITE_API_URL.includes("staging"); export const windowsStartMenuPath = path.join( SystemPath.getPath("appData"), diff --git a/src/main/index.ts b/src/main/index.ts index 17eacd97..de88a548 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -143,15 +143,15 @@ app.on("window-all-closed", () => { WindowManager.mainWindow = null; }); -let canAppBeClose = false; +let canAppBeClosed = false; app.on("before-quit", async (e) => { - if (!canAppBeClose) { + if (!canAppBeClosed) { e.preventDefault(); /* Disconnects libtorrent */ PythonRPC.kill(); await clearGamesPlaytime(); - canAppBeClose = true; + canAppBeClosed = true; app.quit(); } });