From a1eef4eab6110d06dead73153d58760ed82a82da Mon Sep 17 00:00:00 2001 From: Moyasee Date: Tue, 11 Nov 2025 16:30:23 +0200 Subject: [PATCH] feat: check updates for installed games --- src/main/services/download-sources-checker.ts | 59 +++++++++++-------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/src/main/services/download-sources-checker.ts b/src/main/services/download-sources-checker.ts index b3675829..c0997a0f 100644 --- a/src/main/services/download-sources-checker.ts +++ b/src/main/services/download-sources-checker.ts @@ -5,6 +5,7 @@ import { updateDownloadSourcesCheckBaseline, updateDownloadSourcesSinceValue, downloadSourcesSublevel, + levelKeys, } from "@main/level"; import { logger } from "./logger"; import { WindowManager } from "./window-manager"; @@ -47,34 +48,49 @@ export class DownloadSourcesChecker { } private static async processApiResponse( - response: unknown, - nonCustomGames: Game[] + response: unknown ): Promise<{ gameId: string; count: number }[]> { if (!response || !Array.isArray(response)) { return []; } 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[]) { - if (gameUpdate.newDownloadOptionsCount > 0) { - const game = nonCustomGames.find( - (g) => - g.shop === gameUpdate.shop && g.objectId === gameUpdate.objectId + logger.info( + `API returned ${gamesWithUpdates.length} games with new download options (out of ${responseArray.length} total updates)` + ); + + 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` ); - - if (game) { - await gamesSublevel.put(`${game.shop}:${game.objectId}`, { - ...game, - newDownloadOptionsCount: gameUpdate.newDownloadOptionsCount, - }); - - gamesWithNewOptions.push({ - gameId: `${game.shop}:${game.objectId}`, - count: gameUpdate.newDownloadOptionsCount, - }); - } + continue; } + + 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; @@ -173,10 +189,7 @@ export class DownloadSourcesChecker { `Updated baseline to: ${now} (will be 'since' on next app start)` ); - const gamesWithNewOptions = await this.processApiResponse( - response, - nonCustomGames - ); + const gamesWithNewOptions = await this.processApiResponse(response); this.sendNewDownloadOptionsEvent(clearedPayload, gamesWithNewOptions);