summaryrefslogtreecommitdiff
path: root/web/src/lib/game/line/buildAbility.ts
blob: 45c6e7a0173a9889c5aa3a0245c170312d74a75a (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
/**
 * The ability-card rendering pipeline (PORTING.md §8b): pre-translate the
 * card's lines with the in-game `t()`, run them through {@link parseLines}
 * (expand → convert → per-line stat application → layout), and return the
 * structured tree the Svelte renderer walks. Also builds the {@link
 * StatMonster} the stat applier needs from the monster model + live standees.
 */

import { gt } from '$lib/i18n/gameI18n';
import { parseLines } from './lineParser';
import type { MonsterAbilityCard, MonsterType } from '$lib/protocol/types';
import type { MonsterData } from '$lib/data/gameData';
import type { ColumnNode } from './types';
import type { StatBlock, StatLevel, StatMonster } from './statApplier';

export interface AbilityRenderOptions {
	/** Frosthaven style (icon-only tokens, sub-line boxes). */
	fh: boolean;
	/** Left-aligned boss stat card layout. */
	left?: boolean;
	/** Resolve stats per line (the reference's `!noCalculation`). */
	applyStats?: boolean;
	/** Force the elite/normal split (the reference's `calculateAll`). */
	applyAll?: boolean;
	scale?: number;
	/** The monster is a flyer (move token → flying graphic). */
	flying?: boolean;
}

type StatSource = {
	health?: number | string;
	move?: number | string;
	attack?: number | string;
	range?: number;
	attributes?: string[];
};

function toBlock(source: StatSource | undefined): StatBlock | undefined {
	if (!source) return undefined;
	return {
		health: source.health,
		move: source.move,
		attack: source.attack,
		range: source.range,
		attributes: source.attributes
	};
}

/**
 * The stat-applier view of a monster at [level], for the standees currently
 * in play. Built from the edition's level stats + the live standee types.
 */
export function statMonsterFor(
	monster: MonsterData,
	level: number,
	standeeTypes: MonsterType[],
	isActive: boolean
): StatMonster {
	const raw = monster.levels.find((l) => l.level === level) ?? monster.levels[0];
	const statLevel: StatLevel = {
		normal: toBlock(raw?.normal as StatSource | undefined),
		elite: toBlock(raw?.elite as StatSource | undefined),
		boss: toBlock(raw?.boss as StatSource | undefined)
	};
	return { level: statLevel, standeeTypes, isActive };
}

/** The full ability-card line tree (lines pre-translated, then parsed). */
export function buildAbilityCardTree(
	card: MonsterAbilityCard,
	stat: StatMonster | null,
	opts: AbilityRenderOptions
): ColumnNode {
	const lines = card.lines.map(gt);
	return parseLines(lines, {
		left: opts.left ?? false,
		applyStats: opts.applyStats ?? false,
		applyAll: opts.applyAll ?? false,
		align: opts.left ? 'left' : 'center',
		frosthavenStyle: opts.fh,
		scale: opts.scale ?? 1,
		monster: stat,
		flying: opts.flying ?? false,
		bossStatCard: opts.left ?? false,
		translate: gt
	});
}