Phase 3: trade/end-game client, bot simulator, first balance pass

Built:
- Client: trade screen, world/end-game screen (landing sites, shipping, node build, final ranking), report sharing + alliance reports tab, alert describers for all notification types, error-code strings (en+nb) for every server CODE.
- tools/bot.js (+ tools/bot/): 50 headless bots against the real server in-process with a fake clock; resource/unit conservation ledger, exit 1 on broken invariants. docs/BALANCE.md has method and every run behind a number.
- Balance changes (evidence in docs/BALANCE.md): secondQueueCoreLevel 10 -> 3, TRANSPORT.capacityPerLink 500 -> 1000 (end game was shipping-limited in bot runs).
- Server fixes: POST /reports/:id/share 500 without a pending reportCleanup; bounced link shipments were silent about cargo overflow.

Tested: npm test 207/207 (new: bot determinism + invariants, client alerts/world/i18n over CODES, two regression tests that failed before their fix); openapi check up to date. Real-browser scratch runs (not in repo): WebSocket push shows an incoming-attack banner ~214 ms after launch, polling fallback ~52 s, 15 scripted flows, 71 screenshots.

NOT tested: Playwright suite (e2e/ still empty), real devices/Safari, nginx proxy websockets, throttled networks, HTTP-level bot load. NOT claimed: balance. Bots are not humans; open balance items (loot waste, escrow refund clipping, no garrisons at landing sites, end game not scaling with world speed) are listed in docs/BALANCE.md.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
This commit is contained in:
jvanvik 2026-10-11 20:24:41 +02:00
parent d84b0c9d12
commit 931f59e11e
49 changed files with 4750 additions and 59 deletions

View file

@ -1,9 +1,11 @@
'use strict';
/*
* Reports: newest first, cursor-paginated (GET /reports?after=<last id>), unread highlighted, a detail sheet per report
* (GET /reports/:id, then POST /reports/:id/read). Detail layouts exist for battle (raid/attack/takeover), scan,
* scan_detected, incumbent, settle, reinforce and movement_failed; coordinates are links that jump to the map.
* `data` differs by report type AND by the recipient's role (attacker / defender / reinforcer / sender / target).
* (GET /reports/:id, then POST /reports/:id/read). Detail layouts exist for battle (raid/attack/takeover), landing (an attack on a backbone
* landing site), scan (also of a landing site), scan_detected, incumbent, settle, reinforce and movement_failed; coordinates are links that
* jump to the map. `data` differs by report type AND by the recipient's role (attacker / defender / reinforcer / sender / target).
* Alliance folder: a report can be shared with your alliance (POST /reports/:id/share) and the "Alliance" tab lists the folder
* (GET /alliances/:id/reports). Shared reports have no recipient role and no read flag: they are told from nobody's side ("attacker won").
*/
(function () {
const { h, clear, icon, toast, openSheet } = UI;
@ -14,6 +16,11 @@
const PAGE = 25;
let box, listEl, moreBtn, statusEl, items = [], next = null, loading = false, visible = false, offAgo = null;
let tabsEl, markBtn, mode = 'mine'; // 'mine' | 'alliance' (the shared folder)
const folder = { items: [], next: null, loaded: false };
const allianceId = () => (App.state.operator ? App.state.operator.allianceId : null);
const curItems = () => (mode === 'alliance' ? folder.items : items);
const curNext = () => (mode === 'alliance' ? folder.next : next);
// ------------------------------------------------------------------ list rows
const nameOf = (o) => (o && o.name) || '?';
@ -22,16 +29,26 @@
// { icon, title, sub, tone } for the list
function describe(it) {
const s = it.summary || {};
const sharedItem = !it.role; // alliance folder: nobody's side
switch (it.type) {
case 'raid': case 'attack': case 'takeover': {
if (sharedItem) {
return { icon: Mil.TYPE_ICON[it.type], title: mil().reportTypeLabel(it.type) + ': ' + townOf(s.target), sub: App.t('rep.vs', { a: nameOf(s.attacker), d: nameOf(s.defender) }) + (s.captured ? ' · ' + App.t('rep.captured') : ''), tone: 'neutral', badge: App.t(s.outcome === 'attacker_won' ? 'rep.attackerWon' : 'rep.defenderWon') };
}
const res = Mil.perspective(it.role, s.outcome);
const mine = it.role === 'attacker';
const title = mil().reportTypeLabel(it.type) + ': ' + townOf(s.target);
const sub = mine ? App.t('rep.sub.you_vs', { name: nameOf(s.defender) }) : App.t('rep.sub.by', { name: nameOf(s.attacker) });
return { icon: Mil.TYPE_ICON[it.type], title, sub: sub + (s.captured ? ' · ' + App.t('rep.captured') : ''), tone: res === 'victory' ? 'good' : 'bad', badge: App.t('rep.' + res) };
}
case 'landing': {
const site = s.site || {};
const taken = !!s.taken;
const res = sharedItem ? null : Mil.perspective(it.role, s.outcome);
return { icon: 'b-backbone_node', title: App.t('rep.landing.title', { n: Number.isInteger(site.index) ? site.index + 1 : '?' }), sub: sharedItem ? App.t('rep.by', { name: nameOf(s.attacker) }) + ' · ' + (taken ? App.t('rep.landing.taken') : App.t('rep.landing.held')) : (taken ? App.t('rep.landing.taken') : App.t('rep.landing.held')), tone: res === null ? 'neutral' : (res === 'victory' ? 'good' : 'bad'), badge: res === null ? null : App.t('rep.' + res) };
}
case 'scan': {
const where = s.target ? townOf(s.target) : (s.site ? s.site.x + ', ' + s.site.y : '?');
const where = s.target ? townOf(s.target) : (s.site ? (s.site.type === 'backbone' ? App.t('wld.site', { n: s.site.index + 1 }) : s.site.x + ', ' + s.site.y) : '?');
return { icon: 'eye', title: App.t('rep.scan.title', { where }), sub: s.success ? App.t('rep.scan.ok') : App.t('rep.scan.failed'), tone: s.success ? 'good' : 'bad' };
}
case 'scan_detected':
@ -58,23 +75,49 @@
function row(it) {
const d = describe(it);
return h('li', null, h('button', { class: 'alert rep tone-' + d.tone + (it.read ? '' : ' new'), type: 'button', dataset: { id: String(it.id) }, onclick: () => openReport(it.id) },
const unread = it.read === false; // shared items have no read flag
const shared = mode === 'alliance';
return h('li', null, h('button', { class: 'alert rep tone-' + d.tone + (unread ? ' new' : ''), type: 'button', dataset: { id: String(it.id) }, onclick: () => openReport(it.id, shared) },
h('span', { class: 'row-ic' }, icon(d.icon)),
h('span', { class: 'row-main' },
h('span', { class: 'row-title' }, h('b', { text: d.title }), d.badge ? h('span', { class: 'pill ' + (d.tone === 'good' ? 'green' : 'amber'), text: d.badge }) : null, it.read ? null : h('span', { class: 'pill accent', text: App.t('alerts.new') })),
d.sub ? h('span', { class: 'row-sub', text: d.sub }) : null),
h('span', { class: 'row-title' }, h('b', { text: d.title }), d.badge ? h('span', { class: 'pill ' + (d.tone === 'good' ? 'green' : 'amber'), text: d.badge }) : null, unread ? h('span', { class: 'pill accent', text: App.t('alerts.new') }) : null),
d.sub ? h('span', { class: 'row-sub', text: d.sub }) : null,
shared && it.sharedBy ? h('span', { class: 'row-sub', text: App.t('rep.sharedBy', { name: it.sharedBy.name }) }) : null),
h('span', { class: 'when mono', dataset: { at: String(it.createdAt) }, text: App.fmt.ago(App.now() - it.createdAt) })));
}
function draw() {
clear(listEl);
if (!items.length) { listEl.appendChild(h('li', null, h('p', { class: 'empty pad', text: loading ? App.t('common.loading') : App.t('rep.empty') }))); }
for (const it of items) listEl.appendChild(row(it));
moreBtn.hidden = next === null;
const list = curItems();
if (mode === 'alliance' && !allianceId()) { listEl.appendChild(h('li', null, h('p', { class: 'empty pad', text: App.t('rep.folder.none') }))); moreBtn.hidden = true; return; }
if (!list.length) { listEl.appendChild(h('li', null, h('p', { class: 'empty pad', text: loading || (mode === 'alliance' && !folder.loaded) ? App.t('common.loading') : App.t(mode === 'alliance' ? 'rep.folder.empty' : 'rep.empty') }))); }
for (const it of list) listEl.appendChild(row(it));
moreBtn.hidden = curNext() === null;
if (markBtn) markBtn.hidden = mode === 'alliance';
drawTabs();
}
function drawTabs() {
if (!tabsEl) return;
tabsEl.hidden = !allianceId();
clear(tabsEl);
for (const k of ['mine', 'alliance']) tabsEl.appendChild(h('button', { class: 'seg-btn', type: 'button', dataset: { tab: k }, 'aria-pressed': String(mode === k), onclick: () => { mode = k; draw(); load(false); } }, App.t(k === 'mine' ? 'rep.tab.mine' : 'rep.tab.alliance')));
}
async function load(more) {
if (loading) return;
if (mode === 'alliance') {
if (!allianceId()) { draw(); return; }
loading = true; statusEl.hidden = true;
try {
const r = await App.api.get('/alliances/' + allianceId() + '/reports', { limit: PAGE, after: more ? folder.next : undefined });
folder.items = more ? folder.items.concat(r.items.filter((x) => !folder.items.some((y) => y.id === x.id))) : r.items;
folder.next = r.next; folder.loaded = true;
} catch (e) { statusEl.textContent = e.code === 'forbidden' ? App.t('rep.folder.forbidden') : App.errMsg(e); statusEl.hidden = false; folder.loaded = true; }
finally { loading = false; }
draw();
return;
}
loading = true; statusEl.hidden = true;
try {
const r = await App.api.get('/reports', { limit: PAGE, after: more ? next : undefined });
@ -118,6 +161,7 @@
town ? mil().endLink(town) : null));
function verdict(role, outcome) {
if (!role) return h('div', { class: 'rep-verdict neutral' }, icon('report'), h('b', { text: App.t(outcome === 'attacker_won' ? 'rep.attackerWon' : 'rep.defenderWon') })); // shared: nobody's side
const res = Mil.perspective(role, outcome);
return h('div', { class: 'rep-verdict ' + res }, icon(res === 'victory' ? 'check' : 'alert'), h('b', { text: App.t('rep.' + res) }));
}
@ -165,6 +209,14 @@
out.push(sec(App.t('rep.scanners'), unitsTable(d.scanners && d.scanners.sent, d.scanners && d.scanners.lost, d.scanners && d.scanners.survivors)));
const intel = d.intel;
if (!intel) { out.push(h('p', { class: 'reason', text: App.t('rep.scan.noIntel') })); return out; }
if (d.site && d.site.type === 'backbone') { // end game landing site: the static defence is the number to beat
out.push(sec(App.t('rep.intel'),
kv(App.t('rep.power.defence'), h('b', { class: 'mono', text: num(intel.defence) })),
kv(App.t('wld.levelShort'), h('b', { class: 'mono', text: App.t('wld.level', { n: intel.level || 0, max: D.ENDGAME.maxLevel }) })),
kv(App.t('rep.landing.holder'), h('span', { text: intel.holder ? '[' + intel.holder.tag + '] ' + intel.holder.name : App.t('wld.incumbent') })),
intel.build ? kv(App.t('wld.building', { n: intel.build.toLevel }), h('span', { text: mil().dayTime(intel.build.dueAt) })) : null));
return out;
}
if (d.site) { // incumbent site
out.push(sec(App.t('rep.intel'),
kv(App.t('rep.power.defence'), h('b', { class: 'mono', text: num(intel.defence) })),
@ -198,7 +250,7 @@
function incumbentBody(r) {
const d = r.data, out = [];
out.push(verdict('attacker', d.outcome));
out.push(verdict(r.role ? 'attacker' : null, d.outcome));
out.push(kv(App.t('rep.site'), mil().coordLink(d.site.x, d.site.y)));
if (d.origin) out.push(kv(App.t('rep.from'), mil().endLink(d.origin)));
out.push(sec(App.t('rep.power'), kv(App.t('rep.power.attack'), h('b', { class: 'mono', text: num(d.power.attack) })), kv(App.t('rep.power.defence'), h('b', { class: 'mono', text: num(d.power.defence) }))));
@ -207,12 +259,30 @@
return out;
}
function landingBody(r) {
const d = r.data, out = [];
const site = d.site || {};
out.push(verdict(r.role, d.outcome));
out.push(h('p', { class: 'row-sub', text: App.t('rep.arrived', { t: mil().dayTime(d.arrivedAt) }) }));
out.push(kv(App.t('rep.site'), h('span', { class: 'rep-party' }, h('b', { text: Number.isInteger(site.index) ? App.t('wld.site', { n: site.index + 1 }) : '?' }), mil().coordLink(site.x, site.y))));
out.push(kv(App.t('rep.landing.holder'), h('span', { text: site.holder ? '[' + site.holder.tag + '] ' + site.holder.name : App.t('wld.incumbent') })));
out.push(kv(App.t('wld.levelShort'), h('b', { class: 'mono', text: App.t('wld.level', { n: site.level || 0, max: D.ENDGAME.maxLevel }) })));
out.push(d.taken ? h('p', { class: 'good', text: App.t('rep.landing.takenLine', { tag: d.attacker && d.attacker.alliance ? '[' + d.attacker.alliance.tag + '] ' + d.attacker.alliance.name : '?' }) }) : h('p', { class: 'row-sub wrap', text: App.t('rep.landing.heldLine') }));
out.push(party(App.t('rep.attacker'), d.attacker.operator, d.origin));
const p = d.power || {};
out.push(sec(App.t('rep.power'), kv(App.t('rep.power.attack'), h('b', { class: 'mono', text: num(p.attack || 0) })), kv(App.t('rep.power.defence'), h('b', { class: 'mono', text: num(p.defence || 0) }))));
out.push(sec(App.t('rep.attackerUnits'), unitsTable(d.attacker.units.sent, d.attacker.units.lost, d.attacker.units.survivors)));
out.push(h('button', { class: 'btn wide', type: 'button', 'data-keep': '', onclick: () => { UI.closeSheet(); App.go('world', Number.isInteger(site.index) ? { site: site.index } : null); } }, icon('b-backbone_node'), App.t('map.info.openWorld')));
return out;
}
function settleBody(r, ctl) {
const d = r.data, out = [];
out.push(h('div', { class: 'rep-verdict victory' }, icon('flag'), h('b', { text: App.t('rep.settle.title', { name: d.town.name }) })));
out.push(kv(App.t('rep.site'), mil().coordLink(d.town.x, d.town.y)));
out.push(kv(App.t('rep.settle.wild'), h('span', { text: (d.wild || []).map((w) => App.buildingName(w)).join(', ') })));
out.push(h('button', { class: 'btn primary wide', type: 'button', onclick: async () => {
if (r.shared) return out;
out.push(h('button', { class: 'btn primary wide', type: 'button', 'data-keep': '', onclick: async () => {
ctl.close();
try { await App.refreshTowns(); } catch (e) { /* the POP list may be stale; setTown below copes */ }
App.setTown(d.town.id); App.go('pop');
@ -245,6 +315,7 @@
function bodyFor(r, ctl) {
switch (r.type) {
case 'raid': case 'attack': case 'takeover': return combatBody(r);
case 'landing': return landingBody(r);
case 'scan': return scanBody(r);
case 'scan_detected': return detectedBody(r);
case 'incumbent': return incumbentBody(r);
@ -256,19 +327,53 @@
}
// ------------------------------------------------------------------ detail sheet
async function openReport(id) {
function shareRow(r) {
const msg = h('p', { class: 'form-error', role: 'alert', hidden: true });
const btn = h('button', { class: 'btn', type: 'button', 'data-act': '' }, icon('users'), App.t('rep.share'));
btn.addEventListener('click', async () => {
btn.disabled = true; msg.hidden = true;
try {
const res = await App.api.post('/reports/' + r.id + '/share', {}, { intent: 'report-share:' + r.id });
toast(App.t(res.changed === false ? 'rep.share.already' : 'rep.share.done'), 'ok');
folder.loaded = false;
} catch (e) {
msg.textContent = e.code === 'forbidden' ? App.t('rep.share.forbidden') : App.errMsg(e); msg.hidden = false; btn.disabled = false;
}
});
return h('div', { class: 'rep-share' }, h('p', { class: 'row-sub wrap', text: App.t('rep.share.hint') }), btn, msg);
}
function unshareRow(r, ctl) {
const msg = h('p', { class: 'form-error', role: 'alert', hidden: true });
const btn = h('button', { class: 'btn danger', type: 'button' }, icon('x'), App.t('rep.unshare'));
btn.addEventListener('click', async () => {
btn.disabled = true; msg.hidden = true;
try {
await App.api.del('/reports/' + r.id + '/share');
toast(App.t('rep.unshare.done'), 'ok');
folder.items = folder.items.filter((x) => x.id !== r.id);
ctl.close(); if (visible) draw();
} catch (e) { msg.textContent = e.code === 'forbidden' ? App.t('rep.unshare.forbidden') : App.errMsg(e); msg.hidden = false; btn.disabled = false; }
});
return h('div', { class: 'rep-share' }, btn, msg);
}
async function openReport(id, shared) {
const body = h('div', { class: 'detail rep-detail' }, h('div', { class: 'boot' }, h('div', { class: 'spinner', 'aria-hidden': 'true' })));
const ctl = openSheet({ title: App.t('screen.reports'), icon: 'report', closeLabel: App.t('common.close'), body });
try {
const res = await App.api.get('/reports/' + id);
const res = await App.api.get(shared ? '/alliances/' + allianceId() + '/reports/' + id : '/reports/' + id);
const r = res.report;
r.shared = !!shared;
ctl.setTitle(mil().reportTypeLabel(r.type));
clear(body);
body.appendChild(h('p', { class: 'row-sub', text: mil().dayTime(r.createdAt) + ' · ' + App.t('rep.role.' + r.role) + ' · ' + App.t('rep.expires', { t: mil().dayTime(r.expiresAt) }) }));
body.appendChild(h('p', { class: 'row-sub', text: mil().dayTime(r.createdAt) + ' · ' + (shared ? App.t('rep.sharedBy', { name: r.sharedBy ? r.sharedBy.name : '?' }) : App.t('rep.role.' + r.role)) + ' · ' + App.t('rep.expires', { t: mil().dayTime(r.expiresAt) }) }));
let parts;
try { parts = bodyFor(r, ctl); } catch (e) { parts = genericBody(r); }
for (const p of parts.flat(2)) if (p) body.appendChild(p);
if (!r.read) {
if (shared) body.appendChild(unshareRow(r, ctl));
else if (allianceId()) body.appendChild(shareRow(r));
if (!shared && !r.read) {
App.api.post('/reports/' + id + '/read', {}).then(() => {
const it = items.find((x) => x.id === id); if (it) it.read = true;
if (visible) draw();
@ -289,22 +394,26 @@
listEl = h('ul', { class: 'alert-list' });
statusEl = h('p', { class: 'form-error', role: 'alert', hidden: true });
moreBtn = h('button', { class: 'btn wide', type: 'button', hidden: true, onclick: () => load(true) }, App.t('rep.older'));
tabsEl = h('div', { class: 'seg rep-tabs', role: 'group', hidden: true });
markBtn = h('button', { class: 'btn small', type: 'button', onclick: markAll }, icon('check'), App.t('rep.markAll'));
box.appendChild(h('div', { class: 'page' },
h('div', { class: 'card-head page-head' }, h('h1', { text: App.t('screen.reports') }),
h('div', { class: 'head-actions' },
h('button', { class: 'btn small', type: 'button', onclick: markAll }, icon('check'), App.t('rep.markAll')),
h('div', { class: 'head-actions' }, markBtn,
h('button', { class: 'icon-btn', type: 'button', 'aria-label': App.t('common.retry'), onclick: () => load(false) }, icon('refresh')))),
statusEl, listEl, moreBtn));
App.on('session', () => { items = []; next = null; });
tabsEl, statusEl, listEl, moreBtn));
App.on('session', () => { items = []; next = null; folder.items = []; folder.next = null; folder.loaded = false; mode = 'mine'; });
App.on('report', () => { if (visible) load(false); });
},
onShow(params) {
visible = true;
box.querySelector('h1').textContent = App.t('screen.reports');
moreBtn.textContent = App.t('rep.older');
markBtn.lastChild.textContent = App.t('rep.markAll');
if (params && params.tab === 'alliance' && allianceId()) mode = 'alliance';
if (mode === 'alliance' && !allianceId()) mode = 'mine';
draw();
load(false);
if (params && params.open) openReport(Number(params.open));
if (params && params.open) openReport(Number(params.open), false);
if (offAgo) offAgo();
offAgo = App.onTick(() => {
const now = App.now();