feat: refactor

This commit is contained in:
Zamitto 2024-09-26 15:33:32 -03:00
parent c72eefdb77
commit 780ab5f909
10 changed files with 68 additions and 131 deletions

View file

@ -1,4 +1,5 @@
import { Cracker, UnlockedAchievement } from "../types";
import { Cracker } from "@shared";
import type { UnlockedAchievement } from "@types";
export const checkUnlockedAchievements = (
type: Cracker,

View file

@ -1,10 +1,11 @@
import path from "node:path";
import fs from "node:fs";
import { Cracker, GameAchievementFiles } from "../types";
import { app } from "electron";
import type { AchievementFile } from "@types";
import { Cracker } from "@shared";
const addGame = (
achievementFiles: GameAchievementFiles,
achievementFiles: Map<string, AchievementFile[]>,
achievementPath: string,
objectId: string,
fileLocation: string[],
@ -17,19 +18,17 @@ const addGame = (
filePath,
};
achievementFiles[objectId]
? achievementFiles[objectId].push(achivementFile)
: (achievementFiles[objectId] = [achivementFile]);
achievementFiles.get(objectId)
? achievementFiles.get(objectId)!.push(achivementFile)
: achievementFiles.set(objectId, [achivementFile]);
};
export const steamFindGameAchievementFiles = (
objectId?: string
): GameAchievementFiles => {
export const findSteamGameAchievementFiles = (objectId?: string) => {
//TODO: change to a automatized method
const publicDir = path.join("C:", "Users", "Public", "Documents");
const appData = app.getPath("appData");
const gameAchievementFiles: GameAchievementFiles = {};
const gameAchievementFiles = new Map<string, AchievementFile[]>();
const crackers = [
Cracker.codex,

View file

@ -1,11 +1,11 @@
import { watch } from "node:fs/promises";
import { getGameAchievementsToWatch } from "./get-game-achievements-to-watch";
import { checkUnlockedAchievements } from "./util/check-unlocked-achievements";
import { parseAchievementFile } from "./util/parseAchievementFile";
import { checkUnlockedAchievements } from "./check-unlocked-achievements";
import { parseAchievementFile } from "./parse-achievement-file";
import { Game } from "@main/entity";
import { mergeAchievements } from "./merge-achievements";
import fs from "node:fs";
import { AchievementFile } from "./types";
import { findSteamGameAchievementFiles } from "./find-steam-game-achivement-files";
import type { AchievementFile } from "@types";
type GameAchievementObserver = {
[id: number]: AbortController;
@ -30,12 +30,34 @@ const processAchievementFile = async (game: Game, file: AchievementFile) => {
}
};
const startFileWatch = async (game: Game, file: AchievementFile) => {
const signal = gameAchievementObserver[game.id]?.signal;
try {
processAchievementFile(game, file);
const watcher = watch(file.filePath, {
signal,
});
for await (const event of watcher) {
if (event.eventType === "change") {
processAchievementFile(game, file);
}
}
} catch (err: any) {
if (err?.name === "AbortError") return;
console.log(`cracker: ${file.type}, steamId ${game.objectID}`);
throw err;
}
};
export const startGameAchievementObserver = async (game: Game) => {
if (game.shop !== "steam") return;
if (gameAchievementObserver[game.id]) return;
console.log(`Starting: ${game.title}`);
const achievementFiles = await getGameAchievementsToWatch(game.id);
const achievementFiles =
findSteamGameAchievementFiles(game.objectID).get(game.objectID) || [];
console.log(
"Achievements files to observe for:",
@ -48,39 +70,16 @@ export const startGameAchievementObserver = async (game: Game) => {
continue;
}
console.log(`cracker: ${file.type}, objectId: ${game.objectID}`);
if (!gameAchievementObserver[game.id]) {
const abortController = new AbortController();
gameAchievementObserver[game.id] = abortController;
}
const signal = gameAchievementObserver[game.id]?.signal;
(async () => {
try {
processAchievementFile(game, file);
const watcher = watch(file.filePath, {
signal,
});
for await (const event of watcher) {
if (event.eventType === "change") {
processAchievementFile(game, file);
}
}
} catch (err: any) {
if (err?.name === "AbortError") return;
console.log(`cracker: ${file.type}, steamId ${game.objectID}`);
throw err;
}
})();
startFileWatch(game, file);
}
};
export const stopGameAchievementObserver = (gameId: number) => {
console.log(`Stopping: ${gameId}`);
gameAchievementObserver[gameId]?.abort();
delete gameAchievementObserver[gameId];
};

View file

@ -1,25 +0,0 @@
import { gameRepository } from "@main/repository";
import { steamFindGameAchievementFiles } from "./steam/steam-find-game-achivement-files";
import { AchievementFile } from "./types";
export const getGameAchievementsToWatch = async (
gameId: number
): Promise<AchievementFile[]> => {
const game = await gameRepository.findOne({ where: { id: gameId } });
if (!game || game.shop !== "steam") return [];
const steamId = Number(game.objectID);
const achievementFiles = steamFindGameAchievementFiles(game.objectID)[
steamId
];
console.log(
"achivements files:",
achievementFiles,
game.title,
game.objectID
);
return achievementFiles || [];
};

View file

@ -1,7 +1,6 @@
import { gameAchievementRepository, gameRepository } from "@main/repository";
import { UnlockedAchievement } from "./types";
import { publishNewAchievementNotification } from "../notifications";
import { GameShop } from "@types";
import type { GameShop, UnlockedAchievement } from "@types";
export const mergeAchievements = async (
objectId: string,

View file

@ -3,19 +3,19 @@ import {
gameRepository,
userPreferencesRepository,
} from "@main/repository";
import { steamFindGameAchievementFiles } from "./steam/steam-find-game-achivement-files";
import { parseAchievementFile } from "./util/parseAchievementFile";
import { findSteamGameAchievementFiles } from "./find-steam-game-achivement-files";
import { parseAchievementFile } from "./parse-achievement-file";
import { HydraApi } from "@main/services";
import { checkUnlockedAchievements } from "./util/check-unlocked-achievements";
import { checkUnlockedAchievements } from "./check-unlocked-achievements";
import { mergeAchievements } from "./merge-achievements";
import { UnlockedAchievement } from "./types";
import type { UnlockedAchievement } from "@types";
export const saveAllLocalSteamAchivements = async () => {
const userPreferences = await userPreferencesRepository.findOne({
where: { id: 1 },
});
const gameAchievementFiles = steamFindGameAchievementFiles();
const gameAchievementFiles = findSteamGameAchievementFiles();
for (const objectId of Object.keys(gameAchievementFiles)) {
const [game, localAchievements] = await Promise.all([

View file

@ -1,57 +0,0 @@
export enum Cracker {
codex = "CODEX",
rune = "RUNE",
onlineFix = "OnlineFix",
goldberg = "Goldberg",
}
export interface CheckedAchievements {
all: Achievement[];
new: Achievement[];
}
export interface UnlockedAchievement {
name: string;
unlockTime: number;
}
export interface Achievement {
id: string;
percent: number;
imageUrl: string;
title: string;
description: string;
achieved: boolean;
curProgress: number;
maxProgress: number;
unlockTime: number;
}
export interface AchievementInfo {
imageUrl: string;
title: string;
description: string;
}
export interface AchievementPercentage {
name: string;
percent: number;
}
export interface CheckedAchievement {
all: Achievement[];
new: Achievement[];
}
export interface AchievementFile {
type: Cracker;
filePath: string;
}
export type GameAchievementFiles = {
[id: string]: AchievementFile[];
};
export type GameAchievementFile = {
[id: string]: AchievementFile[];
};

View file

@ -23,3 +23,10 @@ export enum SteamContentDescriptor {
FrequentNudityOrSexualContent = 4,
GeneralMatureContent = 5,
}
export enum Cracker {
codex = "CODEX",
rune = "RUNE",
onlineFix = "OnlineFix",
goldberg = "Goldberg",
}

View file

@ -1,4 +1,4 @@
import type { DownloadSourceStatus, Downloader } from "@shared";
import type { Cracker, DownloadSourceStatus, Downloader } from "@shared";
import type { SteamAppDetails } from "./steam.types";
export type GameStatus =
@ -265,5 +265,19 @@ export interface UserStats {
friendsCount: number;
}
export interface UnlockedAchievement {
name: string;
unlockTime: number;
}
export interface AchievementFile {
type: Cracker;
filePath: string;
}
export type GameAchievementFiles = {
[id: string]: AchievementFile[];
};
export * from "./steam.types";
export * from "./real-debrid.types";