chore: merge with main

This commit is contained in:
Hydra 2024-04-30 03:30:48 +01:00
commit 5b74ef512a
6 changed files with 91 additions and 72 deletions

View file

@ -1,29 +1,40 @@
import { shuffle } from "lodash-es";
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 () => {
return getRandomSteam250List().then(async (games) => {
const shuffledList = shuffle(games);
const state = { games: Array<Steam250Game>(), index: 0 };
for (const game of shuffledList) {
const repacks = searchRepacks(formatName(game.title));
const getRandomGame = async (_event: Electron.IpcMainInvokeEvent) => {
if (state.games.length == 0) {
const steam250List = await getSteam250List();
if (repacks.length) {
const results = await searchGames({ query: game.title });
const filteredSteam250List = steam250List.filter((game) => {
const repacks = searchRepacks(game.title);
const catalogue = searchGames({ query: game.title });
if (results.length) {
return results[0].objectID;
}
}
}
return repacks.length && catalogue.length;
});
return null;
});
state.games = shuffle(filteredSteam250List);
}
if (state.games.length == 0) {
return "";
}
const resultObjectId = state.games[state.index].objectID;
state.index += 1;
if (state.index == state.games.length) {
state.index = 0;
state.games = shuffle(state.games);
}
return resultObjectId;
};
registerEvent(getRandomGame, {

View file

@ -1,11 +1,15 @@
import { registerEvent } from "../register-event";
import { searchGames } from "../helpers/search-games";
import { CatalogueEntry } from "@types";
registerEvent(
(_event: Electron.IpcMainInvokeEvent, query: string) =>
searchGames({ query, take: 12 }),
{
name: "searchGames",
memoize: true,
}
);
const searchGamesEvent = async (
_event: Electron.IpcMainInvokeEvent,
query: string
): Promise<CatalogueEntry[]> => {
return searchGames({ query, take: 12 });
};
registerEvent(searchGamesEvent, {
name: "searchGames",
memoize: true,
});

View file

@ -42,11 +42,11 @@ export interface SearchGamesArgs {
skip?: number;
}
export const searchGames = async ({
export const searchGames = ({
query,
take,
skip,
}: SearchGamesArgs): Promise<CatalogueEntry[]> => {
}: SearchGamesArgs): CatalogueEntry[] => {
const results = steamGamesIndex
.search(formatName(query || ""), { limit: take, offset: skip })
.map((index) => {
@ -61,11 +61,9 @@ export const searchGames = async ({
};
});
return Promise.all(results).then((resultsWithRepacks) =>
orderBy(
resultsWithRepacks,
[({ repacks }) => repacks.length, "repacks"],
["desc"]
)
return orderBy(
results,
[({ repacks }) => repacks.length, "repacks"],
["desc"]
);
};

View file

@ -1,23 +1,31 @@
import axios from "axios";
import { JSDOM } from "jsdom";
import { shuffle } from "lodash-es";
export interface Steam250Game {
title: string;
objectID: string;
}
export const requestSteam250 = async (path: string) => {
return axios.get(`https://steam250.com${path}`).then((response) => {
const { window } = new JSDOM(response.data);
const { document } = window;
return axios
.get(`https://steam250.com${path}`)
.then((response) => {
const { window } = new JSDOM(response.data);
const { document } = window;
return Array.from(document.querySelectorAll(".appline .title a"))
.filter(($title) => Boolean(($title as HTMLAnchorElement).href))
.map(($title) => {
const steamGameUrl = ($title as HTMLAnchorElement).href;
return Array.from(document.querySelectorAll(".appline .title a"))
.map(($title) => {
const steamGameUrl = ($title as HTMLAnchorElement).href;
if (!steamGameUrl) return null;
return {
title: $title.textContent!,
objectID: steamGameUrl.split("/").pop()!,
};
});
});
return {
title: $title.textContent,
objectID: steamGameUrl.split("/").pop(),
} as Steam250Game;
})
.filter((game) => game != null);
})
.catch((_) => [] as Steam250Game[]);
};
const steam250Paths = [
@ -27,7 +35,15 @@ const steam250Paths = [
"/most_played",
];
export const getRandomSteam250List = async () => {
const [path] = shuffle(steam250Paths);
return requestSteam250(path);
export const getSteam250List = async () => {
const gamesList = (
await Promise.all(steam250Paths.map((path) => requestSteam250(path)))
).flat();
const gamesMap: Map<string, Steam250Game> = gamesList.reduce((map, item) => {
map.set(item.objectID, item);
return map;
}, new Map());
return [...gamesMap.values()];
};