feat: sync library

This commit is contained in:
Zamitto 2024-06-15 02:15:58 -03:00
parent 15176a12de
commit ce13f6aa21
6 changed files with 167 additions and 7 deletions

View file

@ -4,8 +4,12 @@ import { IsNull, Not } from "typeorm";
import { gameRepository } from "@main/repository";
import { getProcesses } from "@main/helpers";
import { WindowManager } from "./window-manager";
import { HydraApi } from "./hydra-api";
const gamesPlaytime = new Map<number, number>();
const gamesPlaytime = new Map<
number,
{ lastTick: number; firstTick: number }
>();
export const watchProcesses = async () => {
const games = await gameRepository.find({
@ -37,7 +41,9 @@ export const watchProcesses = async () => {
if (gameProcess) {
if (gamesPlaytime.has(game.id)) {
const zero = gamesPlaytime.get(game.id) ?? 0;
const gamePlaytime = gamesPlaytime.get(game.id)!;
const zero = gamePlaytime.lastTick;
const delta = performance.now() - zero;
if (WindowManager.mainWindow) {
@ -48,12 +54,57 @@ export const watchProcesses = async () => {
playTimeInMilliseconds: game.playTimeInMilliseconds + delta,
lastTimePlayed: new Date(),
});
}
gamesPlaytime.set(game.id, performance.now());
gamesPlaytime.set(game.id, {
...gamePlaytime,
lastTick: performance.now(),
});
} else {
if (game.remoteId) {
HydraApi.put(`/games/${game.remoteId}`, {
playTimeDeltaInMilliseconds: 0,
lastTimePlayed: new Date(),
});
} else {
HydraApi.post("/games", {
objectId: game.objectID,
playTimeInMilliseconds: Math.round(game.playTimeInMilliseconds),
shop: game.shop,
lastTimePlayed: new Date(),
}).then((response) => {
const { id: remoteId } = response.data;
gameRepository.update({ objectID: game.objectID }, { remoteId });
});
}
gamesPlaytime.set(game.id, {
lastTick: performance.now(),
firstTick: performance.now(),
});
}
} else if (gamesPlaytime.has(game.id)) {
const gamePlaytime = gamesPlaytime.get(game.id)!;
gamesPlaytime.delete(game.id);
if (game.remoteId) {
HydraApi.put(`/games/${game.remoteId}`, {
playTimeInMilliseconds: Math.round(
performance.now() - gamePlaytime.firstTick
),
lastTimePlayed: game.lastTimePlayed,
});
} else {
HydraApi.post("/games", {
objectId: game.objectID,
playTimeInMilliseconds: Math.round(game.playTimeInMilliseconds),
shop: game.shop,
lastTimePlayed: game.lastTimePlayed,
}).then((response) => {
const { id: remoteId } = response.data;
gameRepository.update({ objectID: game.objectID }, { remoteId });
});
}
if (WindowManager.mainWindow) {
WindowManager.mainWindow.webContents.send("on-game-close", game.id);
}