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] 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(); +};