mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-02-13 11:42:10 +00:00
refactor get-random-game
This commit is contained in:
parent
d84c665caf
commit
0759813ed5
1 changed files with 18 additions and 20 deletions
|
@ -4,37 +4,35 @@ 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";
|
||||||
import { formatName } from "@main/helpers";
|
|
||||||
|
|
||||||
let gamesList: Steam250Game[] = [];
|
let gamesList: Steam250Game[] = [];
|
||||||
let nextGameIndex = 0;
|
let nextGameIndex = 0;
|
||||||
|
|
||||||
const getRandomGame = async (_event: Electron.IpcMainInvokeEvent) => {
|
const getRandomGame = async (_event: Electron.IpcMainInvokeEvent) => {
|
||||||
if (gamesList.length == 0) {
|
if (gamesList.length == 0) {
|
||||||
console.log("fetching steam 250 pages");
|
const steam250List = await getSteam250List();
|
||||||
gamesList = shuffle(await getSteam250List());
|
|
||||||
} else {
|
const filteredSteam250List = steam250List.filter((game) => {
|
||||||
console.log("getting cached list");
|
const repacks = searchRepacks(game.title);
|
||||||
|
const catalogue = searchGames({ query: game.title });
|
||||||
|
|
||||||
|
return repacks.length && catalogue.length;
|
||||||
|
});
|
||||||
|
|
||||||
|
gamesList = shuffle(filteredSteam250List);
|
||||||
}
|
}
|
||||||
|
|
||||||
let resultObjectId = "";
|
if (gamesList.length == 0) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
while (!resultObjectId) {
|
const resultObjectId = gamesList[nextGameIndex].objectID;
|
||||||
const nextGame = gamesList[nextGameIndex];
|
|
||||||
const repacks = searchRepacks(formatName(nextGame.title));
|
|
||||||
|
|
||||||
if (repacks.length) {
|
nextGameIndex += 1;
|
||||||
const catalogueResults = await searchGames({ query: nextGame.title });
|
|
||||||
if (catalogueResults.length) {
|
|
||||||
resultObjectId = nextGame.objectID;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
nextGameIndex += 1;
|
|
||||||
|
|
||||||
if (nextGameIndex == gamesList.length) {
|
if (nextGameIndex == gamesList.length) {
|
||||||
nextGameIndex = 0;
|
nextGameIndex = 0;
|
||||||
gamesList = shuffle(gamesList);
|
gamesList = shuffle(gamesList);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return resultObjectId;
|
return resultObjectId;
|
||||||
|
|
Loading…
Reference in a new issue