Built: units/research/training, movements (scan, raid, attack, takeover, reinforce, settle, transport), deterministic combat resolved in the event queue, reports, power/offline units, founding + conquest + incumbent sites; alliances, roles, diplomacy (24h pact cancel via events), messages, rankings; vanilla-JS phone-first client (auth, POP, map, alerts, settings, en/nb); systemd units, backup timer, DEPLOY.md, tools/admin.js. Tested: npm test 96/96 (fake clock, in-memory db, a few real-HTTP tests); client checked with headless Chromium screenshots only. NOT done: marketplace/transport trade, client screens for military/social, WebSocket push, bots/balance, e2e suite. All unit stats and costs are unbalanced first guesses. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
76 lines
4.4 KiB
JavaScript
76 lines
4.4 KiB
JavaScript
'use strict';
|
|
const test = require('node:test');
|
|
const assert = require('node:assert');
|
|
const { createTestServer } = require('./helpers');
|
|
|
|
const post = (body) => ({ method: 'POST', body });
|
|
const DAY = 24 * 3600 * 1000;
|
|
|
|
test('rankings: operators and alliances by footprint, ties by id, rank-offset cursor', async (t0) => {
|
|
const t = await createTestServer({ contract: true });
|
|
t0.after(() => t.close());
|
|
const a = await t.signupPeering('rank_a'), b = await t.signupPeering('rank_b'), c = await t.signup('rank_c');
|
|
const ia = (await t.api('/alliances', post({ name: 'Rank A', tag: 'RA' }), a.token)).body.allianceId;
|
|
const ib = (await t.api('/alliances', post({ name: 'Rank B', tag: 'RB' }), b.token)).body.allianceId;
|
|
const inv = await t.api('/alliances/' + ia + '/invite', post({ operatorId: c.operatorId }), a.token);
|
|
t.setBuilding(c.townId, 'peering_office', 1);
|
|
await t.api('/alliance-invites/' + inv.body.inviteId + '/accept', { method: 'POST' }, c.token);
|
|
|
|
// all three have the same buildings (+ an office): after a day the footprint is equal; give b a core router bump
|
|
t.setBuilding(b.townId, 'core_router', 5);
|
|
t.clock.advance(DAY);
|
|
let r = await t.api('/rankings/operators', {}, a.token);
|
|
assert.equal(r.body.total, 3);
|
|
assert.deepEqual(r.body.items.map((x) => x.name), ['rank_b', 'rank_a', 'rank_c'], 'b leads, a and c tie and a has the lower id');
|
|
assert.deepEqual(r.body.items.map((x) => x.rank), [1, 2, 3]);
|
|
assert.equal(r.body.you.rank, 2);
|
|
assert.equal(r.body.items[1].allianceTag, 'RA'); assert.equal(r.body.items[0].towns, 1);
|
|
assert.ok(r.body.items[0].footprint > r.body.items[1].footprint && r.body.items[1].footprint > 0);
|
|
|
|
// the number is the rules' footprintAt over the town
|
|
const R = require('../public/js/rules.js');
|
|
const { loadTown } = require('../server/state');
|
|
const fpB = R.footprintAt(loadTown(t.srv.db, b.townId), t.clock.now());
|
|
assert.ok(Math.abs(r.body.items[0].footprint - fpB) < 0.01, r.body.items[0].footprint + ' vs ' + fpB);
|
|
|
|
r = await t.api('/rankings/operators?limit=2', {}, a.token);
|
|
assert.equal(r.body.items.length, 2); assert.equal(r.body.next, 2);
|
|
r = await t.api('/rankings/operators?limit=2&after=2', {}, a.token);
|
|
assert.deepEqual(r.body.items.map((x) => x.rank), [3]); assert.equal(r.body.next, null);
|
|
|
|
r = await t.api('/rankings/alliances', {}, a.token);
|
|
assert.deepEqual(r.body.items.map((x) => x.tag), ['RA', 'RB'], 'two members beat one stronger member');
|
|
assert.equal(r.body.items[0].members, 2); assert.equal(r.body.items[0].towns, 2);
|
|
assert.equal(r.body.you.rank, 1);
|
|
const sumA = r.body.items[0].footprint;
|
|
const ops = (await t.api('/rankings/operators', {}, a.token)).body.items;
|
|
const expect = ops.filter((x) => x.allianceTag === 'RA').reduce((s, x) => s + x.footprint, 0);
|
|
assert.ok(Math.abs(sumA - expect) < 0.02);
|
|
assert.ok(ib);
|
|
r = await t.api('/rankings/alliances', {}, c.token);
|
|
assert.equal(r.body.you.tag, 'RA');
|
|
});
|
|
|
|
test('rankings: battles read operator_stats when the military stage created it', async (t0) => {
|
|
const t = await createTestServer({ contract: true });
|
|
t0.after(() => t.close());
|
|
const a = await t.signup('war_a'), b = await t.signup('war_b'), c = await t.signup('war_c');
|
|
const hasTable = !!t.srv.db.get("SELECT 1 AS x FROM sqlite_master WHERE name = 'operator_stats'");
|
|
let r = await t.api('/rankings/battles', {}, a.token);
|
|
assert.equal(r.status, 200);
|
|
if (!hasTable) {
|
|
assert.equal(r.body.available, false); assert.deepEqual(r.body.items, []);
|
|
return;
|
|
}
|
|
assert.equal(r.body.available, true); assert.deepEqual(r.body.items, [], 'nobody has points yet');
|
|
t.srv.db.run('INSERT INTO operator_stats (operator_id, attack_points, defense_points) VALUES (?, ?, ?)', a.operatorId, 50, 5);
|
|
t.srv.db.run('INSERT INTO operator_stats (operator_id, attack_points, defense_points) VALUES (?, ?, ?)', b.operatorId, 10, 90);
|
|
r = await t.api('/rankings/battles', {}, c.token);
|
|
assert.deepEqual(r.body.items.map((x) => x.name), ['war_a', 'war_b']);
|
|
assert.equal(r.body.by, 'attack');
|
|
r = await t.api('/rankings/battles?by=defense', {}, c.token);
|
|
assert.deepEqual(r.body.items.map((x) => x.name), ['war_b', 'war_a']);
|
|
r = await t.api('/rankings/battles?by=total', {}, a.token);
|
|
assert.deepEqual(r.body.items.map((x) => x.name), ['war_b', 'war_a']); assert.equal(r.body.you.name, 'war_a');
|
|
assert.equal((await t.api('/rankings/battles?by=bogus', {}, a.token)).status, 422);
|
|
});
|