From c57f49c1af0915998f80f20822ce4655d0d359e6 Mon Sep 17 00:00:00 2001 From: TheThomaas Date: Thu, 5 Feb 2026 15:25:40 +0100 Subject: [PATCH] Create api.ts --- src/main/services/api.ts | 118 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 src/main/services/api.ts diff --git a/src/main/services/api.ts b/src/main/services/api.ts new file mode 100644 index 0000000..43fef5a --- /dev/null +++ b/src/main/services/api.ts @@ -0,0 +1,118 @@ +import { ipcMain } from "electron" +import { + Game, + addGame, + updateGame, + deleteGame, + getAllGames, + getGame +} from './database.js'; +import { spawn } from "child_process"; + +export let umbraApi = { + getGames: "GET /games", + getRecentGames: "POST /games/recent", + addGame: "POST /games", + getGame: "GET /game", + updateGame: "PUT /game", + removeGame: "DELETE /game", + // startGame: "POST /game/launch", + getGameStatus: "GET /game/status", + stopGame: "POST /game/stop", +} + +export function init() { + // Get all games { page, limit } + ipcMain.handle(umbraApi.getGames, async (args) => { return getAllGames(args); }) + + // Get recent games { page, limit, since } + ipcMain.handle(umbraApi.getRecentGames, async (args) => { return getAllGames(args); }) + + // Add Game + ipcMain.handle(umbraApi.addGame, async (_, game: Game) => { return addGame(game); }) + + // Get Game + ipcMain.handle(umbraApi.getGame, async (_, id: number) => { return getGame(id); }) + + // Update Game + ipcMain.handle(umbraApi.updateGame, async (_, game: Game) => { return updateGame(game); }) + + // Remove Game + ipcMain.handle(umbraApi.removeGame, async (_, id: number) => { return deleteGame(id); }) + + // Start Game + // ipcMain.handle(umbraApi.startGame, async (_, { id, path }) => { + // // console.log(getGame(id)) + // try { + // let game = getGame(id); + // const appProcess = spawn(path, [], { detached: true, stdio: 'ignore' }); + // await updateGame({ ...game, is_running: 1}); + + // appProcess.on('data', (data) => { + // console.log(`stdout: ${data}`); + // }); + + // // Surveiller la fermeture de l'application + // appProcess.on('close', async (code) => { + // console.log(`child process close all stdio with code ${code}`); + // await updateGame({ ...game, is_running: 0}); + // console.log(game, getGame(id)) + // }); + + // /*appProcess.on('exit', async (code) => { + // console.log(`child process exited with code ${code}`); + // await updateGame({ ...game, is_running: 0}); + // });*/ + + // appProcess.on('error', async (err) => { + // console.error(`Erreur avec l'application ${id} : ${err.message}`); + // await updateGame({ ...game, is_running: 0}); + // }); + // } catch (err: any) { + // console.error(`Erreur lors du lancement de l'application ${id} : ${err.message}`); + // } + // }) + + // ipcMain.handle(umbraApi.startGame, async (_, { id, path }) => { ipcRenderer.send('launch-app', { id, path }) }) + ipcMain.on("startGame", async (event, { id, path }) => { + // ipcMain.on(umbraApi.startGame, async (event, { id, path }) => { + try { + let game = getGame(id); + const appProcess = spawn(path, [], { detached: true, stdio: 'ignore' }); + await updateGame({ ...game, is_running: 1}); + event.reply('app-status', { id, status: 1 }); + + appProcess.on('data', (data) => { + console.log(`stdout: ${data}`); + }); + + // Surveiller la fermeture de l'application + appProcess.on('close', async (code) => { + console.log(`child process close all stdio with code ${code}`); + event.reply('app-status', { id, status: 0 }); + await updateGame({ ...game, is_running: 0}); + }); + + /*appProcess.on('exit', async (code) => { + console.log(`child process exited with code ${code}`); + event.reply('app-status', { id, status: 0 }); + await updateGame({ ...game, is_running: 0}); + });*/ + + appProcess.on('error', async (err) => { + console.error(`Erreur avec l'application ${id} : ${err.message}`); + event.reply('app-status', { id, status: 0 }); + await updateGame({ ...game, is_running: 0}); + }); + } catch (err: any) { + console.error(`Erreur lors du lancement de l'application ${id} : ${err.message}`); + // event.reply('app-status', { id, status: `${err.message}` }); + } + }); + + // Get Game Status + ipcMain.handle(umbraApi.getGameStatus, async (_, id) => { return getGame(id).is_running; }) + + // Stop Game + ipcMain.handle(umbraApi.stopGame, async (args) => { return getAllGames(args); }) +} \ No newline at end of file