feat: rebuild the orbital vault as an operational map

This commit is contained in:
2026-11-09 22:01:14 +07:00
parent 2129e80c38
commit fba4a1e628
25 changed files with 1342 additions and 1333 deletions

View File

@@ -22,6 +22,18 @@ export function schedulePhrase(cron?: string | null, dryRun?: boolean): string {
export type VaultStatus = "green" | "amber" | "grey" | "red";
export function orbitStatus(status: string): { label: string; tone: VaultStatus; busy?: boolean } {
switch (status) {
case "failed": return { label: "Failed", tone: "red" };
case "running": return { label: "Running", tone: "amber", busy: true };
case "rehearsing": return { label: "Rehearsing", tone: "amber", busy: true };
case "rehearsed": return { label: "Rehearsed", tone: "amber" };
case "completed": return { label: "Completed", tone: "green" };
case "idle": return { label: "Idle", tone: "grey" };
default: return { label: status || "Unknown", tone: "grey" };
}
}
export function vaultStatusFromOrbits(
orbits: { last_status: string }[],
): VaultStatus {

View File

@@ -1,137 +1,38 @@
import type { Vec2 } from "./galaxyLayout";
export type Rect = { x: number; y: number; width: number; height: number };
type Label = { id: string; x: number; y: number; width: number };
export const LABEL_HEIGHT = 24;
const GAP = 4;
export type LabelPlacement = {
x: number;
y: number;
};
const CHAR_W = 6.4;
const LABEL_H = 14;
const LABEL_PAD = 6;
function labelWidth(name: string): number {
const n = name.length > 16 ? 15 : name.length;
return n * CHAR_W;
function overlaps(a: Rect, b: Rect) {
return a.x < b.x + b.width + GAP && a.x + a.width + GAP > b.x &&
a.y < b.y + b.height + GAP && a.y + a.height + GAP > b.y;
}
function dist(a: LabelPlacement, b: LabelPlacement): number {
return Math.hypot(a.x - b.x, a.y - b.y);
}
/** Radial outward labels with simple bbox separation (lock mode). */
export function layoutLockLabels(
starPositions: Vec2[],
names: string[],
starR: number,
): LabelPlacement[] {
const vaultPad = 108;
const labels: LabelPlacement[] = starPositions.map((p, i) => {
const ang = Math.atan2(p.y, p.x);
const base = starR + 28;
return {
x: p.x + Math.cos(ang) * base,
y: p.y + Math.sin(ang) * base,
};
});
for (let iter = 0; iter < 48; iter++) {
for (let i = 0; i < labels.length; i++) {
const p = starPositions[i];
const r = dist(labels[i], { x: 0, y: 0 });
if (r < vaultPad) {
const ang = Math.atan2(labels[i].y, labels[i].x);
labels[i].x = Math.cos(ang) * vaultPad;
labels[i].y = Math.sin(ang) * vaultPad;
}
for (let j = i + 1; j < labels.length; j++) {
const wi = labelWidth(names[i]);
const wj = labelWidth(names[j]);
const dx = labels[j].x - labels[i].x;
const dy = labels[j].y - labels[i].y;
const needX = (wi + wj) / 2 + LABEL_PAD;
const needY = LABEL_H + LABEL_PAD;
if (Math.abs(dx) < needX && Math.abs(dy) < needY) {
const push = (needX - Math.abs(dx) + needY - Math.abs(dy)) * 0.35;
const ang = Math.atan2(dy, dx) || 0;
labels[i].x -= Math.cos(ang) * push;
labels[i].y -= Math.sin(ang) * push;
labels[j].x += Math.cos(ang) * push;
labels[j].y += Math.sin(ang) * push;
}
}
const outward = Math.atan2(
labels[i].y - starPositions[i].y,
labels[i].x - starPositions[i].x,
);
const minDist = starR + 26;
const dStar = dist(labels[i], starPositions[i]);
if (dStar < minDist) {
labels[i].x = starPositions[i].x + Math.cos(outward) * minDist;
labels[i].y = starPositions[i].y + Math.sin(outward) * minDist;
export function placeLabels(
candidates: Label[],
width: number,
height: number,
obstacles: Rect[],
): Label[] {
const occupied = [...obstacles];
const placed: Label[] = [];
for (const label of candidates) {
const { x, y, width: w } = label;
const offsets = [12, 28, 44].flatMap((gap) => [
[gap, -LABEL_HEIGHT / 2], [-w - gap, -LABEL_HEIGHT / 2],
[-w / 2, -LABEL_HEIGHT - gap], [-w / 2, gap],
[gap, -LABEL_HEIGHT - gap / 2], [-w - gap, gap / 2],
]);
for (const [dx, dy] of offsets) {
const rect = { x: x + dx, y: y + dy, width: w, height: LABEL_HEIGHT };
if (rect.x < GAP || rect.y < GAP || rect.x + w > width - GAP ||
rect.y + LABEL_HEIGHT > height - GAP || occupied.some((other) => overlaps(rect, other))) {
continue;
}
occupied.push(rect);
placed.push({ id: label.id, ...rect });
break;
}
}
const yMin = -160;
const yMax = 108;
const xMin = -230;
const xMax = 230;
for (let i = 0; i < labels.length; i++) {
const star = starPositions[i];
const hitsChrome =
labels[i].y < yMin ||
labels[i].y > yMax ||
labels[i].x < xMin ||
labels[i].x > xMax;
const hitsNeighbor = starPositions.some(
(other, k) => k !== i && dist(labels[i], other) < starR + 20,
);
if (hitsChrome || hitsNeighbor) {
const ang = Math.atan2(star.y, star.x);
labels[i] = {
x: star.x - Math.cos(ang) * (starR + 34),
y: star.y - Math.sin(ang) * (starR + 34),
};
}
labels[i].x = Math.max(xMin, Math.min(xMax, labels[i].x));
labels[i].y = Math.max(yMin, Math.min(yMax, labels[i].y));
const gap = dist(labels[i], star);
if (gap < starR + 16) {
const ang = Math.atan2(labels[i].y - star.y, labels[i].x - star.x) || 0;
labels[i].x = star.x + Math.cos(ang) * (starR + 18);
labels[i].y = Math.max(
yMin,
Math.min(yMax, star.y + Math.sin(ang) * (starR + 18)),
);
}
}
for (let iter = 0; iter < 24; iter++) {
for (let i = 0; i < labels.length; i++) {
for (let j = i + 1; j < labels.length; j++) {
const wi = labelWidth(names[i]);
const wj = labelWidth(names[j]);
const dx = labels[j].x - labels[i].x;
const dy = labels[j].y - labels[i].y;
const needX = (wi + wj) / 2 + 8;
const needY = LABEL_H + 6;
if (Math.abs(dx) < needX && Math.abs(dy) < needY) {
const push = (needX - Math.abs(dx)) * 0.55 + 1;
const dir = dx === 0 ? (i % 2 === 0 ? -1 : 1) : Math.sign(dx);
labels[i].x -= dir * push;
labels[j].x += dir * push;
}
}
labels[i].x = Math.max(xMin, Math.min(xMax, labels[i].x));
labels[i].y = Math.max(yMin, Math.min(yMax, labels[i].y));
}
}
return labels;
}
export function shortenOrbitName(name: string): string {
return name.length > 16 ? `${name.slice(0, 14)}…` : name;
return placed;
}

View File

@@ -1,68 +1,105 @@
/** Face-on orrery: two even shells around the vault. */
import type { Orbit, Snapshot } from "../api";
export type Vec2 = { x: number; y: number };
type OrbitSlot = { radius: number; angle: number; speed: number };
export type ShellBody = {
index: number;
shell: "inner" | "outer";
radius: number;
baseAngle: number;
size: number;
warm: boolean;
};
export const SCENE_WIDTH = 660;
export const SCENE_HEIGHT = 480;
export const ORBIT_TILT = 0.68;
const INNER_R = 112;
const OUTER_R = 178;
const shells = [
{ radius: 128, count: 10, step: 3, speed: 0.047 },
{ radius: 190, count: 18, step: 7, speed: 0.033 },
{ radius: 260, count: 28, step: 11, speed: 0.023 },
];
/** Inner shell is fewer, larger, warmer. Outer shell is the rest. */
export function assignShells(count: number): ShellBody[] {
if (count <= 0) return [];
let innerCount = Math.max(1, Math.round(count * 0.38));
if (count > 4) innerCount = Math.max(3, innerCount);
innerCount = Math.min(innerCount, count);
const outerCount = count - innerCount;
const bodies: ShellBody[] = [];
for (let i = 0; i < innerCount; i++) {
bodies.push({
index: i,
shell: "inner",
radius: INNER_R,
baseAngle: -Math.PI / 2 + (i / innerCount) * Math.PI * 2,
size: 11,
warm: true,
});
const slots = Array.from({ length: 28 }, (_, index) =>
shells.flatMap(({ radius, count, step, speed }, shell) =>
index < count
? [{ radius, speed, angle: ((index * step) / count) * 2 * Math.PI - 1.2 + shell * 0.9 }]
: [],
),
).flat();
export const orbitRadii = shells.map(({ radius }) => radius);
export const orbitCapacity = slots.length;
export function assignOrbitSlots(
ids: string[],
previous = new Map<string, OrbitSlot>(),
selected?: string,
): Map<string, OrbitSlot> {
const currentIds = new Set(ids);
const next = new Map([...previous].filter(([id]) => currentIds.has(id)));
const occupied = new Set(next.values());
const free = slots.filter((slot) => !occupied.has(slot));
// shortcut: overview has a fixed number of slots, add level of detail if dense fleets need spatial navigation.
// Reserve the final slot for inspection when the overview is full.
const selectedSlot = slots[slots.length - 1];
for (const [id, slot] of next) {
if (slot === selectedSlot && id !== selected) next.delete(id);
}
for (let i = 0; i < outerCount; i++) {
bodies.push({
index: innerCount + i,
shell: "outer",
radius: OUTER_R,
baseAngle: -Math.PI / 2 + ((i + 0.5) / outerCount) * Math.PI * 2,
size: 6.4,
warm: false,
});
const available = free.filter((slot) => slot !== selectedSlot);
for (const id of ids) {
if (next.has(id)) continue;
const slot = available.shift();
if (!slot) break;
next.set(id, slot);
}
return bodies;
if (selected && currentIds.has(selected) && !next.has(selected)) {
next.set(selected, selectedSlot);
}
return next;
}
export function shellPose(body: ShellBody, time: number): Vec2 & { angle: number } {
const speed = body.shell === "inner" ? 0.18 : 0.11;
const angle = body.baseAngle + time * speed;
export function orbitPose(slot: OrbitSlot, time: number): Vec2 {
const angle = slot.angle + time * slot.speed;
return {
angle,
x: Math.cos(angle) * body.radius,
y: Math.sin(angle) * body.radius,
x: Math.cos(angle) * slot.radius,
y: Math.sin(angle) * slot.radius * ORBIT_TILT,
};
}
/** Point on a smooth inward spiral. u = 0 is the source; u = 1 is the shadow. */
export function spiralPoint(source: Vec2, u: number): Vec2 {
const clamped = Math.min(1, Math.max(0, u));
const a0 = Math.atan2(source.y, source.x);
const r0 = Math.hypot(source.x, source.y);
const fall = 1 - Math.pow(clamped, 0.92);
const spin = clamped * Math.PI * 1.05;
const r = r0 * fall;
const a = a0 + spin;
return { x: Math.cos(a) * r, y: Math.sin(a) * r };
export function moonPoses(star: Vec2, count: number, distance: number, obstacles: Vec2[] = []): Vec2[] {
const direction = Math.atan2(star.y, star.x);
const margin = distance / 2;
for (const offset of [0, Math.PI / 4, -Math.PI / 4, Math.PI / 2, -Math.PI / 2, Math.PI]) {
const positions = Array.from({ length: count }, (_, index) => {
const angle = direction + offset + (index - (count - 1) / 2) * Math.PI / 3;
return { x: star.x + Math.cos(angle) * distance, y: star.y + Math.sin(angle) * distance };
});
if (positions.every(({ x, y }) =>
Math.abs(x) < SCENE_WIDTH / 2 - margin && Math.abs(y) < SCENE_HEIGHT / 2 - margin &&
obstacles.every((point) => Math.hypot(point.x - x, point.y - y) >= 14),
)) return positions;
}
return [];
}
export function groupSnapshots(orbits: Orbit[], snapshots: Snapshot[]): Map<string, Snapshot[]> {
const ids = new Set(orbits.map(({ id }) => id));
const names = new Map<string, string | null>();
for (const { id, name } of orbits) {
names.set(name, names.has(name) ? null : id);
}
const groups = new Map<string, Snapshot[]>();
for (const snapshot of snapshots) {
const id = snapshot.orbit_id ?? names.get(snapshot.orbit_name);
if (!id || !ids.has(id)) continue;
const group = groups.get(id) ?? [];
group.push(snapshot);
groups.set(id, group);
}
for (const group of groups.values()) {
group.sort((a, b) => Date.parse(b.taken_at) - Date.parse(a.taken_at));
}
return groups;
}
export function spiralPoint(source: Vec2, progress: number): Vec2 {
const u = Math.min(1, Math.max(0, progress));
const angle = Math.atan2(source.y, source.x) + u * Math.PI * 1.15;
const radius = Math.hypot(source.x, source.y) * (1 - u ** 1.4);
return { x: Math.cos(angle) * radius, y: Math.sin(angle) * radius };
}