mirror of
https://github.com/hydralauncher/hydra.git
synced 2026-01-21 18:13:55 +00:00
feat: save achievements cache
This commit is contained in:
@@ -2,7 +2,7 @@ import type { GameShop } from "@types";
|
||||
|
||||
import { registerEvent } from "../register-event";
|
||||
import { HydraApi } from "@main/services";
|
||||
import { gameRepository } from "@main/repository";
|
||||
import { gameAchievementRepository, gameRepository } from "@main/repository";
|
||||
import { GameAchievement } from "@main/entity";
|
||||
|
||||
const getGameAchievements = async (
|
||||
@@ -16,31 +16,60 @@ const getGameAchievements = async (
|
||||
achievements: true,
|
||||
},
|
||||
});
|
||||
const gameAchievements = await HydraApi.get(
|
||||
|
||||
const cachedAchievements = game?.achievements?.achievements;
|
||||
|
||||
const apiAchievement = HydraApi.get(
|
||||
"/games/achievements",
|
||||
{ objectId, shop },
|
||||
{ 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(
|
||||
game?.achievements?.unlockedAchievements || "[]"
|
||||
) as { name: string; unlockTime: number }[];
|
||||
|
||||
return gameAchievements.map((achievement) => {
|
||||
const unlockedAchiement = unlockedAchievements.find((localAchievement) => {
|
||||
return localAchievement.name == achievement.name;
|
||||
return gameAchievements
|
||||
.map((achievement) => {
|
||||
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);
|
||||
|
||||
@@ -48,7 +48,6 @@ export const saveAllLocalSteamAchivements = async () => {
|
||||
);
|
||||
|
||||
console.log(achievementFile.filePath);
|
||||
console.log(localAchievementFile);
|
||||
|
||||
for (const a of Object.keys(localAchievementFile)) {
|
||||
// TODO: use checkUnlockedAchievements after refactoring it to be generic
|
||||
|
||||
Reference in New Issue
Block a user