feat: merge achievements with remote

This commit is contained in:
Zamitto 2024-09-26 18:27:35 -03:00
parent 54dae87a58
commit d7c05247c3

View File

@ -2,6 +2,22 @@ import { gameAchievementRepository, gameRepository } from "@main/repository";
import { publishNewAchievementNotification } from "../notifications"; import { publishNewAchievementNotification } from "../notifications";
import type { GameShop, UnlockedAchievement } from "@types"; import type { GameShop, UnlockedAchievement } from "@types";
import { WindowManager } from "../window-manager"; import { WindowManager } from "../window-manager";
import { HydraApi } from "../hydra-api";
const saveAchievementsOnLocal = async (
objectId: string,
shop: string,
achievements: any[]
) => {
return gameAchievementRepository.upsert(
{
objectId,
shop,
unlockedAchievements: JSON.stringify(achievements),
},
["objectId", "shop"]
);
};
export const mergeAchievements = async ( export const mergeAchievements = async (
objectId: string, objectId: string,
@ -29,15 +45,15 @@ export const mergeAchievements = async (
}); });
}); });
if (!newAchievements.length) return; if (newAchievements.length) {
WindowManager.mainWindow?.webContents.send(
"on-achievement-unlocked",
objectId,
shop
);
}
WindowManager.mainWindow?.webContents.send( for (const achievement of newAchievements.slice(0, 3)) {
"on-achievement-unlocked",
objectId,
shop
);
for (const achievement of newAchievements) {
const completeAchievement = JSON.parse( const completeAchievement = JSON.parse(
localGameAchievement?.achievements || "[]" localGameAchievement?.achievements || "[]"
).find((steamAchievement) => { ).find((steamAchievement) => {
@ -53,27 +69,24 @@ export const mergeAchievements = async (
} }
} }
const mergedAchievements = unlockedAchievements.concat(newAchievements); const mergedLocalAchievements = unlockedAchievements.concat(newAchievements);
gameAchievementRepository.upsert( if (game?.remoteId) {
{ return HydraApi.put("/profile/games/achievements", {
objectId, id: game.remoteId,
shop, achievements: mergedLocalAchievements,
unlockedAchievements: JSON.stringify(mergedAchievements), })
}, .then((response) => {
["objectId", "shop"] return saveAchievementsOnLocal(
); response.objectId,
response.shop,
response.achievements
);
})
.catch(() => {
return saveAchievementsOnLocal(objectId, shop, mergedLocalAchievements);
});
}
// return HydraApi.get("/profile/games/achievements").then(async (response) => { return saveAchievementsOnLocal(objectId, shop, mergedLocalAchievements);
// console.log(response);
// });
// if (game.remoteId) {
// HydraApi.put("/profile/games/achievements", {
// id: game.remoteId,
// achievements: unlockedAchievements,
// }).catch(() => {
// console.log("erro");
// });
// }
}; };