Files
galaxy-game/ui/frontend/src/lib/selection.svelte.ts
T
Ilia Denisov 676556db4e ui/phase-19: ship-group decoder + map binding + selection store
Wires Phase 19's data and rendering layers without yet adding the
inspector UI:

  - game-state.ts grows ReportLocalShipGroup / ReportOtherShipGroup
    / ReportIncomingShipGroup / ReportUnidentifiedShipGroup /
    ReportLocalFleet types and walks the matching FlatBuffers
    vectors (LocalGroup, OtherGroup, IncomingGroup,
    UnidentifiedGroup, LocalFleet) inside decodeReport. The Tech
    map is folded into the fixed-shape ShipGroupTech struct;
    cargo strings normalise to the closed CargoLoadType | "NONE"
    union; UUIDs come back as canonical 36-char strings.
  - synthetic-report.ts mirrors the new fields so the DEV-only
    lobby loader can feed JSON produced by legacy-report-to-json
    straight into the live UI surface.
  - selection.svelte.ts widens its discriminated union with a
    `kind: "shipGroup"` branch carrying a ShipGroupRef
    (local UUID / other / incoming / unidentified by index).
  - world.ts adds Style.strokeDashPx and render.ts.drawLine
    honours it via manual segmentation (PixiJS v8 has no native
    dash API). Ignored on points and circles.
  - state-binding.ts now returns { world, hitLookup }: the
    hit-lookup map keys every primitive id back to a concrete
    HitTarget so the click handler can dispatch to selectPlanet
    or selectShipGroup. Ship-group primitives live in a separate
    ship-groups.ts that emits one point per local / other /
    unidentified group, plus a dashed origin→destination line +
    clickable point per incoming group. Position is interpolated
    along the trajectory for in-hyperspace groups.
  - map.svelte threads the hitLookup into handleMapClick.

Vitest:
  - tests/helpers/empty-ship-groups.ts exposes EMPTY_SHIP_GROUPS
    so existing fixtures can spread the new five empty arrays
    without enumerating every field.
  - state-binding-groups.test.ts covers each group variant's
    primitive geometry and lookup correctness.
  - All previously-existing fixture builders pick up the spread
    so GameReport stays a complete object.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-10 13:23:56 +02:00

95 lines
3.3 KiB
TypeScript

// Per-game selection state: which on-map object the user is
// currently inspecting. Phase 13 modelled planets only; Phase 19
// widened the union to ship groups (own / foreign / incoming /
// unidentified).
//
// The store is in-memory only: lifetime matches the in-game shell
// layout instance, which itself is preserved across active-view
// switches inside `/games/:id/*`. Persisting selection across
// reloads is intentionally out of scope — the Phase 13 acceptance
// criterion calls out "across view switches", and survival across a
// reload would be a surprising contrast with the empty-state copy
// users see on first load.
//
// Like `GameStateStore` and `OrderDraftStore`, the store is
// instantiated by the layout and shared with descendants through
// Svelte context. The map view pushes selection events into it; the
// inspector tab and the mobile bottom-sheet read from it.
//
// The store deliberately carries no Svelte component imports so it
// can be tested directly without rendering any UI.
/**
* ShipGroupRef identifies a ship group inside the current report.
* `local` groups carry a stable engine UUID (passed through
* `report.localGroup.id` and used by the upcoming Phase 20 order
* envelopes). The remaining variants do not — they are addressed by
* their position in the matching report array, which is fine for
* the read-only inspector: a new report load reseeds the store and
* any stale index resolves to a missing entry on lookup, collapsing
* the inspector cleanly.
*/
export type ShipGroupRef =
| { variant: "local"; id: string }
| { variant: "other"; index: number }
| { variant: "incoming"; index: number }
| { variant: "unidentified"; index: number };
/**
* Selected describes the currently selected map object. The
* discriminated union is closed: every map-clickable surface maps
* to one of these variants. Future phases (e.g. fleet selection)
* extend by adding a new branch — extension is purely additive.
*/
export type Selected =
| { kind: "planet"; id: number }
| { kind: "shipGroup"; ref: ShipGroupRef };
/**
* SELECTION_CONTEXT_KEY is the Svelte context key the in-game shell
* layout uses to expose its `SelectionStore` instance to descendants.
* Map view, inspector tab, and the mobile bottom-sheet resolve the
* store via `getContext(SELECTION_CONTEXT_KEY)`.
*/
export const SELECTION_CONTEXT_KEY = Symbol("selection");
export class SelectionStore {
selected: Selected | null = $state(null);
private destroyed = false;
/**
* selectPlanet sets the active selection to the planet identified
* by its engine `number`. A no-op once the store has been disposed.
*/
selectPlanet(id: number): void {
if (this.destroyed) return;
this.selected = { kind: "planet", id };
}
/**
* selectShipGroup sets the active selection to a ship group. The
* `ref` discriminator carries the variant + the right id shape for
* lookup against the current report.
*/
selectShipGroup(ref: ShipGroupRef): void {
if (this.destroyed) return;
this.selected = { kind: "shipGroup", ref };
}
/**
* clear drops the current selection. The mobile sheet's close
* button calls this; otherwise selection persists across active-
* view switches.
*/
clear(): void {
if (this.destroyed) return;
this.selected = null;
}
dispose(): void {
this.destroyed = true;
this.selected = null;
}
}