mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-02-09 03:37:45 +03:00
feat: call update game endpoint on process watcher at each 120 ticks
This commit is contained in:
parent
856a4c706a
commit
bab041b5f7
@ -10,6 +10,6 @@ export const startMainLoop = async () => {
|
|||||||
DownloadManager.watchDownloads(),
|
DownloadManager.watchDownloads(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
await sleep(500);
|
await sleep(1000);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -4,12 +4,16 @@ import { WindowManager } from "./window-manager";
|
|||||||
import { createGame, updateGamePlaytime } from "./library-sync";
|
import { createGame, updateGamePlaytime } from "./library-sync";
|
||||||
import { GameRunning } from "@types";
|
import { GameRunning } from "@types";
|
||||||
import { PythonInstance } from "./download";
|
import { PythonInstance } from "./download";
|
||||||
|
import { Game } from "@main/entity";
|
||||||
|
|
||||||
export const gamesPlaytime = new Map<
|
export const gamesPlaytime = new Map<
|
||||||
number,
|
number,
|
||||||
{ lastTick: number; firstTick: number }
|
{ lastTick: number; firstTick: number; lastSyncTick: number }
|
||||||
>();
|
>();
|
||||||
|
|
||||||
|
const TICKS_TO_UPDATE_API = 120;
|
||||||
|
let currentTick = 1;
|
||||||
|
|
||||||
export const watchProcesses = async () => {
|
export const watchProcesses = async () => {
|
||||||
const games = await gameRepository.find({
|
const games = await gameRepository.find({
|
||||||
where: {
|
where: {
|
||||||
@ -30,48 +34,17 @@ export const watchProcesses = async () => {
|
|||||||
|
|
||||||
if (gameProcess) {
|
if (gameProcess) {
|
||||||
if (gamesPlaytime.has(game.id)) {
|
if (gamesPlaytime.has(game.id)) {
|
||||||
const gamePlaytime = gamesPlaytime.get(game.id)!;
|
onTickGame(game);
|
||||||
|
|
||||||
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(),
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
if (game.remoteId) {
|
onOpenGame(game);
|
||||||
updateGamePlaytime(game, 0, new Date());
|
|
||||||
} else {
|
|
||||||
createGame({ ...game, lastTimePlayed: new Date() });
|
|
||||||
}
|
|
||||||
|
|
||||||
gamesPlaytime.set(game.id, {
|
|
||||||
lastTick: performance.now(),
|
|
||||||
firstTick: performance.now(),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} else if (gamesPlaytime.has(game.id)) {
|
} else if (gamesPlaytime.has(game.id)) {
|
||||||
const gamePlaytime = gamesPlaytime.get(game.id)!;
|
onCloseGame(game);
|
||||||
gamesPlaytime.delete(game.id);
|
|
||||||
|
|
||||||
if (game.remoteId) {
|
|
||||||
updateGamePlaytime(
|
|
||||||
game,
|
|
||||||
performance.now() - gamePlaytime.firstTick,
|
|
||||||
game.lastTimePlayed!
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
createGame(game);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
currentTick++;
|
||||||
|
|
||||||
if (WindowManager.mainWindow) {
|
if (WindowManager.mainWindow) {
|
||||||
const gamesRunning = Array.from(gamesPlaytime.entries()).map((entry) => {
|
const gamesRunning = Array.from(gamesPlaytime.entries()).map((entry) => {
|
||||||
return {
|
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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user