Built: units/movements/reports/send/expansion screens, alliance/messages/rankings screens, ws client + server /ws push (docs/PROTOCOL.md), Internet exchange trade, backbone endgame + world end, shared reports, buildDone relative levels. Tested: npm test 175/175. Two adversarial reviews found issues (trade-leg recall exploit, __proto__ via validate.js, rankings DoS, static headers, ...): fixes follow in the next commit. NOT done: bots/balance, e2e suite, real-device checks. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
542 lines
31 KiB
JavaScript
542 lines
31 KiB
JavaScript
'use strict';
|
|
/*
|
|
* Alliance screen. Not in an alliance: found one (Peering office needed) and answer invitations. In one: overview with pinned
|
|
* notes, members (role changes, kick, transfer, invite), diplomacy. What is offered follows data.js SOCIAL.permissions through
|
|
* rules.js allianceCan/outranks (via UpstreamSocial); the server re-checks everything and its error codes are translated.
|
|
* Every player-controlled string is set with textContent.
|
|
*/
|
|
(function () {
|
|
const { h, clear, icon, toast, openSheet } = UI;
|
|
const Soc = UpstreamSocial;
|
|
const R = UpstreamRules;
|
|
const D = UpstreamData;
|
|
const S = D.SOCIAL;
|
|
const X = () => window.SocUI;
|
|
|
|
let box, body, tabsEl, content;
|
|
let mine = null; // GET /alliances/mine
|
|
let members = null; // GET /alliances/:id/members items
|
|
let tab = 'overview';
|
|
let gen = 0; // bumped on every load: stale async results are dropped
|
|
let offTick = null;
|
|
const cache = {}; // per-tab data
|
|
|
|
const myRole = () => (mine ? mine.role : null);
|
|
const acts = () => Soc.selfActions(myRole(), members ? members.length : (mine && mine.alliance ? mine.alliance.memberCount : 0));
|
|
const kv = (k, v) => h('div', { class: 'kv' }, h('span', { class: 'k', text: k }), h('span', { class: 'v' }, v));
|
|
|
|
// ------------------------------------------------------------------ load
|
|
async function load() {
|
|
const my = ++gen;
|
|
const had = !!mine;
|
|
if (!had) { clear(body); body.appendChild(X().loading()); }
|
|
try {
|
|
const m = await App.api.get('/alliances/mine');
|
|
if (my !== gen) return;
|
|
mine = m;
|
|
const al = m.alliance;
|
|
const cur = X().myAlliance();
|
|
if ((al && (!cur || cur.id !== al.id)) || (!al && cur)) X().refreshOperator().catch(() => {});
|
|
if (al) {
|
|
const r = await App.api.get('/alliances/' + al.id + '/members');
|
|
if (my !== gen) return;
|
|
members = r.items;
|
|
for (const k of Object.keys(cache)) delete cache[k];
|
|
drawIn();
|
|
} else {
|
|
members = null;
|
|
const inv = await App.api.get('/alliance-invites');
|
|
if (my !== gen) return;
|
|
cache.invites = inv.items;
|
|
drawNone();
|
|
}
|
|
} catch (e) {
|
|
if (my !== gen) return;
|
|
clear(body);
|
|
body.appendChild(X().errorBox(e, load));
|
|
}
|
|
}
|
|
|
|
// Reload after a change of membership (found, join, leave, kick...).
|
|
function reload() { mine = null; members = null; tab = 'overview'; return load(); }
|
|
|
|
// ------------------------------------------------------------------ not in an alliance
|
|
function drawNone() {
|
|
clear(body);
|
|
stopTick();
|
|
const have = mine.peeringLevel, need = S.minPeeringLevel, ok = have >= need;
|
|
const err = h('p', { class: 'form-error', role: 'alert', hidden: true });
|
|
const nameIn = h('input', { type: 'text', maxlength: String(S.nameLen[1] * 2), autocomplete: 'off', autocapitalize: 'words', spellcheck: 'false', id: 'soc-name' });
|
|
const tagIn = h('input', { type: 'text', maxlength: String(S.tagLen[1]), autocomplete: 'off', autocapitalize: 'characters', spellcheck: 'false', id: 'soc-tag' });
|
|
const create = h('button', { class: 'btn primary wide', type: 'button' }, icon('plus'), App.t('soc.create.go'));
|
|
const valid = () => {
|
|
const n = Array.from(Soc.cleanName(nameIn.value)).length, t = tagIn.value.trim();
|
|
return ok && n >= S.nameLen[0] && n <= S.nameLen[1] && /^[A-Za-z0-9]+$/.test(t) && t.length >= S.tagLen[0] && t.length <= S.tagLen[1];
|
|
};
|
|
const sync = () => { create.disabled = !valid(); };
|
|
nameIn.addEventListener('input', sync); tagIn.addEventListener('input', sync);
|
|
create.addEventListener('click', () => X().guard(create, async () => {
|
|
err.hidden = true;
|
|
try {
|
|
await App.api.post('/alliances', { name: Soc.cleanName(nameIn.value), tag: tagIn.value.trim() }, { intent: 'ally:create' });
|
|
toast(App.t('soc.create.done'), 'ok');
|
|
await reload();
|
|
} catch (e) {
|
|
err.textContent = e.code === 'validation_failed' ? App.t('soc.create.invalid', { n1: S.nameLen[0], n2: S.nameLen[1], t1: S.tagLen[0], t2: S.tagLen[1] }) : X().err(e);
|
|
err.hidden = false;
|
|
}
|
|
}));
|
|
sync();
|
|
|
|
body.appendChild(h('div', { class: 'card soc-card' },
|
|
h('div', { class: 'card-head' }, h('h2', null, icon('users'), App.t('soc.create.title'))),
|
|
h('p', { class: 'detail-desc', text: App.t('soc.create.intro') }),
|
|
h('ul', { class: 'reqs soc-req' }, h('li', { class: ok ? 'ok' : 'miss' }, icon(ok ? 'check' : 'lock'),
|
|
h('span', { text: App.buildingName('peering_office') + ' ' + App.t('pop.level', { n: need }) }), h('small', { text: App.t('sheet.have', { n: have }) }))),
|
|
ok ? null : h('p', { class: 'reason', text: App.t('soc.create.needOffice', { building: App.buildingName('peering_office'), n: need }) }),
|
|
h('label', { class: 'field', for: 'soc-name' }, h('span', { class: 'field-lbl', text: App.t('soc.create.name') }), nameIn,
|
|
h('span', { class: 'field-hint', text: App.t('soc.create.nameHint', { a: S.nameLen[0], b: S.nameLen[1] }) })),
|
|
h('label', { class: 'field', for: 'soc-tag' }, h('span', { class: 'field-lbl', text: App.t('soc.create.tag') }), tagIn,
|
|
h('span', { class: 'field-hint', text: App.t('soc.create.tagHint', { a: S.tagLen[0], b: S.tagLen[1] }) })),
|
|
err, create));
|
|
|
|
const invCard = h('div', { class: 'card soc-card' }, h('div', { class: 'card-head' }, h('h2', null, icon('mail'), App.t('soc.invites.title')),
|
|
h('button', { class: 'icon-btn', type: 'button', 'aria-label': App.t('common.retry'), onclick: () => load() }, icon('refresh'))));
|
|
const items = cache.invites || [];
|
|
if (!items.length) invCard.appendChild(h('p', { class: 'empty', text: App.t('soc.invites.none') }));
|
|
else {
|
|
if (!ok) invCard.appendChild(h('p', { class: 'reason', text: App.t('soc.invites.needOffice', { building: App.buildingName('peering_office') }) }));
|
|
const list = h('ul', { class: 'soc-list' });
|
|
const now = App.now();
|
|
for (const inv of items) {
|
|
const e2 = h('p', { class: 'form-error', role: 'alert', hidden: true });
|
|
const acc = h('button', { class: 'btn small primary', type: 'button', text: App.t('soc.invites.accept') });
|
|
const dec = h('button', { class: 'btn small', type: 'button', text: App.t('soc.invites.decline') });
|
|
const answer = (accept) => X().guard(accept ? acc : dec, async () => {
|
|
e2.hidden = true;
|
|
try {
|
|
await App.api.post('/alliance-invites/' + inv.id + (accept ? '/accept' : '/decline'), {}, { intent: 'ally:invite:' + inv.id });
|
|
toast(App.t(accept ? 'soc.invites.accepted' : 'soc.invites.declined'), 'ok');
|
|
await reload();
|
|
} catch (e) { e2.textContent = X().err(e); e2.hidden = false; }
|
|
});
|
|
acc.addEventListener('click', () => answer(true));
|
|
dec.addEventListener('click', () => answer(false));
|
|
list.appendChild(h('li', { class: 'soc-item soc-invite' },
|
|
h('div', { class: 'row-main' },
|
|
h('span', { class: 'row-title' }, X().tagPill(inv.alliance.tag), h('b', { text: inv.alliance.name })),
|
|
h('span', { class: 'row-sub', text: App.t('soc.invites.from', { name: inv.invitedBy.name }) + ' · ' + App.t('soc.invites.expires', { t: App.fmt.dur(Math.max(0, inv.expiresAt - now), 2) }) })),
|
|
h('div', { class: 'soc-actions' }, dec, acc), e2));
|
|
}
|
|
invCard.appendChild(list);
|
|
}
|
|
body.appendChild(invCard);
|
|
}
|
|
|
|
// ------------------------------------------------------------------ in an alliance
|
|
function drawIn() {
|
|
clear(body);
|
|
stopTick();
|
|
const a = mine.alliance;
|
|
const sizeP = a.sizeCap > 0 ? Math.min(1, a.memberCount / a.sizeCap) : 0;
|
|
body.appendChild(h('div', { class: 'card soc-card soc-head' },
|
|
h('div', { class: 'soc-title' }, X().tagPill(a.tag), h('h2', { class: 'soc-name', text: a.name })),
|
|
h('div', { class: 'pill-row' }, X().rolePill(mine.role),
|
|
h('span', { class: 'pill', text: App.t('soc.size', { n: a.memberCount, cap: a.sizeCap }) }),
|
|
h('span', { class: 'pill', text: App.t('soc.leaderIs', { name: a.leader ? a.leader.name : '-' }) })),
|
|
h('div', { class: 'bar', 'aria-hidden': 'true', style: { '--p': String(sizeP) } }, h('i'))));
|
|
|
|
const tabs = [['overview', 'soc.tab.overview'], ['members', 'soc.tab.members'], ['diplomacy', 'soc.tab.diplomacy']];
|
|
tabsEl = h('div', { class: 'seg soc-tabs', role: 'group', 'aria-label': App.t('soc.tab.label') },
|
|
tabs.map(([id, key]) => h('button', { class: 'seg-btn', type: 'button', dataset: { tab: id }, 'aria-pressed': String(tab === id), text: App.t(key), onclick: () => setTab(id) })));
|
|
content = h('div', { class: 'soc-content' });
|
|
body.appendChild(tabsEl);
|
|
body.appendChild(content);
|
|
drawTab();
|
|
}
|
|
|
|
function setTab(id) {
|
|
tab = id;
|
|
for (const b of tabsEl.children) b.setAttribute('aria-pressed', String(b.dataset.tab === id));
|
|
drawTab();
|
|
}
|
|
|
|
function drawTab() {
|
|
stopTick();
|
|
clear(content);
|
|
if (tab === 'members') drawMembers();
|
|
else if (tab === 'diplomacy') drawDiplomacy();
|
|
else drawOverview();
|
|
}
|
|
|
|
// ---------------------------------------------------------------- overview: notes, leave / disband
|
|
const nameOf = (id) => { const m = (members || []).find((x) => x.operatorId === id); return m ? m.name : '#' + id; };
|
|
|
|
async function drawOverview() {
|
|
const my = gen;
|
|
const A = acts();
|
|
const notesCard = h('div', { class: 'card soc-card' }, h('div', { class: 'card-head' }, h('h2', null, icon('pencil'), App.t('soc.notes.title')),
|
|
h('span', { class: 'pill dim mono', id: 'soc-notes-count' })));
|
|
const notesList = h('ul', { class: 'soc-list' });
|
|
notesCard.appendChild(notesList);
|
|
content.appendChild(notesCard);
|
|
|
|
function drawNotes(items) {
|
|
clear(notesList);
|
|
notesCard.querySelector('#soc-notes-count').textContent = items.length + '/' + S.maxNotes;
|
|
if (!items.length) notesList.appendChild(h('li', { class: 'empty', text: App.t('soc.notes.none') }));
|
|
for (const n of items) {
|
|
const del = h('button', { class: 'icon-btn', type: 'button', 'aria-label': App.t('soc.notes.remove'), onclick: (ev) => X().guard(ev.currentTarget, async () => {
|
|
await App.api.del('/alliances/' + mine.alliance.id + '/notes/' + n.id);
|
|
cache.notes = cache.notes.filter((x) => x.id !== n.id);
|
|
drawNotes(cache.notes);
|
|
}) }, icon('x'));
|
|
notesList.appendChild(h('li', { class: 'soc-item soc-note' },
|
|
h('div', { class: 'row-main' }, h('p', { class: 'soc-text-out', text: n.text }),
|
|
h('span', { class: 'row-sub', text: nameOf(n.authorId) + ' · ' + App.fmt.ago(App.now() - n.createdAt) })),
|
|
A.editNotes ? del : null));
|
|
}
|
|
}
|
|
|
|
if (A.editNotes) {
|
|
const ta = h('textarea', { class: 'soc-text', rows: 2, maxlength: String(S.noteMax * 2), 'aria-label': App.t('soc.notes.add'), placeholder: App.t('soc.notes.placeholder') });
|
|
const cnt = h('span', { class: 'soc-count mono' });
|
|
const err = h('p', { class: 'form-error', role: 'alert', hidden: true });
|
|
const add = h('button', { class: 'btn small primary', type: 'button' }, icon('plus'), App.t('soc.notes.add'));
|
|
const sync = () => {
|
|
const st = Soc.composeState(ta.value, S.noteMax);
|
|
cnt.textContent = st.n + '/' + S.noteMax; cnt.classList.toggle('over', st.over);
|
|
add.disabled = st.empty || st.over || (cache.notes && cache.notes.length >= S.maxNotes);
|
|
};
|
|
ta.addEventListener('input', sync);
|
|
add.addEventListener('click', () => X().guard(add, async () => {
|
|
err.hidden = true;
|
|
try {
|
|
const r = await App.api.post('/alliances/' + mine.alliance.id + '/notes', { text: ta.value });
|
|
cache.notes = (cache.notes || []).concat([r.note]);
|
|
ta.value = '';
|
|
drawNotes(cache.notes); sync();
|
|
} catch (e) { err.textContent = X().err(e); err.hidden = false; }
|
|
}));
|
|
notesCard.appendChild(h('div', { class: 'soc-addnote' }, ta, h('div', { class: 'soc-composer-bar' }, cnt, add), err));
|
|
sync();
|
|
}
|
|
|
|
// leave / disband
|
|
const danger = h('div', { class: 'card soc-card' }, h('div', { class: 'card-head' }, h('h2', null, icon('shield'), App.t('soc.manage.title'))));
|
|
const row = h('div', { class: 'btn-row soc-stack' });
|
|
if (A.leaveNeedsTransfer) {
|
|
danger.appendChild(h('p', { class: 'reason', text: App.t('soc.leave.mustTransfer') }));
|
|
row.appendChild(h('button', { class: 'btn', type: 'button', onclick: () => setTab('members') }, icon('users'), App.t('soc.leave.goTransfer')));
|
|
} else {
|
|
row.appendChild(h('button', { class: 'btn danger', type: 'button', onclick: confirmLeave }, icon('logout'), App.t('soc.leave.go')));
|
|
}
|
|
if (A.disband) row.appendChild(h('button', { class: 'btn danger', type: 'button', onclick: confirmDisband }, icon('x'), App.t('soc.disband.go')));
|
|
danger.appendChild(row);
|
|
content.appendChild(danger);
|
|
|
|
if (cache.notes) { drawNotes(cache.notes); return; }
|
|
notesList.appendChild(X().loading());
|
|
try {
|
|
const r = await App.api.get('/alliances/' + mine.alliance.id + '/notes');
|
|
if (my !== gen) return;
|
|
cache.notes = r.items;
|
|
if (tab === 'overview') drawNotes(cache.notes);
|
|
} catch (e) { if (my === gen && tab === 'overview') { clear(notesList); notesList.appendChild(X().errorBox(e, drawTab)); } }
|
|
}
|
|
|
|
function confirmLeave() {
|
|
const alone = members && members.length <= 1;
|
|
X().confirmSheet({
|
|
title: App.t('soc.leave.go'), icon: 'logout', danger: true, ok: App.t('soc.leave.go'),
|
|
text: App.t(alone ? 'soc.leave.confirmAlone' : 'soc.leave.confirm', { name: mine.alliance.name }),
|
|
onOk: async () => { await App.api.post('/alliances/' + mine.alliance.id + '/leave', {}, { intent: 'ally:leave' }); toast(App.t('soc.leave.done'), 'ok'); await reload(); },
|
|
});
|
|
}
|
|
|
|
function confirmDisband() {
|
|
X().confirmSheet({
|
|
title: App.t('soc.disband.go'), icon: 'alert', danger: true, ok: App.t('soc.disband.go'),
|
|
text: App.t('soc.disband.confirm', { name: mine.alliance.name }),
|
|
onOk: async () => { await App.api.post('/alliances/' + mine.alliance.id + '/disband', {}, { intent: 'ally:disband' }); toast(App.t('soc.disband.done'), 'ok'); await reload(); },
|
|
});
|
|
}
|
|
|
|
// ---------------------------------------------------------------- members
|
|
function drawMembers() {
|
|
const A = acts();
|
|
const now = App.now();
|
|
const card = h('div', { class: 'card soc-card' }, h('div', { class: 'card-head' }, h('h2', null, icon('users'), App.t('soc.members')),
|
|
h('span', { class: 'pill dim mono', text: App.t('soc.size', { n: members.length, cap: mine.alliance.sizeCap }) })));
|
|
const list = h('ul', { class: 'soc-list' });
|
|
for (const m of members) {
|
|
const you = m.operatorId === X().myOperatorId();
|
|
list.appendChild(h('li', null, h('button', { class: 'row-btn soc-member', type: 'button', onclick: () => openMember(m) },
|
|
h('span', { class: 'row-main' },
|
|
h('span', { class: 'row-title' }, h('b', { text: m.name }), you ? h('span', { class: 'pill accent', text: App.t('soc.you') }) : null, X().rolePill(m.role)),
|
|
h('span', { class: 'row-sub', text: App.t('soc.member.stats', { towns: m.towns, fp: App.fmt.dec(m.footprint) }) + ' · ' + (m.lastOnline ? App.t('soc.member.online', { t: App.fmt.ago(now - m.lastOnline) }) : App.t('soc.member.never')) })))));
|
|
}
|
|
card.appendChild(list);
|
|
content.appendChild(card);
|
|
|
|
if (A.invite) drawInvite();
|
|
}
|
|
|
|
async function drawInvite() {
|
|
const my = gen;
|
|
const card = h('div', { class: 'card soc-card' }, h('div', { class: 'card-head' }, h('h2', null, icon('mail'), App.t('soc.invite.title'))));
|
|
const err = h('p', { class: 'form-error', role: 'alert', hidden: true });
|
|
const nameIn = h('input', { type: 'text', maxlength: '40', autocomplete: 'off', spellcheck: 'false', 'aria-label': App.t('soc.invite.name'), placeholder: App.t('soc.invite.name'), enterkeyhint: 'send' });
|
|
const go = h('button', { class: 'btn primary', type: 'button' }, icon('up'), App.t('soc.invite.go'));
|
|
const pend = h('ul', { class: 'soc-list' });
|
|
const sync = () => { go.disabled = !Soc.cleanName(nameIn.value); };
|
|
nameIn.addEventListener('input', sync);
|
|
nameIn.addEventListener('keydown', (ev) => { if (ev.key === 'Enter' && !go.disabled) go.click(); });
|
|
go.addEventListener('click', () => X().guard(go, async () => {
|
|
err.hidden = true;
|
|
const name = Soc.cleanName(nameIn.value);
|
|
try {
|
|
const r = await App.api.post('/alliances/' + mine.alliance.id + '/invite', { name }, { intent: 'ally:invite:' + name.toLowerCase() });
|
|
toast(App.t(r.changed ? 'soc.invite.sent' : 'soc.invite.already', { name }), 'ok');
|
|
nameIn.value = ''; sync();
|
|
loadPending();
|
|
} catch (e) { err.textContent = e.code === 'not_found' ? App.t('soc.invite.unknown', { name }) : X().err(e); err.hidden = false; }
|
|
}));
|
|
sync();
|
|
card.appendChild(h('div', { class: 'soc-inline' }, nameIn, go));
|
|
card.appendChild(err);
|
|
if (acts().viewInvites) { card.appendChild(h('div', { class: 'sub', text: App.t('soc.invite.pending') })); card.appendChild(pend); }
|
|
content.appendChild(card);
|
|
|
|
async function loadPending() {
|
|
if (!acts().viewInvites) return;
|
|
try {
|
|
const r = await App.api.get('/alliances/' + mine.alliance.id + '/invites');
|
|
if (my !== gen || tab !== 'members') return;
|
|
clear(pend);
|
|
if (!r.items.length) pend.appendChild(h('li', { class: 'empty', text: App.t('soc.invite.none') }));
|
|
const now = App.now();
|
|
for (const inv of r.items) {
|
|
const cancel = h('button', { class: 'icon-btn', type: 'button', 'aria-label': App.t('soc.invite.cancel'), onclick: (ev) => X().guard(ev.currentTarget, async () => {
|
|
await App.api.del('/alliances/' + mine.alliance.id + '/invites/' + inv.id);
|
|
loadPending();
|
|
}) }, icon('x'));
|
|
pend.appendChild(h('li', { class: 'soc-item' }, h('div', { class: 'row-main' }, h('b', { text: inv.operator.name }),
|
|
h('span', { class: 'row-sub', text: App.t('soc.invites.from', { name: inv.invitedBy.name }) + ' · ' + App.t('soc.invites.expires', { t: App.fmt.dur(Math.max(0, inv.expiresAt - now), 2) }) })),
|
|
acts().cancelInvite ? cancel : null));
|
|
}
|
|
} catch (e) { if (my === gen && tab === 'members') { clear(pend); pend.appendChild(X().errorBox(e, loadPending)); } }
|
|
}
|
|
loadPending();
|
|
}
|
|
|
|
// Member sheet: message, role (leader), transfer (leader), kick (outranking).
|
|
function openMember(m) {
|
|
const act = Soc.memberActions(myRole(), m, X().myOperatorId());
|
|
const err = h('p', { class: 'form-error', role: 'alert', hidden: true });
|
|
const bodyEl = h('div', { class: 'detail' });
|
|
const sheet = openSheet({ title: m.name, icon: 'users', closeLabel: App.t('common.close'), body: bodyEl });
|
|
const after = async () => { sheet.close(); await load(); };
|
|
const run = (btn, fn) => X().guard(btn, async () => { err.hidden = true; try { await fn(); } catch (e) { err.textContent = X().err(e); err.hidden = false; } });
|
|
|
|
bodyEl.appendChild(h('div', { class: 'pill-row' }, X().rolePill(m.role), h('span', { class: 'pill mono', text: 'AS' + m.asNumber })));
|
|
bodyEl.appendChild(h('div', null,
|
|
kv(App.t('soc.member.pops'), String(m.towns)), kv(App.t('soc.footprint'), App.fmt.dec(m.footprint)),
|
|
kv(App.t('soc.member.lastOnline'), m.lastOnline ? App.fmt.ago(App.now() - m.lastOnline) : App.t('soc.member.never')),
|
|
kv(App.t('soc.member.joined'), App.fmt.ago(App.now() - m.joinedAt))));
|
|
if (act.message) bodyEl.appendChild(h('button', { class: 'btn wide', type: 'button', onclick: () => { sheet.close(); X().openCompose({ operator: { operatorId: m.operatorId, name: m.name } }); } }, icon('mail'), App.t('soc.member.message')));
|
|
if (act.roles.length) {
|
|
bodyEl.appendChild(h('div', { class: 'sub', text: App.t('soc.member.setRole') }));
|
|
bodyEl.appendChild(h('div', { class: 'seg' }, act.roles.map((r) => h('button', { class: 'seg-btn', type: 'button', text: X().role(r), onclick: (ev) => run(ev.currentTarget, async () => {
|
|
await App.api.post('/alliances/' + mine.alliance.id + '/role', { operatorId: m.operatorId, role: r }, { intent: 'ally:role:' + m.operatorId + ':' + r });
|
|
toast(App.t('soc.member.roleSet', { name: m.name, role: X().role(r) }), 'ok');
|
|
await after();
|
|
}) }))));
|
|
bodyEl.appendChild(h('p', { class: 'field-hint', text: App.t('soc.member.roleHint') }));
|
|
}
|
|
const row = h('div', { class: 'btn-row soc-stack' });
|
|
if (act.transfer) row.appendChild(h('button', { class: 'btn', type: 'button', onclick: () => {
|
|
sheet.close();
|
|
X().confirmSheet({ title: App.t('soc.transfer.go'), icon: 'shield', ok: App.t('soc.transfer.go'), text: App.t('soc.transfer.confirm', { name: m.name }),
|
|
onOk: async () => { await App.api.post('/alliances/' + mine.alliance.id + '/transfer', { operatorId: m.operatorId }, { intent: 'ally:transfer:' + m.operatorId }); toast(App.t('soc.transfer.done', { name: m.name }), 'ok'); await load(); } });
|
|
} }, icon('shield'), App.t('soc.transfer.go')));
|
|
if (act.kick) row.appendChild(h('button', { class: 'btn danger', type: 'button', onclick: () => {
|
|
sheet.close();
|
|
X().confirmSheet({ title: App.t('soc.kick.go'), icon: 'alert', danger: true, ok: App.t('soc.kick.go'), text: App.t('soc.kick.confirm', { name: m.name }),
|
|
onOk: async () => { await App.api.post('/alliances/' + mine.alliance.id + '/kick', { operatorId: m.operatorId }, { intent: 'ally:kick:' + m.operatorId }); toast(App.t('soc.kick.done', { name: m.name }), 'ok'); await load(); } });
|
|
} }, icon('x'), App.t('soc.kick.go')));
|
|
if (row.children.length) bodyEl.appendChild(row);
|
|
bodyEl.appendChild(err);
|
|
}
|
|
|
|
// ---------------------------------------------------------------- diplomacy
|
|
async function drawDiplomacy() {
|
|
const my = gen;
|
|
const A = acts();
|
|
const card = h('div', { class: 'card soc-card' }, h('div', { class: 'card-head' }, h('h2', null, icon('shield'), App.t('soc.dip.title')),
|
|
A.diplomacy ? h('button', { class: 'btn small primary', type: 'button', onclick: openPicker }, icon('plus'), App.t('soc.dip.new')) : null));
|
|
const holder = h('div', { class: 'soc-dip' }, X().loading());
|
|
card.appendChild(holder);
|
|
if (!A.diplomacy) card.appendChild(h('p', { class: 'field-hint', text: App.t('soc.dip.readOnly') }));
|
|
content.appendChild(card);
|
|
|
|
const draw = (d) => {
|
|
cache.dip = d;
|
|
clear(holder);
|
|
stopTick();
|
|
if (d.proposalsIn.length) holder.appendChild(proposalBlock('soc.dip.incoming', d.proposalsIn, 'in'));
|
|
holder.appendChild(h('div', { class: 'sub', text: App.t('soc.dip.relations') }));
|
|
if (!d.relations.length) holder.appendChild(h('p', { class: 'empty', text: App.t('soc.dip.none') }));
|
|
const list = h('ul', { class: 'soc-list' });
|
|
for (const r of d.relations) list.appendChild(relationRow(r));
|
|
holder.appendChild(list);
|
|
if (d.proposalsOut.length) holder.appendChild(proposalBlock('soc.dip.outgoing', d.proposalsOut, 'out'));
|
|
const cds = holder.querySelectorAll('[data-eff]');
|
|
if (cds.length) {
|
|
offTick = App.onTick((now) => {
|
|
for (const el of cds) {
|
|
if (!el.isConnected) continue;
|
|
const left = Math.max(0, Number(el.dataset.eff) - now);
|
|
const s = left > 0 ? App.t('soc.dip.in', { t: App.fmt.dur(left, 2) }) : App.t('soc.dip.applying');
|
|
if (el.textContent !== s) el.textContent = s;
|
|
}
|
|
});
|
|
}
|
|
};
|
|
|
|
const refetch = async () => {
|
|
try {
|
|
const d = await App.api.get('/alliances/' + mine.alliance.id + '/diplomacy');
|
|
if (my !== gen || tab !== 'diplomacy') return;
|
|
draw(d);
|
|
} catch (e) { if (my === gen && tab === 'diplomacy') { clear(holder); holder.appendChild(X().errorBox(e, drawTab)); } }
|
|
};
|
|
refreshDip = refetch;
|
|
if (cache.dip) draw(cache.dip);
|
|
refetch();
|
|
}
|
|
let refreshDip = () => {};
|
|
|
|
function allianceLabel(other) {
|
|
return h('span', { class: 'row-title' }, other.tag ? X().tagPill(other.tag) : null, h('b', { text: other.name || '#' + other.id }));
|
|
}
|
|
|
|
function relationRow(r) {
|
|
const A = acts();
|
|
const now = App.now();
|
|
const p = r.pending;
|
|
const eta = p ? h('span', { class: 'soc-eta mono', dataset: { eff: p.effectiveAt }, text: App.t('soc.dip.in', { t: App.fmt.dur(Math.max(0, p.effectiveAt - now), 2) }) }) : null;
|
|
const revoke = p && p.byUs && A.diplomacy
|
|
? h('button', { class: 'btn small', type: 'button', text: App.t('soc.dip.revoke'), onclick: (ev) => X().guard(ev.currentTarget, async () => {
|
|
await App.api.del('/alliances/' + mine.alliance.id + '/diplomacy/' + r.other.id + '/pending');
|
|
toast(App.t('soc.dip.revoked'), 'ok'); refreshDip();
|
|
}) }) : null;
|
|
return h('li', { class: 'soc-item soc-rel' },
|
|
h('div', { class: 'row-main' }, allianceLabel(r.other),
|
|
h('span', { class: 'row-sub' }, h('span', { class: 'pill soc-state soc-state-' + r.state, text: X().state(r.state) }),
|
|
p ? h('span', { class: 'soc-pending' }, ' → ', h('b', { text: X().state(p.to) }), ' · ', eta, ' · ', p.byUs ? App.t('soc.dip.byUs') : App.t('soc.dip.byThem')) : null)),
|
|
revoke);
|
|
}
|
|
|
|
function proposalBlock(titleKey, items, dir) {
|
|
const A = acts();
|
|
const wrap = h('div', null, h('div', { class: 'sub', text: App.t(titleKey) }));
|
|
const list = h('ul', { class: 'soc-list' });
|
|
const now = App.now();
|
|
for (const pr of items) {
|
|
const mk = (label, cls, path, method) => h('button', { class: 'btn small ' + cls, type: 'button', text: label, onclick: (ev) => X().guard(ev.currentTarget, async () => {
|
|
try {
|
|
if (method === 'del') await App.api.del(path); else await App.api.post(path, {}, { intent: 'dip:' + path });
|
|
refreshDip();
|
|
} catch (e) { toast(X().err(e), 'error'); refreshDip(); }
|
|
}) });
|
|
const btns = !A.diplomacy ? [] : dir === 'in'
|
|
? [mk(App.t('soc.dip.decline'), '', '/diplomacy/proposals/' + pr.id + '/decline'), mk(App.t('soc.dip.accept'), 'primary', '/diplomacy/proposals/' + pr.id + '/accept')]
|
|
: [mk(App.t('soc.dip.withdraw'), '', '/diplomacy/proposals/' + pr.id, 'del')];
|
|
list.appendChild(h('li', { class: 'soc-item soc-rel' },
|
|
h('div', { class: 'row-main' }, allianceLabel(pr.other),
|
|
h('span', { class: 'row-sub' }, h('span', { class: 'pill soc-state soc-state-' + pr.state, text: X().state(pr.state) }), h('span', { text: App.t('soc.dip.expires', { t: App.fmt.dur(Math.max(0, pr.expiresAt - now), 2) }) }))),
|
|
h('div', { class: 'soc-actions' }, btns)));
|
|
}
|
|
wrap.appendChild(list);
|
|
return wrap;
|
|
}
|
|
|
|
// Pick another alliance (directory search), then the move.
|
|
function openPicker() {
|
|
const results = h('ul', { class: 'soc-list' });
|
|
const q = h('input', { type: 'text', maxlength: '40', autocomplete: 'off', spellcheck: 'false', placeholder: App.t('soc.dip.search'), 'aria-label': App.t('soc.dip.search'), enterkeyhint: 'search' });
|
|
const more = h('button', { class: 'btn small wide', type: 'button', hidden: true, text: App.t('soc.more') });
|
|
let items = [], next = null, seq = 0, timer = null;
|
|
const sheet = openSheet({ title: App.t('soc.dip.pick'), icon: 'shield', closeLabel: App.t('common.close'), onClose: () => clearTimeout(timer),
|
|
body: h('div', { class: 'detail' }, q, results, more) });
|
|
|
|
function drawResults() {
|
|
clear(results);
|
|
const rows = items.filter((x) => x.id !== mine.alliance.id);
|
|
if (!rows.length) results.appendChild(h('li', { class: 'empty', text: App.t('soc.dip.noResults') }));
|
|
for (const a of rows) {
|
|
results.appendChild(h('li', null, h('button', { class: 'row-btn', type: 'button', onclick: () => { sheet.close(); openMove(a); } },
|
|
h('span', { class: 'row-main' }, allianceLabel(a), h('span', { class: 'row-sub', text: App.t('soc.size', { n: a.memberCount, cap: a.sizeCap }) })))));
|
|
}
|
|
more.hidden = next === null;
|
|
}
|
|
async function search(append) {
|
|
const my = ++seq;
|
|
try {
|
|
const r = await App.api.get('/alliances', { q: q.value.trim() || undefined, after: append ? next : undefined, limit: 30 });
|
|
if (my !== seq) return;
|
|
items = append ? items.concat(r.items) : r.items; next = r.next;
|
|
drawResults();
|
|
} catch (e) { if (my === seq) { clear(results); results.appendChild(X().errorBox(e, () => search(false))); } }
|
|
}
|
|
q.addEventListener('input', () => { clearTimeout(timer); timer = setTimeout(() => search(false), 250); });
|
|
more.addEventListener('click', () => search(true));
|
|
search(false);
|
|
}
|
|
|
|
function openMove(other) {
|
|
const d = cache.dip || { relations: [] };
|
|
const cur = Soc.relationTo(d.relations, other.id);
|
|
const opts = Soc.diplomacyOptions(cur.state, cur.pending);
|
|
const err = h('p', { class: 'form-error', role: 'alert', hidden: true });
|
|
const list = h('div', { class: 'soc-moves' });
|
|
const sheet = openSheet({ title: other.name, icon: 'shield', closeLabel: App.t('common.close'),
|
|
body: h('div', { class: 'detail' },
|
|
h('div', { class: 'pill-row' }, X().tagPill(other.tag), h('span', { class: 'pill soc-state soc-state-' + cur.state, text: App.t('soc.dip.current', { state: X().state(cur.state) }) })),
|
|
cur.pending ? h('p', { class: 'reason', text: App.t('soc.dip.locked') }) : null,
|
|
!opts.length && !cur.pending ? h('p', { class: 'empty', text: App.t('soc.dip.noMoves') }) : null,
|
|
list, err) });
|
|
for (const o of opts) {
|
|
const hint = o.kind === 'delayed' ? App.t('soc.kind.delayed.hint', { t: App.fmt.dur(R.pactCancelMs(App.state.world ? App.state.world.speed : 1), 2) }) : App.t('soc.kind.' + o.kind + '.hint');
|
|
list.appendChild(h('button', { class: 'row-btn soc-move', type: 'button', onclick: (ev) => X().guard(ev.currentTarget, async () => {
|
|
err.hidden = true;
|
|
try {
|
|
const r = await App.api.post('/alliances/' + mine.alliance.id + '/diplomacy', { targetId: other.id, state: o.state }, { intent: 'dip:' + other.id + ':' + o.state });
|
|
toast(App.t('soc.dip.result.' + r.result, { state: X().state(r.state), name: other.name }), 'ok');
|
|
sheet.close();
|
|
cache.dip = null;
|
|
if (tab === 'diplomacy') drawTab();
|
|
} catch (e) { err.textContent = X().err(e); err.hidden = false; }
|
|
}) },
|
|
h('span', { class: 'row-main' }, h('span', { class: 'row-title' }, h('span', { class: 'pill soc-state soc-state-' + o.state, text: X().state(o.state) }), h('b', { text: App.t('soc.kind.' + o.kind) })), h('span', { class: 'row-sub soc-wrap', text: hint }))));
|
|
}
|
|
}
|
|
|
|
function stopTick() { if (offTick) { offTick(); offTick = null; } }
|
|
|
|
App.registerScreen({
|
|
id: 'alliance', titleKey: 'screen.alliance', navKey: 'nav.alliance', icon: 'users', hud: false, navGroup: 'more',
|
|
render(container) {
|
|
box = container;
|
|
body = h('div', { class: 'soc-body' });
|
|
box.appendChild(h('div', { class: 'page soc-page' },
|
|
h('div', { class: 'card-head page-head' }, h('h1', { id: 'soc-alliance-h', text: App.t('screen.alliance') }),
|
|
h('button', { class: 'icon-btn', type: 'button', 'aria-label': App.t('common.retry'), onclick: () => load() }, icon('refresh'))),
|
|
body));
|
|
App.on('session', () => { mine = null; members = null; tab = 'overview'; for (const k of Object.keys(cache)) delete cache[k]; });
|
|
},
|
|
onShow() { box.querySelector('#soc-alliance-h').textContent = App.t('screen.alliance'); load(); },
|
|
onHide() { stopTick(); gen++; },
|
|
});
|
|
}());
|