fix: changing get random game to work with shop block

This commit is contained in:
Hydra 2024-05-13 09:35:29 +01:00
parent 2475d48bc4
commit 5ab51e4547
No known key found for this signature in database
6 changed files with 58 additions and 42 deletions

View file

@ -1,9 +1,10 @@
import { shuffle } from "lodash-es"; import { shuffle } from "lodash-es";
import { Steam250Game, getSteam250List } from "@main/services"; import { 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 type { Steam250Game } from "@types";
const state = { games: Array<Steam250Game>(), index: 0 }; const state = { games: Array<Steam250Game>(), index: 0 };
@ -25,8 +26,6 @@ const getRandomGame = async (_event: Electron.IpcMainInvokeEvent) => {
return ""; return "";
} }
const resultObjectId = state.games[state.index].objectID;
state.index += 1; state.index += 1;
if (state.index == state.games.length) { if (state.index == state.games.length) {
@ -34,7 +33,7 @@ const getRandomGame = async (_event: Electron.IpcMainInvokeEvent) => {
state.games = shuffle(state.games); state.games = shuffle(state.games);
} }
return resultObjectId; return state.games[state.index];
}; };
registerEvent(getRandomGame, { registerEvent(getRandomGame, {

View file

@ -1,10 +1,7 @@
import axios from "axios"; import axios from "axios";
import { JSDOM } from "jsdom"; import { JSDOM } from "jsdom";
export interface Steam250Game { import type { Steam250Game } from "@types";
title: string;
objectID: string;
}
export const requestSteam250 = async (path: string) => { export const requestSteam250 = async (path: string) => {
return axios return axios

View file

@ -6,6 +6,7 @@ import type {
GameShop, GameShop,
HowLongToBeatCategory, HowLongToBeatCategory,
ShopDetails, ShopDetails,
Steam250Game,
TorrentProgress, TorrentProgress,
UserPreferences, UserPreferences,
} from "@types"; } from "@types";
@ -41,7 +42,7 @@ declare global {
shop: GameShop, shop: GameShop,
language: string language: string
) => Promise<ShopDetails | null>; ) => Promise<ShopDetails | null>;
getRandomGame: () => Promise<string>; getRandomGame: () => Promise<Steam250Game>;
getHowLongToBeat: ( getHowLongToBeat: (
objectID: string, objectID: string,
shop: GameShop, shop: GameShop,

View file

@ -3,11 +3,21 @@ import { average } from "color.js";
import { useCallback, useEffect, useState } from "react"; import { useCallback, useEffect, useState } from "react";
import { useNavigate, useParams, useSearchParams } from "react-router-dom"; import { useNavigate, useParams, useSearchParams } from "react-router-dom";
import type { Game, GameRepack, GameShop, ShopDetails } from "@types"; import {
Steam250Game,
type Game,
type GameRepack,
type GameShop,
type ShopDetails,
} from "@types";
import { Button } from "@renderer/components"; import { Button } from "@renderer/components";
import { setHeaderTitle } from "@renderer/features"; import { setHeaderTitle } from "@renderer/features";
import { getSteamLanguage, steamUrlBuilder } from "@renderer/helpers"; import {
buildGameDetailsPath,
getSteamLanguage,
steamUrlBuilder,
} from "@renderer/helpers";
import { useAppDispatch, useDownload } from "@renderer/hooks"; import { useAppDispatch, useDownload } from "@renderer/hooks";
import starsAnimation from "@renderer/assets/lottie/stars.json"; import starsAnimation from "@renderer/assets/lottie/stars.json";
@ -35,7 +45,7 @@ export function GameDetails() {
const { objectID, shop } = useParams(); const { objectID, shop } = useParams();
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [isLoadingRandomGame, setIsLoadingRandomGame] = useState(false); const [randomGame, setRandomGame] = useState<Steam250Game | null>(null);
const [color, setColor] = useState({ dark: "", light: "" }); const [color, setColor] = useState({ dark: "", light: "" });
const [gameDetails, setGameDetails] = useState<ShopDetails | null>(null); const [gameDetails, setGameDetails] = useState<ShopDetails | null>(null);
const [repacks, setRepacks] = useState<GameRepack[]>([]); const [repacks, setRepacks] = useState<GameRepack[]>([]);
@ -87,6 +97,10 @@ export function GameDetails() {
setIsGamePlaying(false); setIsGamePlaying(false);
dispatch(setHeaderTitle(title)); dispatch(setHeaderTitle(title));
window.electron.getRandomGame().then((randomGame) => {
setRandomGame(randomGame);
});
Promise.all([ Promise.all([
window.electron.getGameShopDetails( window.electron.getGameShopDetails(
objectID!, objectID!,
@ -98,7 +112,6 @@ export function GameDetails() {
.then(([appDetails, repacks]) => { .then(([appDetails, repacks]) => {
if (appDetails) setGameDetails(appDetails); if (appDetails) setGameDetails(appDetails);
setRepacks(repacks); setRepacks(repacks);
setIsLoadingRandomGame(false);
}) })
.finally(() => { .finally(() => {
setIsLoading(false); setIsLoading(false);
@ -163,15 +176,15 @@ export function GameDetails() {
}); });
}; };
const handleRandomizerClick = async () => { const handleRandomizerClick = () => {
setIsLoadingRandomGame(true); if (randomGame) {
const randomGameObjectID = await window.electron.getRandomGame(); navigate(
buildGameDetailsPath(
const searchParams = new URLSearchParams({ { ...randomGame, shop: "steam" },
fromRandomizer: "1", { fromRandomizer: "1" }
}); )
);
navigate(`/game/steam/${randomGameObjectID}?${searchParams.toString()}`); }
}; };
return ( return (
@ -254,7 +267,7 @@ export function GameDetails() {
className={styles.randomizerButton} className={styles.randomizerButton}
onClick={handleRandomizerClick} onClick={handleRandomizerClick}
theme="outline" theme="outline"
disabled={isLoadingRandomGame} disabled={!randomGame}
> >
<div style={{ width: 16, height: 16, position: "relative" }}> <div style={{ width: 16, height: 16, position: "relative" }}>
<Lottie <Lottie

View file

@ -1,11 +1,15 @@
import { useCallback, useEffect, useRef, useState } from "react"; import { useCallback, useEffect, useState } from "react";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { useNavigate, useSearchParams } from "react-router-dom"; import { useNavigate, useSearchParams } from "react-router-dom";
import Skeleton, { SkeletonTheme } from "react-loading-skeleton"; import Skeleton, { SkeletonTheme } from "react-loading-skeleton";
import { Button, GameCard, Hero } from "@renderer/components"; import { Button, GameCard, Hero } from "@renderer/components";
import type { CatalogueCategory, CatalogueEntry } from "@types"; import {
Steam250Game,
type CatalogueCategory,
type CatalogueEntry,
} from "@types";
import starsAnimation from "@renderer/assets/lottie/stars.json"; import starsAnimation from "@renderer/assets/lottie/stars.json";
@ -21,8 +25,7 @@ export function Home() {
const navigate = useNavigate(); const navigate = useNavigate();
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [isLoadingRandomGame, setIsLoadingRandomGame] = useState(false); const [randomGame, setRandomGame] = useState<Steam250Game | null>(null);
const randomGameObjectID = useRef<string | null>(null);
const [searchParams] = useSearchParams(); const [searchParams] = useSearchParams();
@ -57,24 +60,22 @@ export function Home() {
}; };
const getRandomGame = useCallback(() => { const getRandomGame = useCallback(() => {
setIsLoadingRandomGame(true); window.electron.getRandomGame().then((game) => {
if (game) setRandomGame(game);
window.electron.getRandomGame().then((objectID) => {
if (objectID) {
randomGameObjectID.current = objectID;
setIsLoadingRandomGame(false);
}
}); });
}, []); }, []);
const handleRandomizerClick = () => { const handleRandomizerClick = () => {
const searchParams = new URLSearchParams({ if (randomGame) {
fromRandomizer: "1", navigate(
}); buildGameDetailsPath(
{ ...randomGame, shop: "steam" },
navigate( {
`/game/steam/${randomGameObjectID.current}?${searchParams.toString()}` fromRandomizer: "1",
); }
)
);
}
}; };
useEffect(() => { useEffect(() => {
@ -106,7 +107,7 @@ export function Home() {
<Button <Button
onClick={handleRandomizerClick} onClick={handleRandomizerClick}
theme="outline" theme="outline"
disabled={isLoadingRandomGame} disabled={!randomGame}
> >
<div style={{ width: 16, height: 16, position: "relative" }}> <div style={{ width: 16, height: 16, position: "relative" }}>
<Lottie <Lottie

View file

@ -133,3 +133,8 @@ export interface HowLongToBeatCategory {
duration: string; duration: string;
accuracy: string; accuracy: string;
} }
export interface Steam250Game {
title: string;
objectID: string;
}