Skip to content

Commit 37b3dcf

Browse files
committed
Update turnIndex on disconnect event if needed
1 parent 7bdb8dc commit 37b3dcf

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

public/local.js

+20-16
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,10 @@ var socket = io();
88
nextTurnTimestamp,
99
turnIndex,
1010
currentGist: {id, url, owner},
11-
playerList:
11+
players:
1212
[
1313
{id, login,avatar_url}, { ... }, { ... }, ...
14-
],
15-
editor:
16-
{
17-
content,
18-
cursorAndSelection: { cursor: {column, row}, range: { end: {column, row}, start: {column, row} },
19-
scroll: {scrollLeft, scrollTop}
20-
}
14+
]
2115
}
2216
2317
-------------------------------------------------------------- */
@@ -31,6 +25,8 @@ let gameState = {
3125

3226
// SAVING LOCAL STATE -- GLOBAL VARS (ugh)
3327
var animationId;
28+
// Later this shouldn't be hard-coded:
29+
const turnDuration = 60000;
3430
// Meant to be temporary:
3531
var currentAccessToken;
3632

@@ -272,7 +268,13 @@ function handlePlayerJoined (newPlayerData) {
272268
}
273269

274270
// When a player disconnects, update using data from server
275-
function handlePlayerLeft (playerId) {
271+
function handlePlayerLeft (playerId) {
272+
273+
// Update turnIndex only if disconnected player comes BEFORE current player in the players array
274+
if ( getPlayerIndexById(playerId, gameState.players) < gameState.turnIndex ) {
275+
gameState.turnIndex--;
276+
}
277+
276278
// Remove disconnected player from player list
277279
removePlayer(playerId, gameState.players);
278280

@@ -302,12 +304,12 @@ function handleTurnChange (disconnectedPlayerId) {
302304
if (previousPlayer.login !== gameState.currentGist.owner) {
303305
// Fork/edit current Gist on behalf of this client (the previous player, whose turn is ending), and send new ID to server
304306
forkAndEditGist(gameState.currentGist.id, editor.getValue());
305-
console.log("handleTurnChange: now forking and editing gist " + gameState.currentGist.id);
307+
console.log("handleTurnChange: now forking and editing gist " + gameState.currentGist.id + " owned by " + gameState.currentGist.owner + "(on behalf of player " + previousPlayer.login + ")");
306308

307309
// Otherwise, just edit the current Gist
308-
} else {
310+
} else {
309311
editGist(gameState.currentGist.id, editor.getValue());
310-
console.log("handleTurnChange: now editing gist " + gameState.currentGist.id);
312+
console.log("handleTurnChange: now editing gist " + gameState.currentGist.id + " owned by " + gameState.currentGist.owner + "(on behalf of player " + previousPlayer.login + ")");
311313
}
312314
}
313315
}
@@ -562,13 +564,15 @@ function forkAndEditGist(gistId, codeEditorContent) {
562564
var gistObject = JSON.parse(responseText);
563565
console.dir(gistObject);
564566

565-
// Send new gist data to server
566-
socket.emit('newGist', {id: gistObject.id, url: gistObject.html_url, owner: gistObject.owner.login});
567-
568567
// Then edit the new gist:
569568
editGist(gistObject.id, codeEditorContent);
570569

571-
updateCurrentGistView({id: gistObject.id, url: gistObject.html_url, owner: gistObject.owner.login});
570+
// Save new Gist data locally and update UI
571+
handleNewGist({id: gistObject.id, url: gistObject.html_url, owner: gistObject.owner.login});
572+
573+
// Send new gist data to server
574+
socket.emit('newGist', gameState.currentGist);
575+
572576

573577
}, handleError);
574578

server.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,11 @@ server.listen(port, function() {
6666

6767
/* ------------------------------------------------------------
6868
GAME STATE:
69-
7069
{
7170
nextTurnTimestamp,
7271
turnIndex,
7372
currentGist: {id, url, owner},
74-
playerList:
73+
players:
7574
[
7675
{id, login,avatar_url}, { ... }, { ... }, ...
7776
],
@@ -168,6 +167,11 @@ io.on('connection', function (socket) {
168167
// Temporarily save ID of current player (before removing from player list, for a later check!)
169168
var currentPlayerId = getCurrentPlayer().id;
170169

170+
// Update turnIndex only if disconnected player comes BEFORE current player in the players array, and there are still players in the game:
171+
if ( getPlayerIndexById(socket.id, gameState.players) < gameState.turnIndex && gameState.players.length > 1) {
172+
gameState.turnIndex--;
173+
}
174+
171175
// Remove disconnected player from player list
172176
removePlayer(socket.id, gameState.players);
173177

0 commit comments

Comments
 (0)