Stage 3-5 server (military, expansion, social), web client for stages 1-2, ops/deploy

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
This commit is contained in:
jvanvik 2026-10-11 18:32:36 +02:00
parent b56b13880e
commit 249818a7aa
111 changed files with 17472 additions and 13 deletions

19
server/events/arrive.js Normal file
View file

@ -0,0 +1,19 @@
'use strict';
/* A movement reached its destination. Payload { movementId }. Dispatches by movement type; everything runs at the
* event's own due time, inside the scheduler's transaction. Idempotent: only an `out` movement is processed. */
const M = require('../military/movements');
const A = require('../military/arrivals');
const combat = require('../military/combat');
module.exports = function arrive(ctx, ev) {
const mv = M.getMovement(ctx.db, ev.payload.movementId);
if (!mv || mv.status !== 'out') return;
ctx.db.run("UPDATE movements SET status = 'done' WHERE id = ?", mv.id);
switch (mv.type) {
case 'scan': case 'raid': case 'attack': case 'takeover': return combat.arrive(ctx, mv, ev);
case 'reinforce': return A.arriveReinforce(ctx, mv, ev);
case 'transport': return A.arriveTransport(ctx, mv, ev);
case 'settle': return A.arriveSettle(ctx, mv, ev);
default: throw new Error('unknown movement type ' + mv.type);
}
};

View file

@ -11,5 +11,6 @@ module.exports = function buildDone(ctx, ev) {
const cur = town.buildings[key];
if (!cur) db.run('INSERT INTO buildings (town_id, key, type, level) VALUES (?, ?, ?, ?)', town.id, key, type, toLevel);
else if (cur.level < toLevel) db.run('UPDATE buildings SET level = ? WHERE town_id = ? AND key = ?', toLevel, town.id, key);
require('../military/power').reschedule(ctx, town.id, ev.dueAt); // building draw / battery cap changed: units' power check
if (userId) ctx.notify(userId, ev.worldId, 'build_done', { townId: town.id, key, type, level: toLevel });
};

View file

@ -0,0 +1,15 @@
'use strict';
/* A unilateral cancel of a pact (or war declared out of a pact) came due. Payload { a, b, to, by }.
* Idempotent and safe against cancellation: it only acts when the relation row still points at THIS event. */
const A = require('../alliances');
const Dip = require('../diplomacy');
module.exports = function diploChange(ctx, ev) {
const { db } = ctx;
const { a, b, to } = ev.payload;
const rel = A.getRelation(db, a, b);
if (!rel || rel.pending_event !== ev.id) return; // revoked, replaced, or already applied
const x = A.getAlliance(db, a), y = A.getAlliance(db, b);
if (!x || !y) return;
Dip.applyChange(ctx, x, y, to, { delayed: true, startedBy: ev.payload.by });
};

View file

@ -1,5 +1,13 @@
'use strict';
/* Event type -> handler. One file per event type. */
module.exports = {
// stage 3/4: military and expansion
trainDone: require('./trainDone'),
researchDone: require('./researchDone'),
arrive: require('./arrive'),
return: require('./return'),
powerCheck: require('./powerCheck'),
reportCleanup: require('./reportCleanup'),
buildDone: require('./buildDone'),
diploChange: require('./diploChange'),
};

View file

@ -0,0 +1,3 @@
'use strict';
/* Battery empty / offline for 24 h: see server/military/power.js. Payload { townId }. */
module.exports = require('../military/power').powerCheck;

View file

@ -0,0 +1,9 @@
'use strict';
/* Delete expired reports (14 days, real time), then arm the next cleanup for the earliest remaining expiry. createReport
* arms it when none is pending, so there is at most one pending cleanup per world and none while no reports exist. */
module.exports = function reportCleanup(ctx, ev) {
const { db } = ctx;
db.run('DELETE FROM reports WHERE world_id = ? AND expires_at <= ?', ev.worldId, ev.dueAt);
const left = db.get('SELECT MIN(expires_at) AS e FROM reports WHERE world_id = ?', ev.worldId);
if (left && left.e !== null) ctx.enqueue({ worldId: ev.worldId, dueAt: left.e, type: 'reportCleanup', payload: { worldId: ev.worldId } });
};

View file

@ -0,0 +1,12 @@
'use strict';
/* A research level finished. Payload { unit, stat, toLevel, userId }. Idempotent: a level never goes down. */
const R = require('../../public/js/rules.js');
module.exports = function researchDone(ctx, ev) {
const { db } = ctx;
const { unit, stat, toLevel, userId } = ev.payload;
if (!db.get('SELECT 1 AS x FROM towns WHERE id = ?', ev.townId)) return;
db.run(`INSERT INTO town_research (town_id, unit, stat, level) VALUES (?, ?, ?, ?)
ON CONFLICT (town_id, unit, stat) DO UPDATE SET level = MAX(level, excluded.level)`, ev.townId, unit, stat, toLevel);
if (userId) ctx.notify(userId, ev.worldId, 'research_done', { townId: ev.townId, unit, stat, level: toLevel, key: R.researchKey(unit, stat) });
};

28
server/events/return.js Normal file
View file

@ -0,0 +1,28 @@
'use strict';
/*
* A return leg arrived: units and cargo go back home. If the origin POP changed hands meanwhile they go to the
* operator's capital (or oldest POP) instead; with no POP left they are lost. Cargo above the Rack hall cap is lost.
*/
const R = require('../../public/js/rules.js');
const D = require('../../public/js/data.js');
const { loadTown, settle } = require('../state');
const M = require('../military/movements');
const U = require('../military/units');
const P = require('../military/power');
const { addCargo } = require('../military/arrivals');
module.exports = function ret(ctx, ev) {
const { db } = ctx;
const mv = M.getMovement(db, ev.payload.movementId);
if (!mv || mv.status !== 'out') return;
db.run("UPDATE movements SET status = 'done' WHERE id = ?", mv.id);
const dest = U.homeTownFor(db, mv.ownerId, mv.toTownId);
const uid = U.userOfOperator(db, mv.ownerId);
if (!dest) { if (uid) ctx.notify(uid, mv.worldId, 'movement_lost', { movementId: mv.id }); return; }
const town = loadTown(db, dest.id);
settle(db, town, ev.dueAt);
U.addHome(db, town.id, mv.units);
const lost = addCargo(db, town, mv.cargo);
P.reschedule(ctx, town.id, ev.dueAt);
if (uid && !mv.extra.link) ctx.notify(uid, mv.worldId, 'movement_returned', { movementId: mv.id, townId: town.id, units: mv.units, cargo: mv.cargo, cargoLost: lost });
};

View file

@ -0,0 +1,17 @@
'use strict';
/* A training batch finished. Payload { unit, count, building, userId }. The POP is settled to the due time at the OLD
* power draw, then the units appear (and the draw rises). */
const { loadTown, settle } = require('../state');
const U = require('../military/units');
const P = require('../military/power');
module.exports = function trainDone(ctx, ev) {
const { db } = ctx;
const { unit, count, userId } = ev.payload;
const town = loadTown(db, ev.townId);
if (!town) return;
settle(db, town, ev.dueAt);
U.addHome(db, town.id, { [unit]: count });
P.reschedule(ctx, town.id, ev.dueAt);
if (userId) ctx.notify(userId, ev.worldId, 'train_done', { townId: town.id, unit, count });
};