Upstream/public/js/alertkinds.js
jvanvik 3c23c51902 Phase 4: close loot/refund leaks, landing-site garrisons, speed-scaled shipping, update/offsite tooling, broad e2e
Built:
- Economy: raid loot capped to the room the raiders have at home (rest stays with the victim, "lootLeft" in the report); trade escrow reserves its room so refunds always fit; trade accept refuses with no_room_for_goods when the goods would not fit. Loot destroyed per 30-day run 31-61k -> ~1k, refunds clipped ~27k -> 0 (docs/BALANCE.md, 3 seeds).
- End game: members of the holding alliance garrison landing sites (migration 005_site_garrison.sql), garrison counts in site defence, 72 h capture shield, withdraw route, POP view lists garrisoned units. Shipping capacity scales with world speed (3x world: winner 76-98 -> ~30-36 days after sites open). Bot runs show real contests (84-194 attacks on held sites at 1x).
- Ops: deploy/update.sh (backup, stop, ff-merge, npm ci --ignore-scripts, health check, rollback), deploy/upstream-offsite (rsync only), admin end-world snapshots results, EPIPE fix, docs/workflows/ filled in.
- e2e 62 -> 163 checks: units, raids/reports, alliance+messages, trade, end game, nb, PWA, reconnect, slow polling fallback (opt-in). Two app bugs found: message push ignored, "New" button stayed English.

Tested: npm test 274/274, openapi check, npm run e2e 163/163 (137 s), bot runs 50x30d seeds 1-3 and 140-day/60-day speed-3 end-game runs INVARIANTS OK, adversarial review pass (findings fixed or documented).
NOT tested: update.sh on a real box (systemd, real npm, runuser; stubs only, shellcheck not installed), garrison card/loot text on a phone, Node 22.13, humans. Known leaks left open and documented (BALANCE S6): loot/shipments can still overflow when stock arrives in flight, bounced backbone shipments lose overflow (reported to the sender). Garrison numbers (heldDefenceMult 2, shield 72 h) are first guesses from 2 seeds. Not claimed: balanced.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
2026-10-12 00:58:12 +02:00

79 lines
8.6 KiB
JavaScript

'use strict';
/*
* What the Alerts feed shows for the notification types of trade, the end game, the world and the social screens
* (alliances, diplomacy, messages). Pure: no DOM, no App. make(ctx) returns { type: (notification, payload) -> { icon, title,
* detail, go, params } } and test/client-alerts.test.js feeds it payloads in the shapes the server writes. In the browser the
* bottom of this file registers the describers in App.alertKinds (the military ones live in milstate.js, build_done in
* screens/alerts.js). Unknown payload fields never throw: a describer falls back to something readable.
*
* ctx = { t(key, vars), num(n), resName(res), townName(id) -> string | null, clockTime(ms), dayTime(ms) }
*/
(function (root, factory) {
if (typeof module === 'object' && module.exports) module.exports = { make: factory };
else {
root.UpstreamAlertKinds = { make: factory };
if (root.App && root.UpstreamI18n) {
const App = root.App, mil = () => App.mil;
const kinds = factory({
t: (k, v) => App.t(k, v), num: (n) => App.fmt.num(n), resName: (r) => App.resName(r),
townName: (id) => (mil() ? mil().townName(id) : null), clockTime: (ms) => mil().clockTime(ms), dayTime: (ms) => mil().dayTime(ms),
});
for (const k of Object.keys(kinds)) if (!App.alertKinds[k]) App.alertKinds[k] = kinds[k];
}
}
}(typeof self !== 'undefined' ? self : this, function make(c) {
const t = c.t;
const RES = ['fiber', 'hardware', 'capital'];
const amt = (a) => (a && a.resource ? c.num(a.amount) + ' ' + c.resName(a.resource) : '');
const cargoText = (cargo) => RES.filter((r) => cargo && cargo[r] > 0).map((r) => c.num(cargo[r]) + ' ' + c.resName(r)).join(', ');
const town = (id) => c.townName(id) || '#' + id;
const tag = (o) => (o ? (o.tag ? '[' + o.tag + ']' : (o.name || '')) : '');
const site = (p) => (Number.isInteger(p.index) ? p.index + 1 : '?');
const known = (prefix, key) => { const k = prefix + key; const s = t(k); return s === k ? String(key) : s; };
const there = (p) => (p.x !== undefined ? t('alerts.at', { x: p.x, y: p.y }) : '');
const worldGo = (p) => ({ go: 'world', params: p && Number.isInteger(p.index) ? { site: p.index } : null });
return {
// ---- trade
trade_accepted: (n, p) => ({ icon: 'swap', title: t('alerts.trade_accepted', { give: amt(p.give), want: amt(p.want) }), detail: t('alerts.build_done.in', { town: town(p.townId) }), go: 'trade', params: { tab: 'mine' } }),
trade_completed: (n, p) => ({ icon: 'swap', title: t('alerts.trade_completed', { give: amt(p.give), want: amt(p.want) }), detail: '', go: 'trade', params: { tab: 'mine' } }),
trade_expired: (n, p) => ({ icon: 'clock', title: t('alerts.trade_expired'), detail: t('alerts.trade_expired.back', { what: amt(p.returned), town: town(p.townId) }), go: 'trade', params: { tab: 'mine' } }),
trade_refund_clipped: (n, p) => ({ icon: 'alert', title: t('alerts.trade_refund_clipped', { what: amt({ resource: p.resource, amount: p.lost }) }), detail: t('alerts.trade_refund_clipped.why', { town: town(p.townId) }), go: 'trade', params: { tab: 'mine' } }),
trade_void: (n, p) => ({ icon: 'alert', title: t('alerts.trade_void'), detail: t('alerts.build_done.in', { town: town(p.townId) }), go: 'trade', params: { tab: 'mine' } }),
// ---- end game
backbone_open: (n, p) => Object.assign({ icon: 'b-backbone_node', title: t('alerts.backbone_open'), detail: t('alerts.backbone_open.n', { n: (p.sites || []).length || 7 }) }, worldGo(null)),
backbone_taken: (n, p) => Object.assign({ icon: 'flag', title: t('alerts.backbone_taken', { n: site(p) }), detail: [t('alerts.level', { n: p.level || 0 }), p.from ? t('alerts.backbone_taken.from', { tag: tag(p.from) }) : t('alerts.backbone_taken.incumbent'), there(p)].filter(Boolean).join(' · ') }, worldGo(p)),
backbone_lost: (n, p) => Object.assign({ icon: 'alert', title: t('alerts.backbone_lost', { n: site(p), tag: tag(p.by) }), detail: [t('alerts.level', { n: p.level || 0 }), there(p)].filter(Boolean).join(' · ') }, worldGo(p)),
backbone_incoming: (n, p) => Object.assign({ icon: 'alert', title: t('alerts.backbone_incoming', { n: site(p) }), detail: [p.arriveAt ? t('alerts.backbone_incoming.at', { time: c.clockTime(p.arriveAt) }) : '', there(p)].filter(Boolean).join(' · ') }, worldGo(p)),
backbone_level: (n, p) => Object.assign({ icon: 'b-backbone_node', title: t('alerts.backbone_level', { n: site(p), level: p.level }), detail: '' }, worldGo(p)),
backbone_delivered: (n, p) => ({ icon: 'box', title: t('alerts.backbone_delivered'), detail: cargoText(p.cargo), go: 'world', params: null }),
backbone_ship_bounced: (n, p) => ({ icon: 'alert', title: t('alerts.backbone_ship_bounced'), detail: cargoText(p.cargo), go: 'movements', params: null }),
backbone_won: (n, p) => ({ icon: 'trophy', title: t('alerts.backbone_won', { name: p.name || tag(p) }), detail: p.endsAt ? t('alerts.backbone_won.ends', { time: c.dayTime(p.endsAt) }) : '', go: 'world', params: null }),
world_ended: (n, p) => ({ icon: 'trophy', title: t('alerts.world_ended'), detail: p.winner && p.winner.name ? t('alerts.world_ended.winner', { name: p.winner.name }) : t('alerts.world_ended.nowinner'), go: 'world', params: null }),
// ---- alliance
alliance_invite: (n, p) => ({ icon: 'users', title: t('alerts.alliance_invite', { name: p.name || tag(p) }), detail: t('alerts.alliance_invite.by', { name: p.by || '?' }), go: 'alliance', params: null }),
alliance_invite_declined: (n, p) => ({ icon: 'users', title: t('alerts.alliance_invite_declined', { name: p.by || '?' }), detail: '', go: 'alliance', params: null }),
alliance_member_joined: (n, p) => ({ icon: 'users', title: t('alerts.alliance_member_joined', { name: p.name || '?' }), detail: '', go: 'alliance', params: null }),
alliance_member_left: (n, p) => ({ icon: 'users', title: t(p.kicked ? 'alerts.alliance_member_kicked' : 'alerts.alliance_member_left', { name: p.name || '?' }), detail: '', go: 'alliance', params: null }),
alliance_kicked: (n, p) => ({ icon: 'alert', title: t('alerts.alliance_kicked', { name: p.name || tag(p) }), detail: '', go: 'alliance', params: null }),
alliance_role: (n, p) => ({ icon: 'users', title: t('alerts.alliance_role', { role: known('soc.role.', p.role) }), detail: p.from ? t('alerts.alliance_role.was', { role: known('soc.role.', p.from) }) : '', go: 'alliance', params: null }),
alliance_leader: (n, p) => ({ icon: 'users', title: t('alerts.alliance_leader', { name: p.name || '?' }), detail: p.from ? t('alerts.alliance_leader.from', { name: p.from }) : '', go: 'alliance', params: null }),
alliance_disbanded: (n, p) => ({ icon: 'alert', title: t('alerts.alliance_disbanded', { name: p.name || tag(p) }), detail: '', go: 'alliance', params: null }),
// ---- diplomacy (diplomacy_changed has two payload shapes: { other: {tag,..} } and, when an alliance was disbanded, { otherTag, reason })
diplomacy_proposal: (n, p) => ({ icon: 'handshake', title: t('alerts.diplomacy_proposal', { tag: tag(p.from), state: known('soc.state.', p.state) }), detail: p.expiresAt ? t('alerts.diplomacy_proposal.until', { time: c.dayTime(p.expiresAt) }) : '', go: 'alliance', params: null }),
diplomacy_declined: (n, p) => ({ icon: 'handshake', title: t('alerts.diplomacy_declined', { tag: tag(p.by), state: known('soc.state.', p.state) }), detail: '', go: 'alliance', params: null }),
diplomacy_changed: (n, p) => ({
icon: 'handshake', title: t('alerts.diplomacy_changed', { tag: p.other ? tag(p.other) : (p.otherTag ? '[' + p.otherTag + ']' : '?'), to: known('soc.state.', p.to) }),
detail: [p.from ? t('alerts.diplomacy_changed.was', { state: known('soc.state.', p.from) }) : '', p.reason === 'disbanded' ? t('alerts.diplomacy_changed.disbanded') : ''].filter(Boolean).join(' · '), go: 'alliance', params: null,
}),
diplomacy_cancel_pending: (n, p) => ({ icon: 'clock', title: t('alerts.diplomacy_cancel_pending', { tag: tag(p.other), state: known('soc.state.', p.from) }), detail: p.effectiveAt ? t('alerts.diplomacy_cancel_pending.at', { time: c.dayTime(p.effectiveAt) }) : '', go: 'alliance', params: null }),
diplomacy_cancel_revoked: (n, p) => ({ icon: 'handshake', title: t('alerts.diplomacy_cancel_revoked', { tag: tag(p.other), state: known('soc.state.', p.state) }), detail: '', go: 'alliance', params: null }),
// ---- messages
message: (n, p) => ({ icon: 'mail', title: t('alerts.message', { name: p.from && p.from.name ? p.from.name : '?' }), detail: '', go: 'messages', params: null }),
alliance_message: (n, p) => ({ icon: 'mail', title: t('alerts.alliance_message', { name: typeof p.from === 'string' ? p.from : '?' }), detail: p.tag ? '[' + p.tag + ']' : '', go: 'messages', params: null }),
};
}));