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 { getRandomSteam250List } from "@main/services";
|
||||
import { Steam250Game, getSteam250List } from "@main/services";
|
||||
|
||||
import { registerEvent } from "../register-event";
|
||||
import { searchGames, searchRepacks } from "../helpers/search-games";
|
||||
import { formatName } from "@main/helpers";
|
||||
|
||||
const getRandomGame = async (_event: Electron.IpcMainInvokeEvent) => {
|
||||
return getRandomSteam250List().then(async (games) => {
|
||||
const shuffledList = shuffle(games);
|
||||
let gamesList = new Array<Steam250Game>();
|
||||
let nextGameIndex = 0;
|
||||
|
||||
for (const game of shuffledList) {
|
||||
const getRandomGame = async (_event: Electron.IpcMainInvokeEvent) => {
|
||||
if (gamesList.length == 0) {
|
||||
console.log("fetching steam 250 pages");
|
||||
gamesList = shuffle(await getSteam250List());
|
||||
} else {
|
||||
console.log("getting cached list");
|
||||
}
|
||||
|
||||
let resultObjectId = "";
|
||||
|
||||
while (!resultObjectId) {
|
||||
const game = gamesList[nextGameIndex];
|
||||
const repacks = searchRepacks(formatName(game.title));
|
||||
|
||||
if (repacks.length) {
|
||||
const results = await searchGames({ query: game.title });
|
||||
|
||||
if (results.length) {
|
||||
return results[0].objectID;
|
||||
resultObjectId = results[0].objectID;
|
||||
}
|
||||
}
|
||||
nextGameIndex += 1;
|
||||
|
||||
if (nextGameIndex == gamesList.length - 1) {
|
||||
nextGameIndex = 0;
|
||||
gamesList = shuffle(gamesList);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return resultObjectId;
|
||||
};
|
||||
|
||||
registerEvent(getRandomGame, {
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
import axios from "axios";
|
||||
import { JSDOM } from "jsdom";
|
||||
import shuffle from "lodash/shuffle";
|
||||
|
||||
export interface Steam250Game {
|
||||
title: string;
|
||||
objectID: string;
|
||||
}
|
||||
|
||||
export const requestSteam250 = async (path: string) => {
|
||||
return axios.get(`https://steam250.com${path}`).then((response) => {
|
||||
|
@ -28,7 +32,8 @@ const steam250Paths = [
|
|||
"/most_played",
|
||||
];
|
||||
|
||||
export const getRandomSteam250List = async () => {
|
||||
const [path] = shuffle(steam250Paths);
|
||||
return requestSteam250(path);
|
||||
export const getSteam250List = async () => {
|
||||
const gamesPromises = steam250Paths.map((path) => requestSteam250(path));
|
||||
const gamesList = (await Promise.all(gamesPromises)).flat();
|
||||
return [...new Set(gamesList)];
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue