@@ -16,6 +16,9 @@ logger.info(`Server version ${version}`);
16
16
const serverId = short . generate ( ) ;
17
17
logger . info ( `Server ${ serverId } ` ) ;
18
18
19
+ const ENABLE_COHERENCE_BACKEND =
20
+ process . env . ENABLE_COHERENCE_BACKEND === "true" ;
21
+
19
22
const BROADCAST_REFRESH_UPDATE = process . env . BROADCAST_REFRESH_UPDATE
20
23
? parseInt ( process . env . BROADCAST_REFRESH_UPDATE )
21
24
: 50 ;
@@ -63,11 +66,17 @@ export async function start(
63
66
io . adapter ( createAdapter ( pubClient , subClient ) ) ;
64
67
}
65
68
66
- // FIXME consider ENABLE_COHERENCE_BACKEND flag
67
- mapPlayersTraces = await cacheSession . getMap ( "playerTraces" ) ;
68
- mapPlayersInfo = await cacheSession . getMap ( "playersInfo" ) ;
69
- mapTrash = await cacheSession . getMap ( "trash" ) ;
70
- mapMarineLife = await cacheSession . getMap ( "marineLife" ) ;
69
+ if ( ENABLE_COHERENCE_BACKEND ) {
70
+ mapPlayersTraces = await cacheSession . getMap ( "playerTraces" ) ;
71
+ mapPlayersInfo = await cacheSession . getMap ( "playersInfo" ) ;
72
+ mapTrash = await cacheSession . getMap ( "trash" ) ;
73
+ mapMarineLife = await cacheSession . getMap ( "marineLife" ) ;
74
+ } else {
75
+ mapPlayersTraces = { } ;
76
+ mapPlayersInfo = { } ;
77
+ mapTrash = { } ;
78
+ mapMarineLife = { } ;
79
+ }
71
80
72
81
io . on ( "connection" , async ( socket ) => {
73
82
let playerIdForSocket ;
@@ -80,23 +89,41 @@ export async function start(
80
89
worldSizeZ : WORLD_SIZE_Z ,
81
90
} ) ;
82
91
83
- const trashFromCache = await readCacheEntries ( mapTrash ) ;
84
- const marineLifeFromCache = await readCacheEntries ( mapMarineLife ) ;
92
+ const trashFromCache = ENABLE_COHERENCE_BACKEND
93
+ ? await readCacheEntries ( mapTrash )
94
+ : mapTrash ;
95
+ const marineLifeFromCache = ENABLE_COHERENCE_BACKEND
96
+ ? await readCacheEntries ( mapMarineLife )
97
+ : mapMarineLife ;
85
98
socket . emit ( "items.all" , { ...trashFromCache , ...marineLifeFromCache } ) ;
86
99
87
100
// FIXME scope this to surrounding players only
88
- socket . emit ( "player.info.all" , await readCacheEntries ( mapPlayersInfo ) ) ;
101
+ socket . emit (
102
+ "player.info.all" ,
103
+ ENABLE_COHERENCE_BACKEND
104
+ ? await readCacheEntries ( mapPlayersInfo )
105
+ : mapPlayersInfo
106
+ ) ;
89
107
90
108
// FIXME send random starting position
91
109
socket . emit ( "game.on" , { } ) ;
92
110
93
111
socket . on ( "player.info.joining" , async ( { id, name } ) => {
94
- await writeCache ( mapPlayersInfo , id , { name } ) ;
112
+ if ( ENABLE_COHERENCE_BACKEND ) {
113
+ await writeCache ( mapPlayersInfo , id , { name } ) ;
114
+ } else {
115
+ mapPlayersInfo [ id ] = { name } ;
116
+ }
95
117
} ) ;
96
118
97
119
socket . on ( "game.start" , async ( { playerId, playerName } ) => {
98
120
playerIdForSocket = playerId ;
99
- await writeCache ( mapPlayersInfo , playerId , { name : playerName } ) ;
121
+ const body = { name : playerName } ;
122
+ if ( ENABLE_COHERENCE_BACKEND ) {
123
+ await writeCache ( mapPlayersInfo , playerId , body ) ;
124
+ } else {
125
+ mapPlayersInfo [ playerId ] = body ;
126
+ }
100
127
io . emit ( "player.info.joined" , {
101
128
id : playerId ,
102
129
name : playerName ,
@@ -105,29 +132,48 @@ export async function start(
105
132
setTimeout ( async ( ) => {
106
133
socket . emit ( "game.end" , { playerId } ) ;
107
134
io . emit ( "player.info.left" , playerId ) ;
108
- await deleteCache ( mapPlayersTraces , playerId ) ;
135
+ if ( ENABLE_COHERENCE_BACKEND ) {
136
+ await deleteCache ( mapPlayersTraces , playerId ) ;
137
+ await deleteCache ( mapPlayersInfo , playerId ) ;
138
+ } else {
139
+ delete mapPlayersTraces [ playerId ] ;
140
+ delete mapPlayersInfo [ playerId ] ;
141
+ }
109
142
await deleteCurrentScore ( playerId ) ;
110
- await deleteCache ( mapPlayersInfo , playerId ) ;
111
143
playerIdForSocket = undefined ;
112
144
} , GAME_DURATION_IN_SECONDS * 1000 ) ;
113
145
} ) ;
114
146
115
147
socket . on ( "player.trace.change" , async ( { id, ...traceData } ) => {
116
148
const body = { ...traceData , updated : new Date ( ) } ;
117
- await writeCache ( mapPlayersTraces , id , body ) ;
149
+ if ( ENABLE_COHERENCE_BACKEND ) {
150
+ await writeCache ( mapPlayersTraces , id , body ) ;
151
+ } else {
152
+ mapPlayersTraces [ id ] = body ;
153
+ }
118
154
} ) ;
119
155
120
156
socket . on ( "items.collision" , async ( { itemId, playerId, playerName } ) => {
121
- if ( await mapTrash . has ( itemId ) ) {
122
- await deleteCache ( mapTrash , itemId ) ;
157
+ const existTrash = ENABLE_COHERENCE_BACKEND
158
+ ? await mapTrash . has ( itemId )
159
+ : mapTrash [ itemId ] ;
160
+ const existMarineLife = ENABLE_COHERENCE_BACKEND
161
+ ? await mapMarineLife . has ( itemId )
162
+ : mapMarineLife [ itemId ] ;
163
+ if ( existTrash ) {
164
+ ENABLE_COHERENCE_BACKEND
165
+ ? await deleteCache ( mapTrash , itemId )
166
+ : delete mapTrash [ itemId ] ;
123
167
io . emit ( "item.destroy" , itemId ) ;
124
168
const jsonResponse = await postCurrentScore (
125
169
playerId ,
126
170
playerName ,
127
171
"INCREMENT"
128
172
) ;
129
- } else if ( await mapMarineLife . has ( itemId ) ) {
130
- await deleteCache ( mapMarineLife , itemId ) ;
173
+ } else if ( existMarineLife ) {
174
+ ENABLE_COHERENCE_BACKEND
175
+ ? await deleteCache ( mapMarineLife , itemId )
176
+ : delete mapMarineLife [ itemId ] ;
131
177
io . emit ( "item.destroy" , itemId ) ;
132
178
const jsonResponse = await postCurrentScore (
133
179
playerId ,
@@ -139,8 +185,13 @@ export async function start(
139
185
140
186
socket . on ( "disconnect" , async ( reason ) => {
141
187
io . emit ( "player.info.left" , playerIdForSocket ) ;
142
- await deleteCache ( mapPlayersTraces , playerIdForSocket ) ;
143
- await deleteCache ( mapPlayersInfo , playerIdForSocket ) ;
188
+ if ( ENABLE_COHERENCE_BACKEND ) {
189
+ await deleteCache ( mapPlayersTraces , playerIdForSocket ) ;
190
+ await deleteCache ( mapPlayersInfo , playerIdForSocket ) ;
191
+ } else {
192
+ delete mapPlayersTraces [ playerIdForSocket ] ;
193
+ delete mapPlayersInfo [ playerIdForSocket ] ;
194
+ }
144
195
logger . info ( `${ playerIdForSocket } disconnected because ${ reason } ` ) ;
145
196
playerIdForSocket = undefined ;
146
197
} ) ;
@@ -153,15 +204,23 @@ export async function start(
153
204
// broadcast all players traces
154
205
setInterval ( async ( ) => {
155
206
// FIXME scope this to surrounding players only
156
- const traces = await readCacheEntries ( mapPlayersTraces ) ;
207
+ const traces = ENABLE_COHERENCE_BACKEND
208
+ ? await readCacheEntries ( mapPlayersTraces )
209
+ : mapPlayersTraces ;
157
210
io . emit ( "player.trace.all" , traces ) ;
158
211
} , BROADCAST_REFRESH_UPDATE ) ;
159
212
160
213
// refresh items
161
214
setInterval ( async ( ) => {
162
- const numberOfPlayers = await mapPlayersInfo . size ;
163
- const numberOfTrash = await mapTrash . size ;
164
- const numberOfMarineLife = await mapMarineLife . size ;
215
+ const numberOfPlayers = ENABLE_COHERENCE_BACKEND
216
+ ? await mapPlayersInfo . size
217
+ : Object . keys ( mapPlayersInfo ) . length ;
218
+ const numberOfTrash = ENABLE_COHERENCE_BACKEND
219
+ ? await mapTrash . size
220
+ : Object . keys ( mapTrash ) . length ;
221
+ const numberOfMarineLife = ENABLE_COHERENCE_BACKEND
222
+ ? await mapMarineLife . size
223
+ : Object . keys ( mapMarineLife ) . length ;
165
224
const deltaOfDesiredNumberOfTrash = numberOfPlayers - numberOfTrash ;
166
225
const deltaOfDesiredNumberOfMarineLife =
167
226
numberOfPlayers * 2 - numberOfMarineLife ;
@@ -171,18 +230,21 @@ export async function start(
171
230
extraTrashPoll [ id ] = trashData ;
172
231
io . emit ( "item.new" , { id, data : extraTrashPoll [ id ] } ) ;
173
232
}
174
- Object . keys ( extraTrashPoll ) . forEach (
175
- async ( key ) => await writeCache ( mapTrash , key , extraTrashPoll [ key ] )
176
- ) ;
233
+ Object . keys ( extraTrashPoll ) . forEach ( async ( key ) => {
234
+ ENABLE_COHERENCE_BACKEND
235
+ ? await writeCache ( mapTrash , key , extraTrashPoll [ key ] )
236
+ : ( mapTrash [ key ] = extraTrashPoll [ key ] ) ;
237
+ } ) ;
177
238
const extraMarineLifePoll = { } ;
178
239
for ( let index = 0 ; index < deltaOfDesiredNumberOfMarineLife ; index ++ ) {
179
240
const { id, ...marineLifeData } = createObject ( "turtle" ) ;
180
241
extraMarineLifePoll [ id ] = marineLifeData ;
181
242
io . emit ( "item.new" , { id, data : extraMarineLifePoll [ id ] } ) ;
182
243
}
183
- Object . keys ( extraMarineLifePoll ) . forEach (
184
- async ( key ) =>
185
- await writeCache ( mapMarineLife , key , extraMarineLifePoll [ key ] )
244
+ Object . keys ( extraMarineLifePoll ) . forEach ( async ( key ) =>
245
+ ENABLE_COHERENCE_BACKEND
246
+ ? await writeCache ( mapMarineLife , key , extraMarineLifePoll [ key ] )
247
+ : ( mapMarineLife [ key ] = extraMarineLifePoll [ key ] )
186
248
) ;
187
249
} , BROADCAST_ITEMS_IN_SECONDS * 1000 ) ;
188
250
@@ -203,24 +265,37 @@ export async function start(
203
265
// FIXME can we delete them without adding elapsed to all of them?
204
266
// FIXME can we use TTL from Coherence
205
267
// FIXME do we need clean up stales to begin with?
206
- const traces = await readCacheEntries ( mapPlayersTraces ) ;
268
+ const traces = ENABLE_COHERENCE_BACKEND
269
+ ? await readCacheEntries ( mapPlayersTraces )
270
+ : mapPlayersTraces ;
207
271
const elapsedTimesById = Object . entries ( traces ) . map ( ( player ) => ( {
208
272
id : player [ 0 ] ,
209
273
elapsed : now - player [ 1 ] . updated ,
210
274
} ) ) ;
211
275
const staleIds = elapsedTimesById . filter ( ( e ) => e . elapsed > 250 ) ;
212
276
staleIds . forEach ( async ( p ) => {
213
277
logger . info ( `Stale player ${ p . id } by ${ p . elapsed } ms` ) ;
214
- await deleteCache ( mapPlayersTraces , p . id ) ;
215
- await deleteCache ( mapPlayersInfo , p . id ) ;
278
+ if ( ENABLE_COHERENCE_BACKEND ) {
279
+ await deleteCache ( mapPlayersTraces , p . id ) ;
280
+ await deleteCache ( mapPlayersInfo , p . id ) ;
281
+ } else {
282
+ delete mapPlayersTraces [ p . id ] ;
283
+ delete mapPlayersInfo [ p . id ] ;
284
+ }
216
285
io . emit ( "player.info.left" , p . id ) ;
217
286
} ) ;
218
287
} , CLEANUP_STALE_IN_SECONDS * 1000 ) ;
219
288
220
289
setInterval ( async ( ) => {
221
- const numPlayers = await mapPlayersInfo . size ;
222
- const numTrash = await mapTrash . size ;
223
- const numMarineLife = await mapMarineLife . size ;
290
+ const numPlayers = ENABLE_COHERENCE_BACKEND
291
+ ? await mapPlayersInfo . size
292
+ : Object . keys ( mapPlayersInfo ) . length ;
293
+ const numTrash = ENABLE_COHERENCE_BACKEND
294
+ ? await mapTrash . size
295
+ : Object . keys ( mapTrash ) . length ;
296
+ const numMarineLife = ENABLE_COHERENCE_BACKEND
297
+ ? await mapMarineLife . size
298
+ : Object . keys ( mapMarineLife ) . length ;
224
299
logger . info (
225
300
`${ numPlayers } Players, ${ numTrash } Trash items and ${ numMarineLife } Marine Life`
226
301
) ;
0 commit comments