feat: get achievement data on demand

This commit is contained in:
Zamitto 2024-10-21 05:07:49 -03:00
parent 0bcf005365
commit d0f42e73ff
4 changed files with 24 additions and 29 deletions

View File

@ -5,13 +5,18 @@ import { getGameAchievementData } from "@main/services/achievements/get-game-ach
export const getUnlockedAchievements = async (
objectId: string,
shop: GameShop
shop: GameShop,
useCachedData: boolean
): Promise<UserAchievement[]> => {
const cachedAchievements = await gameAchievementRepository.findOne({
where: { objectId, shop },
});
const achievementsData = await getGameAchievementData(objectId, shop);
const achievementsData = await getGameAchievementData(
objectId,
shop,
useCachedData
);
const unlockedAchievements = JSON.parse(
cachedAchievements?.unlockedAchievements || "[]"
@ -57,12 +62,12 @@ export const getUnlockedAchievements = async (
});
};
const getGameAchievementsEvent = async (
const getUnlockedAchievementsEvent = async (
_event: Electron.IpcMainInvokeEvent,
objectId: string,
shop: GameShop
): Promise<UserAchievement[]> => {
return getUnlockedAchievements(objectId, shop);
return getUnlockedAchievements(objectId, shop, false);
};
registerEvent("getUnlockedAchievements", getGameAchievementsEvent);
registerEvent("getUnlockedAchievements", getUnlockedAchievementsEvent);

View File

@ -1,4 +1,4 @@
import { gameAchievementRepository, gameRepository } from "@main/repository";
import { gameRepository } from "@main/repository";
import { parseAchievementFile } from "./parse-achievement-file";
import { Game } from "@main/entity";
import { mergeAchievements } from "./merge-achievements";
@ -13,7 +13,6 @@ import type { AchievementFile, UnlockedAchievement } from "@types";
import { achievementsLogger } from "../logger";
import { Cracker } from "@shared";
import { IsNull, Not } from "typeorm";
import { getGameAchievementData } from "./get-game-achievement-data";
const fileStats: Map<string, number> = new Map();
const fltFiles: Map<string, Set<string>> = new Map();
@ -196,16 +195,6 @@ export class AchievementWatcherManager {
return Promise.all(
games.map((game) => {
gameAchievementRepository
.findOne({
where: { objectId: game.objectID, shop: game.shop },
})
.then((localAchievements) => {
if (!localAchievements || !localAchievements.achievements) {
getGameAchievementData(game.objectID, game.shop);
}
});
const gameAchievementFiles: AchievementFile[] = [];
for (const objectId of getAlternativeObjectIds(game.objectID)) {
@ -233,16 +222,6 @@ export class AchievementWatcherManager {
return Promise.all(
games.map((game) => {
gameAchievementRepository
.findOne({
where: { objectId: game.objectID, shop: game.shop },
})
.then((localAchievements) => {
if (!localAchievements || !localAchievements.achievements) {
getGameAchievementData(game.objectID, game.shop);
}
});
const gameAchievementFiles = findAchievementFiles(game);
const achievementFileInsideDirectory =
findAchievementFileInExecutableDirectory(game);

View File

@ -9,8 +9,19 @@ import { logger } from "../logger";
export const getGameAchievementData = async (
objectId: string,
shop: GameShop
shop: GameShop,
useCachedData: boolean
) => {
if (useCachedData) {
const cachedAchievements = await gameAchievementRepository.findOne({
where: { objectId, shop },
});
if (cachedAchievements && cachedAchievements.achievements) {
return JSON.parse(cachedAchievements.achievements) as AchievementData[];
}
}
const userPreferences = await userPreferencesRepository.findOne({
where: { id: 1 },
});

View File

@ -26,7 +26,7 @@ const saveAchievementsOnLocal = async (
.then(() => {
if (!sendUpdateEvent) return;
return getUnlockedAchievements(objectId, shop)
return getUnlockedAchievements(objectId, shop, true)
.then((achievements) => {
WindowManager.mainWindow?.webContents.send(
`on-update-achievements-${objectId}-${shop}`,