changes after pr review

This commit is contained in:
Zamitto 2024-04-29 14:12:06 -03:00
parent e6cc7a2861
commit eb8dc38f1e
3 changed files with 13 additions and 13 deletions

View File

@ -5,11 +5,10 @@ import { Steam250Game, getSteam250List } from "@main/services";
import { registerEvent } from "../register-event"; import { registerEvent } from "../register-event";
import { searchGames, searchRepacks } from "../helpers/search-games"; import { searchGames, searchRepacks } from "../helpers/search-games";
let gamesList: Steam250Game[] = []; const state = { games: Array<Steam250Game>(), index: 0 };
let nextGameIndex = 0;
const getRandomGame = async (_event: Electron.IpcMainInvokeEvent) => { const getRandomGame = async (_event: Electron.IpcMainInvokeEvent) => {
if (gamesList.length == 0) { if (state.games.length == 0) {
const steam250List = await getSteam250List(); const steam250List = await getSteam250List();
const filteredSteam250List = steam250List.filter((game) => { const filteredSteam250List = steam250List.filter((game) => {
@ -19,20 +18,20 @@ const getRandomGame = async (_event: Electron.IpcMainInvokeEvent) => {
return repacks.length && catalogue.length; return repacks.length && catalogue.length;
}); });
gamesList = shuffle(filteredSteam250List); state.games = shuffle(filteredSteam250List);
} }
if (gamesList.length == 0) { if (state.games.length == 0) {
return ""; return "";
} }
const resultObjectId = gamesList[nextGameIndex].objectID; const resultObjectId = state.games[state.index].objectID;
nextGameIndex += 1; state.index += 1;
if (nextGameIndex == gamesList.length) { if (state.index == state.games.length) {
nextGameIndex = 0; state.index = 0;
gamesList = shuffle(gamesList); state.games = shuffle(state.games);
} }
return resultObjectId; return resultObjectId;

View File

@ -6,7 +6,7 @@ const searchGamesEvent = async (
_event: Electron.IpcMainInvokeEvent, _event: Electron.IpcMainInvokeEvent,
query: string query: string
): Promise<CatalogueEntry[]> => { ): Promise<CatalogueEntry[]> => {
return Promise.all(searchGames({ query, take: 12 })); return searchGames({ query, take: 12 });
}; };
registerEvent(searchGamesEvent, { registerEvent(searchGamesEvent, {

View File

@ -36,8 +36,9 @@ const steam250Paths = [
]; ];
export const getSteam250List = async () => { export const getSteam250List = async () => {
const gamesPromises = steam250Paths.map((path) => requestSteam250(path)); const gamesList = (
const gamesList = (await Promise.all(gamesPromises)).flat(); await Promise.all(steam250Paths.map((path) => requestSteam250(path)))
).flat();
const gamesMap: Map<string, Steam250Game> = gamesList.reduce((map, item) => { const gamesMap: Map<string, Steam250Game> = gamesList.reduce((map, item) => {
map.set(item.objectID, item); map.set(item.objectID, item);