minor refactoring & internal name changes

This commit is contained in:
Jesse D. McDonald 2020-03-25 17:56:54 -05:00
parent aa33042c30
commit 57b5da99b5
1 changed files with 35 additions and 15 deletions

View File

@ -50,11 +50,28 @@ $(function (){
function cbSquare(where) { function cbSquare(where) {
if (where === PS.PHANTOM) { if (where === PS.PHANTOM) {
return $('#cb_phantom').first() return $('#cb_phantom').first()
} else { } else if (where.match(/^[a-h][1-8]$/)) {
return $('#cb_' + where).first() 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) { function pieceStartMove(piece, event) {
const side = piece.data('side'); const side = piece.data('side');
const type = piece.data('type'); const type = piece.data('type');
@ -83,7 +100,7 @@ $(function (){
} }
$('#cb_board').data('last_state', currentGame.moves); $('#cb_board').data('last_state', currentGame.moves);
setCurrentBoard(currentGame); setCurrentGame(currentGame);
putState(); putState();
} }
@ -94,11 +111,11 @@ $(function (){
return; return;
} }
pieceEndMove(selected, this.id.replace(/^cb_/, '')); pieceEndMove(selected, cbSquareLocation(this));
} }
function squareDropDestination(ev, ui) { function squareDropDestination(ev, ui) {
pieceEndMove(ui.draggable, this.id.replace(/^cb_/, '')); pieceEndMove(ui.draggable, cbSquareLocation(this));
} }
function pieceClickUnselect(ev, ui) { function pieceClickUnselect(ev, ui) {
@ -278,13 +295,16 @@ $(function (){
} }
} }
function setCurrentBoard(game, animate) { function setVisibleGame(game, animate) {
currentGame = game;
/* navigation should not include the redo stack */ /* navigation should not include the redo stack */
visibleGame = new PS.Game(game); visibleGame = new PS.Game(game);
visibleGame.clearRedo(); visibleGame.clearRedo();
renderBoard(animate); renderBoard(animate);
}
function setCurrentGame(game, animate) {
currentGame = game;
setVisibleGame(game);
const moves = game.moves; const moves = game.moves;
@ -386,7 +406,7 @@ $(function (){
closeNotifications(); closeNotifications();
/* 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 */
setCurrentBoard(new PS.Game()); setCurrentGame(new PS.Game());
boardElem.data('last_state', currentGame.moves); boardElem.data('last_state', currentGame.moves);
boardElem.data('lightName', 'Light'); boardElem.data('lightName', 'Light');
@ -424,7 +444,7 @@ $(function (){
newGame.undo(); newGame.undo();
} }
setCurrentBoard(newGame, moves.past.length > currentGame.moves.length); setCurrentGame(newGame, moves.past.length > currentGame.moves.length);
} catch (err) { } catch (err) {
debug('Error replaying board state', err); debug('Error replaying board state', err);
} }
@ -615,7 +635,7 @@ $(function (){
if (currentGame.canUndo) { if (currentGame.canUndo) {
currentGame.undo(); currentGame.undo();
$('#cb_board').data('last_state', currentGame.moves); $('#cb_board').data('last_state', currentGame.moves);
setCurrentBoard(currentGame); setCurrentGame(currentGame);
putState(); putState();
} }
}); });
@ -624,7 +644,7 @@ $(function (){
if (currentGame.canRedo) { if (currentGame.canRedo) {
currentGame.redo(); currentGame.redo();
$('#cb_board').data('last_state', currentGame.moves); $('#cb_board').data('last_state', currentGame.moves);
setCurrentBoard(currentGame, true); setCurrentGame(currentGame, true);
putState(); putState();
} }
}); });
@ -638,7 +658,7 @@ $(function (){
} }
$('#cb_board').data('last_state', currentGame.moves); $('#cb_board').data('last_state', currentGame.moves);
setCurrentBoard(currentGame); setCurrentGame(currentGame);
putState(); putState();
}); });
@ -895,9 +915,9 @@ $(function (){
getCurrentGame: function() { return currentGame; }, getCurrentGame: function() { return currentGame; },
getVisibleGame: function() { return visibleGame; }, getVisibleGame: function() { return visibleGame; },
setCurrentGame: function(game) { setCurrentBoard(game) }, setCurrentGame: setCurrentGame,
setVisibleGame: function(game) { visibleGame = game; renderBoard(); }, setVisibleGame: setVisibleGame,
refresh: function() { setCurrentBoard(currentGame); }, refresh: function() { setCurrentGame(currentGame); },
renderBoard: renderBoard, renderBoard: renderBoard,
putState: putState, putState: putState,
putMeta: putMeta, putMeta: putMeta,