Upstream/server/commands/diploPropose.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

54 lines
3.7 KiB
JavaScript

'use strict';
/* POST /alliances/:id/diplomacy { targetId, state }. Needs `diplomacy`. What happens depends on the move
* (rules.diplomacyMove): 'immediate' applies at once (declaring war from neutral), 'delayed' schedules a cancel that
* takes 24 h / world speed (a `diploChange` event), 'proposal' asks the other alliance to accept. */
const R = require('../../public/js/rules.js');
const { GameError } = require('../errors');
const { loadWorld } = require('../state');
const A = require('../alliances');
const Dip = require('../diplomacy');
module.exports = function diploPropose(ctx, { userId, allianceId, targetId, state }) {
const { db, now, scheduler } = ctx;
const { op, alliance } = A.requireRole(db, userId, allianceId, 'diplomacy');
if (targetId === alliance.id) throw new GameError('validation_failed', { errors: [{ path: 'targetId', message: 'cannot be your own alliance' }] });
const other = A.getAlliance(db, targetId);
if (!other || other.world_id !== alliance.world_id) throw new GameError('not_found', { what: 'alliance' });
const rel = A.getRelation(db, alliance.id, other.id);
const from = rel ? rel.state : 'neutral';
if (rel && rel.pending_to) throw new GameError('diplomacy_pending', { from, to: rel.pending_to, effectiveAt: rel.pending_at, startedBy: rel.pending_by });
const move = R.diplomacyMove(from, state);
if (!move) throw new GameError('diplomacy_invalid', { from, to: state });
if (move === 'immediate') {
Dip.applyChange(ctx, alliance, other, state, { by: op.name });
return { result: 'applied', from, state };
}
if (move === 'delayed') {
const world = loadWorld(db, alliance.world_id);
const dueAt = now + R.pactCancelMs(world.speed);
A.withdrawProposals(db, alliance.id, other.id, now);
const eventId = scheduler.enqueue({ worldId: alliance.world_id, dueAt, type: 'diploChange', payload: { a: alliance.id, b: other.id, to: state, by: alliance.id } });
const [x, y] = A.pairOf(alliance.id, other.id);
db.run('UPDATE alliance_relations SET pending_to = ?, pending_at = ?, pending_by = ?, pending_event = ? WHERE a_id = ? AND b_id = ?', state, dueAt, alliance.id, eventId, x, y);
for (const [me, o2] of [[alliance, other], [other, alliance]]) {
A.notifyAlliance(ctx, me.id, 'diplomacy_cancel_pending', { allianceId: me.id, other: Dip.brief(o2), from, to: state, effectiveAt: dueAt, startedBy: alliance.id });
}
return { result: 'scheduled', from, state, effectiveAt: dueAt, eventId };
}
// proposal
const reverse = db.get("SELECT id, state AS st, expires_at FROM alliance_proposals WHERE from_id = ? AND to_id = ? AND status = 'pending'", other.id, alliance.id);
if (reverse && reverse.expires_at > now && reverse.st === state) throw new GameError('proposal_exists', { proposalId: reverse.id });
db.run("UPDATE alliance_proposals SET status = 'expired', answered_at = ? WHERE status = 'pending' AND expires_at <= ?", now, now);
// a new proposal from the same side replaces the old one
db.run("UPDATE alliance_proposals SET status = 'withdrawn', answered_at = ? WHERE from_id = ? AND to_id = ? AND status = 'pending'", now, alliance.id, other.id);
const expiresAt = now + Math.round(A.S.proposalTtlHours * A.HOUR);
const r = db.run('INSERT INTO alliance_proposals (world_id, from_id, to_id, state, proposed_by, created_at, expires_at) VALUES (?, ?, ?, ?, ?, ?, ?)',
alliance.world_id, alliance.id, other.id, state, op.id, now, expiresAt);
const proposalId = Number(r.lastInsertRowid);
A.notifyAlliance(ctx, other.id, 'diplomacy_proposal', { proposalId, from: Dip.brief(alliance), state, expiresAt }, { roles: ['leader', 'diplomat'] });
return { result: 'proposed', from, state, proposalId, expiresAt };
};