From bab041b5f7780f57d5afebf3e93022054161f1e8 Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Mon, 19 Aug 2024 20:53:34 -0300 Subject: [PATCH] feat: call update game endpoint on process watcher at each 120 ticks --- src/main/services/main-loop.ts | 2 +- src/main/services/process-watcher.ts | 112 ++++++++++++++++++--------- 2 files changed, 76 insertions(+), 38 deletions(-) diff --git a/src/main/services/main-loop.ts b/src/main/services/main-loop.ts index ca72707f..f2ec51ba 100644 --- a/src/main/services/main-loop.ts +++ b/src/main/services/main-loop.ts @@ -10,6 +10,6 @@ export const startMainLoop = async () => { DownloadManager.watchDownloads(), ]); - await sleep(500); + await sleep(1000); } }; diff --git a/src/main/services/process-watcher.ts b/src/main/services/process-watcher.ts index 0f7efa62..080f1efc 100644 --- a/src/main/services/process-watcher.ts +++ b/src/main/services/process-watcher.ts @@ -4,12 +4,16 @@ import { WindowManager } from "./window-manager"; import { createGame, updateGamePlaytime } from "./library-sync"; import { GameRunning } from "@types"; import { PythonInstance } from "./download"; +import { Game } from "@main/entity"; export const gamesPlaytime = new Map< number, - { lastTick: number; firstTick: number } + { lastTick: number; firstTick: number; lastSyncTick: number } >(); +const TICKS_TO_UPDATE_API = 120; +let currentTick = 1; + export const watchProcesses = async () => { const games = await gameRepository.find({ where: { @@ -30,48 +34,17 @@ export const watchProcesses = async () => { if (gameProcess) { if (gamesPlaytime.has(game.id)) { - const gamePlaytime = gamesPlaytime.get(game.id)!; - - const zero = gamePlaytime.lastTick; - const delta = performance.now() - zero; - - await gameRepository.update(game.id, { - playTimeInMilliseconds: game.playTimeInMilliseconds + delta, - lastTimePlayed: new Date(), - }); - - gamesPlaytime.set(game.id, { - ...gamePlaytime, - lastTick: performance.now(), - }); + onTickGame(game); } else { - if (game.remoteId) { - updateGamePlaytime(game, 0, new Date()); - } else { - createGame({ ...game, lastTimePlayed: new Date() }); - } - - gamesPlaytime.set(game.id, { - lastTick: performance.now(), - firstTick: performance.now(), - }); + onOpenGame(game); } } else if (gamesPlaytime.has(game.id)) { - const gamePlaytime = gamesPlaytime.get(game.id)!; - gamesPlaytime.delete(game.id); - - if (game.remoteId) { - updateGamePlaytime( - game, - performance.now() - gamePlaytime.firstTick, - game.lastTimePlayed! - ); - } else { - createGame(game); - } + onCloseGame(game); } } + currentTick++; + if (WindowManager.mainWindow) { const gamesRunning = Array.from(gamesPlaytime.entries()).map((entry) => { return { @@ -86,3 +59,68 @@ export const watchProcesses = async () => { ); } }; + +function onOpenGame(game: Game) { + const now = performance.now(); + + gamesPlaytime.set(game.id, { + lastTick: now, + firstTick: now, + lastSyncTick: now, + }); + + if (game.remoteId) { + updateGamePlaytime(game, 0, new Date()); + } else { + createGame({ ...game, lastTimePlayed: new Date() }); + } +} + +function onTickGame(game: Game) { + const now = performance.now(); + const gamePlaytime = gamesPlaytime.get(game.id)!; + + const delta = now - gamePlaytime.lastTick; + + gameRepository.update(game.id, { + playTimeInMilliseconds: game.playTimeInMilliseconds + delta, + lastTimePlayed: new Date(), + }); + + gamesPlaytime.set(game.id, { + ...gamePlaytime, + lastTick: now, + }); + + if (currentTick % TICKS_TO_UPDATE_API === 0) { + if (game.remoteId) { + updateGamePlaytime( + game, + now - gamePlaytime.lastSyncTick, + game.lastTimePlayed! + ); + } else { + createGame(game); + } + + gamesPlaytime.set(game.id, { + ...gamePlaytime, + lastSyncTick: now, + }); + } +} + +const onCloseGame = (game: Game) => { + const gamePlaytime = gamesPlaytime.get(game.id)!; + gamesPlaytime.delete(game.id); + + if (game.remoteId) { + updateGamePlaytime( + game, + performance.now() - gamePlaytime.firstTick, + game.lastTimePlayed! + ); + } else { + createGame(game); + } +};