From 7bdc889b87e59d3ea293e765a49b085e20b16026 Mon Sep 17 00:00:00 2001 From: Jesse McDonald Date: Sun, 15 Mar 2020 01:18:20 -0500 Subject: [PATCH] compare board recursively, not via unstable JSON serialization --- .gitmodules | 3 +++ index.html | 7 ++++--- js/chess.js | 29 +++++++++++++++++------------ node-deep-equal | 1 + 4 files changed, 25 insertions(+), 15 deletions(-) create mode 160000 node-deep-equal diff --git a/.gitmodules b/.gitmodules index 432a1ff..dadb758 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "gun"] path = gun url = https://github.com/amark/gun +[submodule "node-deep-equal"] + path = node-deep-equal + url = gogs@jessemcdonald.info:nybble/node-deep-equal.git diff --git a/index.html b/index.html index 6237678..7b4340b 100644 --- a/index.html +++ b/index.html @@ -260,20 +260,21 @@ + + + - - - + diff --git a/js/chess.js b/js/chess.js index 42ca822..9bdf029 100644 --- a/js/chess.js +++ b/js/chess.js @@ -510,7 +510,14 @@ function renderBoard(board){ $('#cb_history').text(renderHistory(board)); } -function setVisibleBoard(board, live){ +function setVisibleBoard(board){ + const cb_board = $('#cb_board').first(); + + const currentBoard = cloneJSON(cb_board.data('board')); + delete currentBoard.subsequent; + + const live = deepEqual(board, currentBoard); + $('#cb_board').data('visible_board', board); renderBoard(board); @@ -521,6 +528,7 @@ function setVisibleBoard(board, live){ $('#cb_nav_next_turn').attr('disabled', board.subsequent ? false : true); if (live) { + /* the 'visible' board may be missing .subsequent */ const liveBoard = $('#cb_board').data('board'); $('#cb_undo').attr('disabled', liveBoard.prior ? false : true); $('#cb_redo').attr('disabled', liveBoard.subsequent ? false : true); @@ -543,14 +551,12 @@ function setCurrentBoard(board){ const cb_board = $('#cb_board').first(); cb_board.data('board', board); - const boardState = JSON.stringify(board); - /* navigation should not include the redo stack */ - const visible = JSON.parse(boardState); + const visible = cloneJSON(board); delete visible.subsequent; - setVisibleBoard(visible, true); + setVisibleBoard(visible); - if ($('#cb_notify')[0].checked && boardState !== cb_board.data('last_state')) { + if ($('#cb_notify')[0].checked && !deepEqual(board, cb_board.data('last_state'))) { /* ignore partial moves and undo/redo */ if (!board.phantom && !board.subsequent) { const gameString = cb_board.data('lightName') + ' vs. ' + cb_board.data('darkName'); @@ -558,7 +564,7 @@ function setCurrentBoard(board){ } } - cb_board.data('last_state', boardState); + cb_board.data('last_state', cloneJSON(board)); } function randomId(){ @@ -576,9 +582,8 @@ function putState(board){ var boardElem = $('#cb_board'); var gameId = boardElem.data('gameId'); var game = gun.get(PacoSakoUUID).get('games').get(gameId); - var state = JSON.stringify(board); - boardElem.data('last_state', state); - game.put({ board: state }); + boardElem.data('last_state', cloneJSON(board)); + game.put({ board: JSON.stringify(board) }); putMeta(); } @@ -628,7 +633,7 @@ function switchGameId(newId){ /* this will be the starting state if no data is received from peers */ var newBoard = initialBoard(); setCurrentBoard(newBoard); - boardElem.data('last_state', JSON.stringify(newBoard)); + boardElem.data('last_state', cloneJSON(newBoard)); boardElem.data('lightName', 'Light'); boardElem.data('darkName', 'Dark'); @@ -841,7 +846,7 @@ $(function (){ $('#cb_nav_last').on('click', function(){ const visible = cloneJSON($('#cb_board').data('board')); delete visible.subsequent; - setVisibleBoard(visible, true); + setVisibleBoard(visible); }); $('#cb_reset').on('click', function(){ diff --git a/node-deep-equal b/node-deep-equal new file mode 160000 index 0000000..007fae2 --- /dev/null +++ b/node-deep-equal @@ -0,0 +1 @@ +Subproject commit 007fae260428db9eb350e752e52fb806adb24c56