prune unnamed, no-move games from the drop-down list
This commit is contained in:
parent
98ead6724f
commit
5286e9e18f
55
js/chess.js
55
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,9 +725,9 @@ $(function (){
|
|||
$('#cb_light_name').on('input', updateMeta);
|
||||
$('#cb_dark_name').on('input', updateMeta);
|
||||
|
||||
gun.get(PacoSakoUUID).get('meta').map().on(function(d){
|
||||
if (d && d['gameId']) {
|
||||
function updateTitle(opt){
|
||||
opt = $(opt);
|
||||
|
||||
const then = opt.data('then');
|
||||
const now = new Date().getTime();
|
||||
var age_str = '';
|
||||
|
|
@ -734,32 +739,39 @@ $(function (){
|
|||
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')) {
|
||||
} else if (opt.data('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 = $('<option></option>');
|
||||
opt.attr('id', 'cb_game_' + d['gameId']);
|
||||
|
||||
function refreshTitle(){
|
||||
updateTitle(opt);
|
||||
window.setTimeout(refreshTitle, 15000);
|
||||
};
|
||||
|
||||
window.setTimeout(refreshTitle, 0);
|
||||
}
|
||||
|
||||
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'));
|
||||
|
||||
opt.data('title', lightName + ' vs. ' + darkName + moves);
|
||||
var opt = $('#cb_game_' + d['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']);
|
||||
}
|
||||
|
||||
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');
|
||||
|
|
@ -776,6 +788,7 @@ $(function (){
|
|||
for (const e of list) {
|
||||
$(e).appendTo(select);
|
||||
}
|
||||
}
|
||||
|
||||
var selOpt = $('#cb_game_' + $('#cb_board').data('gameId'));
|
||||
if (selOpt.length === 1) {
|
||||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue