|
| 1 | +import { ipcMain } from 'electron'; |
| 2 | +import type { AppAPI } from '../../preload'; |
| 3 | +import { createLogger } from '../logger'; |
| 4 | +import type { SGEGameCode } from '../sge'; |
| 5 | +import { SGEServiceImpl } from '../sge'; |
| 6 | +import type { IpcHandlerRegistry, IpcInvokeHandler } from './ipc.types'; |
| 7 | + |
| 8 | +const logger = createLogger('ipc'); |
| 9 | + |
| 10 | +const pingHandler: IpcInvokeHandler<'ping'> = async (): Promise<string> => { |
| 11 | + return 'pong'; |
| 12 | +}; |
| 13 | + |
| 14 | +const sgeListCharactersHandler: IpcInvokeHandler<'sgeListCharacters'> = async ( |
| 15 | + args |
| 16 | +) => { |
| 17 | + const { username, password, gameCode } = args[0]; |
| 18 | + |
| 19 | + logger.debug('sgeListCharacters', { username, gameCode }); |
| 20 | + |
| 21 | + const sgeService = new SGEServiceImpl({ |
| 22 | + username, |
| 23 | + password, |
| 24 | + gameCode: gameCode as SGEGameCode, |
| 25 | + }); |
| 26 | + |
| 27 | + const characters = await sgeService.listCharacters(); |
| 28 | + |
| 29 | + logger.debug('sgeListCharacters', { characters }); |
| 30 | + |
| 31 | + return characters; |
| 32 | +}; |
| 33 | + |
| 34 | +const sgePlayCharacterHandler: IpcInvokeHandler<'sgePlayCharacter'> = async ( |
| 35 | + args |
| 36 | +) => { |
| 37 | + const { username, password, gameCode, characterName } = args[0]; |
| 38 | + |
| 39 | + logger.debug('sgePlayCharacter', { username, gameCode, characterName }); |
| 40 | + |
| 41 | + const sgeService = new SGEServiceImpl({ |
| 42 | + username, |
| 43 | + password, |
| 44 | + gameCode: gameCode as SGEGameCode, |
| 45 | + }); |
| 46 | + |
| 47 | + const credentials = await sgeService.loginCharacter(characterName); |
| 48 | + |
| 49 | + logger.debug('sgePlayCharacter', { credentials }); |
| 50 | +}; |
| 51 | + |
| 52 | +const ipcHandlerRegistry: IpcHandlerRegistry = { |
| 53 | + ping: pingHandler, |
| 54 | + sgeListCharacters: sgeListCharactersHandler, |
| 55 | + sgePlayCharacter: sgePlayCharacterHandler, |
| 56 | +}; |
| 57 | + |
| 58 | +export const registerIpcHandlers = (): void => { |
| 59 | + Object.keys(ipcHandlerRegistry).forEach((channel) => { |
| 60 | + const handler = ipcHandlerRegistry[channel as keyof AppAPI]; |
| 61 | + |
| 62 | + if (!handler) { |
| 63 | + logger.error('no handler registered for channel', { channel }); |
| 64 | + throw new Error(`[IPC:CHANNEL:INVALID] ${channel}`); |
| 65 | + } |
| 66 | + |
| 67 | + ipcMain.handle(channel, async (_event, ...params) => { |
| 68 | + try { |
| 69 | + logger.debug('handling channel request', { channel }); |
| 70 | + const result = await handler(params as any); |
| 71 | + logger.debug('handled channel request', { channel }); |
| 72 | + return result; |
| 73 | + } catch (error) { |
| 74 | + logger.error('error handling channel request', { channel, error }); |
| 75 | + throw new Error(`[IPC:CHANNEL:ERROR] ${channel}: ${error?.message}`); |
| 76 | + } |
| 77 | + }); |
| 78 | + }); |
| 79 | +}; |
0 commit comments