Upstream/server/commands/reportShare.js
jvanvik 931f59e11e Phase 3: trade/end-game client, bot simulator, first balance pass
Built:
- Client: trade screen, world/end-game screen (landing sites, shipping, node build, final ranking), report sharing + alliance reports tab, alert describers for all notification types, error-code strings (en+nb) for every server CODE.
- tools/bot.js (+ tools/bot/): 50 headless bots against the real server in-process with a fake clock; resource/unit conservation ledger, exit 1 on broken invariants. docs/BALANCE.md has method and every run behind a number.
- Balance changes (evidence in docs/BALANCE.md): secondQueueCoreLevel 10 -> 3, TRANSPORT.capacityPerLink 500 -> 1000 (end game was shipping-limited in bot runs).
- Server fixes: POST /reports/:id/share 500 without a pending reportCleanup; bounced link shipments were silent about cargo overflow.

Tested: npm test 207/207 (new: bot determinism + invariants, client alerts/world/i18n over CODES, two regression tests that failed before their fix); openapi check up to date. Real-browser scratch runs (not in repo): WebSocket push shows an incoming-attack banner ~214 ms after launch, polling fallback ~52 s, 15 scripted flows, 71 screenshots.

NOT tested: Playwright suite (e2e/ still empty), real devices/Safari, nginx proxy websockets, throttled networks, HTTP-level bot load. NOT claimed: balance. Bots are not humans; open balance items (loot waste, escrow refund clipping, no garrisons at landing sites, end game not scaling with world speed) are listed in docs/BALANCE.md.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
2026-10-11 20:24:41 +02:00

53 lines
3.6 KiB
JavaScript

'use strict';
/*
* The alliance's shared report folder. Two commands in one file (registered separately in commands/index.js):
* reportShare { userId, reportId } put one of YOUR reports into your alliance's folder (needs `shareReport`)
* reportUnshare { userId, reportId } take it out again: whoever shared it, or `unshareReport` (leader, officer)
* The alliance is the caller's alliance in the report's world. Sharing an already shared report changes nothing
* ({ changed: false }). A shared report lives at least reportRetentionMs from the moment it was shared (the expiry is pushed
* out; it is one row for everybody, so the participants keep it that long too) and is deleted with the report.
*/
const R = require('../../public/js/rules.js');
const D = require('../../public/js/data.js');
const { GameError } = require('../errors');
const A = require('../alliances');
const { ensureCleanup } = require('../military/reports');
const ctxOf = require('../military/ctx');
function context(db, userId, reportId) {
const rep = db.get(`SELECT r.id, r.world_id, r.expires_at FROM reports r JOIN report_recipients rr ON rr.report_id = r.id AND rr.user_id = ? WHERE r.id = ?`, userId, reportId);
if (!rep) throw new GameError('not_found', { what: 'report' }); // not yours: never reveal it exists
const op = db.get('SELECT * FROM operators WHERE user_id = ? AND world_id = ?', userId, rep.world_id);
if (!op) throw new GameError('not_found', { what: 'report' });
const m = A.membership(db, op.id);
if (!m) throw new GameError('not_in_alliance');
return { rep, op, member: m };
}
function reportShare(ctx0, { userId, reportId }) {
const ctx = ctxOf(ctx0); // ensureCleanup enqueues: a plain command ctx has no enqueue (was a 500 when no cleanup event was pending)
const { db, now } = ctx;
const { rep, op, member } = context(db, userId, reportId);
if (!R.allianceCan(member.role, 'shareReport')) throw new GameError('forbidden', { need: 'shareReport', role: member.role });
const r = db.run('INSERT OR IGNORE INTO report_shares (report_id, alliance_id, shared_by, created_at) VALUES (?, ?, ?, ?)', rep.id, member.alliance_id, op.id, now);
if (!r.changes) return { changed: false, reportId: rep.id, allianceId: member.alliance_id };
const until = now + D.MILITARY.reportRetentionMs;
if (rep.expires_at < until) db.run('UPDATE reports SET expires_at = ? WHERE id = ?', until, rep.id);
ensureCleanup(ctx, rep.world_id, now);
return { changed: true, reportId: rep.id, allianceId: member.alliance_id };
}
function reportUnshare(ctx, { userId, reportId }) {
const { db } = ctx;
// the report may no longer list the caller as a recipient (it is only theirs through the alliance): look the share up directly
const share = db.get(`SELECT s.*, r.world_id FROM report_shares s JOIN reports r ON r.id = s.report_id WHERE s.report_id = ?
AND s.alliance_id IN (SELECT m.alliance_id FROM alliance_members m JOIN operators o ON o.id = m.operator_id WHERE o.user_id = ?)`, reportId, userId);
if (!share) throw new GameError('not_found', { what: 'shared report' });
const op = db.get('SELECT * FROM operators WHERE user_id = ? AND world_id = ?', userId, share.world_id);
const member = A.membership(db, op.id);
if (op.id !== share.shared_by && !R.allianceCan(member.role, 'unshareReport')) throw new GameError('forbidden', { need: 'unshareReport', role: member.role });
db.run('DELETE FROM report_shares WHERE report_id = ? AND alliance_id = ?', share.report_id, share.alliance_id);
return { changed: true, reportId: share.report_id, allianceId: share.alliance_id };
}
module.exports = { reportShare, reportUnshare };