# CLAUDE.md This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. ## What this repository is This is a **greenfield project** to re-architect **X-haven Assistant** — a companion app for the Gloomhaven/Frosthaven board games — as a **server-driven web app**. The existing app is a Flutter client where each device runs its own copy of the game and syncs state over an ad-hoc local-network socket; that network layer is unreliable (frequent disconnects/desyncs). The goal is to move all game state onto a web server, so each player just opens the game page in a browser tab and the server is the single source of truth. The design intent lives in `README.md` at the repo root. The reference implementation (what we are re-architecting) lives in `FrosthavenAssistant/`. ## The reference app is READ-ONLY `FrosthavenAssistant/` is a checkout of the existing upstream app. **Do not modify anything in it.** It is also gitignored (see `.gitignore`), so the new web-app code in this repo is kept separate from it. Treat it purely as the source of truth for game logic, data files, and behavior to be reproduced. Reference layout: - `frosthaven_assistant/` — the Flutter app (client + embedded server). Entry point `lib/main.dart`. - `frosthaven_assistant_server/` — standalone Dart server library (the `GameServer` base class and wire protocol). Path dependency of the app. - `room-data-converter/` — one-off Dart tool for reshaping scenario room/monster JSON. - `docs/manual/` — user manual (HTML), incl. `09-multiplayer.html` for the networking model. ## Target architecture (the web app we are building) Key requirements from the root `README.md` — these differ from the existing app and should drive design: - **Server owns all state.** The web server runs the app and is the authoritative source of game state. Clients do **not** keep or sync their own state. - **Clients send input only.** A player's browser submits user actions to the server; it does not carry game state locally. - **Spectator clients.** Beyond the players, other clients may open the page just to view the current state (read-only observers). - **Per-player secret data.** Some data must be visible only on the owning player's browser until a game event reveals it. The canonical example: each player's initiative/"priority" input is shown only in their own tab until *all* players have entered and the draw-action is tapped. (The existing app already does this — see "secret initiative" below — so the web version needs an equivalent server-gated, per-connection visibility model.) ## Reference app architecture (source of truth for game logic) The Flutter app uses a **Command pattern + ValueNotifier** hybrid: - **All state mutations go through `Command` objects** in `frosthaven_assistant/lib/Resource/commands/` (77 files: `AddMonsterCommand`, `DrawCommand`, `ChangeHealthCommand`, etc.). Each implements `execute()`/`undo()`/`describe()`. Mutation methods are gated by a private `_StateModifier` capability token, so only commands can mutate state. Never mutate state directly. - **State is exposed as `ValueNotifier`s** (`ValueListenable` getters); the UI rebuilds via `ValueListenableBuilder`. `GameState` (in `lib/Resource/state/`) is the central holder, split across `part` files. - **`ActionHandler`** provides undo/redo (deep command history + `GameSaveState` snapshots) and is network-aware. - **Dependency injection via GetIt** (`getIt()`), set up in `lib/services/service_locator.dart` (`setupGetIt()`). Singletons: `GameData`, `Settings`, `GameState`, `Communication`, `Network`, `Connection`, `Client`. - **`GameData`** (repository pattern) loads campaign/monster/ability/scenario data from JSON assets under `frosthaven_assistant/assets/data/` (`editions/`, `rooms/`). Test fixtures mirror this in `assets/testData/`. Existing networking (what we are replacing) is in `lib/services/network/` plus the `frosthaven_assistant_server` package: - **Wire protocol:** messages framed as `S3nD:[EOM]` over a raw TCP socket (default port 4567). State updates travel in a JSON envelope `{i: index, d: description, e: event, s: state}` — a monotonically-increasing **command index** plus a full serialized state string, with mismatch recovery (a client that drifts is overwritten by server state). - **Secret initiative:** `InitiativeWidgetViewModel.isSecret` hides an initiative value on a connected device unless that device is the one that made the change (tracked by `CharacterWidgetInternal.localCharacterInitChanges`). This is the model to generalize into the server-gated per-player visibility for the web app. ## Commands (for the reference Flutter app) Run these from inside `FrosthavenAssistant/frosthaven_assistant/`: ```bash flutter run # run the app flutter doctor # verify Flutter toolchain flutter test # run all unit tests flutter test test/path/file.dart # run a single test file flutter analyze # static analysis ``` Unit tests depend on **mockito** codegen — generate mocks before running tests: ```bash dart run build_runner build # REQUIRED: generates .mocks.dart flutter test ``` If `build_runner` errors, try `dart pub upgrade` then re-run the build. The standalone server package is plain Dart (`dart run`/`dart test` from `FrosthavenAssistant/frosthaven_assistant_server/`); it is also the base for the Dockerized `ghcr.io/tarmslitaren/x-haven-server` (default port 4567).