feat: remove awaits

This commit is contained in:
Zamitto 2024-10-06 13:39:26 -03:00
parent af83152997
commit 7f09a7796f
8 changed files with 72 additions and 142 deletions

View File

@ -1,88 +1,45 @@
import type { GameAchievement, GameShop } from "@types";
import { registerEvent } from "../register-event";
import { HydraApi } from "@main/services";
import {
gameAchievementRepository,
gameRepository,
userPreferencesRepository,
} from "@main/repository";
import { UserNotLoggedInError } from "@shared";
import { Game } from "@main/entity";
const getAchievementsDataFromApi = async (
objectId: string,
shop: string,
game: Game | null
) => {
const userPreferences = await userPreferencesRepository.findOne({
where: { id: 1 },
});
return HydraApi.get("/games/achievements", {
objectId,
shop,
language: userPreferences?.language || "en",
})
.then((achievements) => {
if (game) {
gameAchievementRepository.upsert(
{
objectId,
shop,
achievements: JSON.stringify(achievements),
},
["objectId", "shop"]
);
}
return achievements;
})
.catch((err) => {
if (err instanceof UserNotLoggedInError) throw err;
return [];
});
};
import { gameAchievementRepository } from "@main/repository";
import { getGameAchievementData } from "@main/services/achievements/get-game-achievement-data";
const getGameAchievements = async (
_event: Electron.IpcMainInvokeEvent,
objectId: string,
shop: GameShop
): Promise<GameAchievement[]> => {
const [game, cachedAchievements] = await Promise.all([
gameRepository.findOne({
where: { objectID: objectId, shop },
}),
gameAchievementRepository.findOne({ where: { objectId, shop } }),
]);
const cachedAchievements = await gameAchievementRepository.findOne({
where: { objectId, shop },
});
const gameAchievements = cachedAchievements?.achievements
const achievementsData = cachedAchievements?.achievements
? JSON.parse(cachedAchievements.achievements)
: await getAchievementsDataFromApi(objectId, shop, game);
: await getGameAchievementData(objectId, shop);
const unlockedAchievements = JSON.parse(
cachedAchievements?.unlockedAchievements || "[]"
) as { name: string; unlockTime: number }[];
return gameAchievements
.map((achievement) => {
return achievementsData
.map((achievementData) => {
const unlockedAchiement = unlockedAchievements.find(
(localAchievement) => {
return (
localAchievement.name.toUpperCase() ==
achievement.name.toUpperCase()
achievementData.name.toUpperCase()
);
}
);
if (unlockedAchiement) {
return {
...achievement,
...achievementData,
unlocked: true,
unlockTime: unlockedAchiement.unlockTime,
};
}
return { ...achievement, unlocked: false, unlockTime: null };
return { ...achievementData, unlocked: false, unlockTime: null };
})
.sort((a, b) => {
if (a.unlocked && !b.unlocked) return -1;

View File

@ -44,10 +44,10 @@ const addGameToLibrary = async (
});
}
updateLocalUnlockedAchivements(objectID);
const game = await gameRepository.findOne({ where: { objectID } });
updateLocalUnlockedAchivements(game!);
createGame(game!).catch(() => {});
});
};

View File

@ -53,10 +53,7 @@ const processAchievementFileDiff = async (
game: Game,
file: AchievementFile
) => {
const unlockedAchievements = await parseAchievementFile(
file.filePath,
file.type
);
const unlockedAchievements = parseAchievementFile(file.filePath, file.type);
logger.log("Achievements from file", file.filePath, unlockedAchievements);

View File

@ -168,6 +168,7 @@ const getPathFromCracker = (cracker: Cracker) => {
};
export const getAlternativeObjectIds = (objectId: string) => {
// Dishonored
if (objectId === "205100") {
return ["205100", "217980", "31292"];
}

View File

@ -1,4 +1,7 @@
import { userPreferencesRepository } from "@main/repository";
import {
gameAchievementRepository,
userPreferencesRepository,
} from "@main/repository";
import { HydraApi } from "../hydra-api";
export const getGameAchievementData = async (
@ -13,5 +16,18 @@ export const getGameAchievementData = async (
shop,
objectId,
language: userPreferences?.language || "en",
});
})
.then(async (achievements) => {
await gameAchievementRepository.upsert(
{
objectId,
shop,
achievements: JSON.stringify(achievements),
},
["objectId", "shop"]
);
return achievements;
})
.catch(() => []);
};

View File

@ -1,57 +1,51 @@
import { Cracker } from "@shared";
import { UnlockedAchievement } from "@types";
import {
existsSync,
createReadStream,
readFileSync,
readdirSync,
} from "node:fs";
import readline from "node:readline";
import { existsSync, readFileSync, readdirSync } from "node:fs";
import { achievementsLogger } from "../logger";
export const parseAchievementFile = async (
export const parseAchievementFile = (
filePath: string,
type: Cracker
): Promise<UnlockedAchievement[]> => {
): UnlockedAchievement[] => {
if (!existsSync(filePath)) return [];
if (type == Cracker.codex) {
const parsed = await iniParse(filePath);
const parsed = iniParse(filePath);
return processDefault(parsed);
}
if (type == Cracker.rune) {
const parsed = await iniParse(filePath);
const parsed = iniParse(filePath);
return processDefault(parsed);
}
if (type === Cracker.onlineFix) {
const parsed = await iniParse(filePath);
const parsed = iniParse(filePath);
return processOnlineFix(parsed);
}
if (type === Cracker.goldberg) {
const parsed = await jsonParse(filePath);
const parsed = jsonParse(filePath);
return processGoldberg(parsed);
}
if (type == Cracker.userstats) {
const parsed = await iniParse(filePath);
const parsed = iniParse(filePath);
return processUserStats(parsed);
}
if (type == Cracker.rld) {
const parsed = await iniParse(filePath);
const parsed = iniParse(filePath);
return processRld(parsed);
}
if (type === Cracker.skidrow) {
const parsed = await iniParse(filePath);
const parsed = iniParse(filePath);
return processSkidrow(parsed);
}
if (type === Cracker._3dm) {
const parsed = await iniParse(filePath);
const parsed = iniParse(filePath);
return process3DM(parsed);
}
@ -67,27 +61,24 @@ export const parseAchievementFile = async (
}
if (type === Cracker.creamAPI) {
const parsed = await iniParse(filePath);
const parsed = iniParse(filePath);
return processCreamAPI(parsed);
}
achievementsLogger.log(`${type} achievements found on ${filePath}`);
achievementsLogger.log(
`Unprocessed ${type} achievements found on ${filePath}`
);
return [];
};
const iniParse = async (filePath: string) => {
const iniParse = (filePath: string) => {
try {
const file = createReadStream(filePath);
const lines = readline.createInterface({
input: file,
crlfDelay: Infinity,
});
const lines = readFileSync(filePath, "utf-8").split(/[\r\n]+/);
let objectName = "";
const object: Record<string, Record<string, string | number>> = {};
for await (const line of lines) {
for (const line of lines) {
if (line.startsWith("###") || !line.length) continue;
if (line.startsWith("[") && line.endsWith("]")) {
@ -100,7 +91,8 @@ const iniParse = async (filePath: string) => {
}
return object;
} catch {
} catch (err) {
achievementsLogger.error(`Error parsing ${filePath}`, err);
return null;
}
};
@ -108,7 +100,8 @@ const iniParse = async (filePath: string) => {
const jsonParse = (filePath: string) => {
try {
return JSON.parse(readFileSync(filePath, "utf-8"));
} catch {
} catch (err) {
achievementsLogger.error(`Error parsing ${filePath}`, err);
return null;
}
};

View File

@ -10,6 +10,7 @@ import { mergeAchievements } from "./merge-achievements";
import type { UnlockedAchievement } from "@types";
import { getGameAchievementData } from "./get-game-achievement-data";
import { achievementsLogger } from "../logger";
import { Game } from "@main/entity";
export const updateAllLocalUnlockedAchievements = async () => {
const gameAchievementFilesMap = findAllAchievementFiles();
@ -28,29 +29,20 @@ export const updateAllLocalUnlockedAchievements = async () => {
gameAchievementFiles.push(...achievementFileInsideDirectory);
const localAchievements = await gameAchievementRepository.findOne({
where: { objectId: game.objectID, shop: "steam" },
});
if (!localAchievements || !localAchievements.achievements) {
await getGameAchievementData(game.objectID, "steam")
.then((achievements) => {
return gameAchievementRepository.upsert(
{
objectId: game.objectID,
shop: "steam",
achievements: JSON.stringify(achievements),
},
["objectId", "shop"]
);
})
.catch(() => {});
}
gameAchievementRepository
.findOne({
where: { objectId: game.objectID, shop: "steam" },
})
.then((localAchievements) => {
if (!localAchievements || !localAchievements.achievements) {
getGameAchievementData(game.objectID, "steam");
}
});
const unlockedAchievements: UnlockedAchievement[] = [];
for (const achievementFile of gameAchievementFiles) {
const parsedAchievements = await parseAchievementFile(
const parsedAchievements = parseAchievementFile(
achievementFile.filePath,
achievementFile.type
);
@ -72,19 +64,8 @@ export const updateAllLocalUnlockedAchievements = async () => {
}
};
export const updateLocalUnlockedAchivements = async (objectId: string) => {
const [game, localAchievements] = await Promise.all([
gameRepository.findOne({
where: { objectID: objectId, shop: "steam", isDeleted: false },
}),
gameAchievementRepository.findOne({
where: { objectId, shop: "steam" },
}),
]);
if (!game) return;
const gameAchievementFiles = await findAchievementFiles(game);
export const updateLocalUnlockedAchivements = async (game: Game) => {
const gameAchievementFiles = findAchievementFiles(game);
const achievementFileInsideDirectory =
findAchievementFileInExecutableDirectory(game);
@ -93,25 +74,10 @@ export const updateLocalUnlockedAchivements = async (objectId: string) => {
console.log("Achievements files for", game.title, gameAchievementFiles);
if (!localAchievements || !localAchievements.achievements) {
await getGameAchievementData(objectId, "steam")
.then((achievements) => {
return gameAchievementRepository.upsert(
{
objectId,
shop: "steam",
achievements: JSON.stringify(achievements),
},
["objectId", "shop"]
);
})
.catch(() => {});
}
const unlockedAchievements: UnlockedAchievement[] = [];
for (const achievementFile of gameAchievementFiles) {
const localAchievementFile = await parseAchievementFile(
const localAchievementFile = parseAchievementFile(
achievementFile.filePath,
achievementFile.type
);
@ -121,5 +87,5 @@ export const updateLocalUnlockedAchivements = async (objectId: string) => {
}
}
mergeAchievements(objectId, "steam", unlockedAchievements, false);
mergeAchievements(game.objectID, "steam", unlockedAchievements, false);
};

View File

@ -12,6 +12,6 @@ export const startMainLoop = async () => {
watchAchievements(),
]);
await sleep(1000);
await sleep(1500);
}
};