mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-02-13 11:42:10 +00:00
feat: get all games on first request
This commit is contained in:
parent
5a24f5ba29
commit
d7810eec38
2 changed files with 37 additions and 15 deletions
|
@ -1,27 +1,44 @@
|
||||||
import shuffle from "lodash/shuffle";
|
import shuffle from "lodash/shuffle";
|
||||||
|
|
||||||
import { getRandomSteam250List } from "@main/services";
|
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";
|
import { formatName } from "@main/helpers";
|
||||||
|
|
||||||
|
let gamesList = new Array<Steam250Game>();
|
||||||
|
let nextGameIndex = 0;
|
||||||
|
|
||||||
const getRandomGame = async (_event: Electron.IpcMainInvokeEvent) => {
|
const getRandomGame = async (_event: Electron.IpcMainInvokeEvent) => {
|
||||||
return getRandomSteam250List().then(async (games) => {
|
if (gamesList.length == 0) {
|
||||||
const shuffledList = shuffle(games);
|
console.log("fetching steam 250 pages");
|
||||||
|
gamesList = shuffle(await getSteam250List());
|
||||||
|
} else {
|
||||||
|
console.log("getting cached list");
|
||||||
|
}
|
||||||
|
|
||||||
for (const game of shuffledList) {
|
let resultObjectId = "";
|
||||||
const repacks = searchRepacks(formatName(game.title));
|
|
||||||
|
|
||||||
if (repacks.length) {
|
while (!resultObjectId) {
|
||||||
const results = await searchGames({ query: game.title });
|
const game = gamesList[nextGameIndex];
|
||||||
|
const repacks = searchRepacks(formatName(game.title));
|
||||||
|
|
||||||
if (results.length) {
|
if (repacks.length) {
|
||||||
return results[0].objectID;
|
const results = await searchGames({ query: game.title });
|
||||||
}
|
|
||||||
|
if (results.length) {
|
||||||
|
resultObjectId = results[0].objectID;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
nextGameIndex += 1;
|
||||||
|
|
||||||
|
if (nextGameIndex == gamesList.length - 1) {
|
||||||
|
nextGameIndex = 0;
|
||||||
|
gamesList = shuffle(gamesList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return resultObjectId;
|
||||||
};
|
};
|
||||||
|
|
||||||
registerEvent(getRandomGame, {
|
registerEvent(getRandomGame, {
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { JSDOM } from "jsdom";
|
import { JSDOM } from "jsdom";
|
||||||
import shuffle from "lodash/shuffle";
|
|
||||||
|
export interface Steam250Game {
|
||||||
|
title: string;
|
||||||
|
objectID: string;
|
||||||
|
}
|
||||||
|
|
||||||
export const requestSteam250 = async (path: string) => {
|
export const requestSteam250 = async (path: string) => {
|
||||||
return axios.get(`https://steam250.com${path}`).then((response) => {
|
return axios.get(`https://steam250.com${path}`).then((response) => {
|
||||||
|
@ -28,7 +32,8 @@ const steam250Paths = [
|
||||||
"/most_played",
|
"/most_played",
|
||||||
];
|
];
|
||||||
|
|
||||||
export const getRandomSteam250List = async () => {
|
export const getSteam250List = async () => {
|
||||||
const [path] = shuffle(steam250Paths);
|
const gamesPromises = steam250Paths.map((path) => requestSteam250(path));
|
||||||
return requestSteam250(path);
|
const gamesList = (await Promise.all(gamesPromises)).flat();
|
||||||
|
return [...new Set(gamesList)];
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue