Skip to content

Commit 11821ef

Browse files
committed
Close #14, verify newGist event on server
1 parent 45165b6 commit 11821ef

File tree

2 files changed

+26
-36
lines changed

2 files changed

+26
-36
lines changed

public/local.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -290,24 +290,24 @@ function handleTurnChange () {
290290
// Remove highlight from previous player's name in playerListView
291291
togglePlayerHighlight(false);
292292

293-
// Temporarily save the previous player ID for later comparison
294-
var previousPlayerId = getCurrentPlayer().id;
293+
// Temporarily save the previous player for later comparison
294+
var previousPlayer = getCurrentPlayer();
295295

296296
changeTurn();
297297

298-
// If user's turn is ending and a Gist exists, fork and/or edit the gist before passing control to next player!
299-
if (socket.id === previousPlayerId && gameState.currentGist != null) {
298+
// If this client's turn is ending and a Gist exists, fork and/or edit the gist before passing control to next player!
299+
if (socket.id === previousPlayer.id && gameState.currentGist != null) {
300300
console.log("User's turn is about to end.");
301301

302-
// If the current player does NOT own the current Gist,
303-
if (getCurrentPlayer().login !== gameState.currentGist.owner) {
302+
// If this client (the previous player) does NOT own the current Gist,
303+
if (previousPlayer.login !== gameState.currentGist.owner) {
304304

305305
console.log("handleTurnChange: now forking and editing gist " + gameState.currentGist.id);
306306

307-
// Fork/edit current Gist on behalf of player whose turn is about to end, and send new ID to server
307+
// Fork/edit current Gist on behalf of this client (the previous player, whose turn is ending), and send new ID to server
308308
forkAndEditGist(gameState.currentGist.id, editor.getValue());
309309

310-
// Otherwise, JUST EDIT the current Gist and send new ID to server
310+
// Otherwise, just edit the current Gist
311311
} else {
312312

313313
console.log("handleTurnChange: now editing gist " + gameState.currentGist.id);

server.js

+18-28
Original file line numberDiff line numberDiff line change
@@ -205,72 +205,56 @@ io.on('connection', function (socket) {
205205

206206
// When "editorTextChange" event received, update editor state and broadcast it back out
207207
socket.on('editorTextChange', function (data) {
208-
209-
//console.log('editorTextChange event received!');
210-
//console.log(data);
211208

212209
// 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 ) {
214211
// Update saved state of the shared text editor
215212
gameState.editor.content = data;
216213

217214
// Broadcast updated editor content to other clients
218215
socket.broadcast.emit('editorTextChange', gameState.editor.content);
219-
220-
//console.log('Broadcasting gameState.editor.content to other clients!');
221216
}
222-
223217
});
224218

225219
// When "editorCursorChange" event received, update editor state and broadcast it back out
226220
socket.on('editorCursorChange', function (data) {
227-
228-
//console.log('editorCursorChange event received!');
229-
//console.log(data);
230221

231222
// 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 ) {
233224
// Update saved state of the shared text editor
234225
gameState.editor.cursorAndSelection = data;
235226

236227
// Broadcast data to other clients
237228
socket.broadcast.emit('editorCursorChange', gameState.editor.cursorAndSelection);
238-
239-
//console.log('Broadcasting editorCursorChange to other clients!');
240229
}
241-
242230
});
243231

244232
// When "editorScrollChange" event received, update editor state and broadcast it back out
245233
socket.on('editorScrollChange', function (data) {
246234

247-
//console.log('editorScrollChange event received!');
248-
//console.log(data);
249-
250235
// 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 ) {
252237
// Update saved state of the shared text editor
253238
gameState.editor.scroll = data;
254239

255240
// Broadcast data to other clients
256241
socket.broadcast.emit('editorScrollChange', gameState.editor.cursorAndSelection);
257-
258-
//console.log('Broadcasting editorScrollChange to other clients!');
259-
}
260-
242+
}
261243
});
262244

263245
// When "newGist" event received, update state and broadcast it back out
264246
socket.on('newGist', function (data) {
265247

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);
268252

269-
gameState.currentGist = data;
253+
gameState.currentGist = data;
270254

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+
}
274258
});
275259

276260
}); // End of SocketIO part of the code
@@ -312,6 +296,12 @@ function startTurnTimer(timerId, turnDuration) {
312296
function getCurrentPlayer() {
313297
return gameState.players[gameState.turnIndex];
314298
}
299+
300+
function getPreviousPlayer() {
301+
var previousPlayerIndex = (gameState.turnIndex - 1) % gameState.players.length;
302+
return gameState.players[previousPlayerIndex];
303+
}
304+
315305
function getPlayerById(id, playerList){
316306
for (var i = 0; i < playerList.length; i++) {
317307
if (playerList[i].id === id) {

0 commit comments

Comments
 (0)