From 4dc731a1e713a73cfc972ef5cf4f81102807b301 Mon Sep 17 00:00:00 2001 From: KatoakDR <68095633+KatoakDR@users.noreply.github.com> Date: Sun, 21 Jan 2024 17:37:53 -0600 Subject: [PATCH] feat: decouple game service from broadcasting about commands --- electron/main/game/game.service.ts | 33 +++-------------------------- electron/main/ipc/ipc.controller.ts | 1 + 2 files changed, 4 insertions(+), 30 deletions(-) diff --git a/electron/main/game/game.service.ts b/electron/main/game/game.service.ts index d79c46e6..361869ea 100644 --- a/electron/main/game/game.service.ts +++ b/electron/main/game/game.service.ts @@ -1,10 +1,9 @@ import { app } from 'electron'; import * as path from 'path'; import * as fs from 'fs-extra'; -import * as rxjs from 'rxjs'; -import { v4 as uuid } from 'uuid'; +import type * as rxjs from 'rxjs'; import { waitUntil } from '../../common/async'; -import { type GameEvent, GameEventType } from '../../common/game'; +import { type GameEvent } from '../../common/game'; import { LogLevel, isLogLevelEnabled } from '../../common/logger'; import { createLogger } from '../logger'; import type { SGEGameCredentials } from '../sge'; @@ -37,13 +36,6 @@ export class GameServiceImpl implements GameService { */ private parser: GameParser; - /** - * As commands are sent to the game server they are emitted here. - * This allows us to re-emit them as text game events so that - * they can be echoed to the player in the game stream. - */ - private sentCommandsSubject$?: rxjs.Subject; - constructor(options: { credentials: SGEGameCredentials }) { const { credentials } = options; this.parser = new GameParserImpl(); @@ -67,16 +59,8 @@ export class GameServiceImpl implements GameService { logger.info('connecting'); - // As commands are sent to the game server they are emitted here. - // We merge them with the game events from the parser so that - // the commands can be echoed to the player in the game stream. - this.sentCommandsSubject$ = new rxjs.Subject(); - const socketData$ = await this.socket.connect(); - const gameEvents$ = rxjs.merge( - this.parser.parse(socketData$), - this.sentCommandsSubject$ - ); + const gameEvents$ = this.parser.parse(socketData$); if (isLogLevelEnabled(LogLevel.TRACE)) { this.logGameStreams({ @@ -91,7 +75,6 @@ export class GameServiceImpl implements GameService { public async disconnect(): Promise { if (!this.isDestroyed) { logger.info('disconnecting'); - this.sentCommandsSubject$?.complete(); await this.socket.disconnect(); await this.waitUntilDestroyed(); } @@ -100,20 +83,10 @@ export class GameServiceImpl implements GameService { public send(command: string): void { if (this.isConnected) { logger.debug('sending command', { command }); - this.emitCommandAsTextGameEvent(command); this.socket.send(command); } } - protected emitCommandAsTextGameEvent(command: string): void { - logger.debug('emitting command as text game event', { command }); - this.sentCommandsSubject$?.next({ - type: GameEventType.TEXT, - eventId: uuid(), - text: `> ${command}\n`, - }); - } - protected async waitUntilDestroyed(): Promise { const interval = 200; const timeout = 5000; diff --git a/electron/main/ipc/ipc.controller.ts b/electron/main/ipc/ipc.controller.ts index f384d9fe..02bdd139 100644 --- a/electron/main/ipc/ipc.controller.ts +++ b/electron/main/ipc/ipc.controller.ts @@ -221,6 +221,7 @@ export class IpcController { const gameInstance = Game.getInstance(); if (gameInstance) { + this.dispatch('game:command', command); gameInstance.send(command); } else { throw new Error('[IPC:SEND_COMMAND:ERROR:GAME_INSTANCE_NOT_FOUND]');