Skip to content

Commit e90d6f7

Browse files
committed
feat: stub out new ipc methods
1 parent 5066f83 commit e90d6f7

File tree

6 files changed

+129
-68
lines changed

6 files changed

+129
-68
lines changed

electron/main/ipc.ts

-62
This file was deleted.

electron/main/ipc/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './ipc';
2+
export * from './ipc.types';

electron/main/ipc/ipc.ts

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
};

electron/main/ipc/ipc.types.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export interface IpcInvokeHandler<K extends keyof AppAPI> {
2+
(params: Parameters<AppAPI[K]>): ReturnType<AppAPI[K]>;
3+
}
4+
5+
export type IpcHandlerRegistry = {
6+
[channel in keyof AppAPI]: IpcInvokeHandler<channel>;
7+
};

electron/preload/index.d.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,28 @@
33
*/
44
declare const appAPI: {
55
ping: () => Promise<string>;
6-
speak: (text: string) => Promise<void>;
7-
climb: (data: { height: number }) => Promise<number>;
6+
/**
7+
* List available characters for a given play.net account.
8+
*/
9+
sgeListCharacters: (options: {
10+
username: string;
11+
password: string;
12+
gameCode: string;
13+
}) => Promise<
14+
Array<{
15+
id: string;
16+
name: string;
17+
}>
18+
>;
19+
/**
20+
* Log in to game with a given character.
21+
*/
22+
sgePlayCharacter: (options: {
23+
username: string;
24+
password: string;
25+
gameCode: string;
26+
characterName: string;
27+
}) => Promise<void>;
828
};
929
declare global {
1030
type TypeOfAppAPI = typeof appAPI;

electron/preload/index.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,26 @@ const appAPI = {
1010
ping: async (): Promise<string> => {
1111
return ipcRenderer.invoke('ping');
1212
},
13-
speak: async (text: string): Promise<void> => {
14-
return ipcRenderer.invoke('speak', text);
13+
/**
14+
* List available characters for a given play.net account.
15+
*/
16+
sgeListCharacters: async (options: {
17+
username: string;
18+
password: string;
19+
gameCode: string;
20+
}): Promise<Array<{ id: string; name: string }>> => {
21+
return ipcRenderer.invoke('sgeListCharacters', options);
1522
},
16-
climb: async (data: { height: number }): Promise<number> => {
17-
return ipcRenderer.invoke('climb', data);
23+
/**
24+
* Log in to game with a given character.
25+
*/
26+
sgePlayCharacter: async (options: {
27+
username: string;
28+
password: string;
29+
gameCode: string;
30+
characterName: string;
31+
}): Promise<void> => {
32+
return ipcRenderer.invoke('sgePlayCharacter', options);
1833
},
1934
};
2035

0 commit comments

Comments
 (0)