Skip to content

Commit ac2d458

Browse files
committed
Close #20, track previous/current players for Gist logic
1 parent fd57f8c commit ac2d458

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

Diff for: public/local.js

+23-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var socket = io();
55

66

77
// SAVING LOCAL STATE -- GLOBAL VARS (ugh)
8+
var previousPlayerId;
89
var currentPlayerId;
910
var nextPlayerId;
1011
var animationId;
@@ -284,34 +285,45 @@ function handleTurnChange (turnData) {
284285
// Remove highlight from previous current player's name in playerListView
285286
togglePlayerHighlight(false);
286287

287-
// Temporarily save the previous player ID for later comparison
288-
var previousPlayerId = currentPlayerId;
288+
//console.log("********* **** handleTurnChange FIRST *** ********");
289+
//console.log("previousPlayerId: " + previousPlayerId + "\n\tcurrentPlayerId: "+ currentPlayerId + "\n\tnextPlayerId: " + nextPlayerId, );
289290

290-
// Update local state
291-
currentPlayerId = turnData.current.id;
292-
nextPlayerId = turnData.next.id;
293-
294-
// If user's turn is ending and a Gist exists, fork and/or edit the gist before passing control to next player!
295-
if (socket.id === previousPlayerId && turnData.gist != null) {
291+
// If current user's turn is ending and a Gist exists, fork and/or edit the gist before passing control to next player!
292+
if (socket.id === currentPlayerId && turnData.gist != null) {
296293
console.log("User's turn is about to end.");
297294

298295
// If the current player is about to change,
299296
if (currentPlayerId !== previousPlayerId) {
300297

298+
console.log("> > > > > > currentPlayerId != previousPlayerId so fork/edit!");
299+
console.log("\t\t prevPlayerId: " + previousPlayerId + ", currPlayerId: " + currentPlayerId);
301300
//console.log("handleTurnChange: now forking and editing gist " + turnData.gist.id);
302301

303302
// fork and edit the current gist on behalf of previous player and send new ID to server
304303
forkAndEditGist(turnData.gist.id, editor.getValue());
305304

306305
// Otherwise, JUST EDIT the current gist on behalf of previous player and send new ID to server
307306
} else {
308-
307+
console.log("< < < < < < currentPlayerId == previousPlayerId so only edit!");
308+
console.log("\t\t prevPlayerId: " + previousPlayerId + ", currPlayerId: " + currentPlayerId);
309309
//console.log("handleTurnChange: now editing gist " + turnData.gist.id);
310310
editGist(turnData.gist.id, editor.getValue());
311311

312312
}
313313
}
314+
315+
316+
317+
// Update local state to reflect completed turn change!
318+
previousPlayerId = turnData.previous.id;
319+
currentPlayerId = turnData.current.id;
320+
nextPlayerId = turnData.next.id;
314321

322+
323+
//console.log("********* **** handleTurnChange SECOND *** ********");
324+
//console.log("previousPlayerId: " + previousPlayerId + "\n\tcurrentPlayerId: "+ currentPlayerId + "\n\tnextPlayerId: " + nextPlayerId, );
325+
//console.dir(turnData);
326+
315327
// If user is no longer the current player, prevent them from typing/broadcasting!
316328
if (socket.id !== currentPlayerId) {
317329
console.log("User's turn is over.");
@@ -342,10 +354,8 @@ function handleUpdateState (turnData) {
342354
// Remove highlight from previous current player's name in playerListView
343355
togglePlayerHighlight(false);
344356

345-
// Temporarily save the previous player ID for later comparison
346-
var previousPlayerId = currentPlayerId;
347-
348-
// Update local state
357+
// Update local state
358+
previousPlayerId = turnData.previous.id;
349359
currentPlayerId = turnData.current.id;
350360
nextPlayerId = turnData.next.id;
351361

Diff for: server.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ var currentGist;
9393
var timerId;
9494
var nextTurnChangeTimestamp;
9595
var currentPlayerIndex;
96+
var previousPlayerIndex;
9697
const turnDuration = 60000; // 3 min: 180000
9798

9899
// When a user connects over websocket,
@@ -285,18 +286,31 @@ function changeTurn(socketId) {
285286
if (currentPlayerIndex == null) {
286287
console.log('\nINITIALIZING FIRST PLAYER\n');
287288
currentPlayerIndex = playerList.indexOf(socketId);
289+
290+
// Initialize previousPlayer to match currentPlayer for first turn!
291+
previousPlayerIndex = currentPlayerIndex;
292+
288293
//console.log('currentPlayerIndex: ' + currentPlayerIndex);
289-
// Otherwise, increment the current player
294+
295+
// Otherwise, increment the previous and current player
290296
} else {
291297
//console.log('\nIncrementing currentPlayerIndex\n');
298+
299+
previousPlayerIndex = currentPlayerIndex;
292300
currentPlayerIndex = (currentPlayerIndex + 1) % playerList.length;
293-
//console.log('NEW currentPlayerIndex: ' + currentPlayerIndex);
301+
console.log('NEW currentPlayerIndex: ' + currentPlayerIndex + ' \n\t NEW previousPlayerIndex : ' + previousPlayerIndex + '\n>>>>>>\n');
302+
console.log(playerList);
294303
}
295304
}
296305

297306
// Returns turnChange object for the current turn
298307
function getTurnData() {
299308
//console.log('getTurnData called');
309+
var previousPlayerId = playerList[previousPlayerIndex];
310+
console.log("getTurnData() >>>>>>>> previousPlayerId: " + previousPlayerId + '\n');
311+
console.log('\n>>>>>>\n');
312+
console.log(playerList);
313+
300314
var currentPlayerId = playerList[currentPlayerIndex];
301315
var currentPlayerName = playerData[currentPlayerId].login;
302316

@@ -305,7 +319,7 @@ function getTurnData() {
305319
var nextPlayerId = playerList[nextPlayerIndex];
306320
var nextPlayerName = playerData[nextPlayerId].login;
307321

308-
return {millisRemaining: nextTurnChangeTimestamp - Date.now(), current: {id: currentPlayerId, name: currentPlayerName}, next: {id: nextPlayerId, name: nextPlayerName}, gist: currentGist};
322+
return {millisRemaining: nextTurnChangeTimestamp - Date.now(), previous: {id: previousPlayerId}, current: {id: currentPlayerId, name: currentPlayerName}, next: {id: nextPlayerId, name: nextPlayerName}, gist: currentGist};
309323
}
310324

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

0 commit comments

Comments
 (0)