summaryrefslogtreecommitdiff
path: root/web/src/lib/game/rules.ts
blob: dad59d6bcacdcae37c89a26803684dc9d08c2e90 (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
/**
 * Game-rule helpers the deck UI needs (Phase 4 §6): the port of the reference
 * `GameMethods` predicates that gate ability-card display and styling. The
 * server owns the rules; these are pure read-only views of the state the
 * client already receives.
 */

import type {
	GameState,
	ListItem,
	ModifierDeck
} from '$lib/protocol/types';
import { isCharacter, isMonster } from '$lib/protocol/types';

const MAX_OG_SOLO_SCENARIO = 100;

/** The editions whose cards always render in the Frosthaven (icon) style. */
export function isFrosthavenStyledEdition(
	edition: string,
	scenario: string
): boolean {
	if (edition === 'Solo') {
		if (scenario.includes('#19 ')) return false; // Forgotten Circles
		// #100+ are the OG solo scenarios.
		for (let i = 1; i <= MAX_OG_SOLO_SCENARIO; i++) {
			if (scenario.includes(`#${i} `)) return true;
		}
		return false;
	}
	return (
		edition === 'Frosthaven' ||
		edition === 'Buttons and Bugs' ||
		edition === 'Gloomhaven 2nd Edition' ||
		edition === 'Skulls in the Snow' ||
		edition === 'Mercenary Packs'
	);
}

/**
 * Whether a monster's ability cards render in the Frosthaven style (the
 * reference `GameMethods.isFrosthavenStyle`). `styleTheme` is the client's
 * 'frosthaven' | 'gloomhaven' | 'original' setting.
 */
export function isFrosthavenStyleForMonster(
	monsterEdition: string,
	state: GameState,
	styleTheme: 'frosthaven' | 'gloomhaven' | 'original'
): boolean {
	const monsterFh = isFrosthavenStyledEdition(
		monsterEdition,
		state.scenario
	);
	if (monsterFh) return true;
	if (styleTheme !== 'frosthaven' && !monsterFh) return false;
	return (
		styleTheme === 'frosthaven' ||
		(styleTheme === 'original' &&
			isFrosthavenStyledEdition(state.currentCampaign, state.scenario))
	);
}

/**
 * Whether a monster is inactive for the current round under the scenario's
 * "inactive monster" special rule (the reference `isInactiveForRule`).
 */
export function isInactiveForRule(
	state: GameState,
	monsterId: string
): boolean {
	const rule = state.scenarioSpecialRules.find(
		(r) => r.type === 'InactiveMonster' && r.name === monsterId
	);
	if (rule != null && rule.list.includes(state.round)) return true;
	return false;
}

/** The campaign is one of the custom (Crimson Scales / Trail of Ashes / CCUG). */
export function isCustomCampaign(campaign: string): boolean {
	return (
		campaign === 'Crimson Scales' ||
		campaign === 'Trail of Ashes' ||
		campaign === 'CCUG'
	);
}

/**
 * The campaign is Crimson Scales or Trail of Ashes (the reference
 * `isCSCampaign` — CCUG has no sanctuary/party-card handling).
 */
export function isCSCampaign(campaign: string): boolean {
	return campaign === 'Crimson Scales' || campaign === 'Trail of Ashes';
}

/** The character items in the current list. */
export function charactersIn(list: ListItem[]): ListItem[] {
	return list.filter((item) => isCharacter(item));
}

/** The party size (character count) — loot values depend on it. */
export function characterCount(state: GameState): number {
	return charactersIn(state.currentList).length;
}

/** A character id → its class name, for deck `name` resolution. */
export function characterClassOf(
	state: GameState,
	id: string
): string | null {
	const item = state.currentList.find(
		(i) => isCharacter(i) && i.id === id
	);
	return item && isCharacter(item) ? item.characterClass : null;
}

/** The ally-deck monster id, when the ally deck is in play. */
export function allyDeckOwnerId(state: GameState): string | null {
	const item = state.currentList.find((i) => isMonster(i) && i.isAlly);
	return item && isMonster(item) ? item.id : null;
}

/** The id of the character whose turn it is (turnState === current). */
export function currentCharacterId(state: GameState): string | null {
	const item = state.currentList.find(
		(i) => isCharacter(i) && i.turnState === 1
	);
	return item && isCharacter(item) ? item.id : null;
}

/** A figure (character or monster) by id — the reference `getFigure`. */
export function figureById(
	state: GameState,
	id: string
): ListItem | null {
	return state.currentList.find((i) => i.id === id) ?? null;
}

/**
 * The modifier deck for a deck [name] — "" is the party (monster) deck,
 * "allies" the ally deck, a character id its personal deck (the reference
 * `GameMethods.getModifierDeck`).
 */
export function modifierDeckByName(
	state: GameState,
	name: string
): ModifierDeck {
	if (name === 'allies') return state.modifierDeckAllies;
	if (name === '') return state.modifierDeck;
	const item = state.currentList.find((i) => isCharacter(i) && i.id === name);
	return item && isCharacter(item)
		? item.characterState.modifierDeck
		: state.modifierDeck;
}

/** Whether a character of [className] is in the game (reference `getFigure`). */
export function hasCharacterClass(
	state: GameState,
	className: string
): boolean {
	return state.currentList.some(
		(i) => isCharacter(i) && i.characterClass === className
	);
}

/** The campaign is an OG-Gloomhaven (non-Frosthaven-styled) edition. */
export function isOgGloomEdition(state: GameState): boolean {
	return !isFrosthavenStyledEdition(state.currentCampaign, state.scenario);
}

/**
 * Whether the ally deck should be shown (the reference
 * `GameMethods.shouldShowAlliesDeck`; the web settings always show the AMD
 * deck, so the `showAmdDeck` gate is dropped).
 */
export function shouldShowAlliesDeck(state: GameState): boolean {
	if (state.showAllyDeck) return true;
	if (!state.allyDeckInOGGloom && isOgGloomEdition(state)) return false;
	return state.currentList.some((i) => isMonster(i) && i.isAlly);
}