feat: implement reset game achievements functionality

This commit is contained in:
Hachi-R 2024-12-17 13:15:55 -03:00
parent 47a5f4d327
commit ac6eb247df
6 changed files with 65 additions and 5 deletions

View file

@ -25,6 +25,7 @@ import "./library/verify-executable-path";
import "./library/remove-game";
import "./library/remove-game-from-library";
import "./library/select-game-wine-prefix";
import "./library/reset-game-achievements";
import "./misc/open-checkout";
import "./misc/open-external";
import "./misc/show-open-dialog";

View file

@ -0,0 +1,52 @@
import { gameAchievementRepository, gameRepository } from "@main/repository";
import { registerEvent } from "../register-event";
import { findAchievementFiles } from "@main/services/achievements/find-achivement-files";
import fs from "fs";
import { WindowManager } from "@main/services";
import { getUnlockedAchievements } from "../user/get-unlocked-achievements";
const resetGameAchievements = async (
_event: Electron.IpcMainInvokeEvent,
gameId: number
) => {
const game = await gameRepository.findOne({ where: { id: gameId } });
if (!game) return;
const achievementFiles = findAchievementFiles(game);
if (achievementFiles.length) {
try {
await Promise.all(
achievementFiles.map(async (achievementFile) => {
await fs.promises.rm(achievementFile.filePath, { recursive: true });
})
);
} catch (error) {
console.error(error);
}
}
await gameAchievementRepository.update(
{ objectId: game.objectID },
{
unlockedAchievements: null,
achievements: null,
}
);
// TODO: remove from db
const gameAchievements = await getUnlockedAchievements(
game.objectID,
game.shop,
false
);
WindowManager.mainWindow?.webContents.send(
`on-update-achievements-${game.objectID}-${game.shop}`,
gameAchievements
);
};
registerEvent("resetGameAchievements", resetGameAchievements);

View file

@ -110,6 +110,8 @@ contextBridge.exposeInMainWorld("electron", {
ipcRenderer.invoke("deleteGameFolder", gameId),
getGameByObjectId: (objectId: string) =>
ipcRenderer.invoke("getGameByObjectId", objectId),
resetGameAchievements: (gameId: number) =>
ipcRenderer.invoke("resetGameAchievements", gameId),
onGamesRunning: (
cb: (
gamesRunning: Pick<GameRunning, "id" | "sessionDurationInMillis">[]

View file

@ -105,7 +105,7 @@ declare global {
) => void
) => () => Electron.IpcRenderer;
onLibraryBatchComplete: (cb: () => void) => () => Electron.IpcRenderer;
resetGameAchievements: (gameId: number) => Promise<void>;
/* User preferences */
getUserPreferences: () => Promise<UserPreferences | null>;
updateUserPreferences: (

View file

@ -122,6 +122,11 @@ export function GameOptionsModal({
const shouldShowWinePrefixConfiguration =
window.electron.platform === "linux";
const handleResetAchievements = async () => {
await window.electron.resetGameAchievements(game.id);
updateGame();
};
return (
<>
<DeleteGameModal
@ -140,7 +145,7 @@ export function GameOptionsModal({
<ResetAchievementsModal
visible={showResetAchievementsModal}
onClose={() => setShowResetAchievementsModal(false)}
// resetAchievements={handleResetAchievements}
resetAchievements={handleResetAchievements}
game={game}
/>

View file

@ -7,19 +7,19 @@ interface ResetAchievementsModalProps {
visible: boolean;
game: Game;
onClose: () => void;
// resetAchievements: () => Promise<void>;
resetAchievements: () => Promise<void>;
}
export function ResetAchievementsModal({
onClose,
game,
visible,
// resetAchievements,
resetAchievements,
}: ResetAchievementsModalProps) {
const { t } = useTranslation("game_details");
const handleResetAchievements = async () => {
// await resetAchievements();
await resetAchievements();
onClose();
};