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:
parent
b56b13880e
commit
249818a7aa
111 changed files with 17472 additions and 13 deletions
120
server/commands/sendMovement.js
Normal file
120
server/commands/sendMovement.js
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
'use strict';
|
||||
/*
|
||||
* POST /movements. Send units from one of your POPs: scan, raid, attack, takeover, reinforce, settle. (Resources go
|
||||
* through sendTransport.) Validates everything against the rules, takes the units out of the POP and enqueues the
|
||||
* `arrive` event. Nothing resolves now: combat happens at arrival, in the event queue, in order.
|
||||
*/
|
||||
const R = require('../../public/js/rules.js');
|
||||
const D = require('../../public/js/data.js');
|
||||
const { GameError } = require('../errors');
|
||||
const { settle, loadWorld } = require('../state');
|
||||
const { ownedTown } = require('./build');
|
||||
const ctxOf = require('../military/ctx');
|
||||
const U = require('../military/units');
|
||||
const M = require('../military/movements');
|
||||
const P = require('../military/power');
|
||||
|
||||
const TYPES = ['scan', 'raid', 'attack', 'takeover', 'reinforce', 'settle'];
|
||||
const HOSTILE = ['raid', 'attack', 'takeover'];
|
||||
|
||||
// The target of a movement: a POP (by id or by tile) or a bare tile. -> { town: row|null, x, y, tile }
|
||||
function resolveTarget(db, world, args) {
|
||||
let town = null, x, y;
|
||||
if (args.toTownId !== undefined && args.toTownId !== null) {
|
||||
town = db.get('SELECT * FROM towns WHERE id = ? AND world_id = ?', args.toTownId, world.id);
|
||||
if (!town) throw new GameError('not_found', { what: 'town' });
|
||||
x = town.x; y = town.y;
|
||||
} else if (Number.isInteger(args.x) && Number.isInteger(args.y)) {
|
||||
x = args.x; y = args.y;
|
||||
town = db.get('SELECT * FROM towns WHERE world_id = ? AND x = ? AND y = ?', world.id, x, y) || null;
|
||||
} else {
|
||||
throw new GameError('validation_failed', { errors: [{ path: 'toTownId', message: 'give toTownId or x and y' }] });
|
||||
}
|
||||
const tile = R.tileAt(world.seed, world.map_radius, x, y);
|
||||
if (tile.type === 'void') throw new GameError('invalid_target', { reason: 'outside_map' });
|
||||
return { town, x, y, tile };
|
||||
}
|
||||
|
||||
module.exports = function sendMovement(ctx0, args) {
|
||||
const ctx = ctxOf(ctx0);
|
||||
const { db, now } = ctx;
|
||||
const { userId, fromTownId, type } = args;
|
||||
if (TYPES.indexOf(type) < 0) throw new GameError('validation_failed', { errors: [{ path: 'type', message: 'must be one of ' + TYPES.join(', ') }] });
|
||||
const from = ownedTown(db, userId, fromTownId);
|
||||
const world = loadWorld(db, from.worldId);
|
||||
const op = U.getOperator(db, from.ownerId);
|
||||
const units = U.cleanUnits(args.units || {});
|
||||
const chk = R.checkUnitsForType(type, units);
|
||||
if (chk) throw new GameError(chk.code, { reason: chk.reason });
|
||||
settle(db, from, now);
|
||||
|
||||
const home = U.homeUnits(db, from.id);
|
||||
for (const k of Object.keys(units)) if ((home[k] || 0) < units[k]) throw new GameError('not_enough_units', { unit: k, have: home[k] || 0, want: units[k] });
|
||||
if (!P.online(from, now)) throw new GameError('units_offline', { townId: from.id });
|
||||
|
||||
const tgt = resolveTarget(db, world, args);
|
||||
if (tgt.town && tgt.town.id === from.id) throw new GameError('invalid_target', { reason: 'same_town' });
|
||||
const dist = R.distance(from.x, from.y, tgt.x, tgt.y);
|
||||
const arriveAt = now + R.movementMs(dist, units, world.speed);
|
||||
const extra = { research: U.researchSnapshot(db, from.id, units) };
|
||||
let defOp = null;
|
||||
let endProtection = false;
|
||||
|
||||
if (type === 'scan' || HOSTILE.indexOf(type) >= 0) {
|
||||
if (tgt.town) {
|
||||
defOp = U.getOperator(db, tgt.town.owner_id);
|
||||
if (defOp.id === op.id) throw new GameError('invalid_target', { reason: 'own_town' });
|
||||
if (type !== 'scan') {
|
||||
if (U.pactBlocks(db, op.id, defOp.id)) throw new GameError('invalid_target', { reason: 'pact' });
|
||||
if (R.isProtected(defOp.protection_until, arriveAt)) throw new GameError('target_protected', { until: defOp.protection_until });
|
||||
if (R.isProtected(op.protection_until, now)) {
|
||||
if (!args.confirmEndProtection) throw new GameError('protection_active', { until: op.protection_until });
|
||||
endProtection = true;
|
||||
}
|
||||
}
|
||||
if (type === 'takeover') {
|
||||
const count = db.get('SELECT COUNT(*) AS n FROM towns WHERE owner_id = ?', defOp.id).n;
|
||||
const block = R.takeoverBlock(!!tgt.town.is_capital, count);
|
||||
if (block === 'last_pop') throw new GameError('cannot_takeover', { reason: block });
|
||||
const gate = R.footprintGate(U.operatorTowns(db, op.id).length, U.operatorFootprint(db, op.id, now));
|
||||
if (!gate.ok) throw new GameError('footprint_too_low', { need: gate.need, have: gate.have });
|
||||
}
|
||||
} else if (tgt.tile.type === 'incumbent') {
|
||||
if (type !== 'scan' && type !== 'attack') throw new GameError('invalid_target', { reason: 'incumbent_scan_or_attack_only' });
|
||||
} else {
|
||||
throw new GameError('invalid_target', { reason: 'nothing_there' });
|
||||
}
|
||||
if ((type === 'attack' || type === 'takeover') && units.exploit_kit && args.target !== undefined && args.target !== null) {
|
||||
const t = String(args.target);
|
||||
const bdef = R.isFieldKey(t) ? { special: false } : R.buildingDef(t);
|
||||
if (!bdef || bdef.special) throw new GameError('validation_failed', { errors: [{ path: 'target', message: 'not a building' }] });
|
||||
extra.target = t;
|
||||
}
|
||||
} else if (type === 'reinforce') {
|
||||
if (!tgt.town) throw new GameError('invalid_target', { reason: 'no_town' });
|
||||
if (!U.mayReinforce(db, op.id, tgt.town.owner_id)) throw new GameError('invalid_target', { reason: 'not_allied' });
|
||||
defOp = U.getOperator(db, tgt.town.owner_id);
|
||||
} else if (type === 'settle') {
|
||||
const own = U.operatorTowns(db, op.id);
|
||||
let nearest = Infinity;
|
||||
for (const t of own) nearest = Math.min(nearest, R.distance(t.x, t.y, tgt.x, tgt.y));
|
||||
const chk2 = R.settleCheck({ tileType: tgt.tile.type, occupied: !!tgt.town, nearestOwnDist: nearest, ownedCount: own.length, footprint: U.operatorFootprint(db, op.id, now) });
|
||||
if (!chk2.ok) throw new GameError(chk2.code, chk2);
|
||||
extra.wild = R.cleanWild(args.wild);
|
||||
}
|
||||
|
||||
// ---- all checks passed: apply
|
||||
if (endProtection) db.run('UPDATE operators SET protection_until = ? WHERE id = ?', now, op.id);
|
||||
U.removeHome(db, from.id, units);
|
||||
P.reschedule(ctx, from.id, now);
|
||||
const mv = M.insertMovement(ctx, {
|
||||
worldId: from.worldId, ownerId: op.id, type, fromTownId: from.id, toTownId: tgt.town ? tgt.town.id : null,
|
||||
from: { x: from.x, y: from.y }, to: { x: tgt.x, y: tgt.y }, units, departAt: now, arriveAt, extra,
|
||||
});
|
||||
if (tgt.town && defOp && defOp.id !== op.id) {
|
||||
const uid = U.userOfOperator(db, defOp.id);
|
||||
if (uid) ctx.notify(uid, from.worldId, 'incoming', { movementId: mv.id, toTownId: tgt.town.id, fromTownId: from.id, arriveAt }); // type hidden
|
||||
}
|
||||
return { movementId: mv.id, type, fromTownId: from.id, toTownId: tgt.town ? tgt.town.id : null, to: { x: tgt.x, y: tgt.y }, units, departAt: now, arriveAt, protectionEnded: endProtection };
|
||||
};
|
||||
module.exports.resolveTarget = resolveTarget;
|
||||
Loading…
Add table
Add a link
Reference in a new issue