Upstream/tools/bot/view.js
jvanvik 931f59e11e 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
2026-10-11 20:24:41 +02:00

54 lines
3.6 KiB
JavaScript

'use strict';
/*
* Read helpers for the bots: what a client would see (own POPs with the same shape rules.js previews use, the map's towns,
* pending queues). Plain reads through the server's own modules; nothing here writes. Created per run, after the data overrides
* were applied (the server modules cache some constants when first required).
*/
module.exports = function makeView(db, world) {
const R = require('../../public/js/rules.js');
const S = require('../../server/state');
const U = require('../../server/military/units');
const trainCmd = require('../../server/commands/train');
const v = {
world,
townRows(opId) { return db.all('SELECT * FROM towns WHERE owner_id = ? ORDER BY is_capital DESC, id', opId); },
towns(opId) { return v.townRows(opId).map((r) => S.townFromRow(db, r, world)); },
town(id) { return S.loadTown(db, id); },
pendingBuilds(townId) { return S.pendingBuilds(db, townId); },
trainQueue(townId, building) { return trainCmd.pendingBatches(db, townId, building); },
researchBusy(townId) { return !!db.get("SELECT 1 AS x FROM events WHERE town_id = ? AND type = 'researchDone' AND status = 'pending' LIMIT 1", townId); },
researchLevels(townId) { return U.researchLevels(db, townId); },
home(townId) { return U.homeUnits(db, townId); },
operator(id) { return db.get('SELECT * FROM operators WHERE id = ?', id); },
// Every POP of every other operator with what the map shows (position, owner, alliance, protection).
otherTowns(opId) {
return db.all(`SELECT t.id, t.x, t.y, t.owner_id AS ownerId, t.is_capital AS isCapital, o.protection_until AS prot, o.alliance_id AS ally, o.name AS opName
FROM towns t JOIN operators o ON o.id = t.owner_id WHERE t.world_id = ? AND t.owner_id != ? ORDER BY t.id`, world.id, opId);
},
outgoing(opId) { return db.all("SELECT id, type, to_town_id AS toTownId, to_x AS tx, to_y AS ty, arrive_at AS arriveAt, units, extra FROM movements WHERE owner_id = ? AND status = 'out' AND type != 'return' ORDER BY id", opId); },
incomingTo(townIds) {
if (!townIds.length) return [];
return db.all("SELECT id, type, owner_id AS ownerId, to_town_id AS toTownId, arrive_at AS arriveAt FROM movements WHERE status = 'out' AND type IN ('raid','attack','takeover','scan') AND to_town_id IN (" + townIds.map(() => '?').join(',') + ')', ...townIds);
},
// New reports of this user since `cursor` ({ at, seen }; mutated). Not "id > last id": see collector.js (ids are reused after a cleanup).
reportsFor(userId, cursor) {
const rows = db.all(`SELECT r.id, r.type, r.created_at AS at, r.data, rr.role FROM reports r JOIN report_recipients rr ON rr.report_id = r.id
WHERE rr.user_id = ? AND r.created_at >= ? AND r.type IN ('scan','raid','attack','takeover','settle','incumbent','landing') ORDER BY r.created_at, r.id`, userId, cursor.at);
const out = [];
for (const x of rows) {
if (x.at === cursor.at && cursor.seen.has(x.id)) continue;
if (x.at > cursor.at) { cursor.at = x.at; cursor.seen = new Set(); }
cursor.seen.add(x.id);
out.push(Object.assign(x, { data: JSON.parse(x.data) }));
}
return out;
},
footprint(opId, now) { return U.operatorFootprint(db, opId, now); },
level(T, type) { return T.buildings[type] ? T.buildings[type].level : 0; },
fieldLevels(T) { const out = []; for (let i = 1; i <= 18; i++) { const b = T.buildings['f' + i]; if (b) out.push({ key: 'f' + i, type: b.type, level: b.level }); } return out; },
peering(opId) { return require('../../server/alliances').peeringLevel(db, opId); },
R, U,
};
return v;
};