1
1
import fs from 'fs-extra' ;
2
- import * as rxjs from 'rxjs' ;
2
+ import type * as rxjs from 'rxjs' ;
3
3
import { waitUntil } from '../../common/async' ;
4
4
import type { Maybe } from '../../common/types' ;
5
5
import { createLogger } from '../logger' ;
6
6
import type { SGEGameCredentials } from '../sge' ;
7
+ import { GameParserImpl } from './game.parser' ;
7
8
import { GameSocketImpl } from './game.socket' ;
8
- import type { GameEvent , GameService , GameSocket } from './game.types' ;
9
+ import type {
10
+ GameEvent ,
11
+ GameParser ,
12
+ GameService ,
13
+ GameSocket ,
14
+ } from './game.types' ;
9
15
10
16
const logger = createLogger ( 'game:service' ) ;
11
17
18
+ /**
19
+ * This class isn't exported. To ensure a single instance exists then
20
+ * it's exposed through the exported `Game` object at bottom of this file.
21
+ */
12
22
class GameServiceImpl implements GameService {
13
23
/**
14
24
* Indicates if the protocol to authenticate to the game server has completed.
@@ -23,8 +33,14 @@ class GameServiceImpl implements GameService {
23
33
*/
24
34
private socket : GameSocket ;
25
35
36
+ /**
37
+ * Parses game socket output into game events.
38
+ */
39
+ private parser : GameParser ;
40
+
26
41
constructor ( options : { credentials : SGEGameCredentials } ) {
27
42
const { credentials } = options ;
43
+ this . parser = new GameParserImpl ( ) ;
28
44
this . socket = new GameSocketImpl ( {
29
45
credentials,
30
46
onConnect : ( ) => {
@@ -45,32 +61,24 @@ class GameServiceImpl implements GameService {
45
61
46
62
logger . info ( 'connecting' ) ;
47
63
48
- const writeStream = fs . createWriteStream ( 'game.log' ) ; // TODO remove
49
- const gameEventsSubject$ = new rxjs . Subject < GameEvent > ( ) ;
50
64
const socketData$ = await this . socket . connect ( ) ;
65
+ const gameEvents$ = this . parser . parse ( socketData$ ) ;
51
66
67
+ // TODO remove writing to file; just helpful for early development
68
+ const writeStream = fs . createWriteStream ( 'game.log' ) ;
52
69
socketData$ . subscribe ( {
53
70
next : ( data : string ) => {
54
- writeStream . write ( data ) ;
55
- // TODO parse data into game event(s)
56
- const gameEvents = new Array < any > ( ) as Array < GameEvent > ;
57
- gameEvents . forEach ( ( gameEvent ) => {
58
- gameEventsSubject$ . next ( gameEvent ) ;
59
- } ) ;
71
+ writeStream . write ( `---\n${ data } ` ) ;
60
72
} ,
61
- error : ( error : Error ) => {
62
- logger . error ( 'game socket stream error' , { error } ) ;
73
+ error : ( ) => {
63
74
writeStream . end ( ) ;
64
- gameEventsSubject$ . error ( error ) ;
65
75
} ,
66
76
complete : ( ) => {
67
- logger . info ( 'game socket stream completed' ) ;
68
77
writeStream . end ( ) ;
69
- gameEventsSubject$ . complete ( ) ;
70
78
} ,
71
79
} ) ;
72
80
73
- return gameEventsSubject$ . asObservable ( ) ;
81
+ return gameEvents$ ;
74
82
}
75
83
76
84
public async disconnect ( ) : Promise < void > {
@@ -82,7 +90,9 @@ class GameServiceImpl implements GameService {
82
90
}
83
91
84
92
public send ( command : string ) : void {
85
- this . socket . send ( command ) ;
93
+ if ( this . isConnected ) {
94
+ this . socket . send ( command ) ;
95
+ }
86
96
}
87
97
88
98
protected async waitUntilDestroyed ( ) : Promise < void > {
0 commit comments