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