mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-03-09 15:40:26 +00:00
first commit
This commit is contained in:
commit
f1bdec484e
165 changed files with 20993 additions and 0 deletions
72
src/main/events/helpers/search-games.ts
Normal file
72
src/main/events/helpers/search-games.ts
Normal file
|
@ -0,0 +1,72 @@
|
|||
import flexSearch from "flexsearch";
|
||||
import orderBy from "lodash/orderBy";
|
||||
|
||||
import type { GameRepack, GameShop, CatalogueEntry } from "@types";
|
||||
|
||||
import { formatName, getSteamAppAsset, repackerFormatter } from "@main/helpers";
|
||||
import { searchSteamGame } from "@main/services";
|
||||
import { stateManager } from "@main/state-manager";
|
||||
|
||||
const { Index } = flexSearch;
|
||||
const repacksIndex = new Index();
|
||||
|
||||
const repacks = stateManager.getValue("repacks");
|
||||
|
||||
for (let i = 0; i < repacks.length; i++) {
|
||||
const repack = repacks[i];
|
||||
const formatter =
|
||||
repackerFormatter[repack.repacker as keyof typeof repackerFormatter];
|
||||
|
||||
repacksIndex.add(i, formatName(formatter(repack.title)));
|
||||
}
|
||||
|
||||
export const searchRepacks = (title: string): GameRepack[] => {
|
||||
const repacks = stateManager.getValue("repacks");
|
||||
|
||||
return orderBy(
|
||||
repacksIndex
|
||||
.search(formatName(title))
|
||||
.map((index) => repacks.at(index as number)!),
|
||||
["uploadDate"],
|
||||
"desc"
|
||||
);
|
||||
};
|
||||
|
||||
export const searchGames = async (query: string): Promise<CatalogueEntry[]> => {
|
||||
const formattedName = formatName(query);
|
||||
|
||||
const steamResults = await searchSteamGame(formattedName);
|
||||
|
||||
const results = steamResults.map((result) => ({
|
||||
objectID: result.objectID,
|
||||
title: result.name,
|
||||
shop: "steam" as GameShop,
|
||||
cover: getSteamAppAsset("library", result.objectID),
|
||||
}));
|
||||
|
||||
const gamesIndex = new Index({
|
||||
tokenize: "full",
|
||||
});
|
||||
|
||||
for (let i = 0; i < results.length; i++) {
|
||||
const game = results[i];
|
||||
gamesIndex.add(i, game.title);
|
||||
}
|
||||
|
||||
const filteredResults = gamesIndex
|
||||
.search(query)
|
||||
.map((index) => results[index as number]);
|
||||
|
||||
return Promise.all(
|
||||
filteredResults.map(async (result) => ({
|
||||
...result,
|
||||
repacks: searchRepacks(result.title),
|
||||
}))
|
||||
).then((resultsWithRepacks) =>
|
||||
orderBy(
|
||||
resultsWithRepacks,
|
||||
[({ repacks }) => repacks.length, "repacks"],
|
||||
["desc"]
|
||||
)
|
||||
);
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue