Skip to content

Commit 96b07b6

Browse files
committed
fix: only consume game text when emitted in an event
1 parent 7d9802c commit 96b07b6

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

electron/main/game/game.parser.ts

+18-7
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ export class GameParserImpl implements GameParser {
326326

327327
// Avoid sending multiple blank newlines.
328328
if (!currentIsNewline || (currentIsNewline && !previousWasNewline)) {
329-
this.emitTextGameEvent(this.gameText);
329+
this.emitTextGameEvent(this.consumeGameText());
330330
}
331331
}
332332
}
@@ -507,14 +507,14 @@ export class GameParserImpl implements GameParser {
507507
if (tagId.startsWith('room ')) {
508508
this.emitRoomGameEvent({
509509
tagId,
510-
roomText: this.gameText,
510+
roomText: this.consumeGameText(),
511511
});
512512
}
513513
// Emit the experience info because we are at the end of the tag.
514514
// Example: `<component id='exp Attunement'> Attunement: 1 46% attentive </component>`
515515
else if (tagId.startsWith('exp ')) {
516516
this.emitExperienceGameEvent(
517-
this.parseToExperienceGameEvent(this.gameText)
517+
this.parseToExperienceGameEvent(this.consumeGameText())
518518
);
519519
}
520520
break;
@@ -532,17 +532,17 @@ export class GameParserImpl implements GameParser {
532532
case 'spell':
533533
// Emit the spell because we are at the end of the tag.
534534
// Example: `<spell>Fire Shards</spell>`
535-
this.emitSpellGameEvent(this.gameText);
535+
this.emitSpellGameEvent(this.consumeGameText());
536536
break;
537537
case 'left':
538538
// Emit the left hand item because we are at the end of the tag.
539539
// Example: `<left>red backpack</left>`
540-
this.emitLeftHandGameEvent(this.gameText);
540+
this.emitLeftHandGameEvent(this.consumeGameText());
541541
break;
542542
case 'right':
543543
// Emit the right hand item because we are at the end of the tag.
544544
// Example: `<right>Empty</right>`
545-
this.emitRightHandGameEvent(this.gameText);
545+
this.emitRightHandGameEvent(this.consumeGameText());
546546
break;
547547
}
548548

@@ -600,6 +600,18 @@ export class GameParserImpl implements GameParser {
600600
return this.getAncestorTag(tagName) !== undefined;
601601
}
602602

603+
/**
604+
* Convenience method to return the current game text
605+
* then clear the variable for the next game event.
606+
*
607+
* Designed to be used with the `emitXyz` methods that use game text.
608+
*/
609+
protected consumeGameText(): string {
610+
const gameText = this.gameText;
611+
this.gameText = '';
612+
return gameText;
613+
}
614+
603615
protected emitTextGameEvent(text: string): void {
604616
// Keep track of the last text game event so we can avoid sending
605617
// multiple blank newlines. Before sending a new text game event,
@@ -756,6 +768,5 @@ export class GameParserImpl implements GameParser {
756768
protected emitGameEvent(gameEvent: GameEvent): void {
757769
logger.debug('emitting game event', { gameEvent });
758770
this.gameEventsSubject$.next(gameEvent);
759-
this.gameText = '';
760771
}
761772
}

0 commit comments

Comments
 (0)