|
427 | 427 |
|
428 | 428 | function sendData() { |
429 | 429 | let data = { |
430 | | - gameState: gameState, |
| 430 | + gameState: JSON.stringify(gameState), // nested arrays aren't allowed to be stored |
431 | 431 | currentPlayer: currentPlayer, |
432 | 432 | messages: messages, |
433 | 433 | lastUpdated: new Date().getTime() |
|
475 | 475 | messages = providedMsgs; |
476 | 476 | } |
477 | 477 |
|
| 478 | + function alignWithServer() { |
| 479 | + M.server.recall("./").then(function (data) { |
| 480 | + updateGame(JSON.parse(data.gameState)); |
| 481 | + currentPlayer = data.currentPlayer; |
| 482 | + showWhoseTurn(false); |
| 483 | + updateMessages(data.messages); |
| 484 | + }).catch(function (error) { |
| 485 | + console.error("There was an error syncing the game state."); |
| 486 | + console.error(error); |
| 487 | + }); |
| 488 | + } |
| 489 | + |
478 | 490 | function monitorStatus() { |
479 | 491 | if (hasGameCode("otrio")) { |
480 | | - serverChecker = setInterval(function () { |
481 | | - M.server.recall("./").then(function (data) { |
482 | | - updateGame(data.gameState); |
483 | | - currentPlayer = data.currentPlayer; |
484 | | - showWhoseTurn(false); |
485 | | - updateMessages(data.messages); |
486 | | - }); |
487 | | - }, 3000); // rechecks every 3 seconds |
| 492 | + serverChecker = setInterval(alignWithServer, 3000); // rechecks every 3 seconds |
488 | 493 | } |
489 | 494 | } |
490 | 495 |
|
|
495 | 500 | monitorStatus(); |
496 | 501 | } else if (status == "joined") { // if using a preexisting game code |
497 | 502 | resetBoard(); |
498 | | - updateGame(data.gameState); |
499 | | - currentPlayer = data.currentPlayer; |
500 | | - showWhoseTurn(false); |
501 | | - updateMessages(data.messages); |
| 503 | + alignWithServer(); |
502 | 504 | monitorStatus(); |
503 | 505 | } else if (status == "left") { // if the player left the specified game |
504 | 506 | if (serverChecker) { |
|
0 commit comments