'use strict'; /* Transport capacity scales with world speed (docs/BALANCE.md B7): trips are per sitting, not per hour, so a link carries * capacityPerLink x speed. One function (rules.transportCapacity) is the only reader of the constant; commands, trade terms and the * client's numbers (the `transport` block of a POP view) all go through it. Fake clock, in-memory db. */ const test = require('node:test'); const assert = require('node:assert'); const T = require('./military-helpers'); const { R, D } = T; const err = (fn) => { try { fn(); } catch (e) { return e; } assert.fail('expected an error'); }; const CAP = D.TRANSPORT.capacityPerLink; test('transportCapacity: capacityPerLink x speed, read at call time', () => { assert.equal(R.transportCapacity(1), CAP); assert.equal(R.transportCapacity(3), 3 * CAP); assert.equal(R.transportCapacity(0.5), Math.round(CAP / 2)); assert.equal(R.transportCapacity(10), 10 * CAP); assert.equal(R.transportCapacity(), CAP, 'no speed = 1x'); const keep = D.TRANSPORT.capacityPerLink; D.TRANSPORT.capacityPerLink = 250; try { assert.equal(R.transportCapacity(4), 1000); } finally { D.TRANSPORT.capacityPerLink = keep; } }); test('transportPlan and tradeTermsCheck use the world speed', () => { const town = { speed: 3, buildings: { internet_exchange: { type: 'internet_exchange', level: 2 } }, res: { fiber: 9000, hardware: 9000, capital: 9000, battery: 0 }, updatedAt: 0, unitDraw: 0, footprint: 0 }; town.buildings.rack_hall = { type: 'rack_hall', level: 20 }; const plan = (n, speed) => R.transportPlan(Object.assign({}, town, { speed }), { fiber: n }, 0, 0); assert.notEqual(plan(3 * CAP, 3).code, 'capacity_exceeded'); assert.equal(plan(3 * CAP + 1, 3).code, 'capacity_exceeded'); assert.equal(plan(3 * CAP + 1, 3).capacity, 3 * CAP); assert.equal(plan(CAP + 1, 1).code, 'capacity_exceeded', 'a 1x world is as before'); const terms = (n, speed) => R.tradeTermsCheck({ giveRes: 'fiber', giveAmount: n, wantRes: 'hardware', wantAmount: n, maxDistance: 10 }, speed); assert.equal(terms(3 * CAP, 3).ok, true); assert.equal(terms(3 * CAP + 1, 3).code, 'capacity_exceeded'); assert.equal(terms(3 * CAP, 1).code, 'capacity_exceeded'); assert.equal(terms(CAP).ok, true, 'speed defaults to 1'); }); test('server: a 3x world sends, ships and trades up to three times the 1x capacity; the POP view reports the real number', () => { const h = T.setup({ speed: 3 }); const a = T.addOperator(h, 'sender', { x: 5, y: 0, infra: { internet_exchange: 2, rack_hall: 20 }, res: 20000 }); const b = T.addOperator(h, 'receiver', { x: 9, y: 0, infra: { rack_hall: 20 } }); const send = (n) => h.run('sendTransport', { userId: a.userId, fromTownId: a.townId, toTownId: b.townId, cargo: { fiber: n } }); assert.equal(err(() => send(3 * CAP + 1)).code, 'capacity_exceeded'); assert.equal(h.queries.town(a.userId, a.townId).town.military.transport.capacityPerLink, 3 * CAP); const mv = send(3 * CAP); assert.equal(mv.cargo.fiber, 3 * CAP); const offer = h.run('tradeOffer', { userId: a.userId, townId: a.townId, giveRes: 'hardware', giveAmount: 2 * CAP, wantRes: 'capital', wantAmount: 2 * CAP, maxDistance: 30 }).offer; assert.equal(offer.give.amount, 2 * CAP, 'an offer may fill a whole 3x transport'); assert.equal(err(() => h.run('tradeOffer', { userId: a.userId, townId: a.townId, giveRes: 'hardware', giveAmount: 3 * CAP + 1, wantRes: 'capital', wantAmount: 3 * CAP + 1, maxDistance: 30 })).code, 'capacity_exceeded'); // the same send in a 1x world is refused const h1 = T.setup({ speed: 1 }); const a1 = T.addOperator(h1, 'sender', { x: 5, y: 0, infra: { internet_exchange: 2, rack_hall: 20 }, res: 20000 }); const b1 = T.addOperator(h1, 'receiver', { x: 9, y: 0 }); assert.equal(err(() => h1.run('sendTransport', { userId: a1.userId, fromTownId: a1.townId, toTownId: b1.townId, cargo: { fiber: 3 * CAP } })).code, 'capacity_exceeded'); }); test('no file outside rules.js and data.js reads TRANSPORT.capacityPerLink directly (a constant read bypasses the world speed)', () => { const fs = require('fs'), path = require('path'); const root = path.join(__dirname, '..'); const hits = []; const walk = (dir) => { for (const f of fs.readdirSync(dir, { withFileTypes: true })) { if (f.name === 'node_modules' || f.name === '.git' || f.name === 'test' || f.name === 'e2e' || f.name === 'docs') continue; const p = path.join(dir, f.name); if (f.isDirectory()) walk(p); else if (/\.js$/.test(f.name) && /TRANSPORT\.capacityPerLink/.test(fs.readFileSync(p, 'utf8'))) hits.push(path.relative(root, p)); } }; walk(root); assert.deepEqual(hits.filter((p) => !/^public\/js\/(rules|data)\.js$/.test(p)), []); });