summaryrefslogtreecommitdiff
path: root/web/src/lib/protocol/types.ts
blob: 6e78d0204b4d99c3e7228aa2f5c8e701f38d9eba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/**
 * 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<string, number>;
	/** 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<string, number>;
	'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<LootPoolKey, number[]>;
}

/** 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<string, number>;
}

// --- 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<string, string | null>;
	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<string, string | null>;
}

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;
}