Merge pull request #58 from hydralauncher/refactor/change-game-delete-to-soft-delete

refactor: change game delete to soft delete
This commit is contained in:
Hydra 2024-04-24 10:41:04 +01:00 committed by GitHub
commit a7cad41d36
18 changed files with 116 additions and 45 deletions

View File

@ -61,6 +61,9 @@ export class Game {
@JoinColumn()
repack: Repack;
@Column("boolean", { default: false })
isDeleted: boolean;
@CreateDateColumn()
createdAt: Date;

View File

@ -10,13 +10,13 @@ import "./catalogue/search-games";
import "./hardware/get-disk-free-space";
import "./library/add-game-to-library";
import "./library/close-game";
import "./library/delete-game-folder";
import "./torrenting/delete-game-folder";
import "./library/get-game-by-object-id";
import "./library/get-library";
import "./library/get-repackers-friendly-names";
import "./library/open-game";
import "./library/open-game-installer";
import "./library/remove-game";
import "./library/remove-game-from-library";
import "./misc/get-or-cache-image";
import "./misc/open-external";
import "./misc/show-open-dialog";
@ -24,6 +24,7 @@ import "./torrenting/cancel-game-download";
import "./torrenting/pause-game-download";
import "./torrenting/resume-game-download";
import "./torrenting/start-game-download";
import "./torrenting/remove-game-from-download";
import "./user-preferences/get-user-preferences";
import "./user-preferences/update-user-preferences";

View File

@ -13,15 +13,34 @@ const addGameToLibrary = async (
gameShop: GameShop,
executablePath: string
) => {
const iconUrl = await getImageBase64(await getSteamGameIconUrl(objectID));
return gameRepository.insert({
title,
iconUrl,
objectID,
shop: gameShop,
executablePath,
const game = await gameRepository.findOne({
where: {
objectID,
},
});
if (game) {
return gameRepository.update(
{
id: game.id,
},
{
shop: gameShop,
executablePath,
isDeleted: false,
}
);
} else {
const iconUrl = await getImageBase64(await getSteamGameIconUrl(objectID));
return gameRepository.insert({
title,
iconUrl,
objectID,
shop: gameShop,
executablePath,
});
}
};
registerEvent(addGameToLibrary, {

View File

@ -9,6 +9,7 @@ const getGameByObjectID = async (
gameRepository.findOne({
where: {
objectID,
isDeleted: false,
},
relations: {
repack: true,

View File

@ -8,6 +8,9 @@ import sortBy from "lodash/sortBy";
const getLibrary = async (_event: Electron.IpcMainInvokeEvent) =>
gameRepository
.find({
where: {
isDeleted: false,
},
order: {
createdAt: "desc",
},

View File

@ -23,7 +23,7 @@ const openGameInstaller = async (
);
if (!fs.existsSync(gamePath)) {
await gameRepository.delete({ id: gameId });
await gameRepository.update({ id: gameId }, { status: null });
return true;
}

View File

@ -0,0 +1,13 @@
import { registerEvent } from "../register-event";
import { gameRepository } from "../../repository";
const removeGameFromLibrary = async (
_event: Electron.IpcMainInvokeEvent,
gameId: number
) => {
gameRepository.update({ id: gameId }, { isDeleted: true });
};
registerEvent(removeGameFromLibrary, {
name: "removeGameFromLibrary",
});

View File

@ -1,11 +0,0 @@
import { registerEvent } from "../register-event";
import { gameRepository } from "../../repository";
const removeGame = async (
_event: Electron.IpcMainInvokeEvent,
gameId: number
) => gameRepository.delete({ id: gameId });
registerEvent(removeGame, {
name: "removeGame",
});

View File

@ -25,14 +25,13 @@ const cancelGameDownload = async (
if (!game) return;
gameRepository
await gameRepository
.update(
{
id: game.id,
},
{
status: GameStatus.Cancelled,
downloadPath: null,
bytesDownloaded: 0,
progress: 0,
}

View File

@ -22,7 +22,10 @@ const deleteGameFolder = async (
if (!game) return;
if (game.folderName) {
const folderPath = path.join(await getDownloadsPath(), game.folderName);
const folderPath = path.join(
game.downloadPath ?? (await getDownloadsPath()),
game.folderName
);
if (fs.existsSync(folderPath)) {
return new Promise((resolve, reject) => {

View File

@ -0,0 +1,34 @@
import { GameStatus } from "@main/constants";
import { gameRepository } from "@main/repository";
import { registerEvent } from "../register-event";
const removeGameFromDownload = async (
_event: Electron.IpcMainInvokeEvent,
gameId: number
) => {
const game = await gameRepository.findOne({
where: {
id: gameId,
status: GameStatus.Cancelled,
},
});
if (!game) return;
gameRepository.update(
{
id: game.id,
},
{
status: null,
downloadPath: null,
bytesDownloaded: 0,
progress: 0,
}
);
};
registerEvent(removeGameFromDownload, {
name: "removeGameFromDownload",
});

View File

@ -57,6 +57,7 @@ const startGameDownload = async (
status: GameStatus.DownloadingMetadata,
downloadPath: downloadPath,
repack: { id: repackId },
isDeleted: false,
}
);
@ -69,13 +70,6 @@ const startGameDownload = async (
game.status = GameStatus.DownloadingMetadata;
writePipe.write({
action: "start",
game_id: game.id,
magnet: repack.magnet,
save_path: downloadPath,
});
return game;
} else {
const iconUrl = await getImageBase64(await getSteamGameIconUrl(objectID));

View File

@ -80,7 +80,10 @@ contextBridge.exposeInMainWorld("electron", {
openGame: (gameId: number, executablePath: string) =>
ipcRenderer.invoke("openGame", gameId, executablePath),
closeGame: (gameId: number) => ipcRenderer.invoke("closeGame", gameId),
removeGame: (gameId: number) => ipcRenderer.invoke("removeGame", gameId),
removeGameFromLibrary: (gameId: number) =>
ipcRenderer.invoke("removeGameFromLibrary", gameId),
removeGameFromDownload: (gameId: number) =>
ipcRenderer.invoke("removeGameFromDownload", gameId),
deleteGameFolder: (gameId: number) =>
ipcRenderer.invoke("deleteGameFolder", gameId),
getGameByObjectID: (objectID: string) =>

View File

@ -63,7 +63,8 @@ declare global {
openGameInstaller: (gameId: number) => Promise<boolean>;
openGame: (gameId: number, executablePath: string) => Promise<void>;
closeGame: (gameId: number) => Promise<boolean>;
removeGame: (gameId: number) => Promise<void>;
removeGameFromLibrary: (gameId: number) => Promise<void>;
removeGameFromDownload: (gameId: number) => Promise<vodi>;
deleteGameFolder: (gameId: number) => Promise<unknown>;
getGameByObjectID: (objectID: string) => Promise<Game | null>;
onPlaytime: (cb: (gameId: number) => void) => () => Electron.IpcRenderer;

View File

@ -58,8 +58,8 @@ export function useDownload() {
deleteGame(gameId);
});
const removeGame = (gameId: number) =>
window.electron.removeGame(gameId).then(() => {
const removeGameFromDownload = (gameId: number) =>
window.electron.removeGameFromDownload(gameId).then(() => {
updateLibrary();
});
@ -124,7 +124,7 @@ export function useDownload() {
pauseDownload,
resumeDownload,
cancelDownload,
removeGame,
removeGameFromDownload,
deleteGame,
isGameDeleting,
clearDownload: () => dispatch(clearDownload()),

View File

@ -12,5 +12,10 @@ export function useLibrary() {
.then((updatedLibrary) => dispatch(setLibrary(updatedLibrary)));
}, [dispatch]);
return { library, updateLibrary };
const removeGameFromLibrary = (gameId: number) =>
window.electron.removeGameFromLibrary(gameId).then(() => {
updateLibrary();
});
return { library, updateLibrary, removeGameFromLibrary };
}

View File

@ -52,8 +52,8 @@ export function Downloads() {
updateLibrary();
});
const removeGame = (gameId: number) =>
window.electron.removeGame(gameId).then(() => {
const removeGameFromDownload = (gameId: number) =>
window.electron.removeGameFromDownload(gameId).then(() => {
updateLibrary();
});
@ -194,7 +194,7 @@ export function Downloads() {
</Button>
<Button
onClick={() => removeGame(game.id)}
onClick={() => removeGameFromDownload(game.id)}
theme="outline"
disabled={deleting}
>

View File

@ -32,11 +32,11 @@ export function HeroPanelActions({
resumeDownload,
pauseDownload,
cancelDownload,
removeGame,
removeGameFromDownload,
isGameDeleting,
} = useDownload();
const { updateLibrary } = useLibrary();
const { updateLibrary, removeGameFromLibrary } = useLibrary();
const { t } = useTranslation("game_details");
@ -63,7 +63,7 @@ export function HeroPanelActions({
try {
if (game) {
await removeGame(game.id);
await removeGameFromLibrary(game.id);
} else {
const gameExecutablePath = await selectGameExecutable();
@ -101,6 +101,9 @@ export function HeroPanelActions({
}
const gameExecutablePath = await selectGameExecutable();
if (!gameExecutablePath) return;
window.electron.openGame(game.id, gameExecutablePath);
};
@ -187,7 +190,7 @@ export function HeroPanelActions({
{t("open_download_options")}
</Button>
<Button
onClick={() => removeGame(game.id).then(getGame)}
onClick={() => removeGameFromDownload(game.id).then(getGame)}
theme="outline"
disabled={deleting}
>