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

35 lines
2.1 KiB
JavaScript

'use strict';
/* POST /diplomacy/proposals/:proposalId/accept | /decline. Needs `diplomacy` in the receiving alliance. The move is
* validated again against the CURRENT relation (it may have changed since the proposal was made). */
const R = require('../../public/js/rules.js');
const { GameError } = require('../errors');
const A = require('../alliances');
const Dip = require('../diplomacy');
module.exports = function diploAnswer(ctx, { userId, proposalId, accept }) {
const { db, now } = ctx;
const p = db.get('SELECT * FROM alliance_proposals WHERE id = ?', proposalId);
if (!p) throw new GameError('not_found', { what: 'proposal' });
const { op, alliance } = A.requireRole(db, userId, p.to_id, 'diplomacy'); // forbidden/not_found for outsiders
if (p.status !== 'pending') {
if ((accept && p.status === 'accepted') || (!accept && p.status === 'declined')) return { changed: false };
throw new GameError(p.status === 'expired' ? 'proposal_expired' : 'conflict', { what: 'proposal is ' + p.status });
}
if (p.expires_at <= now) throw new GameError('proposal_expired', { expiresAt: p.expires_at });
const proposer = A.getAlliance(db, p.from_id);
if (!proposer) throw new GameError('not_found', { what: 'alliance' });
if (!accept) {
db.run("UPDATE alliance_proposals SET status = 'declined', answered_at = ? WHERE id = ?", now, p.id);
A.notifyAlliance(ctx, proposer.id, 'diplomacy_declined', { proposalId: p.id, by: Dip.brief(alliance), state: p.state }, { roles: ['leader', 'diplomat'] });
return { changed: true };
}
const rel = A.getRelation(db, alliance.id, proposer.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 });
if (R.diplomacyMove(from, p.state) !== 'proposal') throw new GameError('diplomacy_invalid', { from, to: p.state });
db.run("UPDATE alliance_proposals SET status = 'accepted', answered_at = ? WHERE id = ?", now, p.id);
Dip.applyChange(ctx, alliance, proposer, p.state, { by: op.name, proposalId: p.id });
return { changed: true, from, state: p.state };
};