Skip to content

Commit 7bdb8dc

Browse files
committed
Fix Gist state updates
1 parent 7f83142 commit 7bdb8dc

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

public/local.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ function handleGameState (serverGameState) {
243243
// If no Gist exists, create it!
244244
if (gameState.currentGist == null) {
245245
createNewGist();
246+
} else { // Otherwise, if a Gist does exist, display it!
247+
updateCurrentGistView(gameState.currentGist);
246248
}
247249

248250
// Update UI
@@ -306,14 +308,13 @@ function handleTurnChange (disconnectedPlayerId) {
306308
} else {
307309
editGist(gameState.currentGist.id, editor.getValue());
308310
console.log("handleTurnChange: now editing gist " + gameState.currentGist.id);
309-
}
311+
}
310312
}
311313
}
312314

313315
changeTurn();
314-
315-
console.log("turn changed!");
316-
console.log("turnIndex: " + gameState.turnIndex + ", # players: " + gameState.players.length, ", current player: " + getCurrentPlayer().id + " - " + getCurrentPlayer().login);
316+
317+
console.log("TURN CHANGED! turnIndex: " + gameState.turnIndex + ", # players: " + gameState.players.length, ", current player: " + getCurrentPlayer().id + " - " + getCurrentPlayer().login);
317318

318319
// If user is no longer the current player, prevent them from typing/broadcasting!
319320
if ( socket.id !== getCurrentPlayer().id ) {
@@ -335,9 +336,12 @@ function handleTurnChange (disconnectedPlayerId) {
335336

336337
// When receiving "newGist" event from server,
337338
function handleNewGist (gistData) {
338-
// Update local state
339339
console.log("called handleNewGist at " + new Date().toString().substring(16,25), 'color: green; font-weight: bold;');
340340
console.log(gistData);
341+
342+
// Update local state
343+
gameState.currentGist = gistData;
344+
341345
// Update views
342346
updateCurrentGistView(gistData);
343347
}
@@ -508,13 +512,11 @@ function createNewGist() {
508512

509513
var gistObject = JSON.parse(responseText);
510514

511-
// Save new gist ID and URL locally
512-
currentGist = {id: gistObject.id, url: gistObject.html_url, owner: gistObject.owner.login};
515+
// Save new Gist data locally and update UI
516+
handleNewGist({id: gistObject.id, url: gistObject.html_url, owner: gistObject.owner.login});
513517

514-
// Send new gist data to server
515-
socket.emit('newGist', {id: gistObject.id, url: gistObject.html_url, owner: gistObject.owner.login});
516-
517-
updateCurrentGistView({id: gistObject.id, url: gistObject.html_url, owner: gistObject.owner.login});
518+
// Send gist data to server
519+
socket.emit('newGist', gameState.currentGist);
518520

519521
}, handleError);
520522

0 commit comments

Comments
 (0)