@@ -326,7 +326,7 @@ export class GameParserImpl implements GameParser {
326
326
327
327
// Avoid sending multiple blank newlines.
328
328
if ( ! currentIsNewline || ( currentIsNewline && ! previousWasNewline ) ) {
329
- this . emitTextGameEvent ( this . gameText ) ;
329
+ this . emitTextGameEvent ( this . consumeGameText ( ) ) ;
330
330
}
331
331
}
332
332
}
@@ -507,14 +507,14 @@ export class GameParserImpl implements GameParser {
507
507
if ( tagId . startsWith ( 'room ' ) ) {
508
508
this . emitRoomGameEvent ( {
509
509
tagId,
510
- roomText : this . gameText ,
510
+ roomText : this . consumeGameText ( ) ,
511
511
} ) ;
512
512
}
513
513
// Emit the experience info because we are at the end of the tag.
514
514
// Example: `<component id='exp Attunement'> Attunement: 1 46% attentive </component>`
515
515
else if ( tagId . startsWith ( 'exp ' ) ) {
516
516
this . emitExperienceGameEvent (
517
- this . parseToExperienceGameEvent ( this . gameText )
517
+ this . parseToExperienceGameEvent ( this . consumeGameText ( ) )
518
518
) ;
519
519
}
520
520
break ;
@@ -532,17 +532,17 @@ export class GameParserImpl implements GameParser {
532
532
case 'spell' :
533
533
// Emit the spell because we are at the end of the tag.
534
534
// Example: `<spell>Fire Shards</spell>`
535
- this . emitSpellGameEvent ( this . gameText ) ;
535
+ this . emitSpellGameEvent ( this . consumeGameText ( ) ) ;
536
536
break ;
537
537
case 'left' :
538
538
// Emit the left hand item because we are at the end of the tag.
539
539
// Example: `<left>red backpack</left>`
540
- this . emitLeftHandGameEvent ( this . gameText ) ;
540
+ this . emitLeftHandGameEvent ( this . consumeGameText ( ) ) ;
541
541
break ;
542
542
case 'right' :
543
543
// Emit the right hand item because we are at the end of the tag.
544
544
// Example: `<right>Empty</right>`
545
- this . emitRightHandGameEvent ( this . gameText ) ;
545
+ this . emitRightHandGameEvent ( this . consumeGameText ( ) ) ;
546
546
break ;
547
547
}
548
548
@@ -600,6 +600,18 @@ export class GameParserImpl implements GameParser {
600
600
return this . getAncestorTag ( tagName ) !== undefined ;
601
601
}
602
602
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
+
603
615
protected emitTextGameEvent ( text : string ) : void {
604
616
// Keep track of the last text game event so we can avoid sending
605
617
// multiple blank newlines. Before sending a new text game event,
@@ -756,6 +768,5 @@ export class GameParserImpl implements GameParser {
756
768
protected emitGameEvent ( gameEvent : GameEvent ) : void {
757
769
logger . debug ( 'emitting game event' , { gameEvent } ) ;
758
770
this . gameEventsSubject$ . next ( gameEvent ) ;
759
- this . gameText = '' ;
760
771
}
761
772
}
0 commit comments