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
173 lines
12 KiB
JavaScript
173 lines
12 KiB
JavaScript
'use strict';
|
|
/* docs/BALANCE.md S2 (trade escrow refunds were clipped silently) and S4 (raid loot and the incumbent bonus were destroyed on
|
|
* arrival when the raiders' home Rack hall was full). Each test below fails on the code before the fix. Fake clock, in-memory db. */
|
|
const test = require('node:test');
|
|
const assert = require('node:assert');
|
|
const T = require('./military-helpers');
|
|
const { R, D, H } = T;
|
|
const { loadTown } = require('../server/state');
|
|
|
|
const capOf = (h, townId) => R.storageCap(loadTown(h.db, townId));
|
|
const fieldsOff = (h, townId) => h.db.run("UPDATE buildings SET level = 0 WHERE town_id = ? AND key LIKE 'f%'", townId);
|
|
|
|
// ---------------------------------------------------------------- S4: loot
|
|
test('rules.lootSplit: a resource with no room at home is not taken and its share goes to the others', () => {
|
|
const rich = { fiber: 1000, hardware: 1000, capital: 1000 };
|
|
assert.deepEqual(R.lootSplit(300, rich, { fiber: Infinity, hardware: Infinity, capital: Infinity }), { fiber: 100, hardware: 100, capital: 100 });
|
|
assert.deepEqual(R.lootSplit(300, rich, { fiber: 1000, hardware: 0, capital: 1000 }), { fiber: 150, hardware: 0, capital: 150 });
|
|
assert.deepEqual(R.lootSplit(300, rich, { fiber: 20, hardware: 0, capital: 0 }), { fiber: 20, hardware: 0, capital: 0 }, 'no more than the room, even if the raiders could carry more');
|
|
assert.deepEqual(R.lootSplit(300, rich, { fiber: 50 }), { fiber: 50, hardware: 125, capital: 125 }, 'a missing key means no limit');
|
|
assert.deepEqual(R.lootSplit(300, rich), { fiber: 100, hardware: 100, capital: 100 }, 'room is optional');
|
|
});
|
|
|
|
function raid(homeStock) {
|
|
const h = T.setup();
|
|
const att = T.addOperator(h, 'raider', { x: 5, y: 0, units: { script_crew: 40 } });
|
|
const def = T.addOperator(h, 'target', { x: 9, y: 0, units: { soc_analyst: 20 }, infra: { rack_hall: 2 } });
|
|
fieldsOff(h, att.townId); fieldsOff(h, def.townId);
|
|
h.db.run('UPDATE towns SET fiber = 600, hardware = 600, capital = 600 WHERE id = ?', def.townId);
|
|
h.db.run('UPDATE towns SET fiber = ?, hardware = ?, capital = ? WHERE id = ?', homeStock.fiber, homeStock.hardware, homeStock.capital, att.townId);
|
|
const sent = h.run('sendMovement', { userId: att.userId, fromTownId: att.townId, type: 'raid', toTownId: def.townId, units: { script_crew: 40 } });
|
|
return { h, att, def, sent };
|
|
}
|
|
|
|
test('raid: loot is limited to what the home Rack hall can bank; the rest stays with the victim and the report says so (S4)', () => {
|
|
const probe = T.setup(); const cap = capOf(probe, T.addOperator(probe, 'p', { x: 5, y: 0 }).townId);
|
|
const { h, att, def, sent } = raid({ fiber: 100, hardware: cap, capital: cap - 30 }); // no room for Hardware, 30 for Capital
|
|
const defBefore = T.town(h, def.townId);
|
|
h.advanceTo(sent.arriveAt);
|
|
const rep = T.reportsOf(h, att.userId)[0].data;
|
|
assert.equal(rep.outcome, 'attacker_won');
|
|
assert.equal(rep.loot.hardware, 0, 'nothing is taken that cannot be banked');
|
|
assert.ok(rep.loot.capital <= 30 + 1e-9, 'Capital is limited to the 30 of room');
|
|
assert.ok(rep.loot.fiber > 0, 'the carry goes to the resource that has room');
|
|
assert.ok(rep.lootLeft.hardware > 0, 'the report says what was left behind for lack of room');
|
|
const defAfter = T.town(h, def.townId);
|
|
assert.equal(defAfter.hardware, defBefore.hardware, 'the victim keeps what was not taken');
|
|
const ret = h.db.get("SELECT * FROM movements WHERE type = 'return' AND status = 'out'");
|
|
assert.deepEqual(JSON.parse(ret.cargo), rep.loot);
|
|
h.advanceTo(ret.arrive_at);
|
|
const back = T.notes(h, att.userId, 'movement_returned')[0];
|
|
assert.ok(!back.cargoLost || Object.values(back.cargoLost).every((v) => v === 0), 'nothing is destroyed on arrival: ' + JSON.stringify(back.cargoLost));
|
|
assert.ok(Math.abs(T.town(h, att.townId).hardware - cap) < 1e-6, 'hardware stays at the hall size');
|
|
});
|
|
|
|
test('raid: two raids from one home that each see the same free room cannot both fill it; the later one is limited, nothing is destroyed on landing', () => {
|
|
const h = T.setup();
|
|
const att = T.addOperator(h, 'raider', { x: 5, y: 0, units: { script_crew: 80 } });
|
|
const def = T.addOperator(h, 'target', { x: 9, y: 0, units: { soc_analyst: 10 }, infra: { rack_hall: 2 } });
|
|
fieldsOff(h, att.townId); fieldsOff(h, def.townId);
|
|
const cap = capOf(h, att.townId), room = 600, carry = room;
|
|
h.db.run('UPDATE towns SET fiber = 2000, hardware = 2000, capital = 2000 WHERE id = ?', def.townId);
|
|
h.db.run('UPDATE towns SET fiber = ?, hardware = ?, capital = ? WHERE id = ?', cap, cap, cap - room, att.townId); // room for ONE raid's worth, in Capital only (each raid alone could fill it)
|
|
const s1 = h.run('sendMovement', { userId: att.userId, fromTownId: att.townId, type: 'raid', toTownId: def.townId, units: { script_crew: 40 } });
|
|
const s2 = h.run('sendMovement', { userId: att.userId, fromTownId: att.townId, type: 'raid', toTownId: def.townId, units: { script_crew: 40 } });
|
|
h.advanceTo(Math.max(s1.arriveAt, s2.arriveAt));
|
|
const reps = T.reportsOf(h, att.userId).map((r) => r.data).filter((d) => d.type === 'raid');
|
|
assert.equal(reps.length, 2);
|
|
assert.ok(reps.every((r) => r.outcome === 'attacker_won'), 'both raids won (the test is about room, not combat)');
|
|
const total = reps.reduce((a, r) => a + r.loot.capital, 0);
|
|
assert.ok(total <= carry + 1e-6, 'together they take no more than the home can bank: ' + total + ' vs ' + carry);
|
|
assert.ok(reps.some((r) => r.lootLeft && (r.lootLeft.fiber > 0 || r.lootLeft.hardware > 0)), 'the reports say what stayed with the victim');
|
|
h.advance(2 * H);
|
|
assert.equal(T.notes(h, att.userId, 'movement_returned').length, 2);
|
|
for (const n of T.notes(h, att.userId, 'movement_returned')) assert.ok(!n.cargoLost || Object.values(n.cargoLost).every((v) => v === 0), 'nothing destroyed: ' + JSON.stringify(n.cargoLost));
|
|
});
|
|
|
|
test('raid: with room everywhere the loot is the same even split as before and lootLeft is empty', () => {
|
|
const { h, att, sent } = raid({ fiber: 100, hardware: 100, capital: 100 });
|
|
h.advanceTo(sent.arriveAt);
|
|
const rep = T.reportsOf(h, att.userId)[0].data;
|
|
assert.ok(rep.loot.fiber > 0 && rep.loot.fiber === rep.loot.hardware);
|
|
assert.deepEqual(rep.lootLeft, {});
|
|
});
|
|
|
|
test('incumbent bonus is delivered in full even when the Hardware store is full (a one-time reward must not vanish at the door)', () => {
|
|
const h = T.setup({ seed: 99 });
|
|
let site = null;
|
|
for (let x = -20; x <= 20 && !site; x++) for (let y = -20; y <= 20 && !site; y++) if (R.tileAt(h.world.seed, 50, x, y).type === 'incumbent' && Math.abs(x) + Math.abs(y) > 6) site = { x, y };
|
|
const need = R.incumbentDefence(h.world.seed, site.x, site.y, 0);
|
|
const crews = Math.ceil(need / 40 * 1.6);
|
|
const a = T.addOperator(h, 'raider', { x: site.x - 3, y: site.y, units: { script_crew: crews } });
|
|
fieldsOff(h, a.townId);
|
|
const cap = capOf(h, a.townId);
|
|
h.db.run('UPDATE towns SET fiber = 100, hardware = ?, capital = 100 WHERE id = ?', cap, a.townId);
|
|
const s = h.run('sendMovement', { userId: a.userId, fromTownId: a.townId, type: 'attack', x: site.x, y: site.y, units: { script_crew: crews } });
|
|
h.advanceTo(s.arriveAt);
|
|
const ret = h.db.get("SELECT * FROM movements WHERE type = 'return' AND status = 'out'");
|
|
h.advanceTo(ret.arrive_at);
|
|
assert.equal(T.town(h, a.townId).hardware, cap + D.INCUMBENT.hardwareBonus, 'the bonus sits on top of the full store (kept, it only stops growing)');
|
|
const back = T.notes(h, a.userId, 'movement_returned')[0];
|
|
assert.ok(!back.cargoLost || !back.cargoLost.hardware, 'and is not reported lost');
|
|
});
|
|
|
|
// ---------------------------------------------------------------- S2: trade escrow
|
|
function poster(o) {
|
|
const h = T.setup();
|
|
const a = T.addOperator(h, 'alice', { x: 5, y: 0, res: 100 });
|
|
const b = T.addOperator(h, 'bob', { x: 9, y: 0 });
|
|
const cap = capOf(h, a.townId);
|
|
h.db.run('UPDATE towns SET fiber = ?, hardware = 0, capital = 0 WHERE id = ?', o && o.fiber !== undefined ? o.fiber : cap - 420, a.townId);
|
|
return { h, a, b, cap };
|
|
}
|
|
const offerOf = (h, a, amount) => h.run('tradeOffer', { userId: a.userId, townId: a.townId, giveRes: 'fiber', giveAmount: amount || 400, wantRes: 'hardware', wantAmount: 300, maxDistance: 30 }).offer;
|
|
const fiberNow = (h, townId) => R.resourcesAt(loadTown(h.db, townId), h.clock.now()).amounts.fiber;
|
|
|
|
test('trade (S2): escrow keeps its room in the Rack hall, so a refund after the store refilled loses nothing and stores nothing extra', () => {
|
|
for (const how of ['cancel', 'expire']) {
|
|
const { h, a, cap } = poster();
|
|
const offer = offerOf(h, a);
|
|
h.advance(30 * H); // production refills everything it may (the offer lives 48 h)
|
|
const before = fiberNow(h, a.townId);
|
|
assert.ok(Math.abs(before - (cap - 400)) < 1e-6, how + ': the stock stops 400 below the hall size while 400 are escrowed, got ' + before);
|
|
if (how === 'cancel') h.run('tradeCancel', { userId: a.userId, offerId: offer.id });
|
|
else h.advanceTo(offer.expiresAt);
|
|
assert.equal(h.db.get('SELECT status FROM trade_offers WHERE id = ?', offer.id).status, how === 'cancel' ? 'cancelled' : 'expired');
|
|
const after = fiberNow(h, a.townId);
|
|
assert.ok(Math.abs(after - (before + 400)) < 1e-6 && Math.abs(after - cap) < 1e-6, how + ': the whole escrow came back and exactly fills the hall: ' + after + ' / ' + cap);
|
|
assert.equal(T.notes(h, a.userId, 'trade_refund_clipped').length, 0, 'nothing was lost, so nothing is reported');
|
|
}
|
|
});
|
|
|
|
test('trade (S2): several offers and other resources - the room is per resource and per open offer', () => {
|
|
const { h, a, cap } = poster({ fiber: 0 });
|
|
h.db.run('UPDATE towns SET fiber = ?, hardware = ? WHERE id = ?', cap - 300, cap - 100, a.townId);
|
|
const o1 = offerOf(h, a, 400), o2 = offerOf(h, a, 300);
|
|
h.run('tradeOffer', { userId: a.userId, townId: a.townId, giveRes: 'hardware', giveAmount: 250, wantRes: 'fiber', wantAmount: 300, maxDistance: 30 });
|
|
h.advance(30 * H);
|
|
const t = loadTown(h.db, a.townId);
|
|
assert.deepEqual(t.escrow, { fiber: 700, hardware: 250, capital: 0 });
|
|
assert.equal(R.resourcesAt(t, h.clock.now()).caps.fiber, cap - 700);
|
|
h.run('tradeCancel', { userId: a.userId, offerId: o1.id });
|
|
assert.ok(Math.abs(fiberNow(h, a.townId) - (cap - 300)) < 1e-6, 'the other offer still holds its 300');
|
|
h.run('tradeCancel', { userId: a.userId, offerId: o2.id });
|
|
assert.ok(Math.abs(fiberNow(h, a.townId) - cap) < 1e-6);
|
|
assert.equal(h.db.get("SELECT COUNT(*) AS n FROM notifications WHERE type = 'trade_refund_clipped'").n, 0);
|
|
});
|
|
|
|
test('trade (S2): accepting settles the poster under the reserved room; afterwards the room is free again', () => {
|
|
const { h, a, b, cap } = poster({ fiber: 0 });
|
|
h.db.run('UPDATE towns SET fiber = ? WHERE id = ?', cap - 420, a.townId);
|
|
const offer = offerOf(h, a, 400);
|
|
h.advance(30 * H);
|
|
assert.ok(Math.abs(fiberNow(h, a.townId) - (cap - 400)) < 1e-6);
|
|
const acc = h.run('tradeAccept', { userId: b.userId, offerId: offer.id, townId: b.townId });
|
|
const stored = h.db.get('SELECT fiber FROM towns WHERE id = ?', a.townId).fiber;
|
|
assert.ok(Math.abs(stored - (cap - 400)) < 1e-6, 'the poster was settled at the moment the escrow left, under the reserved cap: ' + stored);
|
|
h.advanceTo(acc.arriveAt + 100 * H);
|
|
assert.ok(Math.abs(fiberNow(h, a.townId) - cap) < 1e-6, 'the hall fills up again once the escrow is gone');
|
|
});
|
|
|
|
test('trade (S2): what cannot be refunded (Rack hall shot down meanwhile) is reported, never silent', () => {
|
|
const { h, a } = poster({ fiber: 3000 });
|
|
const offer = offerOf(h, a, 400);
|
|
const total = () => h.db.get('SELECT fiber FROM towns WHERE id = ?', a.townId).fiber;
|
|
const before = total();
|
|
T.setBuildings(h, a.townId, { rack_hall: 1 }); // cap 1000 << stock 2600
|
|
const r = h.run('tradeCancel', { userId: a.userId, offerId: offer.id });
|
|
assert.equal(r.returned.amount, 0, 'stock above the hall size is kept but a refund does not lift it further');
|
|
assert.ok(Math.abs(total() - before) < 1e-6);
|
|
const note = T.notes(h, a.userId, 'trade_refund_clipped');
|
|
assert.equal(note.length, 1);
|
|
assert.deepEqual([note[0].resource, note[0].lost, note[0].credited], ['fiber', 400, 0]);
|
|
});
|