From 57b5da99b5f1992f061067934dee829ba648b5f2 Mon Sep 17 00:00:00 2001 From: Jesse McDonald Date: Wed, 25 Mar 2020 17:56:54 -0500 Subject: [PATCH] minor refactoring & internal name changes --- js/pacosako_ui.js | 50 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/js/pacosako_ui.js b/js/pacosako_ui.js index 6268a6c..d4fdfea 100644 --- a/js/pacosako_ui.js +++ b/js/pacosako_ui.js @@ -50,11 +50,28 @@ $(function (){ function cbSquare(where) { if (where === PS.PHANTOM) { return $('#cb_phantom').first() - } else { + } else if (where.match(/^[a-h][1-8]$/)) { return $('#cb_' + where).first() + } else { + return null; } } + function cbSquareLocation(square) { + const id = square.id || square[0].id; + + if (id === 'cb_phantom') { + return PS.PHANTOM; + } + + const found = id.match(/^cb_([a-h][1-8])$/); + if (found) { + return found[1]; + } + + return null; + } + function pieceStartMove(piece, event) { const side = piece.data('side'); const type = piece.data('type'); @@ -83,7 +100,7 @@ $(function (){ } $('#cb_board').data('last_state', currentGame.moves); - setCurrentBoard(currentGame); + setCurrentGame(currentGame); putState(); } @@ -94,11 +111,11 @@ $(function (){ return; } - pieceEndMove(selected, this.id.replace(/^cb_/, '')); + pieceEndMove(selected, cbSquareLocation(this)); } function squareDropDestination(ev, ui) { - pieceEndMove(ui.draggable, this.id.replace(/^cb_/, '')); + pieceEndMove(ui.draggable, cbSquareLocation(this)); } function pieceClickUnselect(ev, ui) { @@ -278,13 +295,16 @@ $(function (){ } } - function setCurrentBoard(game, animate) { - currentGame = game; - + function setVisibleGame(game, animate) { /* navigation should not include the redo stack */ visibleGame = new PS.Game(game); visibleGame.clearRedo(); renderBoard(animate); + } + + function setCurrentGame(game, animate) { + currentGame = game; + setVisibleGame(game); const moves = game.moves; @@ -386,7 +406,7 @@ $(function (){ closeNotifications(); /* this will be the starting state if no data is received from peers */ - setCurrentBoard(new PS.Game()); + setCurrentGame(new PS.Game()); boardElem.data('last_state', currentGame.moves); boardElem.data('lightName', 'Light'); @@ -424,7 +444,7 @@ $(function (){ newGame.undo(); } - setCurrentBoard(newGame, moves.past.length > currentGame.moves.length); + setCurrentGame(newGame, moves.past.length > currentGame.moves.length); } catch (err) { debug('Error replaying board state', err); } @@ -615,7 +635,7 @@ $(function (){ if (currentGame.canUndo) { currentGame.undo(); $('#cb_board').data('last_state', currentGame.moves); - setCurrentBoard(currentGame); + setCurrentGame(currentGame); putState(); } }); @@ -624,7 +644,7 @@ $(function (){ if (currentGame.canRedo) { currentGame.redo(); $('#cb_board').data('last_state', currentGame.moves); - setCurrentBoard(currentGame, true); + setCurrentGame(currentGame, true); putState(); } }); @@ -638,7 +658,7 @@ $(function (){ } $('#cb_board').data('last_state', currentGame.moves); - setCurrentBoard(currentGame); + setCurrentGame(currentGame); putState(); }); @@ -895,9 +915,9 @@ $(function (){ getCurrentGame: function() { return currentGame; }, getVisibleGame: function() { return visibleGame; }, - setCurrentGame: function(game) { setCurrentBoard(game) }, - setVisibleGame: function(game) { visibleGame = game; renderBoard(); }, - refresh: function() { setCurrentBoard(currentGame); }, + setCurrentGame: setCurrentGame, + setVisibleGame: setVisibleGame, + refresh: function() { setCurrentGame(currentGame); }, renderBoard: renderBoard, putState: putState, putMeta: putMeta,