diff --git a/js/chess.js b/js/chess.js index 03208ef..a63553b 100644 --- a/js/chess.js +++ b/js/chess.js @@ -24,6 +24,7 @@ let initialBoard = (function (){ 'rnbqkbnr', ], 'player': 'light', + 'timestamp': new Date().getTime(), }); return function (){ return JSON.parse(init); } })(); @@ -265,6 +266,8 @@ function movePiece(priorBoard, side, from, to){ var board = cloneJSON(undoBoard); board['prior'] = undoBoard; + board['timestamp'] = new Date().getTime(); + board['move'] = { 'side': normalizeSide(side), 'type': type, @@ -523,12 +526,14 @@ 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; meta.put({ 'gameId': gameId, 'lightName': lightName, 'darkName': darkName, 'moves': countMoves(board), - 'timestamp': new Date().getTime() + 'timestamp': board['timestamp'] || new Date().getTime(), + 'status': stat, }); } @@ -720,61 +725,69 @@ $(function (){ $('#cb_light_name').on('input', updateMeta); $('#cb_dark_name').on('input', updateMeta); + function updateTitle(opt){ + opt = $(opt); + + const then = opt.data('then'); + const now = new Date().getTime(); + var age_str = ''; + if (then > now) { + age_str = ' (future)'; + } else if ((now - then) < 60*60*1000) { + age_str = ' (' + Math.floor((now - then) / (60*1000)) + 'm)'; + } else if ((now - then) < 24*60*60*1000) { + age_str = ' (' + Math.floor((now - then) / (60*60*1000)) + 'h)'; + } else if ((now - then) < 14*24*60*60*1000) { + age_str = ' (' + Math.floor((now - then) / (24*60*60*1000)) + 'd)'; + } else if (opt.data('gameId') !== $('#cb_board').data('gameId')) { + opt.remove(); + return; + } + opt.text(opt.data('title') + age_str); + } + gun.get(PacoSakoUUID).get('meta').map().on(function(d){ if (d && d['gameId']) { - function updateTitle(opt){ - const then = opt.data('then'); - const now = new Date().getTime(); - var age_str = ''; - if (then > now) { - age_str = ' (future)'; - } else if ((now - then) < 60*60*1000) { - age_str = ' (' + Math.floor((now - then) / (60*1000)) + 'm)'; - } else if ((now - then) < 24*60*60*1000) { - age_str = ' (' + Math.floor((now - then) / (60*60*1000)) + 'h)'; - } else if ((now - then) < 14*24*60*60*1000) { - age_str = ' (' + Math.floor((now - then) / (24*60*60*1000)) + 'd)'; - } else if (d['gameId'] !== $('#cb_board').data('gameId')) { - opt.remove(); - return; - } - opt.text(opt.data('title') + age_str); - } - - var opt = $('#cb_game_' + d['gameId']); - if (opt.length === 0) { - opt = $(''); - opt.attr('id', 'cb_game_' + d['gameId']); - - function refreshTitle(){ - updateTitle(opt); - window.setTimeout(refreshTitle, 15000); - }; - - window.setTimeout(refreshTitle, 0); - } - 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')); - opt.data('title', lightName + ' vs. ' + darkName + moves); - opt.data('then', d['timestamp'] || new Date().getTime()); - opt.addClass('cb-game-option'); - opt.appendTo('#cb_select_game'); - updateTitle(opt); + var opt = $('#cb_game_' + d['gameId']); - var select = $('#cb_select_game'); - var list = select.children('.cb-game-option').get(); - list.sort(function(a,b) { - const then_a = $(a).data('then'); - const then_b = $(b).data('then'); - return (then_a < then_b) ? 1 : (then_a === then_b) ? 0 : -1; - }); + 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 = $(''); + opt.attr('id', 'cb_game_' + d['gameId']); + } - for (const e of list) { - $(e).appendTo(select); + var stat = ''; + if (d['status']) { + stat = ', ' + d['status']; + } + + opt.data('gameId', d['gameId']); + opt.data('title', lightName + ' vs. ' + darkName + moves + stat); + opt.data('then', d['timestamp'] || new Date().getTime()); + opt.addClass('cb-game-option'); + opt.appendTo('#cb_select_game'); + updateTitle(opt); + + var select = $('#cb_select_game'); + var list = select.children('.cb-game-option').get(); + list.sort(function(a,b) { + const then_a = $(a).data('then'); + const then_b = $(b).data('then'); + return (then_a < then_b) ? 1 : (then_a === then_b) ? 0 : -1; + }); + + for (const e of list) { + $(e).appendTo(select); + } } var selOpt = $('#cb_game_' + $('#cb_board').data('gameId')); @@ -786,6 +799,12 @@ $(function (){ } }); + window.setInterval(function(){ + $('#cb_select_game').first().children('.cb-game-option').each(function(idx,opt){ + updateTitle(opt); + }); + }, 15000); + window.onpopstate = function(event){ var gameId = location.hash.replace(/^#\//, ''); if (gameId.length === 16) {