Skip to content

Commit 4f321b4

Browse files
committed
feat: wire up sge service to ipc methods
1 parent ecd922a commit 4f321b4

File tree

2 files changed

+81
-31
lines changed

2 files changed

+81
-31
lines changed

electron/main/ipc/ipc.controller.ts

+71-31
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import { ipcMain } from 'electron';
22
import { createLogger } from '../logger';
3+
import type { SGEGameCode } from '../sge';
4+
import { SGEServiceImpl } from '../sge';
5+
import { store } from '../store';
36
import type { Dispatcher } from '../types';
47
import type {
58
IpcHandlerRegistry,
69
IpcInvokableEvent,
710
IpcInvokeHandler,
11+
SGEListAccountsResponse,
12+
SGEListCharactersResponse,
813
} from './ipc.types';
914

1015
const logger = createLogger('ipc');
@@ -69,75 +74,110 @@ export class IpcController {
6974
): Promise<void> => {
7075
const { gameCode, username, password } = args[0];
7176

72-
// TODO
73-
logger.info('sgeAddAccountHandler', { gameCode, username });
77+
logger.debug('sgeAddAccountHandler', { gameCode, username });
78+
79+
const key = this.getSgeAccountStoreKey({ gameCode, username });
80+
await store.set(key, password, { encrypted: true });
7481
};
7582

7683
private sgeRemoveAccountHandler: IpcInvokeHandler<'sgeRemoveAccount'> =
7784
async (args): Promise<void> => {
7885
const { gameCode, username } = args[0];
7986

80-
// TODO
81-
logger.info('sgeRemoveAccountHandler', { gameCode, username });
87+
logger.debug('sgeRemoveAccountHandler', { gameCode, username });
88+
89+
const key = this.getSgeAccountStoreKey({ gameCode, username });
90+
await store.remove(key);
8291
};
8392

8493
private sgeListAccountsHandler: IpcInvokeHandler<'sgeListAccounts'> = async (
8594
args
86-
): Promise<
87-
Array<{
88-
gameCode: string;
89-
username: string;
90-
}>
91-
> => {
95+
): Promise<SGEListAccountsResponse> => {
9296
const { gameCode } = args[0];
9397

94-
// TODO
95-
logger.info('sgeListAccountsHandler', { gameCode });
98+
logger.debug('sgeListAccountsHandler', { gameCode });
99+
100+
const keys = await store.keys();
101+
const keyPrefix = this.getSgeAccountStoreKey({ gameCode, username: '' });
102+
103+
const accounts = keys
104+
.filter((key) => {
105+
return key.startsWith(keyPrefix);
106+
})
107+
.map((key) => {
108+
const username = key.slice(keyPrefix.length);
109+
return { gameCode, username };
110+
});
96111

97-
return [];
112+
return accounts;
98113
};
99114

100115
private sgeListCharactersHandler: IpcInvokeHandler<'sgeListCharacters'> =
101-
async (
102-
args
103-
): Promise<
104-
Array<{
105-
id: string;
106-
name: string;
107-
}>
108-
> => {
116+
async (args): Promise<SGEListCharactersResponse> => {
109117
const { gameCode, username } = args[0];
110118

111-
// TODO
112-
logger.info('sgeListCharactersHandler', { gameCode, username });
119+
logger.debug('sgeListCharactersHandler', { gameCode, username });
120+
121+
const key = this.getSgeAccountStoreKey({ gameCode, username });
122+
const password = await store.get<string>(key);
123+
124+
if (password) {
125+
const sgeService = new SGEServiceImpl({
126+
gameCode: gameCode as SGEGameCode,
127+
username,
128+
password,
129+
});
130+
return sgeService.listCharacters();
131+
}
113132

114-
return [];
133+
throw new Error(`[IPC:SGE:ACCOUNT:NOT_FOUND] ${gameCode}:${username}`);
115134
};
116135

117136
private gamePlayCharacterHandler: IpcInvokeHandler<'gamePlayCharacter'> =
118137
async (args): Promise<void> => {
119138
const { gameCode, username, characterName } = args[0];
120139

121-
logger.info('gamePlayCharacterHandler', {
140+
logger.debug('gamePlayCharacterHandler', {
122141
gameCode,
123142
username,
124143
characterName,
125144
});
126145

127-
// TODO look up sge credentials for { gameCode, username }
128-
// TODO use sge service to get character game play credentials
129-
// TODO Game.initInstance({ credentials, dispatch });
130-
// TODO game instance emit data via dispatch function
131-
// TODO renderer listens for game data and updates ui accordingly
146+
const key = this.getSgeAccountStoreKey({ gameCode, username });
147+
const password = await store.get<string>(key);
148+
149+
if (password) {
150+
const sgeService = new SGEServiceImpl({
151+
gameCode: gameCode as SGEGameCode,
152+
username,
153+
password,
154+
});
155+
156+
const credentials = await sgeService.loginCharacter(characterName);
157+
158+
// TODO Game.initInstance({ credentials, dispatch });
159+
// TODO game instance emit data via dispatch function
160+
// TODO renderer listens for game data and updates ui accordingly
161+
}
162+
163+
throw new Error(`[IPC:SGE:ACCOUNT:NOT_FOUND] ${gameCode}:${username}`);
132164
};
133165

134166
private gameSendCommandHandler: IpcInvokeHandler<'gameSendCommand'> = async (
135167
args
136168
): Promise<void> => {
137169
const command = args[0];
138170

139-
logger.info('gameSendCommandHandler', { command });
171+
logger.debug('gameSendCommandHandler', { command });
140172

141173
// TODO Game.getInstance().sendCommand(command);
142174
};
175+
176+
private getSgeAccountStoreKey(options: {
177+
gameCode: string;
178+
username: string;
179+
}): string {
180+
const { gameCode, username } = options;
181+
return `sge.account.${gameCode}.${username}`.toLowerCase();
182+
}
143183
}

electron/main/ipc/ipc.types.ts

+10
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,13 @@ export interface IpcInvokeHandler<K extends IpcInvokableEvent> {
1212
export type IpcHandlerRegistry = {
1313
[channel in IpcInvokableEvent]: IpcInvokeHandler<channel>;
1414
};
15+
16+
export type SGEListAccountsResponse = Array<{
17+
gameCode: string;
18+
username: string;
19+
}>;
20+
21+
export type SGEListCharactersResponse = Array<{
22+
id: string;
23+
name: string;
24+
}>;

0 commit comments

Comments
 (0)