The matrix answers "what is this faction's score", not "who is tied to whom". Add a graph view behind a Matrix/Graph switch on the same page. The player faction sits at the center and every other faction is placed on a ring by classification (majors innermost, then minors, corporations, specials), so its relations read as spokes and the rest as chords. Positions are deterministic: rings ordered by type, members by name, starting at twelve o'clock, with faint guide rings so the structure is legible without hovering. Relations are straight lines. A relationship runs both ways, so where both sides have an opinion the two directions are drawn side by side on either side of the axis, each spanning the full distance between the factions, colored and weighted by its own score, with an arrowhead at the faction being regarded. The two directions differ independently: a faction can be resented by one that it regards neutrally. A pair with only one opinion keeps a single centered line. A line that would cut through an unrelated faction steps aside just far enough to clear it, and only for factions actually alongside the line rather than beyond either end. Straight stays straight. Hovering a faction isolates its web and opens a panel listing what it regards and who regards it; clicking opens the dossier. Pan, wheel zoom and reset are local, and the whole view is hand-drawn SVG, so no graph library was added. The layout is pure and unit-tested: input order cannot move a node, the two directions of a pair run parallel rather than overlapping, single-direction pairs span the whole pair, and a blocked line dodges instead of crossing a node.
195 lines
8.1 KiB
TypeScript
195 lines
8.1 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
BAND_COLORS,
|
|
layoutFactionGraph,
|
|
linkStrokeWidth,
|
|
type GraphFaction,
|
|
type GraphScore,
|
|
} from "@/lib/intelligence/factionGraphLayout";
|
|
|
|
const FACTION = (
|
|
id: number,
|
|
name: string,
|
|
type: string,
|
|
discovered = true,
|
|
): GraphFaction => ({ id, name, type, discovered });
|
|
|
|
describe("faction relation graph layout", () => {
|
|
const factions: GraphFaction[] = [
|
|
FACTION(1, "Batch 41-7", "playerFaction"),
|
|
FACTION(2, "Worldmark", "majorFaction"),
|
|
FACTION(3, "Autonomous Remnant", "majorFaction"),
|
|
FACTION(4, "Harbor Guild", "minorFaction"),
|
|
FACTION(5, "Vertex Corp", "corporation"),
|
|
];
|
|
|
|
it("puts the player faction at the center and everyone else on rings", () => {
|
|
const layout = layoutFactionGraph(factions, []);
|
|
const hub = layout.nodes.find((node) => node.id === 1)!;
|
|
const majors = layout.nodes.filter((node) => node.type === "majorFaction");
|
|
const minor = layout.nodes.find((node) => node.type === "minorFaction")!;
|
|
const corp = layout.nodes.find((node) => node.type === "corporation")!;
|
|
|
|
expect(layout.hasHub).toBe(true);
|
|
expect(hub.ring).toBe(0);
|
|
expect(majors.every((node) => node.ring === 1)).toBe(true);
|
|
expect(minor.ring).toBe(2);
|
|
expect(corp.ring).toBe(3);
|
|
|
|
const distance = (node: { x: number; y: number }) =>
|
|
Math.hypot(node.x - hub.x, node.y - hub.y);
|
|
expect(distance(majors[0])).toBeCloseTo(distance(majors[1]), 5);
|
|
expect(distance(minor)).toBeGreaterThan(distance(majors[0]));
|
|
expect(distance(corp)).toBeGreaterThan(distance(minor));
|
|
});
|
|
|
|
it("keeps positions stable regardless of input order", () => {
|
|
const forward = layoutFactionGraph(factions, []);
|
|
const shuffled = layoutFactionGraph([...factions].reverse(), []);
|
|
const positions = (layout: ReturnType<typeof layoutFactionGraph>) =>
|
|
new Map(layout.nodes.map((node) => [node.id, `${node.x},${node.y}`]));
|
|
|
|
expect(positions(shuffled)).toEqual(positions(forward));
|
|
});
|
|
|
|
it("draws one straight line per direction, running side by side", () => {
|
|
const scores: GraphScore[] = [
|
|
{ subjectId: 3, targetId: 1, score: -100 },
|
|
{ subjectId: 1, targetId: 3, score: 0 },
|
|
{ subjectId: 2, targetId: 1, score: 25 },
|
|
{ subjectId: 1, targetId: 2, score: 40 },
|
|
{ subjectId: 4, targetId: 4, score: 60 },
|
|
{ subjectId: 99, targetId: 1, score: 10 },
|
|
];
|
|
const layout = layoutFactionGraph(factions, scores);
|
|
|
|
// Zero scores, self scores and unknown factions never produce a link.
|
|
expect(layout.links).toHaveLength(2);
|
|
|
|
const pair = layout.links.find((link) => link.halves.length === 2)!;
|
|
expect(pair.halves.map((half) => half.key).sort()).toEqual(["1:2", "2:1"]);
|
|
|
|
// Each direction spans the whole pair, and the two lines are parallel and
|
|
// separated, rather than overlapping or meeting at a midpoint.
|
|
const forward = pair.halves.find((half) => half.key === "1:2")!;
|
|
const backward = pair.halves.find((half) => half.key === "2:1")!;
|
|
const forwardLength = Math.hypot(forward.x2 - forward.x1, forward.y2 - forward.y1);
|
|
const backwardLength = Math.hypot(backward.x2 - backward.x1, backward.y2 - backward.y1);
|
|
expect(forwardLength).toBeCloseTo(backwardLength, 0);
|
|
|
|
// Perpendicular distance between the two lines.
|
|
const dx = forward.x2 - forward.x1;
|
|
const dy = forward.y2 - forward.y1;
|
|
const length = Math.hypot(dx, dy) || 1;
|
|
const separation = Math.abs(
|
|
(backward.x1 - forward.x1) * (dy / length) - (backward.y1 - forward.y1) * (dx / length),
|
|
);
|
|
expect(separation).toBeGreaterThan(6);
|
|
expect(separation).toBeLessThan(16);
|
|
expect(forward.band.label).toBe("Friendly");
|
|
expect(backward.band.label).toBe("Friendly");
|
|
|
|
const single = layout.links.find((link) => link.halves.length === 1)!;
|
|
expect(single.halves[0].key).toBe("3:1");
|
|
expect(single.halves[0].band.label).toBe("Hostile");
|
|
expect(BAND_COLORS[single.halves[0].band.key]).toBe("#f87171");
|
|
});
|
|
|
|
it("keeps lines straight and trims them to the node edges", () => {
|
|
const layout = layoutFactionGraph(factions, [
|
|
{ subjectId: 2, targetId: 3, score: -20 },
|
|
{ subjectId: 1, targetId: 4, score: 30 },
|
|
]);
|
|
const hub = layout.nodes.find((node) => node.id === 1)!;
|
|
const minor = layout.nodes.find((node) => node.id === 4)!;
|
|
const half = layout.links.find((link) => link.halves[0].key === "1:4")!.halves[0];
|
|
|
|
// Coordinates are rounded to two decimals, hence the small tolerance.
|
|
const startDistance = Math.hypot(half.x1 - hub.x, half.y1 - hub.y);
|
|
expect(startDistance).toBeGreaterThanOrEqual(hub.radius - 0.05);
|
|
|
|
// The half ends at the segment's midpoint, which lies on the straight line
|
|
// between the trimmed start and the target's center.
|
|
const vx = half.x2 - half.x1;
|
|
const vy = half.y2 - half.y1;
|
|
const cross = vx * (minor.y - half.y1) - vy * (minor.x - half.x1);
|
|
// Perpendicular deviation of the far node from the half's line, in pixels.
|
|
const deviation = Math.abs(cross) / (Math.hypot(vx, vy) || 1);
|
|
expect(deviation).toBeLessThan(0.5);
|
|
|
|
// The far end stops at the target's edge, not its center.
|
|
const endDistance = Math.hypot(minor.x - half.x2, minor.y - half.y2);
|
|
expect(endDistance).toBeGreaterThanOrEqual(minor.radius - 0.05);
|
|
expect(Math.hypot(half.x2 - half.x1, half.y2 - half.y1)).toBeGreaterThan(startDistance);
|
|
});
|
|
|
|
it("nudges a segment aside instead of drawing it through another faction", () => {
|
|
// Worldmark and Harbor Guild sit on opposite sides of the hub on the same
|
|
// spoke: a straight Worldmark-to-Harbor segment would cut through the hub.
|
|
const lineThroughHub: GraphScore[] = [{ subjectId: 2, targetId: 4, score: 30 }];
|
|
const layout = layoutFactionGraph(factions, lineThroughHub);
|
|
const hub = layout.nodes.find((node) => node.id === 1)!;
|
|
const half = layout.links[0].halves[0];
|
|
|
|
const start = { x: half.x1, y: half.y1 };
|
|
const end = layout.nodes.find((node) => node.id === 4)!;
|
|
const dx = end.x - start.x;
|
|
const dy = end.y - start.y;
|
|
const length = Math.hypot(dx, dy) || 1;
|
|
// Distance from the hub center to the infinite line through the segment.
|
|
const distance =
|
|
Math.abs((hub.x - start.x) * (dy / length) - (hub.y - start.y) * (dx / length));
|
|
expect(distance).toBeGreaterThanOrEqual(hub.radius);
|
|
});
|
|
|
|
it("scales stroke width with magnitude and caps it", () => {
|
|
expect(linkStrokeWidth(5)).toBeLessThan(linkStrokeWidth(60));
|
|
expect(linkStrokeWidth(100)).toBe(linkStrokeWidth(400));
|
|
expect(linkStrokeWidth(-60)).toBe(linkStrokeWidth(60));
|
|
});
|
|
|
|
it("reports a square canvas with one guide ring per used ring", () => {
|
|
const layout = layoutFactionGraph(factions, []);
|
|
expect(layout.width).toBe(layout.height);
|
|
expect(layout.rings).toHaveLength(3);
|
|
expect(layout.rings).toEqual([...layout.rings].sort((a, b) => a - b));
|
|
for (const node of layout.nodes) {
|
|
if (node.ring === 0) continue;
|
|
const distance = Math.hypot(node.x - layout.center.x, node.y - layout.center.y);
|
|
expect(distance).toBeCloseTo(layout.rings[node.ring - 1], 0);
|
|
}
|
|
});
|
|
|
|
it("keeps every node inside the reported canvas", () => {
|
|
const layout = layoutFactionGraph(factions, []);
|
|
for (const node of layout.nodes) {
|
|
expect(node.x - node.radius).toBeGreaterThanOrEqual(0);
|
|
expect(node.y - node.radius).toBeGreaterThanOrEqual(0);
|
|
expect(node.x + node.radius).toBeLessThanOrEqual(layout.width);
|
|
expect(node.y + node.radius).toBeLessThanOrEqual(layout.height);
|
|
}
|
|
});
|
|
|
|
it("returns an empty layout when there are no factions", () => {
|
|
expect(layoutFactionGraph([], [])).toEqual({
|
|
nodes: [],
|
|
links: [],
|
|
width: 0,
|
|
height: 0,
|
|
center: { x: 0, y: 0 },
|
|
rings: [],
|
|
hasHub: false,
|
|
});
|
|
});
|
|
|
|
it("still lays out factions when none is the player faction", () => {
|
|
const layout = layoutFactionGraph(
|
|
factions.filter((faction) => faction.type !== "playerFaction"),
|
|
[{ subjectId: 2, targetId: 3, score: 12 }],
|
|
);
|
|
expect(layout.hasHub).toBe(false);
|
|
expect(layout.nodes).toHaveLength(4);
|
|
expect(layout.links).toHaveLength(1);
|
|
});
|
|
});
|