mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-03-09 15:40:26 +00:00
feat: save achievements cache
This commit is contained in:
parent
7e3cf0a00e
commit
f3a5f90bc7
4 changed files with 90 additions and 29 deletions
|
@ -2,7 +2,7 @@ import type { GameShop } from "@types";
|
||||||
|
|
||||||
import { registerEvent } from "../register-event";
|
import { registerEvent } from "../register-event";
|
||||||
import { HydraApi } from "@main/services";
|
import { HydraApi } from "@main/services";
|
||||||
import { gameRepository } from "@main/repository";
|
import { gameAchievementRepository, gameRepository } from "@main/repository";
|
||||||
import { GameAchievement } from "@main/entity";
|
import { GameAchievement } from "@main/entity";
|
||||||
|
|
||||||
const getGameAchievements = async (
|
const getGameAchievements = async (
|
||||||
|
@ -16,31 +16,60 @@ const getGameAchievements = async (
|
||||||
achievements: true,
|
achievements: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const gameAchievements = await HydraApi.get(
|
|
||||||
|
const cachedAchievements = game?.achievements?.achievements;
|
||||||
|
|
||||||
|
const apiAchievement = HydraApi.get(
|
||||||
"/games/achievements",
|
"/games/achievements",
|
||||||
{ objectId, shop },
|
{ objectId, shop },
|
||||||
{ needsAuth: false }
|
{ needsAuth: false }
|
||||||
);
|
)
|
||||||
|
.then((achievements) => {
|
||||||
|
if (game) {
|
||||||
|
gameAchievementRepository.upsert(
|
||||||
|
{
|
||||||
|
game: { id: game.id },
|
||||||
|
achievements: JSON.stringify(achievements),
|
||||||
|
},
|
||||||
|
["game"]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return achievements;
|
||||||
|
})
|
||||||
|
.catch(() => []);
|
||||||
|
|
||||||
|
const gameAchievements = cachedAchievements
|
||||||
|
? JSON.parse(cachedAchievements)
|
||||||
|
: await apiAchievement;
|
||||||
|
|
||||||
const unlockedAchievements = JSON.parse(
|
const unlockedAchievements = JSON.parse(
|
||||||
game?.achievements?.unlockedAchievements || "[]"
|
game?.achievements?.unlockedAchievements || "[]"
|
||||||
) as { name: string; unlockTime: number }[];
|
) as { name: string; unlockTime: number }[];
|
||||||
|
|
||||||
return gameAchievements.map((achievement) => {
|
return gameAchievements
|
||||||
const unlockedAchiement = unlockedAchievements.find((localAchievement) => {
|
.map((achievement) => {
|
||||||
return localAchievement.name == achievement.name;
|
const unlockedAchiement = unlockedAchievements.find(
|
||||||
|
(localAchievement) => {
|
||||||
|
return localAchievement.name == achievement.name;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (unlockedAchiement) {
|
||||||
|
return {
|
||||||
|
...achievement,
|
||||||
|
unlocked: true,
|
||||||
|
unlockTime: unlockedAchiement.unlockTime * 1000,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return { ...achievement, unlocked: false, unlockTime: null };
|
||||||
|
})
|
||||||
|
.sort((a, b) => {
|
||||||
|
if (a.unlocked && !b.unlocked) return -1;
|
||||||
|
if (!a.unlocked && b.unlocked) return 1;
|
||||||
|
return b.unlockTime - a.unlockTime;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (unlockedAchiement) {
|
|
||||||
return {
|
|
||||||
...achievement,
|
|
||||||
unlocked: true,
|
|
||||||
unlockTime: unlockedAchiement.unlockTime * 1000,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return { ...achievement, unlocked: false, unlockTime: null };
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
registerEvent("getGameAchievements", getGameAchievements);
|
registerEvent("getGameAchievements", getGameAchievements);
|
||||||
|
|
|
@ -48,7 +48,6 @@ export const saveAllLocalSteamAchivements = async () => {
|
||||||
);
|
);
|
||||||
|
|
||||||
console.log(achievementFile.filePath);
|
console.log(achievementFile.filePath);
|
||||||
console.log(localAchievementFile);
|
|
||||||
|
|
||||||
for (const a of Object.keys(localAchievementFile)) {
|
for (const a of Object.keys(localAchievementFile)) {
|
||||||
// TODO: use checkUnlockedAchievements after refactoring it to be generic
|
// TODO: use checkUnlockedAchievements after refactoring it to be generic
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { formatDistance, subMilliseconds } from "date-fns";
|
import { format, formatDistance, subMilliseconds } from "date-fns";
|
||||||
import type { FormatDistanceOptions } from "date-fns";
|
import type { FormatDistanceOptions } from "date-fns";
|
||||||
import {
|
import {
|
||||||
ptBR,
|
ptBR,
|
||||||
|
@ -67,5 +67,10 @@ export function useDate() {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
format: (timestamp: number): string => {
|
||||||
|
const locale = getDateLocale();
|
||||||
|
return format(timestamp, locale == enUS ? "MM/dd/yyyy - HH:mm" : "dd/MM/yyyy - HH:mm");
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,8 +5,9 @@ import { Button } from "@renderer/components";
|
||||||
|
|
||||||
import * as styles from "./sidebar.css";
|
import * as styles from "./sidebar.css";
|
||||||
import { gameDetailsContext } from "@renderer/context";
|
import { gameDetailsContext } from "@renderer/context";
|
||||||
import { useFormat } from "@renderer/hooks";
|
import { useDate, useFormat } from "@renderer/hooks";
|
||||||
import { DownloadIcon, PeopleIcon } from "@primer/octicons-react";
|
import { DownloadIcon, PeopleIcon } from "@primer/octicons-react";
|
||||||
|
import { SPACING_UNIT, vars } from "@renderer/theme.css";
|
||||||
|
|
||||||
export function Sidebar() {
|
export function Sidebar() {
|
||||||
const [_howLongToBeat, _setHowLongToBeat] = useState<{
|
const [_howLongToBeat, _setHowLongToBeat] = useState<{
|
||||||
|
@ -21,6 +22,7 @@ export function Sidebar() {
|
||||||
useContext(gameDetailsContext);
|
useContext(gameDetailsContext);
|
||||||
|
|
||||||
const { t } = useTranslation("game_details");
|
const { t } = useTranslation("game_details");
|
||||||
|
const { format } = useDate();
|
||||||
|
|
||||||
const { numberFormatter } = useFormat();
|
const { numberFormatter } = useFormat();
|
||||||
|
|
||||||
|
@ -46,16 +48,42 @@ export function Sidebar() {
|
||||||
isLoading={howLongToBeat.isLoading}
|
isLoading={howLongToBeat.isLoading}
|
||||||
/> */}
|
/> */}
|
||||||
|
|
||||||
{achievements.map((achievement, index) => (
|
{achievements.length && (
|
||||||
<div key={index}>
|
<div
|
||||||
<img
|
style={{
|
||||||
src={achievement.unlocked ? achievement.icon : achievement.icongray}
|
display: "flex",
|
||||||
/>
|
flexDirection: "column",
|
||||||
<p>{achievement.displayName}</p>
|
gap: `${SPACING_UNIT}px`,
|
||||||
{achievement.unlockTime &&
|
padding: `${SPACING_UNIT}px`,
|
||||||
new Date(achievement.unlockTime).toDateString()}
|
}}
|
||||||
|
>
|
||||||
|
{achievements.map((achievement, index) => (
|
||||||
|
<div
|
||||||
|
key={index}
|
||||||
|
style={{
|
||||||
|
display: "flex",
|
||||||
|
flexDirection: "row",
|
||||||
|
alignItems: "center",
|
||||||
|
gap: `${SPACING_UNIT}px`,
|
||||||
|
}}
|
||||||
|
title={achievement.description}
|
||||||
|
>
|
||||||
|
<img
|
||||||
|
style={{
|
||||||
|
height: "72px",
|
||||||
|
}}
|
||||||
|
src={
|
||||||
|
achievement.unlocked ? achievement.icon : achievement.icongray
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<div>
|
||||||
|
<p>{achievement.displayName}</p>
|
||||||
|
{achievement.unlockTime && format(achievement.unlockTime)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
</div>
|
</div>
|
||||||
))}
|
)}
|
||||||
|
|
||||||
{stats && (
|
{stats && (
|
||||||
<>
|
<>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue