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

37
server/commands/train.js Normal file
View file

@ -0,0 +1,37 @@
'use strict';
/*
* POST /towns/:id/train. Queue a batch of one unit type. Batches of the same training building run one after another
* (each is a `trainDone` event); different buildings train in parallel. Units appear when the batch finishes.
*/
const R = require('../../public/js/rules.js');
const D = require('../../public/js/data.js');
const { GameError } = require('../errors');
const { settle, spend } = require('../state');
const { ownedTown } = require('./build');
// Pending batches of a training building, in due order: [{ id, dueAt, ...payload }].
function pendingBatches(db, townId, building) {
return db.all("SELECT id, due_at, payload FROM events WHERE town_id = ? AND type = 'trainDone' AND status = 'pending' ORDER BY due_at, id", townId)
.map((e) => Object.assign({ id: e.id, dueAt: e.due_at }, JSON.parse(e.payload)))
.filter((b) => !building || b.building === building);
}
module.exports = function train(ctx, { userId, townId, unit, count }) {
const { db, now, scheduler } = ctx;
const town = ownedTown(db, userId, townId);
const def = R.unitDef(unit);
if (!def) throw new GameError('unknown_unit', { unit });
settle(db, town, now);
const queue = pendingBatches(db, town.id, def.building);
if (queue.length >= D.MILITARY.maxTrainQueue) throw new GameError('queue_full', { building: def.building, max: D.MILITARY.maxTrainQueue });
const tail = queue.length ? queue[queue.length - 1].dueAt : 0;
const plan = R.trainPlan(town, unit, count, now, tail);
if (!plan.ok) throw new GameError(plan.code, { unit, count, cost: plan.cost, missing: plan.missing });
spend(db, town, plan.cost);
const eventId = scheduler.enqueue({
worldId: town.worldId, townId: town.id, type: 'trainDone', dueAt: plan.dueAt,
payload: { unit, count, building: def.building, startAt: plan.startAt, cost: plan.cost, userId },
});
return { eventId, unit, count, building: def.building, startAt: plan.startAt, dueAt: plan.dueAt, cost: plan.cost };
};
module.exports.pendingBatches = pendingBatches;