compare board recursively, not via unstable JSON serialization
This commit is contained in:
parent
e26504030f
commit
7bdc889b87
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -260,20 +260,21 @@
|
|||
</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>
|
||||
console.real_log = console.log;
|
||||
console.log = () => {};
|
||||
//console.STAT = console.real_log;
|
||||
</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/sea.js"></script>
|
||||
<script src="gun/lib/webrtc.js"></script>
|
||||
<script>
|
||||
console.log = console.real_log;
|
||||
</script>
|
||||
<script src="node-deep-equal/index.js"></script>
|
||||
<script src="js/chess.js"></script>
|
||||
<script src="js/todo.js"></script>
|
||||
</body>
|
||||
|
|
|
|||
29
js/chess.js
29
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(){
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
Subproject commit 007fae260428db9eb350e752e52fb806adb24c56
|
||||
Loading…
Reference in New Issue