replace all 'var' declarations with 'let'
This commit is contained in:
parent
807f834aaa
commit
cbb8c28542
160
js/chess.js
160
js/chess.js
|
|
@ -1,13 +1,13 @@
|
|||
'use strict';
|
||||
|
||||
var gun = Gun({
|
||||
let gun = Gun({
|
||||
peers: ['https://jessemcdonald.info/gun'],
|
||||
/* workaround for persistent errors accumulating which break synchronization */
|
||||
localStorage: false,
|
||||
});
|
||||
|
||||
let initialBoard = (function (){
|
||||
var init = JSON.stringify({
|
||||
let init = JSON.stringify({
|
||||
light: [
|
||||
'rnbqkbnr',
|
||||
'pppppppp',
|
||||
|
|
@ -63,24 +63,24 @@ function boardGet(board, where, side){
|
|||
return board.phantom.type[0];
|
||||
}
|
||||
} else {
|
||||
var column = 'abcdefgh'.indexOf(where[0]);
|
||||
var row = Number(where[1]) - 1;
|
||||
let column = 'abcdefgh'.indexOf(where[0]);
|
||||
let row = Number(where[1]) - 1;
|
||||
return board[side][row][column];
|
||||
}
|
||||
}
|
||||
|
||||
function boardPut(board, where, side, piece){
|
||||
side = normalizeSide(side);
|
||||
var column = 'abcdefgh'.indexOf(where[0]);
|
||||
var row = Number(where[1]) - 1;
|
||||
var data = board[side][row];
|
||||
var prefix = data.substring(0, column);
|
||||
var suffix = data.substring(column + 1, 8);
|
||||
let column = 'abcdefgh'.indexOf(where[0]);
|
||||
let row = Number(where[1]) - 1;
|
||||
let data = board[side][row];
|
||||
let prefix = data.substring(0, column);
|
||||
let suffix = data.substring(column + 1, 8);
|
||||
board[side][row] = prefix + piece + suffix;
|
||||
}
|
||||
|
||||
function countMoves(board) {
|
||||
var n = 0;
|
||||
let n = 0;
|
||||
while (board && board.prior) {
|
||||
if (board.prior.player !== board.player) {
|
||||
++n;
|
||||
|
|
@ -91,8 +91,8 @@ function countMoves(board) {
|
|||
}
|
||||
|
||||
function offsetSquare(from, columnsRight, rowsUp){
|
||||
var column = 'abcdefgh'.indexOf(from[0]);
|
||||
var row = Number(from[1]) - 1;
|
||||
let column = 'abcdefgh'.indexOf(from[0]);
|
||||
let row = Number(from[1]) - 1;
|
||||
|
||||
if (column < 0 || column > 7 || row < 0 || row > 7) {
|
||||
return null;
|
||||
|
|
@ -109,14 +109,14 @@ function offsetSquare(from, columnsRight, rowsUp){
|
|||
}
|
||||
|
||||
function validDestination(board, side, where, canCapture){
|
||||
var ours = boardGet(board, where, side);
|
||||
var theirs = boardGet(board, where, otherSide(side));
|
||||
let ours = boardGet(board, where, side);
|
||||
let theirs = boardGet(board, where, otherSide(side));
|
||||
return ((theirs === ' ') ? (ours === ' ') : canCapture);
|
||||
}
|
||||
|
||||
function scanPath(accum, board, side, from, canCapture, columnsLeft, rowsUp, remainder){
|
||||
while (true) {
|
||||
var there = offsetSquare(from, columnsLeft, rowsUp);
|
||||
let there = offsetSquare(from, columnsLeft, rowsUp);
|
||||
if (!there || !validDestination(board, side, there, canCapture)) { return; }
|
||||
accum.push(there);
|
||||
if (boardGet(board, there, otherSide(side)) !== ' ' || remainder < 1) { return; }
|
||||
|
|
@ -150,7 +150,7 @@ function legalMoves(board, side, type, from, canCapture){
|
|||
|
||||
const ortho = [[-1, 0], [1, 0], [0, -1], [0, 1]];
|
||||
const diag = [[-1, -1], [-1, 1], [1, -1], [1, 1]];
|
||||
var legals = [];
|
||||
let legals = [];
|
||||
|
||||
if (from === 'phantom') {
|
||||
from = board.phantom.from;
|
||||
|
|
@ -223,7 +223,7 @@ function legalMoves(board, side, type, from, canCapture){
|
|||
if (boardGet(board, there, otherSide(side)) !== ' ') {
|
||||
legals.push(there);
|
||||
} else {
|
||||
var otherBoard = board;
|
||||
let otherBoard = board;
|
||||
while (otherBoard && otherBoard.move && otherBoard.move.side[0] === side[0]) {
|
||||
otherBoard = otherBoard.prior;
|
||||
}
|
||||
|
|
@ -250,26 +250,26 @@ function legalMoves(board, side, type, from, canCapture){
|
|||
}
|
||||
|
||||
function movePiece(priorBoard, side, from, to){
|
||||
var other = otherSide(side);
|
||||
var type = boardGet(priorBoard, from, side);
|
||||
var took = boardGet(priorBoard, to, other);
|
||||
var replacedFrom = to;
|
||||
var replaced = boardGet(priorBoard, replacedFrom, side);
|
||||
var alongside = boardGet(priorBoard, from, other);
|
||||
var actuallyFrom = (from === 'phantom') ? priorBoard.phantom.from : from;
|
||||
let other = otherSide(side);
|
||||
let type = boardGet(priorBoard, from, side);
|
||||
let took = boardGet(priorBoard, to, other);
|
||||
let replacedFrom = to;
|
||||
let replaced = boardGet(priorBoard, replacedFrom, side);
|
||||
let alongside = boardGet(priorBoard, from, other);
|
||||
let actuallyFrom = (from === 'phantom') ? priorBoard.phantom.from : from;
|
||||
|
||||
const legals = legalMoves(priorBoard, side, type, from, alongside === ' ');
|
||||
if (!legals.includes(to)) {
|
||||
return priorBoard;
|
||||
}
|
||||
|
||||
var undoBoard = priorBoard;
|
||||
let undoBoard = priorBoard;
|
||||
if (undoBoard.subsequent) {
|
||||
undoBoard = cloneJSON(undoBoard);
|
||||
delete undoBoard.subsequent;
|
||||
}
|
||||
|
||||
var board = cloneJSON(undoBoard);
|
||||
let board = cloneJSON(undoBoard);
|
||||
board.prior = undoBoard;
|
||||
|
||||
board.timestamp = new Date().getTime();
|
||||
|
|
@ -308,7 +308,7 @@ function movePiece(priorBoard, side, from, to){
|
|||
alongside = 'q';
|
||||
}
|
||||
if (type === 'p' && alongside === ' ' && replaced === ' ' && to[0] !== actuallyFrom[0]) {
|
||||
var otherBoard = priorBoard;
|
||||
let otherBoard = priorBoard;
|
||||
while (otherBoard && otherBoard.move && otherBoard.move.side[0] === side[0]) {
|
||||
otherBoard = otherBoard.prior;
|
||||
}
|
||||
|
|
@ -351,7 +351,7 @@ function movePiece(priorBoard, side, from, to){
|
|||
}
|
||||
|
||||
function renderHistory(board) {
|
||||
var list = [];
|
||||
let list = [];
|
||||
|
||||
while (board && board.move) {
|
||||
list.push(board.move);
|
||||
|
|
@ -362,8 +362,8 @@ function renderHistory(board) {
|
|||
const SHY = '\u00ad' /* soft hyphen */
|
||||
const ZWSP = '\u200b'; /* zero-width space */
|
||||
|
||||
var result = '';
|
||||
var n = 0;
|
||||
let result = '';
|
||||
let n = 0;
|
||||
|
||||
while (list.length > 0) {
|
||||
const move = list.pop();
|
||||
|
|
@ -434,8 +434,8 @@ function pieceStopDrag(ev, ui){
|
|||
}
|
||||
|
||||
function placePiece(where, type, count){
|
||||
var piece_id = 'cb_piece_' + type + '_' + count;
|
||||
var piece = $($('#' + piece_id)[0] || $('#cb_piece_' + type + ' img').clone());
|
||||
let piece_id = 'cb_piece_' + type + '_' + count;
|
||||
let piece = $($('#' + piece_id)[0] || $('#cb_piece_' + type + ' img').clone());
|
||||
piece.attr('style', '');
|
||||
piece.attr('id', piece_id);
|
||||
piece.data({ type: type, location: where });
|
||||
|
|
@ -459,26 +459,26 @@ function renderBoard(board){
|
|||
$('#cb_phantom').appendTo('#cb_hidden');
|
||||
|
||||
for (const side of ['light', 'dark']) {
|
||||
var counters = {};
|
||||
for (var row = 0; row < 8; ++row) {
|
||||
for (var column = 0; column < 8; ++column) {
|
||||
var here = 'abcdefgh'[column] + String(row+1);
|
||||
var type = board[side][row][column];
|
||||
let counters = {};
|
||||
for (let row = 0; row < 8; ++row) {
|
||||
for (let column = 0; column < 8; ++column) {
|
||||
let here = 'abcdefgh'[column] + String(row+1);
|
||||
let type = board[side][row][column];
|
||||
if (type !== ' ') {
|
||||
if (!counters[type]) {
|
||||
counters[type] = 0;
|
||||
}
|
||||
var count = ++counters[type];
|
||||
let count = ++counters[type];
|
||||
placePiece(here, type + side[0], count);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var clss = isLight(board.player) ? '.cb-lt-piece' : '.cb-dk-piece';
|
||||
let clss = isLight(board.player) ? '.cb-lt-piece' : '.cb-dk-piece';
|
||||
|
||||
if (board.phantom) {
|
||||
var where = board.phantom.from;
|
||||
let where = board.phantom.from;
|
||||
placePiece('phantom', board.phantom.type, 'ph');
|
||||
$('#cb_phantom').appendTo('#cb_' + where);
|
||||
$('#cb_board .ui-draggable').draggable('disable');
|
||||
|
|
@ -499,7 +499,7 @@ function renderBoard(board){
|
|||
$('#cb_' + board.move.to).addClass('cb-end');
|
||||
}
|
||||
|
||||
var msg = '';
|
||||
let msg = '';
|
||||
if (board.move && board.move.took === 'k') {
|
||||
msg = (isDark(board.move.side) ? 'Dark' : 'Light') + ' player won!';
|
||||
} else {
|
||||
|
|
@ -568,32 +568,32 @@ function setCurrentBoard(board){
|
|||
}
|
||||
|
||||
function randomId(){
|
||||
var res = '';
|
||||
for (var i = 0; i < 4; ++i) {
|
||||
var part = Math.floor(Math.random() * 65536).toString(16);
|
||||
let res = '';
|
||||
for (let i = 0; i < 4; ++i) {
|
||||
let part = Math.floor(Math.random() * 65536).toString(16);
|
||||
res = res + ('0000'.substring(part.length, 4) + part);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
var PacoSakoUUID = '7c38edd4-c931-49c8-9f1a-84de560815db';
|
||||
let PacoSakoUUID = '7c38edd4-c931-49c8-9f1a-84de560815db';
|
||||
|
||||
function putState(board){
|
||||
var boardElem = $('#cb_board');
|
||||
var gameId = boardElem.data('gameId');
|
||||
var game = gun.get(PacoSakoUUID).get('games').get(gameId);
|
||||
let boardElem = $('#cb_board');
|
||||
let gameId = boardElem.data('gameId');
|
||||
let game = gun.get(PacoSakoUUID).get('games').get(gameId);
|
||||
boardElem.data('last_state', cloneJSON(board));
|
||||
game.put({ board: JSON.stringify(board) });
|
||||
putMeta();
|
||||
}
|
||||
|
||||
function putMeta(){
|
||||
var board = $('#cb_board').data('board');
|
||||
var gameId = $('#cb_board').data('gameId');
|
||||
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;
|
||||
let board = $('#cb_board').data('board');
|
||||
let gameId = $('#cb_board').data('gameId');
|
||||
let lightName = $('#cb_light_name').val();
|
||||
let darkName = $('#cb_dark_name').val();
|
||||
let meta = gun.get(PacoSakoUUID).get('meta').get(gameId);
|
||||
let stat = (board.move && board.move.took === 'k') ? 'mate' : null;
|
||||
meta.put({
|
||||
gameId: gameId,
|
||||
lightName: lightName,
|
||||
|
|
@ -605,8 +605,8 @@ function putMeta(){
|
|||
}
|
||||
|
||||
function switchGameId(newId){
|
||||
var boardElem = $('#cb_board');
|
||||
var gameId = boardElem.data('gameId');
|
||||
let boardElem = $('#cb_board');
|
||||
let gameId = boardElem.data('gameId');
|
||||
|
||||
if (newId === gameId) {
|
||||
return;
|
||||
|
|
@ -631,7 +631,7 @@ function switchGameId(newId){
|
|||
closeNotifications();
|
||||
|
||||
/* this will be the starting state if no data is received from peers */
|
||||
var newBoard = initialBoard();
|
||||
let newBoard = initialBoard();
|
||||
setCurrentBoard(newBoard);
|
||||
boardElem.data('last_state', cloneJSON(newBoard));
|
||||
|
||||
|
|
@ -656,7 +656,7 @@ function switchGameId(newId){
|
|||
}
|
||||
});
|
||||
|
||||
var selOpt = $('#cb_game_' + newId);
|
||||
let selOpt = $('#cb_game_' + newId);
|
||||
if (selOpt.length === 1) {
|
||||
$('#cb_select_game')[0].selectedIndex = selOpt.index();
|
||||
} else {
|
||||
|
|
@ -755,13 +755,13 @@ $(function (){
|
|||
$(this).droppable('disable');
|
||||
},
|
||||
drop: function(ev, ui) {
|
||||
var dragged = ui.draggable;
|
||||
var type = dragged.data('type');
|
||||
var from = dragged.data('location');
|
||||
var to = this.id.replace(/^cb_/, '');
|
||||
let dragged = ui.draggable;
|
||||
let type = dragged.data('type');
|
||||
let from = dragged.data('location');
|
||||
let to = this.id.replace(/^cb_/, '');
|
||||
dragged.appendTo('#cb_hidden');
|
||||
|
||||
var newBoard = movePiece($('#cb_board').data('board'), type[1], from, to);
|
||||
let newBoard = movePiece($('#cb_board').data('board'), type[1], from, to);
|
||||
renderBoard(newBoard);
|
||||
putState(newBoard);
|
||||
},
|
||||
|
|
@ -806,7 +806,7 @@ $(function (){
|
|||
});
|
||||
|
||||
$('#cb_nav_first').on('click', function(){
|
||||
var board = $('#cb_board').data('visible_board');
|
||||
let board = $('#cb_board').data('visible_board');
|
||||
if (board) {
|
||||
while (board.prior) {
|
||||
board = stepBack(board);
|
||||
|
|
@ -816,7 +816,7 @@ $(function (){
|
|||
});
|
||||
|
||||
$('#cb_nav_prev_turn').on('click', function(){
|
||||
var board = $('#cb_board').data('visible_board');
|
||||
let board = $('#cb_board').data('visible_board');
|
||||
board = stepBack(board);
|
||||
const player = board.player;
|
||||
while (board.prior && board.prior.player === player) {
|
||||
|
|
@ -834,7 +834,7 @@ $(function (){
|
|||
});
|
||||
|
||||
$('#cb_nav_next_turn').on('click', function(){
|
||||
var board = $('#cb_board').data('visible_board');
|
||||
let board = $('#cb_board').data('visible_board');
|
||||
const player = board.player;
|
||||
board = stepForward(board);
|
||||
while (board.subsequent && board.player === player) {
|
||||
|
|
@ -854,9 +854,9 @@ $(function (){
|
|||
});
|
||||
|
||||
$('#cb_pass').on('click', function(){
|
||||
var board = $('#cb_board').data('board');
|
||||
let board = $('#cb_board').data('board');
|
||||
if (!board.phantom) {
|
||||
var newBoard = cloneJSON(board);
|
||||
let newBoard = cloneJSON(board);
|
||||
newBoard.prior = board;
|
||||
newBoard.move = { side: board.player, pass: true };
|
||||
newBoard.player = otherSide(board.player);
|
||||
|
|
@ -866,11 +866,11 @@ $(function (){
|
|||
});
|
||||
|
||||
$('#cb_select_game').on('change', function(){
|
||||
var optIndex = $('#cb_select_game')[0].selectedIndex;
|
||||
let optIndex = $('#cb_select_game')[0].selectedIndex;
|
||||
if (optIndex === 0) {
|
||||
switchGameId(randomId());
|
||||
} else if (optIndex >= 1) {
|
||||
var opt = $('#cb_select_game option')[optIndex];
|
||||
let opt = $('#cb_select_game option')[optIndex];
|
||||
if (opt) {
|
||||
switchGameId(opt.id.replace(/^cb_game_/, ''));
|
||||
}
|
||||
|
|
@ -890,7 +890,7 @@ $(function (){
|
|||
$('#cb_light_name').on('input', updateMeta);
|
||||
$('#cb_dark_name').on('input', updateMeta);
|
||||
|
||||
var gameId = location.hash.replace(/^#\//, '');
|
||||
let gameId = location.hash.replace(/^#\//, '');
|
||||
if (gameId.length !== 16) {
|
||||
gameId = randomId();
|
||||
}
|
||||
|
|
@ -902,7 +902,7 @@ $(function (){
|
|||
|
||||
const then = opt.data('then');
|
||||
const now = new Date().getTime();
|
||||
var age_str = '';
|
||||
let age_str = '';
|
||||
if (then > now) {
|
||||
age_str = ' (future)';
|
||||
} else if ((now - then) < 60*60*1000) {
|
||||
|
|
@ -925,7 +925,7 @@ $(function (){
|
|||
const moves = !d.moves ? '' :
|
||||
(', ' + d.moves + (d.moves === 1 ? ' move' : ' moves'));
|
||||
|
||||
var opt = $('#cb_game_' + d.gameId);
|
||||
let opt = $('#cb_game_' + d.gameId);
|
||||
|
||||
if (!(d.lightName || d.darkName) && !d.moves && d.gameId !== $('#cb_board').data('gameId')) {
|
||||
if (opt.length >= 1) {
|
||||
|
|
@ -937,7 +937,7 @@ $(function (){
|
|||
opt.attr('id', 'cb_game_' + d.gameId);
|
||||
}
|
||||
|
||||
var stat = '';
|
||||
let stat = '';
|
||||
if (d.status) {
|
||||
stat = ', ' + d.status;
|
||||
}
|
||||
|
|
@ -949,8 +949,8 @@ $(function (){
|
|||
opt.appendTo('#cb_select_game');
|
||||
updateTitle(opt);
|
||||
|
||||
var select = $('#cb_select_game');
|
||||
var list = select.children('.cb-game-option').get();
|
||||
let select = $('#cb_select_game');
|
||||
let list = select.children('.cb-game-option').get();
|
||||
list.sort(function(a,b) {
|
||||
const then_a = $(a).data('then');
|
||||
const then_b = $(b).data('then');
|
||||
|
|
@ -962,7 +962,7 @@ $(function (){
|
|||
}
|
||||
}
|
||||
|
||||
var selOpt = $('#cb_game_' + $('#cb_board').data('gameId'));
|
||||
let selOpt = $('#cb_game_' + $('#cb_board').data('gameId'));
|
||||
if (selOpt.length === 1) {
|
||||
$('#cb_select_game')[0].selectedIndex = selOpt.index();
|
||||
} else {
|
||||
|
|
@ -979,8 +979,8 @@ $(function (){
|
|||
|
||||
window.setInterval(function(){
|
||||
const peers = gun._.opt.peers;
|
||||
var n_open = 0;
|
||||
var n_total = 0;
|
||||
let n_open = 0;
|
||||
let n_total = 0;
|
||||
for (const peerId in peers) {
|
||||
++n_total;
|
||||
try {
|
||||
|
|
@ -1003,7 +1003,7 @@ $(function (){
|
|||
}, 1000);
|
||||
|
||||
window.onpopstate = function(event){
|
||||
var gameId = location.hash.replace(/^#\//, '');
|
||||
let gameId = location.hash.replace(/^#\//, '');
|
||||
if (gameId.length === 16) {
|
||||
switchGameId(gameId);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue