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
32 lines
2.4 KiB
JavaScript
32 lines
2.4 KiB
JavaScript
'use strict';
|
|
/* POST /alliances/:id/invite. Needs the `invite` permission. Target by operator id or name (same world). Idempotent:
|
|
* a second invite to the same operator returns the pending one. */
|
|
const { GameError } = require('../errors');
|
|
const A = require('../alliances');
|
|
|
|
module.exports = function allianceInvite(ctx, { userId, allianceId, operatorId, name }) {
|
|
const { db, now } = ctx;
|
|
const S = A.S;
|
|
const { op, alliance } = A.requireRole(db, userId, allianceId, 'invite');
|
|
const target = operatorId !== undefined
|
|
? db.get('SELECT * FROM operators WHERE id = ? AND world_id = ?', operatorId, alliance.world_id)
|
|
: db.get('SELECT * FROM operators WHERE name = ? COLLATE NOCASE AND world_id = ?', name, alliance.world_id);
|
|
if (!target) throw new GameError('not_found', { what: 'operator' });
|
|
if (target.id === op.id) throw new GameError('validation_failed', { errors: [{ path: 'operatorId', message: 'you cannot invite yourself' }] });
|
|
const m = A.membership(db, target.id);
|
|
if (m) throw new GameError('already_in_alliance', { allianceId: m.alliance_id });
|
|
|
|
db.run("UPDATE alliance_invites SET status = 'expired', answered_at = ? WHERE status = 'pending' AND expires_at <= ?", now, now);
|
|
const open = db.get("SELECT id, expires_at FROM alliance_invites WHERE alliance_id = ? AND operator_id = ? AND status = 'pending'", alliance.id, target.id);
|
|
if (open) return { inviteId: open.id, operatorId: target.id, expiresAt: open.expires_at, changed: false };
|
|
|
|
if (A.memberCount(db, alliance.id) >= A.refreshCap(db, alliance)) throw new GameError('alliance_full', { cap: alliance.size_cap });
|
|
const pending = db.get("SELECT COUNT(*) AS n FROM alliance_invites WHERE alliance_id = ? AND status = 'pending'", alliance.id).n;
|
|
if (pending >= S.maxPendingInvites) throw new GameError('conflict', { what: 'too many pending invites', max: S.maxPendingInvites });
|
|
|
|
const expiresAt = now + Math.round(S.inviteTtlHours * A.HOUR);
|
|
const r = db.run('INSERT INTO alliance_invites (alliance_id, operator_id, invited_by, created_at, expires_at) VALUES (?, ?, ?, ?, ?)', alliance.id, target.id, op.id, now, expiresAt);
|
|
const inviteId = Number(r.lastInsertRowid);
|
|
A.notifyOperator(ctx, target.id, 'alliance_invite', { inviteId, allianceId: alliance.id, tag: alliance.tag, name: alliance.name, by: op.name, expiresAt });
|
|
return { inviteId, operatorId: target.id, expiresAt, changed: true };
|
|
};
|