/**
* Character page: missions (with completing, resetting and giving one for staff), progress (completion per group and
* zone, compared with the server) and giving back what was lost since a snapshot. Names, states, groups and the
* possible changes all come from the server.
*/
(function () {
'use strict';
var config = window.characterToolsConfig;
if (!config) return;
var nf = new Intl.NumberFormat();
var canChange = !!config.canMissions;
function count(c) { return nf.format(c.done) + ' / ' + nf.format(c.available); }
function percent(c) { return c.available ? Math.round(100 * c.done / c.available) : 0; }
function bar(c) {
return '
' +
'
';
}
function group(m) { return [m.type, m.subtype].filter(Boolean).join(' / '); }
// ---- missions ----
var missions = [], changes = [], found = [];
function taskList(m) {
var tasks = m.tasks.map(function (t) {
return '
';
}
function loadProgress() {
api.get('/api/characters/' + config.id + '/progress').then(function (d) {
if (!d.success) { document.getElementById('progressBody').innerHTML = '
' + esc(d.error) + '
'; return; }
renderProgress(d);
// Averages come from reading every character; ask again once they're ready
if (d.average_pending && progressRetries++ < 10) setTimeout(loadProgress, 3000);
});
}
function wireProgress() {
var button = document.getElementById('progressBtn');
if (!button) return;
button.addEventListener('click', function () {
bootstrap.Modal.getOrCreateInstance(document.getElementById('progressModal')).show();
progressRetries = 0;
loadProgress();
});
}
// ---- giving back lost items ----
var restoreSnapshot = null, missing = [];
function renderRestore(data) {
missing = data.missing;
var box = document.getElementById('restoreCompare');
var restorable = missing.filter(function (m) { return m.restorable > 0; });
var html = '
' + esc(fmt.unix(data.taken_at)) + ' → now
';
var sets = data.sets.filter(function (s) { return s.lots.some(function (lot) { return restorable.some(function (m) { return m.lot === lot; }); }); });
if (sets.length) html += '
' + sets.map(function (s) {
return '';
}).join('') + '
';
html += '
Item
Then
Now
Accounted for
Give back
' +
missing.map(function (m) {
var accounted = [];
if (m.mailed) accounted.push(nf.format(m.mailed) + ' in their mailbox');
if (m.elsewhere) accounted.push(nf.format(m.elsewhere) + ' someone else has');
var disabled = m.restorable ? '' : ' disabled';
return '
'; });
}
function wireRestore() {
var button = document.getElementById('restoreBtn');
if (!button) return;
button.addEventListener('click', function () {
bootstrap.Modal.getOrCreateInstance(document.getElementById('restoreModal')).show();
api.get('/api/characters/' + config.id + '/snapshots').then(function (d) {
var list = document.getElementById('restoreSnapshots');
if (!d.success) { list.innerHTML = '
' + esc(d.error) + '
'; return; }
list.innerHTML = d.snapshots.map(function (s) {
return '
';
});
});
document.getElementById('restoreSnapshots').addEventListener('click', function (e) {
var item = e.target.closest('[data-snapshot]');
if (item) { e.preventDefault(); openRestoreSnapshot(item.dataset.snapshot); }
});
document.getElementById('restoreCompare').addEventListener('click', function (e) {
var set = e.target.closest('[data-set]');
if (!set) return;
var lots = []; // every missing piece of the set
document.querySelectorAll('#restoreCompare [data-pick]').forEach(function (box) {
var m = missing.filter(function (x) { return String(x.lot) === box.dataset.pick; })[0];
if (m && String(m.set) === set.dataset.set && !box.disabled) { box.checked = true; lots.push(m.lot); }
});
if (!lots.length) toast('Nothing of that set can be given back', 'warning');
});
document.getElementById('restoreSend').addEventListener('click', function () {
var items = [];
document.querySelectorAll('#restoreCompare [data-pick]:checked').forEach(function (box) {
var input = document.querySelector('#restoreCompare [data-count="' + box.dataset.pick + '"]');
items.push({ lot: Number(box.dataset.pick), count: Number(input.value) || 0 });
});
if (!items.length) { toast('Tick the items to give back', 'warning'); return; }
if (!confirm('Mail ' + items.length + ' kind(s) of item back to this character?')) return;
api.action('/api/characters/' + config.id + '/restore/' + restoreSnapshot, { items: items, note: document.getElementById('restoreNote').value.trim() })
.then(function (d) { return d.result; }).then(function (r) { if (r && r.success) openRestoreSnapshot(restoreSnapshot); }).catch(function () {});
});
}
wireMissions();
wireProgress();
wireRestore();
})();