Upstream/server/commands/research.js
jvanvik 249818a7aa 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
2026-10-11 18:32:36 +02:00

26 lines
1.5 KiB
JavaScript

'use strict';
/* POST /towns/:id/research. One research at a time per POP (a `researchDone` event). +5% attack or defence per level
* of one unit type; level n needs a Research lab of level n + RESEARCH.labOffset. Incumbent sites speed it up. */
const R = require('../../public/js/rules.js');
const { GameError } = require('../errors');
const { settle, spend } = require('../state');
const { ownedTown } = require('./build');
const U = require('../military/units');
module.exports = function research(ctx, { userId, townId, unit, stat }) {
const { db, now, scheduler } = ctx;
const town = ownedTown(db, userId, townId);
if (!R.unitDef(unit)) throw new GameError('unknown_unit', { unit });
settle(db, town, now);
const busy = !!db.get("SELECT 1 AS x FROM events WHERE town_id = ? AND type = 'researchDone' AND status = 'pending' LIMIT 1", town.id);
const current = U.researchLevels(db, town.id)[R.researchKey(unit, stat)] || 0;
const plan = R.researchPlan(town, unit, stat, current, busy, now, U.researchBonusOf(db, town.ownerId));
if (!plan.ok) throw new GameError(plan.code, { unit, stat, toLevel: plan.toLevel, cost: plan.cost, missing: plan.missing });
spend(db, town, plan.cost);
const dueAt = now + plan.durationMs;
const eventId = scheduler.enqueue({
worldId: town.worldId, townId: town.id, type: 'researchDone', dueAt,
payload: { unit, stat, toLevel: plan.toLevel, startedAt: now, cost: plan.cost, userId },
});
return { eventId, unit, stat, toLevel: plan.toLevel, startedAt: now, dueAt, cost: plan.cost };
};