diff --git a/src/main/services/download/download-manager.ts b/src/main/services/download/download-manager.ts index 0bcf68db..98951875 100644 --- a/src/main/services/download/download-manager.ts +++ b/src/main/services/download/download-manager.ts @@ -98,6 +98,17 @@ export class DownloadManager { } } + public static async watchSeedingList() { + const seedingList = await PythonInstance.getSeedingList(); + + if (seedingList) { + WindowManager.mainWindow?.webContents.send( + "on-seeding-list", + JSON.parse(JSON.stringify(seedingList)) + ); + } + } + static async pauseDownload() { if (this.currentDownloader === Downloader.Torrent) { await PythonInstance.pauseDownload(); diff --git a/src/main/services/download/python-instance.ts b/src/main/services/download/python-instance.ts index 8b683e53..f056f8c0 100644 --- a/src/main/services/download/python-instance.ts +++ b/src/main/services/download/python-instance.ts @@ -60,14 +60,15 @@ export class PythonInstance { ); } - public static async getStatus() { - if (this.downloadingGameId === -1) return null; - + public static async getSeedingList() { const response = await this.rpc.get("/status"); - if (response.data === null) return null; + return response.data?.seeding || []; + } - console.log(response.data); + public static async getStatus() { + const response = await this.rpc.get("/status"); + if (response.data?.downloading === null) return null; try { const { @@ -80,7 +81,7 @@ export class PythonInstance { folderName, status, gameId, - } = response.data; + } = response.data?.downloading!; this.downloadingGameId = gameId; diff --git a/src/main/services/download/types.ts b/src/main/services/download/types.ts index b4aac2e1..78e7a2a8 100644 --- a/src/main/services/download/types.ts +++ b/src/main/services/download/types.ts @@ -20,7 +20,7 @@ export enum LibtorrentStatus { Seeding = 5, } -export interface LibtorrentPayload { +export interface LibtorrentGamePayload { progress: number; numPeers: number; numSeeds: number; @@ -32,6 +32,23 @@ export interface LibtorrentPayload { gameId: number; } +export interface LibtorrentPayload { + downloading?: LibtorrentGamePayload; + seeding: LibtorrentGamePayload[]; +} + +export interface LibtorrentSeedingPayload { + progress: number; + numPeers: number; + numSeeds: number; + uploadSpeed: number; + // isCheckingFiles: boolean; + fileSize: number; + folderName: string; + status: LibtorrentStatus; + gameId: number; +} + export interface ProcessPayload { exe: string; pid: number; diff --git a/src/main/services/main-loop.ts b/src/main/services/main-loop.ts index b4836b46..2ef72904 100644 --- a/src/main/services/main-loop.ts +++ b/src/main/services/main-loop.ts @@ -10,6 +10,7 @@ export const startMainLoop = async () => { watchProcesses(), DownloadManager.watchDownloads(), AchievementWatcherManager.watchAchievements(), + DownloadManager.watchSeedingList(), ]); await sleep(1500); diff --git a/src/preload/index.ts b/src/preload/index.ts index d80043f3..5ed7f1f1 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -15,6 +15,7 @@ import type { import type { CatalogueCategory } from "@shared"; import type { AxiosProgressEvent } from "axios"; import { GameAchievement } from "@main/entity"; +import { LibtorrentSeedingPayload } from "@main/services/download/types"; contextBridge.exposeInMainWorld("electron", { /* Torrenting */ @@ -36,6 +37,12 @@ contextBridge.exposeInMainWorld("electron", { ipcRenderer.on("on-download-progress", listener); return () => ipcRenderer.removeListener("on-download-progress", listener); }, + onSeedingList: (cb: (value: LibtorrentSeedingPayload[]) => void) => { + const listener = (_event: Electron.IpcRendererEvent, value: LibtorrentSeedingPayload[]) => cb(value); + ipcRenderer.on("on-seeding-list", listener); + return () => ipcRenderer.removeListener("on-seeding-list", listener); + }, + /* Catalogue */ searchGames: (query: string) => ipcRenderer.invoke("searchGames", query), diff --git a/src/renderer/src/declaration.d.ts b/src/renderer/src/declaration.d.ts index 4065b64c..675c9ab9 100644 --- a/src/renderer/src/declaration.d.ts +++ b/src/renderer/src/declaration.d.ts @@ -49,6 +49,9 @@ declare global { onDownloadProgress: ( cb: (value: DownloadProgress) => void ) => () => Electron.IpcRenderer; + onSeedingList: ( + cb: (value: LibtorrentSeedingPayload[]) => void + ) => () => Electron.IpcRenderer; /* Catalogue */ searchGames: (query: string) => Promise; diff --git a/src/renderer/src/pages/downloads/downloads.tsx b/src/renderer/src/pages/downloads/downloads.tsx index ed541745..9cdb6f4c 100644 --- a/src/renderer/src/pages/downloads/downloads.tsx +++ b/src/renderer/src/pages/downloads/downloads.tsx @@ -2,12 +2,12 @@ import { useTranslation } from "react-i18next"; import { useDownload, useLibrary } from "@renderer/hooks"; -import { useMemo, useRef, useState } from "react"; +import { useEffect, useMemo, useRef, useState } from "react"; import { BinaryNotFoundModal } from "../shared-modals/binary-not-found-modal"; import * as styles from "./downloads.css"; import { DeleteGameModal } from "./delete-game-modal"; import { DownloadGroup } from "./download-group"; -import type { LibraryGame } from "@types"; +import type { LibraryGame, SeedingList } from "@types"; import { orderBy } from "lodash-es"; import { ArrowDownIcon } from "@primer/octicons-react"; @@ -30,6 +30,14 @@ export default function Downloads() { const { lastPacket } = useDownload(); + const [seedingList, setSeedingList] = useState([]); + + useEffect(() => { + window.electron.onSeedingList((value) => setSeedingList(value)); + }, []); + + console.log("sexo", seedingList); + const handleOpenGameInstaller = (gameId: number) => window.electron.openGameInstaller(gameId).then((isBinaryInPath) => { if (!isBinaryInPath) setShowBinaryNotFoundModal(true); diff --git a/src/types/index.ts b/src/types/index.ts index 1ad6e6a1..786516c9 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -151,6 +151,13 @@ export interface DownloadProgress { game: LibraryGame; } +export interface SeedingList { + progress: number; + numPeers: number; + numSeeds: number; + uploadSpeed: number; +} + export interface UserPreferences { downloadsPath: string | null; language: string; diff --git a/torrent-client/torrent_downloader.py b/torrent-client/torrent_downloader.py index b3b7dfe1..500d1bec 100644 --- a/torrent-client/torrent_downloader.py +++ b/torrent-client/torrent_downloader.py @@ -169,6 +169,6 @@ class TorrentDownloader: elif status.state == 5: response['seeding'].append(torrent_info) - print(response) - # return response - return None \ No newline at end of file + # print(response) + return response + # return None \ No newline at end of file