summaryrefslogtreecommitdiff
path: root/web/src/lib/game/stats.ts
blob: b5c41eda9d2fef3eb0336966e28ebc6d218988bb (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
/**
 * Reads current stat values for the status menu from the live state.
 *
 * Health / max / xp / chill / conditions live on the figure; bless, curse,
 * empower and enfeeble live as modifier-deck cards — the server keeps their
 * counter in sync with the matching cards in the character's draw pile, so
 * counting the cards here mirrors the server's own computation.
 */

import type {
	CharacterItem,
	ModifierDeck,
	MonsterInstance
} from '$lib/protocol/types';

/**
 * The wire `figureId` for a standee (a monster instance or a summon): the
 * reference app builds it as `name + gfx + standeeNr`.
 */
export function standeeFigureId(standee: MonsterInstance): string {
	return standee.name + standee.gfx + standee.standeeNr.toString();
}

/** Count modifier cards matching [predicate] in the draw pile. */
function countCards(
	deck: ModifierDeck | undefined,
	predicate: (gfx: string) => boolean
): number {
	if (!deck?.drawPile) return 0;
	return deck.drawPile.filter((c) => c && predicate(c.gfx)).length;
}

/**
 * The stat counters for a character, keyed by the modifier-deck removable
 * name (which is also the `gfx` wire arg for empower/enfeeble).
 */
export function characterStatCounters(
	character: CharacterItem
): {
	bless: number;
	curse: number;
	empower: number;
	enfeeble: number;
} {
	const deck = character.characterState.modifierDeck;
	return {
		bless: countCards(deck, (g) => g === 'bless'),
		curse: countCards(deck, (g) => g === 'curse'),
		empower: countCards(deck, (g) => g.includes('empower')),
		enfeeble: countCards(deck, (g) => g.includes('enfeeble'))
	};
}

/** The stat counters for a standee (its owner's modifier deck). */
export function standeeStatCounters(
	deck: ModifierDeck | undefined
): {
	bless: number;
	curse: number;
	empower: number;
	enfeeble: number;
} {
	return {
		bless: countCards(deck, (g) => g === 'bless'),
		curse: countCards(deck, (g) => g === 'curse'),
		empower: countCards(deck, (g) => g.includes('empower')),
		enfeeble: countCards(deck, (g) => g.includes('enfeeble'))
	};
}