compare board recursively, not via unstable JSON serialization

This commit is contained in:
Jesse D. McDonald 2020-03-15 01:18:20 -05:00
parent e26504030f
commit 7bdc889b87
4 changed files with 25 additions and 15 deletions

3
.gitmodules vendored
View File

@ -7,3 +7,6 @@
[submodule "gun"] [submodule "gun"]
path = gun path = gun
url = https://github.com/amark/gun url = https://github.com/amark/gun
[submodule "node-deep-equal"]
path = node-deep-equal
url = gogs@jessemcdonald.info:nybble/node-deep-equal.git

View File

@ -260,20 +260,21 @@
</div> </div>
</div> </div>
<script src="jquery-ui/external/jquery/jquery.js"></script>
<script src="jquery-ui/dist/jquery-ui.min.js"></script>
<script src="jquery-ui-touch-punch/jquery.ui.touch-punch.min.js"></script>
<script> <script>
console.real_log = console.log; console.real_log = console.log;
console.log = () => {}; console.log = () => {};
//console.STAT = console.real_log; //console.STAT = console.real_log;
</script> </script>
<script src="jquery-ui/external/jquery/jquery.js"></script>
<script src="jquery-ui/dist/jquery-ui.min.js"></script>
<script src="jquery-ui-touch-punch/jquery.ui.touch-punch.min.js"></script>
<script src="gun/gun.js"></script> <script src="gun/gun.js"></script>
<script src="gun/sea.js"></script> <script src="gun/sea.js"></script>
<script src="gun/lib/webrtc.js"></script> <script src="gun/lib/webrtc.js"></script>
<script> <script>
console.log = console.real_log; console.log = console.real_log;
</script> </script>
<script src="node-deep-equal/index.js"></script>
<script src="js/chess.js"></script> <script src="js/chess.js"></script>
<script src="js/todo.js"></script> <script src="js/todo.js"></script>
</body> </body>

View File

@ -510,7 +510,14 @@ function renderBoard(board){
$('#cb_history').text(renderHistory(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); $('#cb_board').data('visible_board', board);
renderBoard(board); renderBoard(board);
@ -521,6 +528,7 @@ function setVisibleBoard(board, live){
$('#cb_nav_next_turn').attr('disabled', board.subsequent ? false : true); $('#cb_nav_next_turn').attr('disabled', board.subsequent ? false : true);
if (live) { if (live) {
/* the 'visible' board may be missing .subsequent */
const liveBoard = $('#cb_board').data('board'); const liveBoard = $('#cb_board').data('board');
$('#cb_undo').attr('disabled', liveBoard.prior ? false : true); $('#cb_undo').attr('disabled', liveBoard.prior ? false : true);
$('#cb_redo').attr('disabled', liveBoard.subsequent ? false : true); $('#cb_redo').attr('disabled', liveBoard.subsequent ? false : true);
@ -543,14 +551,12 @@ function setCurrentBoard(board){
const cb_board = $('#cb_board').first(); const cb_board = $('#cb_board').first();
cb_board.data('board', board); cb_board.data('board', board);
const boardState = JSON.stringify(board);
/* navigation should not include the redo stack */ /* navigation should not include the redo stack */
const visible = JSON.parse(boardState); const visible = cloneJSON(board);
delete visible.subsequent; 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 */ /* ignore partial moves and undo/redo */
if (!board.phantom && !board.subsequent) { if (!board.phantom && !board.subsequent) {
const gameString = cb_board.data('lightName') + ' vs. ' + cb_board.data('darkName'); 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(){ function randomId(){
@ -576,9 +582,8 @@ function putState(board){
var boardElem = $('#cb_board'); var boardElem = $('#cb_board');
var gameId = boardElem.data('gameId'); var gameId = boardElem.data('gameId');
var game = gun.get(PacoSakoUUID).get('games').get(gameId); var game = gun.get(PacoSakoUUID).get('games').get(gameId);
var state = JSON.stringify(board); boardElem.data('last_state', cloneJSON(board));
boardElem.data('last_state', state); game.put({ board: JSON.stringify(board) });
game.put({ board: state });
putMeta(); putMeta();
} }
@ -628,7 +633,7 @@ function switchGameId(newId){
/* this will be the starting state if no data is received from peers */ /* this will be the starting state if no data is received from peers */
var newBoard = initialBoard(); var newBoard = initialBoard();
setCurrentBoard(newBoard); setCurrentBoard(newBoard);
boardElem.data('last_state', JSON.stringify(newBoard)); boardElem.data('last_state', cloneJSON(newBoard));
boardElem.data('lightName', 'Light'); boardElem.data('lightName', 'Light');
boardElem.data('darkName', 'Dark'); boardElem.data('darkName', 'Dark');
@ -841,7 +846,7 @@ $(function (){
$('#cb_nav_last').on('click', function(){ $('#cb_nav_last').on('click', function(){
const visible = cloneJSON($('#cb_board').data('board')); const visible = cloneJSON($('#cb_board').data('board'));
delete visible.subsequent; delete visible.subsequent;
setVisibleBoard(visible, true); setVisibleBoard(visible);
}); });
$('#cb_reset').on('click', function(){ $('#cb_reset').on('click', function(){

1
node-deep-equal Submodule

@ -0,0 +1 @@
Subproject commit 007fae260428db9eb350e752e52fb806adb24c56