feat: get achievement from game directory on launch

This commit is contained in:
Zamitto 2024-10-05 00:49:52 -03:00
parent 5e313a0374
commit 2a6b757e37

View File

@ -2,6 +2,7 @@ import { gameAchievementRepository, gameRepository } from "@main/repository";
import {
findAllAchievementFiles,
findAchievementFiles,
findAchievementFileInExecutableDirectory,
} from "./find-achivement-files";
import { parseAchievementFile } from "./parse-achievement-file";
import { mergeAchievements } from "./merge-achievements";
@ -12,26 +13,30 @@ import { achievementsLogger } from "../logger";
export const updateAllLocalUnlockedAchievements = async () => {
const gameAchievementFilesMap = await findAllAchievementFiles();
for (const objectId of gameAchievementFilesMap.keys()) {
const gameAchievementFiles = gameAchievementFilesMap.get(objectId)!;
const games = await gameRepository.find({
where: {
isDeleted: false,
},
});
const [game, localAchievements] = await Promise.all([
gameRepository.findOne({
where: { objectID: objectId, shop: "steam", isDeleted: false },
}),
gameAchievementRepository.findOne({
where: { objectId, shop: "steam" },
}),
]);
for (const game of games) {
const gameAchievementFiles =
gameAchievementFilesMap.get(game.objectID) || [];
const achievementFileInsideDirectory =
findAchievementFileInExecutableDirectory(game);
if (!game) continue;
gameAchievementFiles.push(...achievementFileInsideDirectory);
const localAchievements = await gameAchievementRepository.findOne({
where: { objectId: game.objectID, shop: "steam" },
});
if (!localAchievements || !localAchievements.achievements) {
await getGameAchievementData(objectId, "steam")
await getGameAchievementData(game.objectID, "steam")
.then((achievements) => {
return gameAchievementRepository.upsert(
{
objectId,
objectId: game.objectID,
shop: "steam",
achievements: JSON.stringify(achievements),
},
@ -60,7 +65,7 @@ export const updateAllLocalUnlockedAchievements = async () => {
);
}
mergeAchievements(objectId, "steam", unlockedAchievements, false);
mergeAchievements(game.objectID, "steam", unlockedAchievements, false);
}
};
@ -78,6 +83,11 @@ export const updateLocalUnlockedAchivements = async (objectId: string) => {
const gameAchievementFiles = await findAchievementFiles(game);
const achievementFileInsideDirectory =
findAchievementFileInExecutableDirectory(game);
gameAchievementFiles.push(...achievementFileInsideDirectory);
console.log("Achievements files for", game.title, gameAchievementFiles);
if (!localAchievements || !localAchievements.achievements) {