Create api.ts
This commit is contained in:
parent
36974fb7c8
commit
c57f49c1af
118
src/main/services/api.ts
Normal file
118
src/main/services/api.ts
Normal file
|
|
@ -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); })
|
||||||
|
}
|
||||||
Reference in a new issue