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
94 lines
4.8 KiB
JavaScript
94 lines
4.8 KiB
JavaScript
'use strict';
|
|
/*
|
|
* Loading towns from the database into the shape rules.js works on, and settling them (storing the current value at a
|
|
* new time before something that changes a rate is applied). Shared by commands, events and queries.
|
|
*/
|
|
const R = require('../public/js/rules.js');
|
|
const D = require('../public/js/data.js');
|
|
|
|
// Cached world rows: they never change while the server runs (config is read at boot and on admin edits).
|
|
function loadWorld(db, id) {
|
|
const w = db.get('SELECT * FROM worlds WHERE id = ?', id);
|
|
return w ? Object.assign(w, { config: JSON.parse(w.config || '{}') }) : null;
|
|
}
|
|
|
|
// rules.js town shape from a towns row. `unitDraw` comes from the military module when it exists.
|
|
function townFromRow(db, row, world) {
|
|
const buildings = {};
|
|
for (const b of db.all('SELECT key, type, level FROM buildings WHERE town_id = ?', row.id)) buildings[b.key] = { type: b.type, level: b.level };
|
|
world = world || loadWorld(db, row.world_id);
|
|
return {
|
|
id: row.id, worldId: row.world_id, ownerId: row.owner_id, name: row.name, x: row.x, y: row.y,
|
|
isCapital: !!row.is_capital, speed: world.speed,
|
|
updatedAt: row.res_at, footprint: row.footprint,
|
|
res: { fiber: row.fiber, hardware: row.hardware, capital: row.capital, battery: row.battery },
|
|
loyalty: row.loyalty, loyaltyAt: row.loyalty_at,
|
|
buildings,
|
|
unitDraw: unitDraw(db, row.id),
|
|
escrow: escrowOf(db, row.id),
|
|
bonus: R.ruralBonus(world.seed, world.map_radius, row.x, row.y),
|
|
};
|
|
}
|
|
|
|
// Total power draw of units stationed in a town. Military stage fills this in (table town_units).
|
|
function unitDraw(db, townId) {
|
|
if (!hasTable(db, 'town_units')) return 0;
|
|
const rows = db.all('SELECT unit, count FROM town_units WHERE town_id = ?', townId);
|
|
let draw = 0;
|
|
for (const r of rows) if (D.UNITS && D.UNITS[r.unit]) draw += D.UNITS[r.unit].draw * r.count;
|
|
// Reinforcements draw from the host, not from their owner.
|
|
if (hasTable(db, 'stationed')) {
|
|
for (const r of db.all('SELECT unit, count FROM stationed WHERE host_town_id = ?', townId)) if (D.UNITS && D.UNITS[r.unit]) draw += D.UNITS[r.unit].draw * r.count;
|
|
}
|
|
// Units of this POP standing in backbone landing sites (garrisons, E6) draw from their OWNER: a site has no power budget.
|
|
if (hasTable(db, 'site_stationed')) {
|
|
for (const r of db.all('SELECT unit, SUM(count) AS count FROM site_stationed WHERE owner_town_id = ? GROUP BY unit', townId)) if (D.UNITS && D.UNITS[r.unit]) draw += D.UNITS[r.unit].draw * r.count;
|
|
}
|
|
return draw;
|
|
}
|
|
|
|
// Goods this POP has escrowed in open Internet exchange offers, per resource: they still occupy the Rack hall (rules.storageCapOf).
|
|
function escrowOf(db, townId) {
|
|
const out = { fiber: 0, hardware: 0, capital: 0 };
|
|
for (const r of db.all("SELECT give_res, SUM(give_amount) AS n FROM trade_offers WHERE town_id = ? AND status = 'open' GROUP BY give_res", townId)) out[r.give_res] = r.n;
|
|
return out;
|
|
}
|
|
|
|
const tableCache = new Map();
|
|
function hasTable(db, name) {
|
|
if (!tableCache.has(db)) tableCache.set(db, new Map());
|
|
const m = tableCache.get(db);
|
|
if (!m.get(name)) m.set(name, !!db.get("SELECT 1 AS x FROM sqlite_master WHERE type = 'table' AND name = ?", name));
|
|
return m.get(name);
|
|
}
|
|
|
|
function loadTown(db, id) {
|
|
const row = db.get('SELECT * FROM towns WHERE id = ?', id);
|
|
return row ? townFromRow(db, row) : null;
|
|
}
|
|
|
|
// Bring the stored values up to `at`. Call before any change to buildings or units of this town. Mutates `town`.
|
|
function settle(db, town, at) {
|
|
if (at < town.updatedAt) at = town.updatedAt; // never go backwards
|
|
const cur = R.resourcesAt(town, at).amounts;
|
|
const fp = R.footprintAt(town, at);
|
|
const loy = R.loyaltyAt(town.loyalty, town.loyaltyAt, at, town.speed);
|
|
db.run('UPDATE towns SET fiber = ?, hardware = ?, capital = ?, battery = ?, footprint = ?, res_at = ?, loyalty = ?, loyalty_at = ? WHERE id = ?',
|
|
cur.fiber, cur.hardware, cur.capital, cur.battery, fp, at, loy, at, town.id);
|
|
town.res = { fiber: cur.fiber, hardware: cur.hardware, capital: cur.capital, battery: cur.battery };
|
|
town.footprint = fp; town.updatedAt = at; town.loyalty = loy; town.loyaltyAt = at;
|
|
return town;
|
|
}
|
|
|
|
// Subtract a cost from a settled town (persists). Caller has already checked affordability.
|
|
function spend(db, town, cost) {
|
|
for (const r of D.RESOURCES) town.res[r] -= cost[r] || 0;
|
|
db.run('UPDATE towns SET fiber = ?, hardware = ?, capital = ? WHERE id = ?', town.res.fiber, town.res.hardware, town.res.capital, town.id);
|
|
}
|
|
|
|
function pendingBuilds(db, townId) {
|
|
return db.all("SELECT id, due_at, payload FROM events WHERE town_id = ? AND type = 'buildDone' AND status = 'pending' ORDER BY due_at, id", townId)
|
|
.map((e) => Object.assign({ id: e.id, dueAt: e.due_at }, JSON.parse(e.payload)));
|
|
}
|
|
|
|
module.exports = { loadWorld, loadTown, townFromRow, escrowOf, settle, spend, pendingBuilds, hasTable };
|