Skip to content

Commit

Permalink
Merge pull request #30 from LearnTeachCode/issue4
Browse files Browse the repository at this point in the history
Cloe #4, change game start condition to check state of the player list
  • Loading branch information
LearningNerd authored Jul 3, 2017
2 parents cd1e478 + 9ae0e3d commit 6c82ede
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ io.on('connection', function (socket) {
if (editorCursorAndSelection != null) {
socket.emit('editorCursorChange', editorCursorAndSelection);
}

// Initialize the turn (and timer) with first connected user
// and create the initial GitHub gist on behlaf of the first user!
if (timerId == null) {
// If there is 1 player logged in (the first player to join, who just triggered the "userLogin" event),
// START THE GAME!!!
if (playerList.length === 1) {
timerId = startTurnTimer(timerId, turnDuration, socket.id);

// Notify the first user to create a new gist now!
Expand Down Expand Up @@ -169,18 +169,17 @@ io.on('connection', function (socket) {
currentPlayerIndex = null;

// Turn off the timer
clearInterval(timerId);
timerId = null;
clearInterval(timerId);
nextTurnChangeTimestamp = null;

// Otherwise, if there are players left,
} else {
// If the disconnected user was the current player, restart timer and change the turn!
if (socket.id === currentPlayerId) {
console.log('\nCURRENT PLAYER disconnected! Restarting turn/timer.\n');

// Turn off the timer
clearInterval(timerId);
timerId = null;
clearInterval(timerId);
nextTurnChangeTimestamp = null;

// Re-initialize the turn (and timer), passing control to the next user
Expand Down Expand Up @@ -318,7 +317,7 @@ function startTurnTimer(timerId, turnDuration, socketId) {
console.log( 'Next turn at: ' + new Date(nextTurnChangeTimestamp).toString().substring(16,25) );

// Initialize the turn data using given user ID
changeTurn(socketId);
changeTurn(socketId);

// Every time the timer goes off,
timerId = setInterval(() => {
Expand Down

0 comments on commit 6c82ede

Please sign in to comment.