Skip to content

Commit 3562b08

Browse files
committed
Refactor turnChange event to only send a Boolean
1 parent 37b3dcf commit 3562b08

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

public/local.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ playerJoined Server All other clients {id, login, avatar_url} handlePlayer
108108
gameState Server One client See game state model! handleGameState Initialize game state for new player that just logged in,
109109
and trigger new gist creation if game is just starting!
110110
playerLeft Server All other clients id Update other clients to remove disconnected player
111-
turnChange Server All clients null ??? handleTurnChange Triggerclients to change the turn
111+
turnChange Server All clients onDisconnect (Boolean) handleTurnChange Trigger clients to change the turn
112112
newGist Client Server {id, url, owner} handleNewGist Broadcast new Gist data
113113
editorTextChange Client Server "just a string!" handleLocalEditorTextChange Broadcast changes to code editor content
114114
editorScrollChange Client Server {scrollLeft, scrollTop} handleLocalEditorScrollChange Broadcast changes to code editor content
@@ -283,14 +283,14 @@ function handlePlayerLeft (playerId) {
283283
}
284284

285285
// When receiving turnChange event from server
286-
function handleTurnChange (disconnectedPlayerId) {
286+
function handleTurnChange (onDisconnect) {
287287
console.log('%c turnChange event received! TIME: ' + new Date().toString().substring(16,25), 'color: blue; font-weight: bold;');
288288

289289
// Update the timestamp of the next turn, reset the clock!
290290
gameState.nextTurnTimestamp = Date.now() + turnDuration;
291291

292292
// If turn change was NOT triggered by current player disconnecting, and a Gist exists,
293-
if (!disconnectedPlayerId && gameState.currentGist != null) {
293+
if (!onDisconnect && gameState.currentGist != null) {
294294

295295
// Temporarily save previous player info before changing turn
296296
var previousPlayer = getCurrentPlayer();

server.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ playerJoined Server All other clients {id, login, avatar_url} Update other
110110
gameState Server One client See game state model! Initialize game state for new player that just logged in,
111111
and trigger new gist creation if game is just starting!
112112
playerLeft Server All other clients id Update other clients to remove disconnected player
113-
turnChange Server All clients null ??? Trigger clients to change the turn
113+
turnChange Server All clients onDisconnect (Boolean) Trigger clients to change the turn
114114
newGist Client Server {id, url, owner} Broadcast new Gist data
115115
editorTextChange Client Server "just a string!" Broadcast changes to code editor content
116116
editorScrollChange Client Server {scrollLeft, scrollTop} Broadcast changes to code editor content
@@ -192,8 +192,8 @@ io.on('connection', function (socket) {
192192
// Restart the timer
193193
timerId = startTurnTimer(timerId, turnDuration);
194194

195-
// Change the turn, including ID of disconnected player to send to clients
196-
changeTurn(socket.id);
195+
// Change the turn, including the onDisconnect=true flag to signal clients NOT to fork/edit the Gist on this turn change
196+
changeTurn(true);
197197
}
198198

199199
} else {
@@ -261,11 +261,11 @@ io.on('connection', function (socket) {
261261
/* -------------------------------------------------
262262
FUNCTIONS
263263
---------------------------------------------------- */
264-
function changeTurn(disconnectedPlayerId) {
264+
function changeTurn(onDisconnect) {
265265
gameState.turnIndex = (gameState.turnIndex + 1) % gameState.players.length;
266266

267267
// Broadcast turnChange to ALL clients, with disconnected player ID (which exists only if current player disconnected):
268-
io.emit('turnChange', disconnectedPlayerId);
268+
io.emit('turnChange', onDisconnect);
269269
}
270270

271271
// Initializes the turn and turn timer, returns timerId

0 commit comments

Comments
 (0)