Skip to content

Commit

Permalink
finished refactoring game identifier
Browse files Browse the repository at this point in the history
# refactored/updated game.service.js also
# added appropriate tests
# removed unused tests
# removed unused imports
# removed unused triedVia steam app methods
# updated steam app tests
  • Loading branch information
lukatarman committed Jan 16, 2024
1 parent 174596a commit cb0a73e
Show file tree
Hide file tree
Showing 7 changed files with 305 additions and 503 deletions.
106 changes: 56 additions & 50 deletions backend/src/features/game-identifier/game.identifier.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
updateMissingDetails,
updateMissingReleaseDates,
recordAttemptsViaSteamDb,
recordHtmlAttempts,
getGames,
getIds,
Expand Down Expand Up @@ -61,10 +59,7 @@ export class GameIdentifier {
}

async #identifyTypes(steamApps, source) {
const htmlDetailsPages = await this.#getSteamAppsHtmlDetailsPagesNR64(
steamApps,
source,
);
const htmlDetailsPages = await this.#getSteamAppsHtmlDetailsPages(steamApps, source);

const updatedSteamApps = recordHtmlAttempts(steamApps, htmlDetailsPages, source);

Expand All @@ -73,7 +68,7 @@ export class GameIdentifier {
return [games, updatedSteamApps];
}

async #getSteamAppsHtmlDetailsPagesNR64(steamApps, source) {
async #getSteamAppsHtmlDetailsPages(steamApps, source) {
const detailsPages = [];

for (let steamApp of steamApps) {
Expand Down Expand Up @@ -104,34 +99,48 @@ export class GameIdentifier {
this.#options.batchSize,
);

if (games.length === 0) {
this.#logger.debugc(
`no games without details in db, retrying in ${
this.#options.globalIterationDelay
} ms`,
);
return;
}
if (this.#gamesIsEmpty(games, "details")) return;

const steamApps = await this.#steamAppsRepository.getSteamAppsById(getIds(games));

const steamApps = await this.#steamAppsRepository.getSteamAppsById(
games.map((game) => game.id),
const [updatedGames, updatedSteamApps] = await this.#updateMissingDetails(
games,
steamApps,
);

const htmlDetailsPages = await this.#getSteamDbHtmlDetailsPages(games);
this.#persistUpdatedDetails(updatedGames, updatedSteamApps);
};

const updatedApps = recordAttemptsViaSteamDb(steamApps, htmlDetailsPages);
#gamesIsEmpty = (games, message) => {
if (games.length === 0) return true;

updateMissingDetails(games, htmlDetailsPages);
this.#logger.debugc(
`no games without ${message} in db, retrying in ${
this.#options.globalIterationDelay
} ms`,
);

this.#persistMissingProperties(games, updatedApps);
return false;
};

async #getSteamDbHtmlDetailsPages(games) {
async #updateMissingDetails(games, steamApps) {
const source = ValidDataSources.validDataSources.steamDb;

const htmlDetailsPages = await this.#getGamesHtmlDetailsPages(games, source);

const updatedSteamApps = recordAttemptsViaSource(steamApps, htmlDetailsPages, source);

const updatedGames = updateGamesMissingDetails(games, htmlDetailsPages);

return [updatedGames, updatedSteamApps];
}

async #getGamesHtmlDetailsPages(games, source) {
const htmlDetailsPages = [];

for (let game of games) {
// TODO https://github.com/lukatarman/steam-game-stats/issues/192
const htmlPage = await this.#steamClient.getSteamDbHtmlDetailsPage(game.id);
const htmlPage = await this.#steamClient.getSourceHtmlDetailsPage(game.id, source);
htmlDetailsPages.push({ page: htmlPage, id: game.id });

await delay(this.#options.unitDelay);
Expand All @@ -140,11 +149,9 @@ export class GameIdentifier {
return htmlDetailsPages;
}

async #persistMissingProperties(games, appsWithoutPages) {
if (appsWithoutPages.length !== 0) {
this.#logger.debugc(`persisting ${appsWithoutPages.length} apps without pages`);
this.#steamAppsRepository.updateSteamAppsById(appsWithoutPages);
}
async #persistUpdatedDetails(games, updatedApps) {
this.#logger.debugc(`persisting ${updatedApps.length} apps without pages`);
this.#steamAppsRepository.updateSteamAppsById(updatedApps);

this.#logger.debugc(`persisting ${games.length} games with updated details`);
await this.#gamesRepository.updateGameDetails(games);
Expand All @@ -157,36 +164,35 @@ export class GameIdentifier {
this.#options.batchSize,
);

if (games.length === 0) {
this.#logger.debugc(
`no games without release dates in db, retrying in ${
this.#options.iterationDelay
} ms`,
);
return;
}
if (this.#gamesIsEmpty(games, "release dates")) return;

const steamApps = await this.#steamAppsRepository.getSteamAppsById(
games.map((game) => game.id),
const steamApps = await this.#steamAppsRepository.getSteamAppsById(getIds(games));

const [updatedGames, updatedSteamApps] = await this.#updateMissingReleaseDates(
games,
steamApps,
);

const htmlDetailsPages = await this.#getSteamDbHtmlDetailsPages(games);
this.#persistReleaseDates(updatedGames, updatedSteamApps);
};

const updatedApps = recordAttemptsViaSteamDb(steamApps, htmlDetailsPages);
async #updateMissingReleaseDates(games, steamApps) {
const source = ValidDataSources.validDataSources.steamDb;

updateMissingReleaseDates(games, htmlDetailsPages);
const htmlDetailsPages = await this.#getGamesHtmlDetailsPages(games, source);

this.#persistReleaseDates(games, updatedApps);
};
const updatedSteamApps = recordAttemptsViaSource(steamApps, htmlDetailsPages, source);

#persistReleaseDates = async (games, appsWithoutPages) => {
if (appsWithoutPages.length !== 0) {
this.#logger.debugc(`persisting ${appsWithoutPages.length} apps without pages`);
this.#steamAppsRepository.updateSteamAppsById(appsWithoutPages);
}
const updatedGames = updateMissingReleaseDates(games, htmlDetailsPages);

this.#logger.debugc(`persisting ${games.length} games with updated release dates`);
return [updatedGames, updatedSteamApps];
}

async #persistReleaseDates(games, steamApps) {
this.#logger.debugc(`persisting ${steamApps.length} apps without pages`);
this.#steamAppsRepository.updateSteamAppsById(steamApps);

this.#logger.debugc(`persisting ${games.length} games with updated release dates`);
await this.#gamesRepository.updateReleaseDates(games);
};
}
}
Loading

0 comments on commit cb0a73e

Please sign in to comment.