Files
galaxy-game/pkg/model/lobby/lobby.go
T
Ilia Denisov ce7a66b3e6 ui/phase-11: map wired to live game state
Replaces the Phase 10 map stub with live planet rendering driven by
`user.games.report`, and wires the header turn counter to the same
data. Phase 11's frontend sits on a per-game `GameStateStore` that
lives in `lib/game-state.svelte.ts`: the in-game shell layout
instantiates one per game, exposes it through Svelte context, and
disposes it on remount. The store discovers the game's current turn
through `lobby.my.games.list`, fetches the matching report, and
exposes a TS-friendly snapshot to the header turn counter, the map
view, and the inspector / order / calculator tabs that later phases
will plug onto the same instance.

The pipeline forced one cross-stage decision: the user surface needs
the current turn number to know which report to fetch, but
`GameSummary` did not expose it. Phase 11 extends the lobby
catalogue (FB schema, transcoder, Go model, backend
gameSummaryWire, gateway decoders, openapi, TS bindings,
api/lobby.ts) with `current_turn:int32`. The data was already
tracked in backend's `RuntimeSnapshot.CurrentTurn`; surfacing it is
a wire change only. Two alternatives were rejected: a brand-new
`user.games.state` message (full wire-flow for one field) and
hard-coding `turn=0` (works for the dev sandbox, which never
advances past zero, but renders the initial state for any real
game). The change crosses Phase 8's already-shipped catalogue per
the project's "decisions baked back into the live plan" rule —
existing tests and fixtures are updated in the same patch.

The state binding lives in `map/state-binding.ts::reportToWorld`:
one Point primitive per planet across all four kinds (local /
other / uninhabited / unidentified) with distinct fill colours,
fill alphas, and point radii so the user can tell them apart at a
glance. The planet engine number is reused as the primitive id so
a hit-test result resolves directly to a planet without an extra
lookup table. Zero-planet reports yield a well-formed empty world;
malformed dimensions fall back to 1×1 so a bad report cannot crash
the renderer.

The map view's mount effect creates the renderer once and skips
re-mount on no-op refreshes (same turn, same wrap mode); a turn
change or wrap-mode flip disposes and recreates it. The renderer's
external API does not yet expose `setWorld`; Phase 24 / 34 will
extract it once high-frequency updates land. The store installs a
`visibilitychange` listener that calls `refresh()` when the tab
regains focus.

Wrap-mode preference uses `Cache` namespace `game-prefs`, key
`<gameId>/wrap-mode`, default `torus`. Phase 11 reads through
`store.wrapMode`; Phase 29 wires the toggle UI on top of
`setWrapMode`.

Tests: Vitest unit coverage for `reportToWorld` (every kind,
ids, styling, empty / zero-dimension edges, priority order) and
for the store lifecycle (init success, missing-membership error,
forbidden-result error, `setTurn`, wrap-mode persistence across
instances, `failBootstrap`). Playwright e2e mocks the gateway for
`lobby.my.games.list` and `user.games.report` and asserts the
live data path: turn counter shows the reported turn,
`active-view-map` flips to `data-status="ready"`, and
`data-planet-count` matches the fixture count. The zero-planet
regression and the missing-membership error path are covered.

Phase 11 status stays `pending` in `ui/PLAN.md` until the local-ci
run lands green; flipping to `done` follows in the next commit per
the per-stage CI gate in `CLAUDE.md`.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-08 21:17:17 +02:00

241 lines
9.0 KiB
Go

// Package lobby defines the public typed command and response payloads
// exposed at the authenticated Gateway -> Game Lobby boundary.
package lobby
import "time"
const (
// MessageTypeMyGamesList is the authenticated gateway message type
// used to read the calling user's own games.
MessageTypeMyGamesList = "lobby.my.games.list"
// MessageTypePublicGamesList is the authenticated gateway message
// type used to read the paginated list of joinable public games.
MessageTypePublicGamesList = "lobby.public.games.list"
// MessageTypeMyApplicationsList is the authenticated gateway message
// type used to read the caller's pending and decided applications to
// public games.
MessageTypeMyApplicationsList = "lobby.my.applications.list"
// MessageTypeMyInvitesList is the authenticated gateway message type
// used to read the caller's pending invites to private games.
MessageTypeMyInvitesList = "lobby.my.invites.list"
// MessageTypeOpenEnrollment is the authenticated gateway message
// type used by the game owner to transition a draft game to
// `enrollment_open`.
MessageTypeOpenEnrollment = "lobby.game.open-enrollment"
// MessageTypeGameCreate is the authenticated gateway message type
// used to create a private game owned by the caller. Public games
// are created exclusively through the admin surface; the gateway
// rejects any attempt to set visibility to `public`.
MessageTypeGameCreate = "lobby.game.create"
// MessageTypeApplicationSubmit is the authenticated gateway message
// type used by a user to submit an application to a public game in
// `enrollment_open`.
MessageTypeApplicationSubmit = "lobby.application.submit"
// MessageTypeInviteRedeem is the authenticated gateway message type
// used to accept an outstanding invite and create the corresponding
// membership.
MessageTypeInviteRedeem = "lobby.invite.redeem"
// MessageTypeInviteDecline is the authenticated gateway message type
// used to terminally decline an outstanding invite. No membership is
// created.
MessageTypeInviteDecline = "lobby.invite.decline"
)
// MyGamesListRequest stores the authenticated read request for the
// caller's games. The request body is intentionally empty; gateway
// derives the calling user identity from the verified session.
type MyGamesListRequest struct{}
// MyGamesListResponse stores the list of games the caller participates
// in, ordered as Lobby returns them.
type MyGamesListResponse struct {
Items []GameSummary `json:"items"`
}
// GameSummary stores one game record returned by the various lobby
// list endpoints. `OwnerUserID` is empty for public games (no human
// owner). `CurrentTurn` carries the runtime's most recently observed
// turn number; the value is zero before the engine produces its first
// snapshot. The user surface uses it to fetch the corresponding
// `user.games.report` without an extra round-trip.
type GameSummary struct {
GameID string `json:"game_id"`
GameName string `json:"game_name"`
GameType string `json:"game_type"`
Status string `json:"status"`
OwnerUserID string `json:"owner_user_id"`
MinPlayers int `json:"min_players"`
MaxPlayers int `json:"max_players"`
EnrollmentEndsAt time.Time `json:"enrollment_ends_at"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
CurrentTurn int32 `json:"current_turn"`
}
// PublicGamesListRequest stores the paginated read request for joinable
// public games. Page numbers start at 1; page sizes are capped by the
// backend.
type PublicGamesListRequest struct {
Page int `json:"page"`
PageSize int `json:"page_size"`
}
// PublicGamesListResponse stores one page of public games together with
// the pagination metadata returned by the backend.
type PublicGamesListResponse struct {
Items []GameSummary `json:"items"`
Page int `json:"page"`
PageSize int `json:"page_size"`
Total int `json:"total"`
}
// MyApplicationsListRequest stores the authenticated read request for
// the caller's applications. Empty body — gateway derives identity from
// the authenticated session.
type MyApplicationsListRequest struct{}
// MyApplicationsListResponse stores the caller's application list,
// ordered as Lobby returns it.
type MyApplicationsListResponse struct {
Items []ApplicationSummary `json:"items"`
}
// ApplicationSummary stores a single application record returned by
// `lobby.my.applications.list` and `lobby.application.submit`.
// `DecidedAt` is nil while the application is pending.
type ApplicationSummary struct {
ApplicationID string `json:"application_id"`
GameID string `json:"game_id"`
ApplicantUserID string `json:"applicant_user_id"`
RaceName string `json:"race_name"`
Status string `json:"status"`
CreatedAt time.Time `json:"created_at"`
DecidedAt *time.Time `json:"decided_at,omitempty"`
}
// MyInvitesListRequest stores the authenticated read request for the
// caller's invites. Empty body.
type MyInvitesListRequest struct{}
// MyInvitesListResponse stores the caller's invite list.
type MyInvitesListResponse struct {
Items []InviteSummary `json:"items"`
}
// InviteSummary stores one invite record returned by
// `lobby.my.invites.list`, `lobby.invite.redeem`, and
// `lobby.invite.decline`. `InvitedUserID` is empty for code-based
// invites; `Code` is empty for user-bound invites; `DecidedAt` is nil
// while the invite is still pending.
type InviteSummary struct {
InviteID string `json:"invite_id"`
GameID string `json:"game_id"`
InviterUserID string `json:"inviter_user_id"`
InvitedUserID string `json:"invited_user_id,omitempty"`
Code string `json:"code,omitempty"`
RaceName string `json:"race_name"`
Status string `json:"status"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`
DecidedAt *time.Time `json:"decided_at,omitempty"`
}
// OpenEnrollmentRequest stores the owner-only command that transitions
// a game from `draft` to `enrollment_open`.
type OpenEnrollmentRequest struct {
// GameID identifies the game whose enrollment the caller wants to
// open. The owner check is enforced by Lobby.
GameID string `json:"game_id"`
}
// OpenEnrollmentResponse stores the resulting game record after a
// successful open-enrollment transition.
type OpenEnrollmentResponse struct {
GameID string `json:"game_id"`
Status string `json:"status"`
}
// GameCreateRequest stores the create-game command issued by the user
// surface. Visibility is always `private` here; the gateway rejects any
// other value before forwarding to backend, mirroring the FUNCTIONAL
// §3.3 invariant.
type GameCreateRequest struct {
GameName string `json:"game_name"`
Description string `json:"description"`
MinPlayers int `json:"min_players"`
MaxPlayers int `json:"max_players"`
StartGapHours int `json:"start_gap_hours"`
StartGapPlayers int `json:"start_gap_players"`
EnrollmentEndsAt time.Time `json:"enrollment_ends_at"`
TurnSchedule string `json:"turn_schedule"`
TargetEngineVersion string `json:"target_engine_version"`
}
// GameCreateResponse stores the freshly created game projected onto the
// shared GameSummary shape — the gateway does not expose runtime fields
// at create time because the game is always in `draft` until the owner
// transitions it.
type GameCreateResponse struct {
Game GameSummary `json:"game"`
}
// ApplicationSubmitRequest stores the submit-application command. The
// game must be public and in `enrollment_open`; backend rejects all
// other states.
type ApplicationSubmitRequest struct {
GameID string `json:"game_id"`
RaceName string `json:"race_name"`
}
// ApplicationSubmitResponse wraps the application created by a
// successful submit-application call.
type ApplicationSubmitResponse struct {
Application ApplicationSummary `json:"application"`
}
// InviteRedeemRequest accepts a pending invite and creates the
// corresponding membership at the backend.
type InviteRedeemRequest struct {
GameID string `json:"game_id"`
InviteID string `json:"invite_id"`
}
// InviteRedeemResponse wraps the invite record updated by a successful
// redeem (status transitions to `accepted`).
type InviteRedeemResponse struct {
Invite InviteSummary `json:"invite"`
}
// InviteDeclineRequest terminally declines a pending invite. No
// membership is created.
type InviteDeclineRequest struct {
GameID string `json:"game_id"`
InviteID string `json:"invite_id"`
}
// InviteDeclineResponse wraps the invite record updated by a successful
// decline (status transitions to `declined`).
type InviteDeclineResponse struct {
Invite InviteSummary `json:"invite"`
}
// ErrorBody stores the canonical Lobby error envelope code/message
// pair.
type ErrorBody struct {
Code string `json:"code"`
Message string `json:"message"`
}
// ErrorResponse wraps ErrorBody for the FlatBuffers payload boundary.
type ErrorResponse struct {
Error ErrorBody `json:"error"`
}