mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-01-23 13:34:54 +03:00
feat: refactor
This commit is contained in:
parent
3dcdcae9a7
commit
c8022896a6
@ -1,23 +1,81 @@
|
||||
import type {
|
||||
AchievementData,
|
||||
GameAchievement,
|
||||
GameShop,
|
||||
RemoteUnlockedAchievement,
|
||||
UnlockedAchievement,
|
||||
UserAchievement,
|
||||
} from "@types";
|
||||
import { registerEvent } from "../register-event";
|
||||
import {
|
||||
gameAchievementRepository,
|
||||
userAuthRepository,
|
||||
userPreferencesRepository,
|
||||
} from "@main/repository";
|
||||
import { getGameAchievementData } from "@main/services/achievements/get-game-achievement-data";
|
||||
import { HydraApi } from "@main/services";
|
||||
import { HydraApi, logger } from "@main/services";
|
||||
|
||||
const getAchievements = async (
|
||||
const getAchievementLocalUser = async (shop: string, objectId: string) => {
|
||||
const cachedAchievements = await gameAchievementRepository.findOne({
|
||||
where: { objectId, shop },
|
||||
});
|
||||
|
||||
const achievementsData: AchievementData[] = cachedAchievements?.achievements
|
||||
? JSON.parse(cachedAchievements.achievements)
|
||||
: await getGameAchievementData(objectId, shop);
|
||||
|
||||
const unlockedAchievements = JSON.parse(
|
||||
cachedAchievements?.unlockedAchievements || "[]"
|
||||
) as UnlockedAchievement[];
|
||||
|
||||
return achievementsData
|
||||
.map((achievementData) => {
|
||||
logger.info("unclockedAchievements", unlockedAchievements);
|
||||
|
||||
const unlockedAchiementData = unlockedAchievements.find(
|
||||
(localAchievement) => {
|
||||
return (
|
||||
localAchievement.name.toUpperCase() ==
|
||||
achievementData.name.toUpperCase()
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
const icongray = achievementData.icongray.endsWith("/")
|
||||
? achievementData.icon
|
||||
: achievementData.icongray;
|
||||
|
||||
if (unlockedAchiementData) {
|
||||
return {
|
||||
...achievementData,
|
||||
unlocked: true,
|
||||
unlockTime: unlockedAchiementData.unlockTime,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
...achievementData,
|
||||
unlocked: false,
|
||||
unlockTime: null,
|
||||
icon: icongray,
|
||||
} as UserAchievement;
|
||||
})
|
||||
.sort((a, b) => {
|
||||
if (a.unlocked && !b.unlocked) return -1;
|
||||
if (!a.unlocked && b.unlocked) return 1;
|
||||
if (a.unlocked && b.unlocked) {
|
||||
return b.unlockTime! - a.unlockTime!;
|
||||
}
|
||||
return Number(a.hidden) - Number(b.hidden);
|
||||
});
|
||||
};
|
||||
|
||||
const getAchievementsRemoteUser = async (
|
||||
shop: string,
|
||||
objectId: string,
|
||||
userId?: string
|
||||
userId: string
|
||||
) => {
|
||||
const userAuth = await userAuthRepository.findOne({ where: { userId } });
|
||||
const userPreferences = await userPreferencesRepository.findOne({
|
||||
where: { id: 1 },
|
||||
});
|
||||
|
||||
const cachedAchievements = await gameAchievementRepository.findOne({
|
||||
where: { objectId, shop },
|
||||
@ -27,31 +85,9 @@ const getAchievements = async (
|
||||
? JSON.parse(cachedAchievements.achievements)
|
||||
: await getGameAchievementData(objectId, shop);
|
||||
|
||||
if (!userId || userAuth) {
|
||||
const unlockedAchievements = JSON.parse(
|
||||
cachedAchievements?.unlockedAchievements || "[]"
|
||||
) as UnlockedAchievement[];
|
||||
|
||||
return { achievementsData, unlockedAchievements };
|
||||
}
|
||||
|
||||
const unlockedAchievements = await HydraApi.get<UnlockedAchievement[]>(
|
||||
const unlockedAchievements = await HydraApi.get<RemoteUnlockedAchievement[]>(
|
||||
`/users/${userId}/games/achievements`,
|
||||
{ shop, objectId, language: "en" }
|
||||
);
|
||||
|
||||
return { achievementsData, unlockedAchievements };
|
||||
};
|
||||
|
||||
export const getGameAchievements = async (
|
||||
objectId: string,
|
||||
shop: GameShop,
|
||||
userId?: string
|
||||
): Promise<GameAchievement[]> => {
|
||||
const { achievementsData, unlockedAchievements } = await getAchievements(
|
||||
shop,
|
||||
objectId,
|
||||
userId
|
||||
{ shop, objectId, language: userPreferences?.language || "en" }
|
||||
);
|
||||
|
||||
return achievementsData
|
||||
@ -74,7 +110,6 @@ export const getGameAchievements = async (
|
||||
...achievementData,
|
||||
unlocked: true,
|
||||
unlockTime: unlockedAchiementData.unlockTime,
|
||||
icongray,
|
||||
};
|
||||
}
|
||||
|
||||
@ -82,8 +117,8 @@ export const getGameAchievements = async (
|
||||
...achievementData,
|
||||
unlocked: false,
|
||||
unlockTime: null,
|
||||
icongray,
|
||||
} as GameAchievement;
|
||||
icon: icongray,
|
||||
} as UserAchievement;
|
||||
})
|
||||
.sort((a, b) => {
|
||||
if (a.unlocked && !b.unlocked) return -1;
|
||||
@ -95,12 +130,24 @@ export const getGameAchievements = async (
|
||||
});
|
||||
};
|
||||
|
||||
export const getGameAchievements = async (
|
||||
objectId: string,
|
||||
shop: GameShop,
|
||||
userId?: string
|
||||
): Promise<UserAchievement[]> => {
|
||||
if (!userId) {
|
||||
return getAchievementLocalUser(shop, objectId);
|
||||
}
|
||||
|
||||
return getAchievementsRemoteUser(shop, objectId, userId);
|
||||
};
|
||||
|
||||
const getGameAchievementsEvent = async (
|
||||
_event: Electron.IpcMainInvokeEvent,
|
||||
objectId: string,
|
||||
shop: GameShop,
|
||||
userId?: string
|
||||
): Promise<GameAchievement[]> => {
|
||||
): Promise<UserAchievement[]> => {
|
||||
return getGameAchievements(objectId, shop, userId);
|
||||
};
|
||||
|
||||
|
@ -12,11 +12,11 @@ import { useAppDispatch, useAppSelector, useDownload } from "@renderer/hooks";
|
||||
|
||||
import type {
|
||||
Game,
|
||||
GameAchievement,
|
||||
GameRepack,
|
||||
GameShop,
|
||||
GameStats,
|
||||
ShopDetails,
|
||||
UserAchievement,
|
||||
} from "@types";
|
||||
|
||||
import { useTranslation } from "react-i18next";
|
||||
@ -64,7 +64,7 @@ export function GameDetailsContextProvider({
|
||||
shop,
|
||||
}: GameDetailsContextProps) {
|
||||
const [shopDetails, setShopDetails] = useState<ShopDetails | null>(null);
|
||||
const [achievements, setAchievements] = useState<GameAchievement[]>([]);
|
||||
const [achievements, setAchievements] = useState<UserAchievement[]>([]);
|
||||
const [game, setGame] = useState<Game | null>(null);
|
||||
const [hasNSFWContentBlocked, setHasNSFWContentBlocked] = useState(false);
|
||||
|
||||
|
@ -1,10 +1,10 @@
|
||||
import type {
|
||||
Game,
|
||||
GameAchievement,
|
||||
GameRepack,
|
||||
GameShop,
|
||||
GameStats,
|
||||
ShopDetails,
|
||||
UserAchievement,
|
||||
} from "@types";
|
||||
|
||||
export interface GameDetailsContext {
|
||||
@ -20,7 +20,7 @@ export interface GameDetailsContext {
|
||||
showRepacksModal: boolean;
|
||||
showGameOptionsModal: boolean;
|
||||
stats: GameStats | null;
|
||||
achievements: GameAchievement[];
|
||||
achievements: UserAchievement[];
|
||||
hasNSFWContentBlocked: boolean;
|
||||
setGameColor: React.Dispatch<React.SetStateAction<string>>;
|
||||
selectGameExecutable: () => Promise<string | null>;
|
||||
|
3
src/renderer/src/declaration.d.ts
vendored
3
src/renderer/src/declaration.d.ts
vendored
@ -28,6 +28,7 @@ import type {
|
||||
GameAchievement,
|
||||
GameArtifact,
|
||||
LudusaviBackup,
|
||||
UserAchievement,
|
||||
} from "@types";
|
||||
import type { AxiosProgressEvent } from "axios";
|
||||
import type { DiskSpace } from "check-disk-space";
|
||||
@ -68,7 +69,7 @@ declare global {
|
||||
objectId: string,
|
||||
shop: GameShop,
|
||||
userId?: string
|
||||
) => Promise<GameAchievement[]>;
|
||||
) => Promise<UserAchievement[]>;
|
||||
onAchievementUnlocked: (
|
||||
cb: (
|
||||
objectId: string,
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { setHeaderTitle } from "@renderer/features";
|
||||
import { useAppDispatch, useDate } from "@renderer/hooks";
|
||||
import { steamUrlBuilder } from "@shared";
|
||||
import type { GameAchievement, GameShop } from "@types";
|
||||
import type { GameShop, UserAchievement } from "@types";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useNavigate, useSearchParams } from "react-router-dom";
|
||||
@ -28,7 +28,7 @@ export function Achievement() {
|
||||
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
const [achievements, setAchievements] = useState<GameAchievement[]>([]);
|
||||
const [achievements, setAchievements] = useState<UserAchievement[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
if (objectId && shop) {
|
||||
@ -130,9 +130,7 @@ export function Achievement() {
|
||||
className={styles.listItemImage({
|
||||
unlocked: achievement.unlocked,
|
||||
})}
|
||||
src={
|
||||
achievement.unlocked ? achievement.icon : achievement.icongray
|
||||
}
|
||||
src={achievement.icon}
|
||||
alt={achievement.displayName}
|
||||
loading="lazy"
|
||||
/>
|
||||
|
@ -91,11 +91,7 @@ export function Sidebar() {
|
||||
className={styles.listItemImage({
|
||||
unlocked: achievement.unlocked,
|
||||
})}
|
||||
src={
|
||||
achievement.unlocked
|
||||
? achievement.icon
|
||||
: achievement.icongray
|
||||
}
|
||||
src={achievement.icon}
|
||||
alt={achievement.displayName}
|
||||
loading="lazy"
|
||||
/>
|
||||
|
@ -37,15 +37,34 @@ export interface AchievementData {
|
||||
hidden: boolean;
|
||||
}
|
||||
|
||||
export interface UserAchievement {
|
||||
name: string;
|
||||
hidden: boolean;
|
||||
displayName: string;
|
||||
description?: string;
|
||||
unlocked: boolean;
|
||||
unlockTime: number | null;
|
||||
icon: string;
|
||||
}
|
||||
|
||||
export interface RemoteUnlockedAchievement {
|
||||
name: string;
|
||||
hidden: boolean;
|
||||
icon: string;
|
||||
displayName: string;
|
||||
description?: string;
|
||||
unlockTime: number;
|
||||
}
|
||||
|
||||
export interface GameAchievement {
|
||||
name: string;
|
||||
hidden: boolean;
|
||||
displayName: string;
|
||||
description?: string;
|
||||
unlocked: boolean;
|
||||
unlockTime: number | null;
|
||||
icon: string;
|
||||
icongray: string;
|
||||
hidden: boolean;
|
||||
}
|
||||
|
||||
export type ShopDetails = SteamAppDetails & {
|
||||
|
Loading…
Reference in New Issue
Block a user