@@ -205,72 +205,56 @@ io.on('connection', function (socket) {
205
205
206
206
// When "editorTextChange" event received, update editor state and broadcast it back out
207
207
socket . on ( 'editorTextChange' , function ( data ) {
208
-
209
- //console.log('editorTextChange event received!');
210
- //console.log(data);
211
208
212
209
// Double check that this user is allowed to type (in case of client-side tampering with the JS!)
213
- if ( socket . id === getCurrentPlayer ( ) . id ) {
210
+ if ( socket . id === getCurrentPlayer ( ) . id ) {
214
211
// Update saved state of the shared text editor
215
212
gameState . editor . content = data ;
216
213
217
214
// Broadcast updated editor content to other clients
218
215
socket . broadcast . emit ( 'editorTextChange' , gameState . editor . content ) ;
219
-
220
- //console.log('Broadcasting gameState.editor.content to other clients!');
221
216
}
222
-
223
217
} ) ;
224
218
225
219
// When "editorCursorChange" event received, update editor state and broadcast it back out
226
220
socket . on ( 'editorCursorChange' , function ( data ) {
227
-
228
- //console.log('editorCursorChange event received!');
229
- //console.log(data);
230
221
231
222
// Double check that this user is allowed to broadcast (in case of client-side tampering with the JS!)
232
- if ( socket . id === getCurrentPlayer ( ) . id ) {
223
+ if ( socket . id === getCurrentPlayer ( ) . id ) {
233
224
// Update saved state of the shared text editor
234
225
gameState . editor . cursorAndSelection = data ;
235
226
236
227
// Broadcast data to other clients
237
228
socket . broadcast . emit ( 'editorCursorChange' , gameState . editor . cursorAndSelection ) ;
238
-
239
- //console.log('Broadcasting editorCursorChange to other clients!');
240
229
}
241
-
242
230
} ) ;
243
231
244
232
// When "editorScrollChange" event received, update editor state and broadcast it back out
245
233
socket . on ( 'editorScrollChange' , function ( data ) {
246
234
247
- //console.log('editorScrollChange event received!');
248
- //console.log(data);
249
-
250
235
// Double check that this user is allowed to broadcast (in case of client-side tampering with the JS!)
251
- if ( socket . id === getCurrentPlayer ( ) . id ) {
236
+ if ( socket . id === getCurrentPlayer ( ) . id ) {
252
237
// Update saved state of the shared text editor
253
238
gameState . editor . scroll = data ;
254
239
255
240
// Broadcast data to other clients
256
241
socket . broadcast . emit ( 'editorScrollChange' , gameState . editor . cursorAndSelection ) ;
257
-
258
- //console.log('Broadcasting editorScrollChange to other clients!');
259
- }
260
-
242
+ }
261
243
} ) ;
262
244
263
245
// When "newGist" event received, update state and broadcast it back out
264
246
socket . on ( 'newGist' , function ( data ) {
265
247
266
- console . log ( '\nnewGist event received!\n' ) ;
267
- //console.log(data);
248
+ // Double check that this user is allowed to broadcast a new gist!
249
+ if ( socket . id === getPreviousPlayer ( ) . id ) {
250
+ console . log ( '\nnewGist event received!\n' ) ;
251
+ //console.log(data);
268
252
269
- gameState . currentGist = data ;
253
+ gameState . currentGist = data ;
270
254
271
- // Broadcast data to other clients
272
- socket . broadcast . emit ( 'newGist' , data ) ;
273
-
255
+ // Broadcast data to other clients
256
+ socket . broadcast . emit ( 'newGist' , data ) ;
257
+ }
274
258
} ) ;
275
259
276
260
} ) ; // End of SocketIO part of the code
@@ -312,6 +296,12 @@ function startTurnTimer(timerId, turnDuration) {
312
296
function getCurrentPlayer ( ) {
313
297
return gameState . players [ gameState . turnIndex ] ;
314
298
}
299
+
300
+ function getPreviousPlayer ( ) {
301
+ var previousPlayerIndex = ( gameState . turnIndex - 1 ) % gameState . players . length ;
302
+ return gameState . players [ previousPlayerIndex ] ;
303
+ }
304
+
315
305
function getPlayerById ( id , playerList ) {
316
306
for ( var i = 0 ; i < playerList . length ; i ++ ) {
317
307
if ( playerList [ i ] . id === id ) {
0 commit comments