stop using Gun.chain.map; monitor game list changes manually
This commit is contained in:
parent
f184df6447
commit
8e639a68eb
|
|
@ -796,60 +796,78 @@ $(function (){
|
||||||
}
|
}
|
||||||
|
|
||||||
window.setTimeout(function(){
|
window.setTimeout(function(){
|
||||||
gun.get(PacoSakoUUID).get('meta').map().on(function(d,key){
|
function updateSelectMeta(d, key) {
|
||||||
if (key.match(/^[0-9a-f]{16}$/)) {
|
d = d || {};
|
||||||
d = d || {};
|
debug('got meta for key ' + key, d);
|
||||||
debug('got meta for key ' + key, d);
|
const lightName = shortenName(String(d.lightName || 'Light'));
|
||||||
const lightName = shortenName(String(d.lightName || 'Light'));
|
const darkName = shortenName(String(d.darkName || 'Dark'));
|
||||||
const darkName = shortenName(String(d.darkName || 'Dark'));
|
const moves = !d.moves ? '' :
|
||||||
const moves = !d.moves ? '' :
|
(', ' + d.moves + (d.moves === 1 ? ' turn' : ' turns'));
|
||||||
(', ' + d.moves + (d.moves === 1 ? ' turn' : ' turns'));
|
|
||||||
|
|
||||||
let opt = $('#cb_game_' + key);
|
let opt = $('#cb_game_' + key);
|
||||||
|
|
||||||
if (!d.lightName && !d.darkName && !d.moves && key !== $('#cb_board').data('gameId')) {
|
if (!d.lightName && !d.darkName && !d.moves && key !== $('#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_' + key);
|
opt.attr('id', 'cb_game_' + key);
|
||||||
}
|
|
||||||
|
|
||||||
let stat = '';
|
|
||||||
if (d.status) {
|
|
||||||
stat = ', ' + d.status;
|
|
||||||
}
|
|
||||||
|
|
||||||
opt.data('gameId', key);
|
|
||||||
opt.data('title', lightName + ' vs. ' + darkName + moves + stat);
|
|
||||||
opt.data('then', d.timestamp || new Date(Gun.state()).getTime());
|
|
||||||
opt.addClass('cb-game-option');
|
|
||||||
opt.appendTo('#cb_select_game');
|
|
||||||
updateTitle(opt);
|
|
||||||
|
|
||||||
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');
|
|
||||||
return (then_a < then_b) ? 1 : (then_a === then_b) ? 0 : -1;
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const e of list) {
|
|
||||||
$(e).appendTo(select);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let selOpt = $('#cb_game_' + $('#cb_board').data('gameId'));
|
let stat = '';
|
||||||
if (selOpt.length === 1) {
|
if (d.status) {
|
||||||
$('#cb_select_game')[0].selectedIndex = selOpt.index();
|
stat = ', ' + d.status;
|
||||||
} else {
|
}
|
||||||
$('#cb_select_game')[0].selectedIndex = -1;
|
|
||||||
|
opt.data('gameId', key);
|
||||||
|
opt.data('title', lightName + ' vs. ' + darkName + moves + stat);
|
||||||
|
opt.data('then', d.timestamp || new Date(Gun.state()).getTime());
|
||||||
|
opt.addClass('cb-game-option');
|
||||||
|
opt.appendTo('#cb_select_game');
|
||||||
|
updateTitle(opt);
|
||||||
|
|
||||||
|
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');
|
||||||
|
return (then_a < then_b) ? 1 : (then_a === then_b) ? 0 : -1;
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const e of list) {
|
||||||
|
$(e).appendTo(select);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
let selOpt = $('#cb_game_' + $('#cb_board').data('gameId'));
|
||||||
|
if (selOpt.length === 1) {
|
||||||
|
$('#cb_select_game')[0].selectedIndex = selOpt.index();
|
||||||
|
} else {
|
||||||
|
$('#cb_select_game')[0].selectedIndex = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let cancellers = {};
|
||||||
|
let cancelAll = gun.get(PacoSakoUUID).get('meta').onWithCancel(function(meta) {
|
||||||
|
for (const gameId in meta) { /* use of 'in' here is deliberate */
|
||||||
|
/* 'gameId' may include extra GUN fields like '_' */
|
||||||
|
if (gameId.match(/^[0-9a-f]{16}$/)) {
|
||||||
|
if (!Gun.obj.is(meta[gameId])) {
|
||||||
|
updateSelectMeta(null, gameId);
|
||||||
|
if (gameId in cancellers) {
|
||||||
|
cancellers[gameId]();
|
||||||
|
delete cancellers[gameId];
|
||||||
|
}
|
||||||
|
} else if (!(gameId in cancellers)) {
|
||||||
|
cancellers[gameId] = gun.get(meta[gameId]).onWithCancel(function(d) {
|
||||||
|
updateSelectMeta(d, gameId);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, { change: true });
|
||||||
}, 1);
|
}, 1);
|
||||||
|
|
||||||
window.setInterval(function(){
|
window.setInterval(function(){
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue