orbit/js/galaxy/SystemReport.js

56 lines
1.9 KiB
JavaScript

import { config } from '../config/Config.js';
/**
* Formats a generated system into a readable "dossier" for UI.
*
* Pure — no Phaser — so it's testable in Node and reusable anywhere a
* system needs describing (the HUD today; the star map, tooltips, saves,
* or a terminal UI later). Scenes stay thin: they render what this
* returns.
*
* The dossier is deliberately compact — the per-settlement and gate
* detail lives elsewhere (star chart, map, the content itself). What it
* carries: identity (type + star class), who controls the system, and
* how many are settled here.
*
* const report = formatSystemReport(content);
* // { title, subtitle, faction, population, status, statusColor }
* // title — the system name
* // subtitle — "Main Sequence system · star G"
* // status — "Faction: Neutral · Pop ~1.2M" (or "… · Unclaimed")
*/
export function formatSystemReport(content, typeDefs = null) {
const types = typeDefs ?? config.get('systems.types', {});
const typeDef = types[content.type] ?? {};
const population = (content.settlements ?? []).reduce(
(sum, s) => sum + (s.population ?? 0),
0,
);
// Faction control — every system is Neutral for now. When factions
// land (the `owner` seam on settlements), this is where the faction's
// name + theme color will plug in (Neutral = light gray).
const faction = 'Neutral';
const statusColor = '#c4cad8';
return {
title: content.name,
subtitle: `${typeDef.label ?? content.type} system · star ${content.star.class}`,
faction,
population,
status:
population > 0
? `Faction: ${faction} · Pop ~${formatPop(population)}`
: `Faction: ${faction} · Unclaimed`,
statusColor,
};
}
/** 1234 → "1.2k", 9000000 → "9.0M" */
export function formatPop(n) {
if (n >= 1e6) return `${(n / 1e6).toFixed(1)}M`;
if (n >= 1e3) return `${(n / 1e3).toFixed(1)}k`;
return String(n);
}