/** * Client-side mirrors of the server's wire protocol and state JSON * (PLAN.md section 3.5). Field names match the server's `toJson()` * exactly — the server is the source of truth. */ // --- Enums (the server serializes enums as their value index) ------------- /** 0 = chooseInitiative, 1 = playTurns. */ export type RoundState = 0 | 1; /** 0 = notDone, 1 = current, 2 = done. */ export type TurnsState = 0 | 1 | 2; /** MonsterType index: 0 normal, 1 elite, 2 boss, 3 summon. */ export type MonsterType = 0 | 1 | 2 | 3; export type Role = 'player' | 'spectator'; // --- State figures --------------------------------------------------------- /** The figure base shared by characters and monster standees. */ export interface FigureState { level: number; health: number; maxHealth: number; chill: number; /** Condition indices (see `Condition`). */ conditions: number[]; conditionsAddedThisTurn: number[]; conditionsAddedPreviousTurn: number[]; } export interface CharacterState extends FigureState { /** * Initiative value. `null` when the value is secret for this * connection (the server masks every non-owned character during * chooseInitiative — PLAN.md section 3.4). */ initiative: number | null; xp: number; display: string; /** This character's personal modifier (AMD) deck. */ modifierDeck: ModifierDeck; summonList: MonsterInstance[]; useFHPerks: boolean; perkList: boolean[]; } export interface MonsterInstance extends FigureState { standeeNr: number; move: number; attack: number; range: number; name: string; /** Artwork file name (resolved under /images/). */ gfx: string; roundSummoned: number; type: MonsterType; plague: number; } // --- Decks & cards (Phase 4) ----------------------------------------------- // // These mirror the server's deck `toJson()` shapes exactly (PLAN.md // section 3.1). The server is the source of truth; the fields below are the // wire format the client receives in `state`. /** * A modifier card. The server serialises only the graphic key; the card * kind (add / multiply / remove) and the effect label are derived from the * gfx on the client (see `modifierCardKind`). */ export interface ModifierCard { gfx: string; } /** * A modifier (AMD) deck: the shared global deck, the ally deck, and each * character's personal deck all have this shape. `revealed` is the count of * draw-pile cards the owner has flipped face-up (Cassandra's special). */ export interface ModifierDeck { addedMinusOnes: number; /** 0 = none, 1 = imbue level 1, 2 = imbue level 2. */ imbuement: number; badOmen: number; corrosiveSpew: boolean; revealed: number; cassandra: boolean; /** * Removable-card counter totals, keyed by gfx ("bless", "curse", * "in-enfeeble", "vi-empower", …) — the deck-header counters. */ removables: Record; /** Bottom → top. The last element is the next card to be drawn. */ drawPile: ModifierCard[]; removedPile: ModifierCard[]; discardPile: ModifierCard[]; } /** `LootType` index: 0 = materiel, 1 = other. */ export type LootType = 0 | 1; /** `LootBaseValue` index: 0 = one, 1 = oneIf4twoIfNot, 2 = oneIf3or4twoIfNot. */ export type LootBaseValue = 0 | 1 | 2; export interface LootCard { gfx: string; /** Owning character id ("" until drawn). */ owner: string; id: number; /** Enhancement amount added to the card's value. */ enhanced: number; baseValue: LootBaseValue; lootType: LootType; } /** * The scenario loot deck. `addedCards` (9 entries) counts how many extra * cards of each material type have been added; `enhancements` maps card id → * enhancement. The `1418` / `1419` booleans track the two special cards. */ /** The resource pool keys, in enhancement-menu order. */ export const LOOT_POOL_KEYS = [ 'hide', 'lumber', 'metal', 'arrowvine', 'axenut', 'corpsecap', 'flamefruit', 'rockroot', 'snowthistle', 'coin' ] as const; export type LootPoolKey = (typeof LOOT_POOL_KEYS)[number]; export interface LootDeck { drawPile: LootCard[]; discardPile: LootCard[]; addedCards: number[]; enhancements: Record; '1418': boolean; '1419': boolean; /** * The card pools, keyed by resource name, as stable card **ids** (in * pool order). The pool shares live instances with the draw/discard * piles, so only ids cross the wire; the enhancement level per id is in * the `enhancements` map. The enhancement menu renders one counter per * id. */ pools: Record; } /** The Crimson Scales sanctuary deck (two 16-card piles). */ export interface SanctuaryDeck { multPile: ModifierCard[]; flipPile: ModifierCard[]; } /** A positioned graphic on an ability card (the reference `GraphicPositional`). */ export interface GraphicPositional { gfx: string; x: number; y: number; scale: number; angle: number; } /** * The serialised form of a monster ability card — only `{nr, deck}`. The full * card (title, initiative, lines, graphics) is reconstructed on the client * from the deck name + nr against the edition's `monsterAbilities` data. */ export interface MonsterAbilityCardRef { nr: number; deck: string; } /** The full reconstructed monster ability card (data + serialised ref). */ export interface MonsterAbilityCard { title: string; nr: number; shuffle: boolean; initiative: number; lines: string[]; deck: string; graphics: GraphicPositional[]; } /** One monster's ability-card deck (a `currentAbilityDecks` entry). */ export interface MonsterAbilityState { /** The deck name (= the monster's `type.deck`). */ name: string; /** Bottom → top; the last element is the next card to be drawn. */ drawPile: MonsterAbilityCardRef[]; discardPile: MonsterAbilityCardRef[]; /** The total round number when the deck last auto-drew (0 = not yet). */ lastRoundDrawn: number; } // --- Current-list items ---------------------------------------------------- export interface CharacterItem { id: string; turnState: TurnsState; note: string; characterState: CharacterState; /** Character-class id (discriminator). */ characterClass: string; edition: string; } export interface MonsterItem { id: string; turnState: TurnsState; note: string; isActive: boolean; /** Monster model name (discriminator). */ type: string; monsterInstances: MonsterInstance[]; isAlly: boolean; v: number; level: number; } export interface NoteRowItem { id: string; /** Discriminator flag. */ noteRow: true; text: string; color: number; linkedId: string; standeeNr: number; } export type ListItem = CharacterItem | MonsterItem | NoteRowItem; export function isCharacter(item: ListItem): item is CharacterItem { return 'characterState' in item && 'characterClass' in item; } export function isMonster(item: ListItem): item is MonsterItem { return 'monsterInstances' in item && 'type' in item; } export function isNoteRow(item: ListItem): item is NoteRowItem { return (item as NoteRowItem).noteRow === true; } export interface SpecialRule { type: string; note: string; name: string; health: string; condition: string; init: number; level: number; startOfRound: boolean; list: unknown[]; } export interface GameState { level: number; solo: boolean; autoScenarioLevel: boolean; difficulty: number; roundState: RoundState; round: number; totalRounds: number; scenario: string; toastMessage: string; scenarioSpecialRules: SpecialRule[]; scenarioSectionsAdded: string[]; currentCampaign: string; currentList: ListItem[]; /** One entry per monster currently in the game. */ currentAbilityDecks: MonsterAbilityState[]; sanctuaryDeck: SanctuaryDeck; /** The shared (party) modifier deck. */ modifierDeck: ModifierDeck; /** The ally modifier deck (Gloomhaven 2nd Edition). */ modifierDeckAllies: ModifierDeck; lootDeck: LootDeck; unlockedClasses: string[]; showAllyDeck: boolean; allyDeckInOGGloom: boolean; /** element index → element-state index. */ elementState: Record; } // --- Server-side settings (Phase 6) ------------------------------------------ /** A character save entry (the server splits the save id for display). */ export interface CharacterSaveEntry { name: string; characterId: string; } /** * The server-side (game-relevant) settings payload — the server's * `Settings.forClient()`, embedded in `welcome` and sent as the `settings` * broadcast (PLAN.md section 4). The save *content* never crosses the wire; * only the names do. */ export interface SettingsPayload { noInit: boolean; noStandees: boolean; randomStandees: boolean; expireConditions: boolean; autoAddStandees: boolean; autoAddSpawns: boolean; hideLootDeck: boolean; /** The server's `Style` enum index (0 frosthaven, 1 gloomhaven, 2 original). */ style: number; showBattleGoalReminder: boolean; showReminders: boolean; showAmdDeck: boolean; fhHazTerrainCalcInOGGloom: boolean; /** The locale the command descriptions are generated in (server-side l10n). */ locale: string; /** The names of the available game-state saves. */ saves: string[]; /** The character saves (name + character id). */ characterSaves: CharacterSaveEntry[]; } // --- Messages -------------------------------------------------------------- export interface GameEvent { type: string; [ key: string ]: unknown; } export interface WelcomeMessage { t: 'welcome'; v: number; session: string; role: Role; index: number; description: string | null; /** The number of undoable actions in the history. */ historyLength: number; /** The action the redo would re-execute (null when exhausted). */ redoDescription: string | null; /** The action the undo would roll back (null at the baseline). */ undoDescription: string | null; event: GameEvent | null; state: GameState; /** character id → owning session id (null when unclaimed). */ seats: Record; settings: SettingsPayload; } export interface UpdateMessage { t: 'update'; index: number; description: string; /** The number of undoable actions in the history. */ historyLength: number; /** The action the redo would re-execute (null when exhausted). */ redoDescription: string | null; /** The action the undo would roll back (null at the baseline). */ undoDescription: string | null; event: GameEvent; state: GameState; } /** * The server-side settings broadcast (sent when a setting or the save list * changes). Same payload as `welcome.settings`. */ export interface SettingsMessage { t: 'settings'; settings: SettingsPayload; } export interface SeatsMessage { t: 'seats'; seats: Record; } export interface ErrorMessage { t: 'error'; code: string; message: string; } export interface PongMessage { t: 'pong'; } export type ServerMessage = | WelcomeMessage | UpdateMessage | SeatsMessage | SettingsMessage | ErrorMessage | PongMessage; // --- Client → server ------------------------------------------------------- export interface CommandArgs { [ key: string ]: unknown; } /** Outbound command message body (without the `t` field, added by the client). */ export function commandMessage(name: string, args: CommandArgs = {}) { return { t: 'cmd', name, args } as const; } export function claimMessage(characterId: string) { return { t: 'claim', characterId } as const; } export function helloMessage() { return { t: 'hello', v: 1 } as const; } export function pingMessage() { return { t: 'ping' } as const; } export const undoMessage = () => ({ t: 'undo' }) as const; export const redoMessage = () => ({ t: 'redo' }) as const; // --- Phase 6 client messages (players only on the server side) --------------- /** * Set one server-side (game-relevant) setting. [name] is one of the known * setting names; [value] its new value (booleans, the style enum index, or * the locale code string). */ export function setSettingMessage( name: string, value: boolean | number | string ) { return { t: 'setSetting', name, value } as const; } /** Save the current game state to a named save slot. */ export function saveMessage(name: string) { return { t: 'save', name } as const; } /** Load a named save (an undoable action on the server). */ export function loadSaveMessage(name: string) { return { t: 'loadSave', name } as const; } /** Delete a named save. */ export function deleteSaveMessage(name: string) { return { t: 'deleteSave', name } as const; } /** Save one character's state under [name]. */ export function saveCharacterMessage(name: string, characterId: string) { return { t: 'saveCharacter', name, characterId } as const; } /** Load a character save ([saveId] = `"$name\n$characterId"`). */ export function loadCharacterSaveMessage(saveId: string) { return { t: 'loadCharacterSave', saveId } as const; } /** Delete a character save. */ export function deleteCharacterSaveMessage(saveId: string) { return { t: 'deleteCharacterSave', saveId } as const; }