refactor: merged game queries to a single query

This commit is contained in:
JackEnx 2024-12-16 18:52:11 -03:00
parent 431ad2ff2c
commit efcf778c95

View File

@ -1,4 +1,3 @@
import { IsNull, Not } from "typeorm";
import { gameRepository } from "@main/repository"; import { gameRepository } from "@main/repository";
import { WindowManager } from "./window-manager"; import { WindowManager } from "./window-manager";
import { createGame, updateGamePlaytime } from "./library-sync"; import { createGame, updateGamePlaytime } from "./library-sync";
@ -32,51 +31,33 @@ const gameExecutables = (
}) })
).data as GameExecutables; ).data as GameExecutables;
const gamesIdWithoutPath = async () => {
const games = await gameRepository.find({
where: {
executablePath: IsNull(),
isDeleted: false,
},
select: {
objectID: true,
},
});
return games
.filter((game) => gameExecutables[game.objectID])
.map((game) => game.objectID);
};
const findGamePathByProcess = ( const findGamePathByProcess = (
processMap: Map<string, Set<string>>, processMap: Map<string, Set<string>>,
gameIds: string[] gameId: string
) => { ) => {
for (const id of gameIds) { if (process.platform === "win32") {
if (process.platform === "win32") { const executables = gameExecutables[gameId].filter(
const executables = gameExecutables[id].filter( (info) => info.os === "win32"
(info) => info.os === "win32" );
);
for (const executable of executables) { for (const executable of executables) {
const exe = executable.name.slice(executable.name.lastIndexOf("/") + 1); const exe = executable.name.slice(executable.name.lastIndexOf("/") + 1);
if (!exe) continue; if (!exe) continue;
const pathSet = processMap.get(exe); const pathSet = processMap.get(exe);
if (pathSet) { if (pathSet) {
const executableName = executable.name.replace(/\//g, "\\"); const executableName = executable.name.replace(/\//g, "\\");
pathSet.forEach((path) => { pathSet.forEach((path) => {
if (path.toLowerCase().endsWith(executableName)) { if (path.toLowerCase().endsWith(executableName)) {
gameRepository.update( gameRepository.update(
{ objectID: id, shop: "steam" }, { objectID: gameId, shop: "steam" },
{ executablePath: path } { executablePath: path }
); );
} }
}); });
}
} }
} }
} }
@ -101,27 +82,25 @@ const getSystemProcessMap = async () => {
}; };
export const watchProcesses = async () => { export const watchProcesses = async () => {
const gameIds = await gamesIdWithoutPath();
const games = await gameRepository.find({ const games = await gameRepository.find({
where: { where: {
executablePath: Not(IsNull()),
isDeleted: false, isDeleted: false,
}, },
}); });
if (!games.length && !gameIds.length) return; if (!games.length) return;
const processMap = await getSystemProcessMap(); const processMap = await getSystemProcessMap();
findGamePathByProcess(processMap, gameIds);
if (!games.length) return;
for (const game of games) { for (const game of games) {
const executablePath = game.executablePath; const executablePath = game.executablePath;
if (!executablePath) continue; if (!executablePath) {
if (gameExecutables[game.objectID]) {
findGamePathByProcess(processMap, game.objectID);
}
continue;
}
const executable = executablePath.slice( const executable = executablePath.slice(
executablePath.lastIndexOf(process.platform === "win32" ? "\\" : "/") + 1 executablePath.lastIndexOf(process.platform === "win32" ? "\\" : "/") + 1