Upstream/server/military/reports.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

32 lines
1.5 KiB
JavaScript

'use strict';
/*
* Reports: one `reports` row shared by every participant (`report_recipients`, with a per-user read flag). Kept
* MILITARY.reportRetentionMs (real time); the `reportCleanup` event deletes expired ones and re-arms itself only while
* reports remain.
*/
const D = require('../../public/js/data.js');
// o: { worldId, type, now, summary, data, recipients: [{ userId, role }] } -> report id
function createReport(ctx, o) {
const { db } = ctx;
const r = db.run('INSERT INTO reports (world_id, type, created_at, expires_at, summary, data) VALUES (?, ?, ?, ?, ?, ?)',
o.worldId, o.type, o.now, o.now + D.MILITARY.reportRetentionMs, JSON.stringify(o.summary || {}), JSON.stringify(o.data || {}));
const id = Number(r.lastInsertRowid);
const seen = new Set();
for (const rc of o.recipients) {
if (!rc.userId || seen.has(rc.userId)) continue;
seen.add(rc.userId);
db.run('INSERT INTO report_recipients (report_id, user_id, role) VALUES (?, ?, ?)', id, rc.userId, rc.role);
ctx.notify(rc.userId, o.worldId, 'report', { reportId: id, type: o.type, role: rc.role });
}
ensureCleanup(ctx, o.worldId, o.now);
return id;
}
function ensureCleanup(ctx, worldId, now) {
const { db } = ctx;
const pending = db.get("SELECT 1 AS x FROM events WHERE type = 'reportCleanup' AND world_id = ? AND status = 'pending' LIMIT 1", worldId);
if (!pending) ctx.enqueue({ worldId, dueAt: now + D.MILITARY.reportRetentionMs, type: 'reportCleanup', payload: { worldId } });
}
module.exports = { createReport, ensureCleanup };