From 6fa4c178a77b8e2b8127264c05e51fd4dac87033 Mon Sep 17 00:00:00 2001 From: lilezek Date: Mon, 29 Apr 2024 20:50:10 +0200 Subject: [PATCH] refactor: moved the game status to a global file (accessible to the render part) and used it in game entity --- src/globals.ts | 10 ++++++++++ src/main/constants.ts | 9 --------- src/main/entity/game.entity.ts | 3 ++- src/main/events/library/get-library.ts | 2 +- src/main/events/torrenting/cancel-game-download.ts | 2 +- src/main/events/torrenting/delete-game-folder.ts | 2 +- src/main/events/torrenting/pause-game-download.ts | 2 +- .../events/torrenting/remove-game-from-download.ts | 2 +- src/main/events/torrenting/resume-game-download.ts | 2 +- src/main/events/torrenting/start-game-download.ts | 2 +- src/main/index.ts | 3 ++- src/types/index.ts | 4 +++- tsconfig.json | 3 ++- 13 files changed, 26 insertions(+), 20 deletions(-) create mode 100644 src/globals.ts diff --git a/src/globals.ts b/src/globals.ts new file mode 100644 index 00000000..e4675aa7 --- /dev/null +++ b/src/globals.ts @@ -0,0 +1,10 @@ +export enum GameStatus { + Seeding = "seeding", + Downloading = "downloading", + Paused = "paused", + CheckingFiles = "checking_files", + DownloadingMetadata = "downloading_metadata", + Cancelled = "cancelled", + Finished = "finished", + Decompressing = "decompressing", +} \ No newline at end of file diff --git a/src/main/constants.ts b/src/main/constants.ts index 43d9d44e..1a87c27b 100644 --- a/src/main/constants.ts +++ b/src/main/constants.ts @@ -34,15 +34,6 @@ export const months = [ "Dec", ]; -export enum GameStatus { - Seeding = "seeding", - Downloading = "downloading", - Paused = "paused", - CheckingFiles = "checking_files", - DownloadingMetadata = "downloading_metadata", - Cancelled = "cancelled", -} - export const defaultDownloadsPath = path.join(os.homedir(), "downloads"); export const databasePath = path.join( diff --git a/src/main/entity/game.entity.ts b/src/main/entity/game.entity.ts index 811ecf74..8798a15d 100644 --- a/src/main/entity/game.entity.ts +++ b/src/main/entity/game.entity.ts @@ -9,6 +9,7 @@ import { } from "typeorm"; import type { GameShop } from "@types"; import { Repack } from "./repack.entity"; +import { GameStatus } from "@globals"; @Entity("game") export class Game { @@ -40,7 +41,7 @@ export class Game { shop: GameShop; @Column("text", { nullable: true }) - status: string; + status: GameStatus | ""; @Column("float", { default: 0 }) progress: number; diff --git a/src/main/events/library/get-library.ts b/src/main/events/library/get-library.ts index c86d1902..047d848b 100644 --- a/src/main/events/library/get-library.ts +++ b/src/main/events/library/get-library.ts @@ -1,9 +1,9 @@ import { gameRepository } from "@main/repository"; -import { GameStatus } from "@main/constants"; import { searchRepacks } from "../helpers/search-games"; import { registerEvent } from "../register-event"; import sortBy from "lodash/sortBy"; +import { GameStatus } from "@globals"; const getLibrary = async (_event: Electron.IpcMainInvokeEvent) => gameRepository diff --git a/src/main/events/torrenting/cancel-game-download.ts b/src/main/events/torrenting/cancel-game-download.ts index a1a2e6b7..bcd4fdab 100644 --- a/src/main/events/torrenting/cancel-game-download.ts +++ b/src/main/events/torrenting/cancel-game-download.ts @@ -1,10 +1,10 @@ -import { GameStatus } from "@main/constants"; import { gameRepository } from "@main/repository"; import { registerEvent } from "../register-event"; import { WindowManager, writePipe } from "@main/services"; import { In } from "typeorm"; +import { GameStatus } from "@globals"; const cancelGameDownload = async ( _event: Electron.IpcMainInvokeEvent, diff --git a/src/main/events/torrenting/delete-game-folder.ts b/src/main/events/torrenting/delete-game-folder.ts index c8821415..e913a23a 100644 --- a/src/main/events/torrenting/delete-game-folder.ts +++ b/src/main/events/torrenting/delete-game-folder.ts @@ -1,7 +1,7 @@ import path from "node:path"; import fs from "node:fs"; -import { GameStatus } from "@main/constants"; +import { GameStatus } from "@globals"; import { gameRepository } from "@main/repository"; import { getDownloadsPath } from "../helpers/get-downloads-path"; diff --git a/src/main/events/torrenting/pause-game-download.ts b/src/main/events/torrenting/pause-game-download.ts index d89f2f72..e1da552a 100644 --- a/src/main/events/torrenting/pause-game-download.ts +++ b/src/main/events/torrenting/pause-game-download.ts @@ -1,9 +1,9 @@ import { WindowManager, writePipe } from "@main/services"; import { registerEvent } from "../register-event"; -import { GameStatus } from "../../constants"; import { gameRepository } from "../../repository"; import { In } from "typeorm"; +import { GameStatus } from "@globals"; const pauseGameDownload = async ( _event: Electron.IpcMainInvokeEvent, diff --git a/src/main/events/torrenting/remove-game-from-download.ts b/src/main/events/torrenting/remove-game-from-download.ts index 47c1ebe6..2ca608ef 100644 --- a/src/main/events/torrenting/remove-game-from-download.ts +++ b/src/main/events/torrenting/remove-game-from-download.ts @@ -1,4 +1,4 @@ -import { GameStatus } from "@main/constants"; +import { GameStatus } from "@globals"; import { gameRepository } from "@main/repository"; import { registerEvent } from "../register-event"; diff --git a/src/main/events/torrenting/resume-game-download.ts b/src/main/events/torrenting/resume-game-download.ts index c1e2e798..9d96ab18 100644 --- a/src/main/events/torrenting/resume-game-download.ts +++ b/src/main/events/torrenting/resume-game-download.ts @@ -1,9 +1,9 @@ import { registerEvent } from "../register-event"; -import { GameStatus } from "../../constants"; import { gameRepository } from "../../repository"; import { getDownloadsPath } from "../helpers/get-downloads-path"; import { In } from "typeorm"; import { writePipe } from "@main/services"; +import { GameStatus } from "@globals"; const resumeGameDownload = async ( _event: Electron.IpcMainInvokeEvent, diff --git a/src/main/events/torrenting/start-game-download.ts b/src/main/events/torrenting/start-game-download.ts index 1bdb1a6b..a5853208 100644 --- a/src/main/events/torrenting/start-game-download.ts +++ b/src/main/events/torrenting/start-game-download.ts @@ -1,12 +1,12 @@ import { getSteamGameIconUrl, writePipe } from "@main/services"; import { gameRepository, repackRepository } from "@main/repository"; -import { GameStatus } from "@main/constants"; import { registerEvent } from "../register-event"; import type { GameShop } from "@types"; import { getImageBase64 } from "@main/helpers"; import { In } from "typeorm"; +import { GameStatus } from "@globals"; const startGameDownload = async ( _event: Electron.IpcMainInvokeEvent, diff --git a/src/main/index.ts b/src/main/index.ts index 5637bbd3..1657540d 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -1,5 +1,5 @@ import { stateManager } from "./state-manager"; -import { GameStatus, repackers } from "./constants"; +import { repackers } from "./constants"; import { getNewGOGGames, getNewRepacksFromCPG, @@ -22,6 +22,7 @@ import { Repack } from "./entity"; import { Notification } from "electron"; import { t } from "i18next"; import { In } from "typeorm"; +import { GameStatus } from "@globals"; startProcessWatcher(); diff --git a/src/types/index.ts b/src/types/index.ts index 768f110b..d9070451 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,3 +1,5 @@ +import { GameStatus } from "@globals"; + export type GameShop = "steam" | "epic"; export type CatalogueCategory = "recently_added" | "trending"; @@ -75,7 +77,7 @@ export interface Game extends Omit { id: number; title: string; iconUrl: string; - status: string; + status: GameStatus | ""; folderName: string; downloadPath: string | null; repacks: GameRepack[]; diff --git a/tsconfig.json b/tsconfig.json index ee7e5c2b..aeb1de53 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -19,7 +19,8 @@ "@main/*": ["src/main/*"], "@renderer/*": ["src/renderer/*"], "@types": ["src/types/index.ts"], - "@locales": ["src/locales/index.ts"] + "@locales": ["src/locales/index.ts"], + "@globals": ["src/globals.ts"] } }, "include": ["src/**/*"]