1
0
Fork 0

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.
This commit is contained in:
Jason Fraley 2026-11-18 14:13:20 -05:00
parent 2384c7f5a4
commit cb06ba79c0
3 changed files with 295 additions and 48 deletions

View file

@ -23,6 +23,14 @@ import {
canViewUndiscoveredFactions, canViewUndiscoveredFactions,
UNDISCOVERED_FACTION_SUMMARY, UNDISCOVERED_FACTION_SUMMARY,
} from "@/lib/intelligence/factionDiscovery"; } 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 { interface FactionDetailPageProps {
params: Promise<{ id: string }>; params: Promise<{ id: string }>;
@ -67,13 +75,41 @@ function reputationLabel(rep: number): string {
} }
function relationshipTone(value: number): { bar: string; text: string; label: 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 const band = relationBand(value);
if (value >= 50) return { bar: "bg-emerald-500", text: "text-emerald-400", label: "Allied" }; const classes = RELATION_BAND_CLASSES[band.key];
if (value >= 20) return { bar: "bg-green-500", text: "text-green-400", label: "Friendly" }; return { bar: classes.bar, text: classes.text, label: band.label };
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" }; /** One labelled bar for a single direction of a relationship. */
return { bar: "bg-red-500", text: "text-red-400", label: "Hostile" }; function RelationMeter({ label, value }: { label: string; value: number }) {
const tone = relationshipTone(value);
const width = Math.min(Math.abs(value), 100) / 2;
return (
<div className="flex flex-col gap-1">
<div className="flex items-center justify-between gap-2 text-xs">
<span className="text-muted-foreground">{label}</span>
<span className={`font-mono ${tone.text}`}>
{formatScore(value)} · {tone.label}
</span>
</div>
<div className="relative h-1.5 w-full overflow-hidden rounded-full bg-muted">
<div
className={`absolute top-0 h-full ${tone.bar}`}
style={{ left: value < 0 ? "50%" : `${50 - width}%`, width: `${width}%` }}
/>
<div className="absolute left-1/2 top-0 h-full w-px bg-border" />
</div>
</div>
);
}
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) { 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, (c): c is Campaign => typeof c === "object" && c !== null,
) ?? []); ) ?? []);
// Relationships (targetFaction populated at depth 2) // Relationships are computed from the relation ledger, never stored on the
const relationships = redacted // faction. Redacted readers get nothing at all (the classified stub below
? [] // renders instead), so the ledger is never queried for them.
: (faction.relationships ?? []).filter( const relations = redacted ? null : await getRelationContext(payload, user);
(r) => typeof r.targetFaction === "object" && r.targetFaction !== null,
); 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 ( return (
<div className="flex flex-col"> <div className="flex flex-col">
@ -286,56 +347,76 @@ export default async function FactionDetailPage({ params }: FactionDetailPagePro
</Item> </Item>
)} )}
{relationships.length > 0 && ( {relationRows.length > 0 && (
<Item variant="outline"> <Item variant="outline">
<ItemHeader> <ItemHeader>
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<ScaleIcon className="size-4 text-muted-foreground" /> <ScaleIcon className="size-4 text-muted-foreground" />
<ItemTitle>Relationships</ItemTitle> <ItemTitle>Relationships</ItemTitle>
</div> </div>
<Link
href="/intelligence/factions/relations"
className="py-1 text-xs text-muted-foreground transition-colors hover:text-primary"
>
All relations
</Link>
</ItemHeader> </ItemHeader>
<ItemContent> <ItemContent>
<div className="flex flex-col gap-3"> <div className="flex flex-col gap-3">
{relationships.map((r) => { {relationRows.map(({ faction: other, inbound, outbound }) => (
const target = r.targetFaction as Faction; <div
const value = r.value; key={other.id}
const tone = relationshipTone(value); className="flex flex-col gap-2 rounded-md border border-border/50 p-3"
const barWidth = Math.min(Math.abs(value), 100); >
return (
<Link <Link
key={r.id ?? target.id} href={`/intelligence/factions/${other.id}`}
href={`/intelligence/factions/${target.id}`} className="flex w-fit items-center gap-2 py-1 text-sm font-medium transition-colors hover:text-primary"
className="flex flex-col gap-1 rounded-md border border-border/50 p-3 transition-colors hover:border-primary/40 hover:bg-muted/30"
> >
<div className="flex items-center justify-between gap-2"> <ShieldIcon className="size-4 text-muted-foreground" />
<Link <span>{other.name}</span>
href={`/intelligence/factions/${target.id}`} </Link>
className="flex items-center gap-2 py-1 text-sm font-medium hover:text-primary transition-colors" <div className="grid grid-cols-1 gap-2 sm:grid-cols-2">
> <RelationMeter label="They see us" value={inbound} />
<ShieldIcon className="size-4 text-muted-foreground" /> <RelationMeter label="We see them" value={outbound} />
<span>{target.name}</span> </div>
</Link> </div>
<span className={`text-xs font-mono ${tone.text}`}> ))}
{value >= 0 ? `+${value}` : value} · {tone.label} </div>
</ItemContent>
</Item>
)}
{developments.length > 0 && (
<Item variant="outline">
<ItemHeader>
<div className="flex items-center gap-2">
<ScaleIcon className="size-4 text-muted-foreground" />
<ItemTitle>Recent Developments</ItemTitle>
</div>
</ItemHeader>
<ItemContent>
<ul className="divide-y divide-border/50">
{developments.map((row) => {
const tone = relationshipTone(row.delta);
return (
<li
key={row.id}
className="flex flex-col gap-1 py-2 first:pt-0 last:pb-0"
>
<div className="flex items-center justify-between gap-2 text-xs">
<span className="text-muted-foreground">
{formatOccurred(row.occurredAt)} ·{" "}
{relationCategoryLabel(row.category)}
</span>
<span className={`shrink-0 font-mono ${tone.text}`}>
{formatScore(row.delta)}
</span> </span>
</div> </div>
<div className="relative h-1.5 w-full overflow-hidden rounded-full bg-muted"> <p className="text-sm">{row.summary}</p>
<div </li>
className={`absolute top-0 h-full ${tone.bar}`}
style={{
left: value < 0 ? "50%" : `${50 - barWidth / 2}%`,
width: `${barWidth / 2}%`,
}}
/>
<div className="absolute left-1/2 top-0 h-full w-px bg-border" />
</div>
{r.notes && (
<p className="text-xs text-muted-foreground mt-1">{r.notes}</p>
)}
</Link>
); );
})} })}
</div> </ul>
</ItemContent> </ItemContent>
</Item> </Item>
)} )}

View file

@ -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<string, number> = {
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 (
<div className="p-5 flex flex-col gap-6 max-w-[96rem] mx-auto w-full">
<div className="flex flex-col gap-1">
<Link
href="/intelligence/factions"
className="flex w-fit items-center gap-1 py-1 text-sm text-muted-foreground transition-colors hover:text-primary"
>
<ArrowLeftIcon className="size-3" />
Back to Factions
</Link>
<div className="flex items-center gap-2">
<ScaleIcon className="size-5 text-muted-foreground" />
<h1 className="text-lg font-semibold">Faction Relations</h1>
</div>
<p className="text-sm text-muted-foreground">
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.
</p>
</div>
<div className="flex flex-wrap items-center gap-3 text-xs text-muted-foreground">
<span className="uppercase tracking-wider">Scale</span>
{RELATION_BANDS.map((band) => (
<span key={band.key} className="flex items-center gap-1.5">
<span
className={`inline-block size-2 rounded-full ${RELATION_BAND_CLASSES[band.key].bar}`}
aria-hidden
/>
<span>{band.label}</span>
</span>
))}
</div>
{factions.length === 0 ? (
<div className="flex flex-col items-center justify-center py-12 text-muted-foreground">
<p className="text-sm">No factions found.</p>
</div>
) : (
<div className="overflow-x-auto rounded-lg border border-border">
<table className="w-full min-w-[720px] border-collapse text-sm">
<thead>
<tr className="border-b border-border">
<th className="sticky left-0 z-10 bg-card px-4 py-3 text-left text-xs font-medium uppercase tracking-wider text-muted-foreground">
Regards
</th>
{factions.map((faction) => (
<th
key={faction.id}
scope="col"
className="px-3 py-3 text-center text-xs font-medium uppercase tracking-wider text-muted-foreground"
>
<Link
href={`/intelligence/factions/${faction.id}`}
className="mx-auto block max-w-[9rem] truncate transition-colors hover:text-primary"
title={faction.name}
>
{faction.name}
</Link>
</th>
))}
</tr>
</thead>
<tbody className="divide-y divide-border">
{factions.map((subject) => (
<tr key={subject.id}>
<th
scope="row"
className="sticky left-0 z-10 bg-card px-4 py-3 text-left font-medium"
>
<Link
href={`/intelligence/factions/${subject.id}`}
className="block max-w-[14rem] truncate transition-colors hover:text-primary"
title={subject.name}
>
{subject.name}
</Link>
</th>
{factions.map((target) => {
if (target.id === subject.id) {
return (
<td
key={target.id}
className="px-3 py-3 text-center text-muted-foreground/40"
>
<span aria-label="not applicable">-</span>
</td>
);
}
const score = context.scores.get(pairKey(subject.id, target.id)) ?? 0;
const band = relationBand(score);
return (
<td
key={target.id}
className="px-3 py-3 text-center"
title={`${subject.name} toward ${target.name}: ${band.label}`}
>
<span
className={`font-mono tabular-nums ${score === 0 ? "text-muted-foreground" : RELATION_BAND_CLASSES[band.key].text}`}
>
{formatScore(score)}
</span>
</td>
);
})}
</tr>
))}
</tbody>
</table>
</div>
)}
<p className="text-xs text-muted-foreground">
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.
</p>
</div>
);
}

View file

@ -21,6 +21,7 @@ import {
NotebookText, NotebookText,
Package, Package,
Radar, Radar,
Scale,
ScrollText, ScrollText,
Search, Search,
Store, Store,
@ -71,6 +72,11 @@ export const sidebarData = {
url: "/intelligence/factions", url: "/intelligence/factions",
icon: Flag, icon: Flag,
}, },
{
title: "Relations",
url: "/intelligence/factions/relations",
icon: Scale,
},
{ {
title: "Tech Tree", title: "Tech Tree",
url: "/intelligence/tech-tree", url: "/intelligence/tech-tree",