hydra/src/main/services/process-watcher.ts

109 lines
3.0 KiB
TypeScript
Raw Normal View History

2024-04-21 08:26:29 +03:00
import path from "node:path";
import { IsNull, Not } from "typeorm";
import { gameRepository } from "@main/repository";
import { getProcesses } from "@main/helpers";
import { WindowManager } from "./window-manager";
2024-06-17 05:43:18 +03:00
import { createGame, updateGamePlaytime } from "./library-sync";
2024-06-20 04:05:22 +03:00
import { RunningGameEvent } from "@types";
2024-04-21 08:26:29 +03:00
2024-06-15 08:15:58 +03:00
const gamesPlaytime = new Map<
number,
{ lastTick: number; firstTick: number }
>();
export const watchProcesses = async () => {
const games = await gameRepository.find({
where: {
executablePath: Not(IsNull()),
isDeleted: false,
},
});
if (games.length === 0) return;
const processes = await getProcesses();
for (const game of games) {
const executablePath = game.executablePath!;
const basename = path.win32.basename(executablePath);
const basenameWithoutExtension = path.win32.basename(
executablePath,
path.extname(executablePath)
);
const gameProcess = processes.find((runningProcess) => {
if (process.platform === "win32") {
return runningProcess.name === basename;
}
2024-04-21 08:26:29 +03:00
return [basename, basenameWithoutExtension].includes(runningProcess.name);
2024-04-21 08:26:29 +03:00
});
if (gameProcess) {
if (gamesPlaytime.has(game.id)) {
2024-06-15 08:15:58 +03:00
const gamePlaytime = gamesPlaytime.get(game.id)!;
const zero = gamePlaytime.lastTick;
const delta = performance.now() - zero;
2024-04-21 08:26:29 +03:00
await gameRepository.update(game.id, {
playTimeInMilliseconds: game.playTimeInMilliseconds + delta,
lastTimePlayed: new Date(),
});
2024-04-21 08:26:29 +03:00
2024-06-15 08:15:58 +03:00
gamesPlaytime.set(game.id, {
...gamePlaytime,
lastTick: performance.now(),
});
} else {
if (game.remoteId) {
2024-06-17 05:43:18 +03:00
updateGamePlaytime(game, 0, new Date());
2024-06-15 08:15:58 +03:00
} else {
2024-06-17 05:43:18 +03:00
createGame({ ...game, lastTimePlayed: new Date() }).then(
(response) => {
const { id: remoteId } = response.data;
gameRepository.update({ objectID: game.objectID }, { remoteId });
}
);
2024-06-15 08:15:58 +03:00
}
gamesPlaytime.set(game.id, {
lastTick: performance.now(),
firstTick: performance.now(),
});
}
} else if (gamesPlaytime.has(game.id)) {
2024-06-15 08:15:58 +03:00
const gamePlaytime = gamesPlaytime.get(game.id)!;
gamesPlaytime.delete(game.id);
2024-04-21 08:26:29 +03:00
2024-06-15 08:15:58 +03:00
if (game.remoteId) {
2024-06-17 05:43:18 +03:00
updateGamePlaytime(
game,
performance.now() - gamePlaytime.firstTick,
game.lastTimePlayed!
);
2024-06-15 08:15:58 +03:00
} else {
2024-06-17 05:43:18 +03:00
createGame(game).then((response) => {
2024-06-15 08:15:58 +03:00
const { id: remoteId } = response.data;
gameRepository.update({ objectID: game.objectID }, { remoteId });
});
}
2024-04-21 08:26:29 +03:00
}
}
if (WindowManager.mainWindow) {
2024-06-20 04:05:22 +03:00
const runningGames = Array.from(gamesPlaytime.entries()).map((entry) => {
2024-06-20 17:27:09 +03:00
return {
id: entry[0],
2024-06-20 17:48:26 +03:00
sessionDurationInMillis: performance.now() - entry[1].firstTick,
2024-06-20 17:27:09 +03:00
};
2024-06-20 04:05:22 +03:00
});
WindowManager.mainWindow.webContents.send(
"on-games-running",
2024-06-20 04:05:22 +03:00
runningGames as RunningGameEvent
);
}
2024-04-21 08:26:29 +03:00
};