From cb06ba79c067a6ca36ebcccf7bd15ebca4864bf9 Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Wed, 18 Nov 2026 14:13:20 -0500 Subject: [PATCH] feat(intelligence): show derived faction relations on the faction and relations pages The faction dossier's Relationships block only ever showed one direction, and only the directions someone had bothered to type by hand. Read both directions from the ledger instead: each row now carries "they see us" and "we see them" meters, and a Recent Developments list under them spells out the events behind those numbers in plain language. Hidden entries move the meters without appearing in that list. Add /intelligence/factions/relations, a full cross-table of every faction against every other, player faction first, with the existing band labels as a legend. Rows and headers link back to dossiers, and the table scrolls sideways rather than truncating scores. A Relations entry in the Intelligence sidebar points at it. --- .../intelligence/factions/[id]/page.tsx | 177 +++++++++++++----- .../intelligence/factions/relations/page.tsx | 160 ++++++++++++++++ src/components/frontend/blocks/sidebarData.ts | 6 + 3 files changed, 295 insertions(+), 48 deletions(-) create mode 100644 src/app/(frontend)/intelligence/factions/relations/page.tsx diff --git a/src/app/(frontend)/intelligence/factions/[id]/page.tsx b/src/app/(frontend)/intelligence/factions/[id]/page.tsx index 6cd41728..3c0dc50b 100644 --- a/src/app/(frontend)/intelligence/factions/[id]/page.tsx +++ b/src/app/(frontend)/intelligence/factions/[id]/page.tsx @@ -23,6 +23,14 @@ import { canViewUndiscoveredFactions, UNDISCOVERED_FACTION_SUMMARY, } from "@/lib/intelligence/factionDiscovery"; +import { + formatScore, + pairKey, + relationBand, + relationCategoryLabel, + RELATION_BAND_CLASSES, +} from "@/lib/intelligence/factionRelations"; +import { getRelationContext, historyFor } from "@/lib/intelligence/factionRelationLedger"; interface FactionDetailPageProps { params: Promise<{ id: string }>; @@ -67,13 +75,41 @@ function reputationLabel(rep: number): string { } function relationshipTone(value: number): { bar: string; text: string; label: string } { - // 50..100 allied (green), 20..49 friendly (green-soft), -19..20 neutral, -20..-49 tense, -50..-100 hostile - if (value >= 50) return { bar: "bg-emerald-500", text: "text-emerald-400", label: "Allied" }; - if (value >= 20) return { bar: "bg-green-500", text: "text-green-400", label: "Friendly" }; - if (value > -20) - return { bar: "bg-muted-foreground", text: "text-muted-foreground", label: "Neutral" }; - if (value > -50) return { bar: "bg-amber-500", text: "text-amber-400", label: "Tense" }; - return { bar: "bg-red-500", text: "text-red-400", label: "Hostile" }; + const band = relationBand(value); + const classes = RELATION_BAND_CLASSES[band.key]; + return { bar: classes.bar, text: classes.text, label: band.label }; +} + +/** One labelled bar for a single direction of a relationship. */ +function RelationMeter({ label, value }: { label: string; value: number }) { + const tone = relationshipTone(value); + const width = Math.min(Math.abs(value), 100) / 2; + return ( +
+
+ {label} + + {formatScore(value)} · {tone.label} + +
+
+
+
+
+
+ ); +} + +function formatOccurred(value: string): string { + return new Date(value).toLocaleDateString("en-US", { + month: "short", + day: "numeric", + year: "numeric", + timeZone: "UTC", + }); } export default async function FactionDetailPage({ params }: FactionDetailPageProps) { @@ -171,12 +207,37 @@ export default async function FactionDetailPage({ params }: FactionDetailPagePro (c): c is Campaign => typeof c === "object" && c !== null, ) ?? []); - // Relationships (targetFaction populated at depth 2) - const relationships = redacted - ? [] - : (faction.relationships ?? []).filter( - (r) => typeof r.targetFaction === "object" && r.targetFaction !== null, - ); + // Relationships are computed from the relation ledger, never stored on the + // faction. Redacted readers get nothing at all (the classified stub below + // renders instead), so the ledger is never queried for them. + const relations = redacted ? null : await getRelationContext(payload, user); + + const relationRows = relations + ? relations.factions + .filter((other) => other.id !== faction.id) + .map((other) => ({ + faction: other, + inbound: relations.scores.get(pairKey(other.id, faction.id)) ?? 0, + outbound: relations.scores.get(pairKey(faction.id, other.id)) ?? 0, + hasRecords: relations.rows.some( + (row) => + (row.subjectId === other.id && row.targetId === faction.id) || + (row.subjectId === faction.id && row.targetId === other.id), + ), + })) + .filter((entry) => entry.inbound !== 0 || entry.outbound !== 0 || entry.hasRecords) + : []; + + // Player-facing timeline: every live entry touching this faction, newest + // first. A mutual entry appears once. + const developments = (() => { + if (!relations) return []; + const { inbound, outbound } = historyFor(relations, faction.id); + return [...inbound, ...outbound] + .filter((row, index, all) => all.findIndex((candidate) => candidate.id === row.id) === index) + .sort((a, b) => new Date(b.occurredAt).getTime() - new Date(a.occurredAt).getTime()) + .slice(0, 8); + })(); return (
@@ -286,56 +347,76 @@ export default async function FactionDetailPage({ params }: FactionDetailPagePro )} - {relationships.length > 0 && ( + {relationRows.length > 0 && (
Relationships
+ + All relations +
- {relationships.map((r) => { - const target = r.targetFaction as Faction; - const value = r.value; - const tone = relationshipTone(value); - const barWidth = Math.min(Math.abs(value), 100); - return ( + {relationRows.map(({ faction: other, inbound, outbound }) => ( +
-
- - - {target.name} - - - {value >= 0 ? `+${value}` : value} · {tone.label} + + {other.name} + +
+ + +
+
+ ))} +
+ + + )} + + {developments.length > 0 && ( + + +
+ + Recent Developments +
+
+ +
    + {developments.map((row) => { + const tone = relationshipTone(row.delta); + return ( +
  • +
    + + {formatOccurred(row.occurredAt)} ·{" "} + {relationCategoryLabel(row.category)} + + + {formatScore(row.delta)}
    -
    -
    -
    -
    - {r.notes && ( -

    {r.notes}

    - )} - +

    {row.summary}

    +
  • ); })} -
+
)} diff --git a/src/app/(frontend)/intelligence/factions/relations/page.tsx b/src/app/(frontend)/intelligence/factions/relations/page.tsx new file mode 100644 index 00000000..e612340a --- /dev/null +++ b/src/app/(frontend)/intelligence/factions/relations/page.tsx @@ -0,0 +1,160 @@ +import config from "@payload-config"; +import { getPayload } from "payload"; +import { headers as nextHeaders } from "next/headers"; +import Link from "next/link"; +import { ArrowLeftIcon, ScaleIcon } from "lucide-react"; +import { getRelationContext } from "@/lib/intelligence/factionRelationLedger"; +import { + formatScore, + pairKey, + relationBand, + RELATION_BANDS, + RELATION_BAND_CLASSES, +} from "@/lib/intelligence/factionRelations"; + +export const metadata = { + title: "Faction Relations | Polaris Task Force", +}; + +const TYPE_ORDER: Record = { + playerFaction: 0, + majorFaction: 1, + minorFaction: 2, + corporation: 3, + special: 4, +}; + +export default async function FactionRelationsPage() { + const payload = await getPayload({ config }); + const { user } = await payload.auth({ + headers: await nextHeaders(), + canSetHeaders: false, + }); + + const context = await getRelationContext(payload, user); + + // Player faction first, then majors down to specials, so the matrix reads in + // the same order the faction list uses. + const factions = [...context.factions].sort( + (a, b) => (TYPE_ORDER[a.type] ?? 99) - (TYPE_ORDER[b.type] ?? 99), + ); + + return ( +
+
+ + + Back to Factions + +
+ +

Faction Relations

+
+

+ Each row reads how that faction currently regards each column, accumulated from recorded + events. The two directions of a relationship are independent, so one faction can respect + another that despises it. +

+
+ +
+ Scale + {RELATION_BANDS.map((band) => ( + + + {band.label} + + ))} +
+ + {factions.length === 0 ? ( +
+

No factions found.

+
+ ) : ( +
+ + + + + {factions.map((faction) => ( + + ))} + + + + {factions.map((subject) => ( + + + {factions.map((target) => { + if (target.id === subject.id) { + return ( + + ); + } + const score = context.scores.get(pairKey(subject.id, target.id)) ?? 0; + const band = relationBand(score); + return ( + + ); + })} + + ))} + +
+ Regards + + + {faction.name} + +
+ + {subject.name} + + + - + + + {formatScore(score)} + +
+
+ )} + +

+ Entries are recorded from the admin panel under Intelligence, Relation Entries. Scores are + clamped to -100..100, and a zero can mean a clean slate or events that cancelled out. +

+
+ ); +} diff --git a/src/components/frontend/blocks/sidebarData.ts b/src/components/frontend/blocks/sidebarData.ts index 74d844e3..a1975e5f 100644 --- a/src/components/frontend/blocks/sidebarData.ts +++ b/src/components/frontend/blocks/sidebarData.ts @@ -21,6 +21,7 @@ import { NotebookText, Package, Radar, + Scale, ScrollText, Search, Store, @@ -71,6 +72,11 @@ export const sidebarData = { url: "/intelligence/factions", icon: Flag, }, + { + title: "Relations", + url: "/intelligence/factions/relations", + icon: Scale, + }, { title: "Tech Tree", url: "/intelligence/tech-tree",