factor the code for the status message out of renderBoard()

This commit is contained in:
Jesse D. McDonald 2020-05-09 11:30:30 -05:00
parent 6f6600c6f4
commit 21214dd5df
1 changed files with 23 additions and 16 deletions

View File

@ -36,6 +36,28 @@ $(function (){
let visibleGame = new PS.Game(currentGame); let visibleGame = new PS.Game(currentGame);
let cancelGameCallback = function() {}; let cancelGameCallback = function() {};
function gameMessage(game) {
let msg = '';
const winner = game.winner;
if (winner) {
const lastMove = game.lastMove;
if (game.status === PS.CHECKMATE) {
msg += 'Checkmate! ';
} else if (lastMove && lastMove.resign) {
msg += (lastMove.side === PS.LIGHT ? 'Light' : 'Dark') + ' player resigned. ';
}
msg += (winner === PS.LIGHT ? 'Light' : 'Dark') + ' player won!';
} else if (game.status === PS.PLAYING) {
if (game.isInCheck()) {
msg += 'Check! ';
}
msg += (game.player === PS.LIGHT ? 'Light' : 'Dark') + ' player\'s turn.';
} else {
msg += 'Game ended in a draw.';
}
return msg;
}
function pieceTypeClass(type) { function pieceTypeClass(type) {
if (type === PS.KING) { if (type === PS.KING) {
return 'cb-king'; return 'cb-king';
@ -325,27 +347,12 @@ $(function (){
} }
let msg = ''; let msg = '';
let winner = game.winner;
if (!liveView) { if (!liveView) {
msg += '(Move ' + String(game.moves.length) + ' of ' + String(currentGame.moves.length) + ') '; msg += '(Move ' + String(game.moves.length) + ' of ' + String(currentGame.moves.length) + ') ';
} }
if (winner) { msg += gameMessage(game);
if (game.status === PS.CHECKMATE) {
msg += 'Checkmate! ';
} else if (lastMove && lastMove.resign) {
msg += (lastMove.side === PS.LIGHT ? 'Light' : 'Dark') + ' player resigned. ';
}
msg += (winner === PS.LIGHT ? 'Light' : 'Dark') + ' player won!';
} else if (playing) {
if (game.isInCheck()) {
msg += 'Check! ';
}
msg += (game.player === PS.LIGHT ? 'Light' : 'Dark') + ' player\'s turn.';
} else {
msg += 'Game ended in a draw.';
}
$('#cb_message').text(msg); $('#cb_message').text(msg);
const viewHistory = game.history || ''; const viewHistory = game.history || '';