feat: save achievements cache

This commit is contained in:
Zamitto 2024-09-24 16:32:48 -03:00
parent 7e3cf0a00e
commit f3a5f90bc7
4 changed files with 90 additions and 29 deletions

View file

@ -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,20 +16,44 @@ 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) => {
const unlockedAchiement = unlockedAchievements.find(
(localAchievement) => {
return localAchievement.name == achievement.name; return localAchievement.name == achievement.name;
}); }
);
if (unlockedAchiement) { if (unlockedAchiement) {
return { return {
@ -40,6 +64,11 @@ const getGameAchievements = async (
} }
return { ...achievement, unlocked: false, unlockTime: null }; 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;
}); });
}; };

View file

@ -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

View file

@ -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");
},
}; };
} }

View file

@ -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.length && (
<div
style={{
display: "flex",
flexDirection: "column",
gap: `${SPACING_UNIT}px`,
padding: `${SPACING_UNIT}px`,
}}
>
{achievements.map((achievement, index) => ( {achievements.map((achievement, index) => (
<div key={index}> <div
key={index}
style={{
display: "flex",
flexDirection: "row",
alignItems: "center",
gap: `${SPACING_UNIT}px`,
}}
title={achievement.description}
>
<img <img
src={achievement.unlocked ? achievement.icon : achievement.icongray} style={{
height: "72px",
}}
src={
achievement.unlocked ? achievement.icon : achievement.icongray
}
/> />
<div>
<p>{achievement.displayName}</p> <p>{achievement.displayName}</p>
{achievement.unlockTime && {achievement.unlockTime && format(achievement.unlockTime)}
new Date(achievement.unlockTime).toDateString()} </div>
</div> </div>
))} ))}
</div>
)}
{stats && ( {stats && (
<> <>