use field syntax rather than indexing by strings

This commit is contained in:
Jesse D. McDonald 2020-03-14 12:57:49 -05:00
parent b9de793ce8
commit 47fc9cba08
1 changed files with 117 additions and 117 deletions

View File

@ -57,10 +57,10 @@ function otherSide(side){
function boardGet(board, where, side){
side = normalizeSide(side);
if (where === 'phantom') {
if (!board['phantom'] || board['phantom']['type'][1] !== side[0]) {
if (!board.phantom || board.phantom.type[1] !== side[0]) {
return ' ';
} else {
return board['phantom']['type'][0];
return board.phantom.type[0];
}
} else {
var column = 'abcdefgh'.indexOf(where[0]);
@ -81,11 +81,11 @@ function boardPut(board, where, side, piece){
function countMoves(board) {
var n = 0;
while (board && board['prior']) {
if (board['prior']['player'] !== board['player']) {
while (board && board.prior) {
if (board.prior.player !== board.player) {
++n;
}
board = board['prior'];
board = board.prior;
}
return n;
}
@ -133,18 +133,18 @@ function hasMoved(board, side, where){
return false;
}
const move = board['move'];
const move = board.move;
if (move && move['side'] === side && move['to'] === where) {
if (move && move.side === side && move.to === where) {
return true;
}
board = board['prior'];
board = board.prior;
}
}
function legalMoves(board, side, type, from, canCapture){
if (board['move'] && board['move']['took'] === 'k') {
if (board.move && board.move.took === 'k') {
return [];
}
@ -153,7 +153,7 @@ function legalMoves(board, side, type, from, canCapture){
var legals = [];
if (from === 'phantom') {
from = board['phantom']['from'];
from = board.phantom.from;
}
if (type === 'k') {
@ -224,16 +224,16 @@ function legalMoves(board, side, type, from, canCapture){
legals.push(there);
} else {
var otherBoard = board;
while (otherBoard && otherBoard['move'] && otherBoard['move']['side'][0] === side[0]) {
otherBoard = otherBoard['prior'];
while (otherBoard && otherBoard.move && otherBoard.move.side[0] === side[0]) {
otherBoard = otherBoard.prior;
}
if (otherBoard && otherBoard['move']) {
const move = otherBoard['move'];
if (move['side'][0] !== side[0] && move['type'] === 'p') {
if (otherBoard && otherBoard.move) {
const move = otherBoard.move;
if (move.side[0] !== side[0] && move.type === 'p') {
const from =
(move['from'] === 'phantom') ?
otherBoard['prior']['phantom']['from'] :
move['from'];
(move.from === 'phantom') ?
otherBoard.prior.phantom.from :
move.from;
if (from[0] === there[0] && from[1] === forward2[1]) {
/* en passant */
legals.push(there);
@ -255,7 +255,7 @@ function movePiece(priorBoard, side, from, to){
var took = boardGet(priorBoard, to, other);
var replaced = boardGet(priorBoard, to, side);
var alongside = boardGet(priorBoard, from, other);
var actuallyFrom = (from === 'phantom') ? priorBoard['phantom']['from'] : from;
var actuallyFrom = (from === 'phantom') ? priorBoard.phantom.from : from;
const legals = legalMoves(priorBoard, side, type, from, alongside === ' ');
if (!legals.includes(to)) {
@ -263,69 +263,69 @@ function movePiece(priorBoard, side, from, to){
}
var undoBoard = priorBoard;
if (undoBoard['subsequent']) {
if (undoBoard.subsequent) {
undoBoard = cloneJSON(undoBoard);
delete undoBoard['subsequent'];
delete undoBoard.subsequent;
}
var board = cloneJSON(undoBoard);
board['prior'] = undoBoard;
board.prior = undoBoard;
board['timestamp'] = new Date().getTime();
board.timestamp = new Date().getTime();
board['move'] = {
board.move = {
'side': normalizeSide(side),
'type': type,
'from': from,
'to': to
};
if (took !== ' ') {
board['move']['took'] = took;
board.move.took = took;
}
if (replaced !== ' ') {
board['move']['replaced'] = replaced;
board.move.replaced = replaced;
}
if (alongside !== ' ') {
board['move']['alongside'] = alongside;
board.move.alongside = alongside;
}
if (type === 'k' && actuallyFrom[0] === 'e' && to[0] === 'g') {
board['move']['castle'] = true;
board.move.castle = true;
boardPut(board, 'h' + actuallyFrom[1], side, ' ');
boardPut(board, 'f' + actuallyFrom[1], side, 'r');
}
if (type === 'k' && actuallyFrom[0] === 'e' && to[0] === 'c') {
board['move']['queen_castle'] = true;
board.move.queen_castle = true;
boardPut(board, 'a' + actuallyFrom[1], side, ' ');
boardPut(board, 'd' + actuallyFrom[1], side, 'r');
}
if (type === 'p' && (isDark(side) ? (to[1] === '1') : (to[1] === '8'))) {
board['move']['promotion'] = 'q'; /* TODO: allow other choices */
board.move.promotion = 'q'; /* TODO: allow other choices */
type = 'q';
}
if (alongside === 'p' && (isDark(other) ? (to[1] === '1') : (to[1] === '8'))) {
board['move']['promotion'] = 'q'; /* TODO: allow other choices */
board.move.promotion = 'q'; /* TODO: allow other choices */
alongside = 'q';
}
if (type === 'p' && alongside === ' ' && to[0] !== actuallyFrom[0]) {
var otherBoard = priorBoard;
while (otherBoard && otherBoard['move'] && otherBoard['move']['side'][0] === side[0]) {
otherBoard = otherBoard['prior'];
while (otherBoard && otherBoard.move && otherBoard.move.side[0] === side[0]) {
otherBoard = otherBoard.prior;
}
if (otherBoard && otherBoard['move']) {
const move = otherBoard['move'];
if (move['type'] === 'p' && move['to'][0] === to[0] && move['to'][1] === actuallyFrom[1]) {
const moveFrom = (move['from'] === 'phantom') ? otherBoard['prior']['phantom']['from'] : move['from'];
if (move['side'][0] === other[0] && moveFrom[1] != to[1]) {
board['move']['en_passant'] = true;
if (otherBoard && otherBoard.move) {
const move = otherBoard.move;
if (move.type === 'p' && move.to[0] === to[0] && move.to[1] === actuallyFrom[1]) {
const moveFrom = (move.from === 'phantom') ? otherBoard.prior.phantom.from : move.from;
if (move.side[0] === other[0] && moveFrom[1] != to[1]) {
board.move.en_passant = true;
alongside = 'p';
boardPut(board, move['to'], other, ' ');
boardPut(board, move.to, other, ' ');
}
}
}
}
if (from === 'phantom') {
delete board['phantom'];
delete board.phantom;
} else {
boardPut(board, from, side, ' ');
boardPut(board, from, other, ' ');
@ -338,9 +338,9 @@ function movePiece(priorBoard, side, from, to){
}
if (replaced === ' ') {
board['player'] = otherSide(board['player']);
board.player = otherSide(board.player);
} else {
board['phantom'] = { 'from': to, 'type': replaced + side[0] };
board.phantom = { 'from': to, 'type': replaced + side[0] };
}
return board;
@ -349,9 +349,9 @@ function movePiece(priorBoard, side, from, to){
function renderHistory(board) {
var list = [];
while (board && board['move']) {
list.push(board['move']);
board = board['prior'];
while (board && board.move) {
list.push(board.move);
board = board.prior;
}
const NBSP = '\u00a0'; /* non-breaking space */
@ -363,48 +363,48 @@ function renderHistory(board) {
while (list.length > 0) {
const move = list.pop();
if (move['from'] === 'phantom') {
const piece = move['type'] === 'p' ? '' : move['type'].toUpperCase();
const took = move['took'] ? 'x' : '';
result += SHY + '*' + piece + took + move['to'];
if (move.from === 'phantom') {
const piece = move.type === 'p' ? '' : move.type.toUpperCase();
const took = move.took ? 'x' : '';
result += SHY + '*' + piece + took + move.to;
} else {
if (n > 0 || move['side'] === 'dark') {
if (n > 0 || move.side === 'dark') {
result += ' ';
}
if (move['side'] === 'light') {
if (move.side === 'light') {
++n;
result += String(n) + '.' + NBSP;
}
if (move['pass']) {
if (move.pass) {
result += '...';
} else if (move['alongside']) {
if (move['side'] === 'light') {
result += move['type'].toUpperCase() + move['alongside'].toUpperCase() + move['from'] + move['to'];
} else if (move.alongside) {
if (move.side === 'light') {
result += move.type.toUpperCase() + move.alongside.toUpperCase() + move.from + move.to;
} else {
result += move['alongside'].toUpperCase() + move['type'].toUpperCase() + move['from'] + move['to'];
result += move.alongside.toUpperCase() + move.type.toUpperCase() + move.from + move.to;
}
} else if (move['castle']) {
} else if (move.castle) {
result += 'O-O';
} else if (move['queen_castle']) {
} else if (move.queen_castle) {
result += 'O-O-O';
} else {
const piece = move['type'] === 'p' ? '' : move['type'].toUpperCase();
const took = move['took'] ? 'x' : '';
result += piece + move['from'] + took + move['to'];
const piece = move.type === 'p' ? '' : move.type.toUpperCase();
const took = move.took ? 'x' : '';
result += piece + move.from + took + move.to;
}
}
if (move['en_passant']) {
if (move.en_passant) {
result += 'e.p.';
}
if (move['promotion']) {
result += '(' + move['promotion'][0].toUpperCase() + ')';
if (move.promotion) {
result += '(' + move.promotion[0].toUpperCase() + ')';
}
if (move['took'] === 'k') {
if (move.took === 'k') {
result += '#';
}
}
@ -417,7 +417,7 @@ function pieceStartDrag(ev, ui){
const dragged = $(this);
const type = dragged.data('type');
const from = dragged.data('location');
const where = (from === 'phantom') ? board['phantom']['from'] : from;
const where = (from === 'phantom') ? board.phantom.from : from;
const canCapture = boardGet(board, from, otherSide(type[1])) === ' ';
const legals = legalMoves(board, type[1], type[0], where, canCapture);
for (const there of legals) {
@ -471,35 +471,35 @@ function renderBoard(board){
}
}
var clss = board['player'] === 'light' ? '.cb-lt-piece' : '.cb-dk-piece';
var clss = board.player === 'light' ? '.cb-lt-piece' : '.cb-dk-piece';
if (board['phantom']) {
var where = board['phantom']['from'];
placePiece('phantom', board['phantom']['type'], 'ph');
if (board.phantom) {
var where = board.phantom.from;
placePiece('phantom', board.phantom.type, 'ph');
$('#cb_phantom').appendTo('#cb_' + where);
$('#cb_board .ui-draggable').draggable('disable');
$('#cb_phantom .ui-draggable-disabled').filter(clss).draggable('enable');
} else {
$('#cb_board .ui-draggable').draggable('disable');
if (!board['move'] || board['move']['took'] !== 'k') {
if (!board.move || board.move.took !== 'k') {
$('#cb_board .ui-draggable-disabled').filter(clss).draggable('enable');
}
}
if (board['move']) {
if (board['move']['from'] === 'phantom') {
$('#cb_' + board['prior']['move']['to']).addClass('cb-start');
if (board.move) {
if (board.move.from === 'phantom') {
$('#cb_' + board.prior.move.to).addClass('cb-start');
} else {
$('#cb_' + board['move']['from']).addClass('cb-start');
$('#cb_' + board.move.from).addClass('cb-start');
}
$('#cb_' + board['move']['to']).addClass('cb-end');
$('#cb_' + board.move.to).addClass('cb-end');
}
var msg = '';
if (board['move'] && board['move']['took'] === 'k') {
msg = (board['move']['side'][0] === 'd' ? 'Dark' : 'Light') + ' player won!';
if (board.move && board.move.took === 'k') {
msg = (board.move.side[0] === 'd' ? 'Dark' : 'Light') + ' player won!';
} else {
msg = (board['player'][0] === 'd' ? 'Dark' : 'Light') + " player's move";
msg = (board.player[0] === 'd' ? 'Dark' : 'Light') + " player's move";
}
$('#cb_message').text(msg);
@ -510,17 +510,17 @@ function setVisibleBoard(board, live){
$('#cb_board').data('visible_board', board);
renderBoard(board);
$('#cb_nav_first').attr('disabled', board['prior'] ? false : true);
$('#cb_nav_prev_turn').attr('disabled', board['prior'] ? false : true);
$('#cb_nav_prev_state').attr('disabled', board['prior'] ? false : true);
$('#cb_nav_next_state').attr('disabled', board['subsequent'] ? false : true);
$('#cb_nav_next_turn').attr('disabled', board['subsequent'] ? false : true);
$('#cb_nav_first').attr('disabled', board.prior ? false : true);
$('#cb_nav_prev_turn').attr('disabled', board.prior ? false : true);
$('#cb_nav_prev_state').attr('disabled', board.prior ? false : true);
$('#cb_nav_next_state').attr('disabled', board.subsequent ? false : true);
$('#cb_nav_next_turn').attr('disabled', board.subsequent ? false : true);
if (live) {
const liveBoard = $('#cb_board').data('board');
$('#cb_undo').attr('disabled', liveBoard['prior'] ? false : true);
$('#cb_redo').attr('disabled', liveBoard['subsequent'] ? false : true);
$('#cb_pass').attr('disabled', liveBoard['phantom'] ? true : false);
$('#cb_undo').attr('disabled', liveBoard.prior ? false : true);
$('#cb_redo').attr('disabled', liveBoard.subsequent ? false : true);
$('#cb_pass').attr('disabled', liveBoard.phantom ? true : false);
$('#cb_nav_last').attr('disabled', true);
$('#cb_board').addClass('cb-live');
$('#cb_board').removeClass('cb-archive');
@ -571,13 +571,13 @@ function putMeta(){
var lightName = $('#cb_light_name').val();
var darkName = $('#cb_dark_name').val();
var meta = gun.get(PacoSakoUUID).get('meta').get(gameId);
var stat = (board['move'] && board['move']['took'] === 'k') ? 'mate' : null;
var stat = (board.move && board.move.took === 'k') ? 'mate' : null;
meta.put({
'gameId': gameId,
'lightName': lightName,
'darkName': darkName,
'moves': countMoves(board),
'timestamp': board['timestamp'] || new Date().getTime(),
'timestamp': board.timestamp || new Date().getTime(),
'status': stat,
});
}
@ -608,18 +608,18 @@ function switchGameId(newId){
$('#cb_board').removeData('skip_notify');
gun.get(PacoSakoUUID).get('games').get(newId).on(function(d){
if (d && d['board'] && $('#cb_board').data('gameId') === newId) {
const board = JSON.parse(d['board']);
if (d && d.board && $('#cb_board').data('gameId') === newId) {
const board = JSON.parse(d.board);
const cb_board = $('#cb_board').first();
if ($('#cb_notify')[0].checked && cb_board.data('skip_notify') !== d['board']) {
if ($('#cb_notify')[0].checked && cb_board.data('skip_notify') !== d.board) {
/* ignore partial moves and undo/redo */
if (!board['phantom'] && !board['subsequent']) {
if (board['move'] && board['move']['took'] === 'k') {
notify((board['move']['side'][0] === 'd' ? 'Dark' : 'Light') + ' player won!');
if (!board.phantom && !board.subsequent) {
if (board.move && board.move.took === 'k') {
notify((board.move.side[0] === 'd' ? 'Dark' : 'Light') + ' player won!');
} else {
notify((board['player'][0] === 'd' ? 'Dark' : 'Light') + ' player\'s turn');
notify((board.player[0] === 'd' ? 'Dark' : 'Light') + ' player\'s turn');
}
$('#cb_board').data('skip_notify', d['board']);
$('#cb_board').data('skip_notify', d.board);
}
}
setCurrentBoard(board);
@ -628,8 +628,8 @@ function switchGameId(newId){
gun.get(PacoSakoUUID).get('meta').get(newId).on(function(d){
if (d && $('#cb_board').data('gameId') === newId) {
$('#cb_light_name').val(d['lightName'] || '');
$('#cb_dark_name').val(d['darkName'] || '');
$('#cb_light_name').val(d.lightName || '');
$('#cb_dark_name').val(d.darkName || '');
}
});
@ -796,12 +796,12 @@ $(function (){
$('#cb_pass').on('click', function(){
var board = $('#cb_board').data('board');
if (!board['phantom']) {
if (!board.phantom) {
var newBoard = cloneJSON(board);
newBoard['prior'] = board;
newBoard['move'] = { 'side': board['player'], 'pass': true };
newBoard['player'] = otherSide(board['player']);
newBoard['timestamp'] = new Date().getTime();
newBoard.prior = board;
newBoard.move = { 'side': board.player, 'pass': true };
newBoard.player = otherSide(board.player);
newBoard.timestamp = new Date().getTime();
putState(newBoard);
}
});
@ -857,32 +857,32 @@ $(function (){
}
gun.get(PacoSakoUUID).get('meta').map().on(function(d){
if (d && d['gameId']) {
const lightName = d['lightName'] ? d['lightName'] : 'Light';
const darkName = d['darkName'] ? d['darkName'] : 'Dark';
const moves = !d['moves'] ? '' :
(', ' + d['moves'] + (d['moves'] === 1 ? ' move' : ' moves'));
if (d && d.gameId) {
const lightName = d.lightName ? d.lightName : 'Light';
const darkName = d.darkName ? d.darkName : 'Dark';
const moves = !d.moves ? '' :
(', ' + d.moves + (d.moves === 1 ? ' move' : ' moves'));
var opt = $('#cb_game_' + d['gameId']);
var opt = $('#cb_game_' + d.gameId);
if (!(d['lightName'] || d['darkName']) && !d['moves'] && d['gameId'] !== $('#cb_board').data('gameId')) {
if (!(d.lightName || d.darkName) && !d.moves && d.gameId !== $('#cb_board').data('gameId')) {
if (opt.length >= 1) {
opt.remove();
}
} else {
if (opt.length === 0) {
opt = $('<option></option>');
opt.attr('id', 'cb_game_' + d['gameId']);
opt.attr('id', 'cb_game_' + d.gameId);
}
var stat = '';
if (d['status']) {
stat = ', ' + d['status'];
if (d.status) {
stat = ', ' + d.status;
}
opt.data('gameId', d['gameId']);
opt.data('gameId', d.gameId);
opt.data('title', lightName + ' vs. ' + darkName + moves + stat);
opt.data('then', d['timestamp'] || new Date().getTime());
opt.data('then', d.timestamp || new Date().getTime());
opt.addClass('cb-game-option');
opt.appendTo('#cb_select_game');
updateTitle(opt);