Skip to content

Commit

Permalink
feat: decouple game service from broadcasting about commands
Browse files Browse the repository at this point in the history
  • Loading branch information
KatoakDR committed Jan 21, 2024
1 parent 6567dd7 commit 4dc731a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 30 deletions.
33 changes: 3 additions & 30 deletions electron/main/game/game.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<GameEvent>;

constructor(options: { credentials: SGEGameCredentials }) {
const { credentials } = options;
this.parser = new GameParserImpl();
Expand All @@ -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<GameEvent>();

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({
Expand All @@ -91,7 +75,6 @@ export class GameServiceImpl implements GameService {
public async disconnect(): Promise<void> {
if (!this.isDestroyed) {
logger.info('disconnecting');
this.sentCommandsSubject$?.complete();
await this.socket.disconnect();
await this.waitUntilDestroyed();
}
Expand All @@ -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<void> {
const interval = 200;
const timeout = 5000;
Expand Down
1 change: 1 addition & 0 deletions electron/main/ipc/ipc.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]');
Expand Down

0 comments on commit 4dc731a

Please sign in to comment.