Skip to content

Commit 4dc731a

Browse files
committed
feat: decouple game service from broadcasting about commands
1 parent 6567dd7 commit 4dc731a

File tree

2 files changed

+4
-30
lines changed

2 files changed

+4
-30
lines changed

electron/main/game/game.service.ts

+3-30
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { app } from 'electron';
22
import * as path from 'path';
33
import * as fs from 'fs-extra';
4-
import * as rxjs from 'rxjs';
5-
import { v4 as uuid } from 'uuid';
4+
import type * as rxjs from 'rxjs';
65
import { waitUntil } from '../../common/async';
7-
import { type GameEvent, GameEventType } from '../../common/game';
6+
import { type GameEvent } from '../../common/game';
87
import { LogLevel, isLogLevelEnabled } from '../../common/logger';
98
import { createLogger } from '../logger';
109
import type { SGEGameCredentials } from '../sge';
@@ -37,13 +36,6 @@ export class GameServiceImpl implements GameService {
3736
*/
3837
private parser: GameParser;
3938

40-
/**
41-
* As commands are sent to the game server they are emitted here.
42-
* This allows us to re-emit them as text game events so that
43-
* they can be echoed to the player in the game stream.
44-
*/
45-
private sentCommandsSubject$?: rxjs.Subject<GameEvent>;
46-
4739
constructor(options: { credentials: SGEGameCredentials }) {
4840
const { credentials } = options;
4941
this.parser = new GameParserImpl();
@@ -67,16 +59,8 @@ export class GameServiceImpl implements GameService {
6759

6860
logger.info('connecting');
6961

70-
// As commands are sent to the game server they are emitted here.
71-
// We merge them with the game events from the parser so that
72-
// the commands can be echoed to the player in the game stream.
73-
this.sentCommandsSubject$ = new rxjs.Subject<GameEvent>();
74-
7562
const socketData$ = await this.socket.connect();
76-
const gameEvents$ = rxjs.merge(
77-
this.parser.parse(socketData$),
78-
this.sentCommandsSubject$
79-
);
63+
const gameEvents$ = this.parser.parse(socketData$);
8064

8165
if (isLogLevelEnabled(LogLevel.TRACE)) {
8266
this.logGameStreams({
@@ -91,7 +75,6 @@ export class GameServiceImpl implements GameService {
9175
public async disconnect(): Promise<void> {
9276
if (!this.isDestroyed) {
9377
logger.info('disconnecting');
94-
this.sentCommandsSubject$?.complete();
9578
await this.socket.disconnect();
9679
await this.waitUntilDestroyed();
9780
}
@@ -100,20 +83,10 @@ export class GameServiceImpl implements GameService {
10083
public send(command: string): void {
10184
if (this.isConnected) {
10285
logger.debug('sending command', { command });
103-
this.emitCommandAsTextGameEvent(command);
10486
this.socket.send(command);
10587
}
10688
}
10789

108-
protected emitCommandAsTextGameEvent(command: string): void {
109-
logger.debug('emitting command as text game event', { command });
110-
this.sentCommandsSubject$?.next({
111-
type: GameEventType.TEXT,
112-
eventId: uuid(),
113-
text: `> ${command}\n`,
114-
});
115-
}
116-
11790
protected async waitUntilDestroyed(): Promise<void> {
11891
const interval = 200;
11992
const timeout = 5000;

electron/main/ipc/ipc.controller.ts

+1
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ export class IpcController {
221221
const gameInstance = Game.getInstance();
222222

223223
if (gameInstance) {
224+
this.dispatch('game:command', command);
224225
gameInstance.send(command);
225226
} else {
226227
throw new Error('[IPC:SEND_COMMAND:ERROR:GAME_INSTANCE_NOT_FOUND]');

0 commit comments

Comments
 (0)