replace all 'var' declarations with 'let'

This commit is contained in:
Jesse D. McDonald 2020-03-15 11:35:06 -05:00
parent 807f834aaa
commit cbb8c28542
1 changed files with 80 additions and 80 deletions

View File

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