feat: check updates for installed games

This commit is contained in:
Moyasee
2025-11-11 16:30:23 +02:00
parent 1521d7c058
commit a1eef4eab6

View File

@@ -5,6 +5,7 @@ import {
updateDownloadSourcesCheckBaseline, updateDownloadSourcesCheckBaseline,
updateDownloadSourcesSinceValue, updateDownloadSourcesSinceValue,
downloadSourcesSublevel, downloadSourcesSublevel,
levelKeys,
} from "@main/level"; } from "@main/level";
import { logger } from "./logger"; import { logger } from "./logger";
import { WindowManager } from "./window-manager"; import { WindowManager } from "./window-manager";
@@ -47,34 +48,49 @@ export class DownloadSourcesChecker {
} }
private static async processApiResponse( private static async processApiResponse(
response: unknown, response: unknown
nonCustomGames: Game[]
): Promise<{ gameId: string; count: number }[]> { ): Promise<{ gameId: string; count: number }[]> {
if (!response || !Array.isArray(response)) { if (!response || !Array.isArray(response)) {
return []; return [];
} }
const gamesWithNewOptions: { gameId: string; count: number }[] = []; const gamesWithNewOptions: { gameId: string; count: number }[] = [];
const responseArray = response as DownloadSourcesChangeResponse[];
const gamesWithUpdates = responseArray.filter(
(update) => update.newDownloadOptionsCount > 0
);
for (const gameUpdate of response as DownloadSourcesChangeResponse[]) { logger.info(
if (gameUpdate.newDownloadOptionsCount > 0) { `API returned ${gamesWithUpdates.length} games with new download options (out of ${responseArray.length} total updates)`
const game = nonCustomGames.find( );
(g) =>
g.shop === gameUpdate.shop && g.objectId === gameUpdate.objectId for (const gameUpdate of gamesWithUpdates) {
const gameKey = levelKeys.game(gameUpdate.shop, gameUpdate.objectId);
const game = await gamesSublevel.get(gameKey).catch(() => null);
if (!game) {
logger.info(
`Skipping update for ${gameKey} - game not found in database`
); );
continue;
if (game) {
await gamesSublevel.put(`${game.shop}:${game.objectId}`, {
...game,
newDownloadOptionsCount: gameUpdate.newDownloadOptionsCount,
});
gamesWithNewOptions.push({
gameId: `${game.shop}:${game.objectId}`,
count: gameUpdate.newDownloadOptionsCount,
});
}
} }
if (game.shop === "custom") {
logger.info(
`Skipping update for ${gameKey} - custom games are excluded`
);
continue;
}
await gamesSublevel.put(gameKey, {
...game,
newDownloadOptionsCount: gameUpdate.newDownloadOptionsCount,
});
gamesWithNewOptions.push({
gameId: gameKey,
count: gameUpdate.newDownloadOptionsCount,
});
} }
return gamesWithNewOptions; return gamesWithNewOptions;
@@ -173,10 +189,7 @@ export class DownloadSourcesChecker {
`Updated baseline to: ${now} (will be 'since' on next app start)` `Updated baseline to: ${now} (will be 'since' on next app start)`
); );
const gamesWithNewOptions = await this.processApiResponse( const gamesWithNewOptions = await this.processApiResponse(response);
response,
nonCustomGames
);
this.sendNewDownloadOptionsEvent(clearedPayload, gamesWithNewOptions); this.sendNewDownloadOptionsEvent(clearedPayload, gamesWithNewOptions);