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

23 lines
1.2 KiB
JavaScript

'use strict';
/* POST /alliances/:id/leave. The leader cannot leave while others remain (transfer first); a leader who is alone
* dissolves the alliance. Leaving an alliance you are not in is a no-op. */
const { GameError } = require('../errors');
const A = require('../alliances');
module.exports = function allianceLeave(ctx, { userId, allianceId }) {
const { db } = ctx;
const alliance = A.getAlliance(db, allianceId);
if (!alliance) throw new GameError('not_found', { what: 'alliance' });
const op = db.get('SELECT * FROM operators WHERE user_id = ? AND world_id = ?', userId, alliance.world_id);
if (!op) throw new GameError('not_found', { what: 'alliance' });
const m = A.membership(db, op.id);
if (!m || m.alliance_id !== alliance.id) return { changed: false };
if (m.role === 'leader') {
if (A.memberCount(db, alliance.id) > 1) throw new GameError('leader_must_transfer');
A.dissolve(ctx, alliance.id, 'last_member_left');
return { changed: true, disbanded: true };
}
A.removeMember(db, op.id);
A.notifyAlliance(ctx, alliance.id, 'alliance_member_left', { allianceId: alliance.id, operatorId: op.id, name: op.name, kicked: false });
return { changed: true, disbanded: false };
};