Files
initiative/packages/domain/src/set-hp.ts
Lukas f4fb69dbc7
All checks were successful
CI / check (push) Successful in 1m13s
CI / build-image (push) Has been skipped
Add jsinspect-plus structural duplication gate, extract shared helpers
Add jsinspect-plus (AST-based structural duplication detector) to pnpm
check with threshold 50 / min 3 instances. Fix all findings:

- Extract condition icon/color maps to shared condition-styles.ts
- Extract useClickOutside hook (5 components)
- Extract dispatchAction + resolveAndRename in use-encounter
- Extract runEncounterAction in application layer (13 use cases)
- Extract findCombatant helper in domain (9 functions)
- Extract TraitSection in stat-block (4 trait rendering blocks)
- Extract DialogHeader in dialog.tsx (4 dialogs)

Net result: -263 lines across 40 files.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-28 02:16:54 +01:00

90 lines
2.1 KiB
TypeScript

import type { DomainEvent } from "./events.js";
import {
type CombatantId,
type DomainError,
type Encounter,
findCombatant,
isDomainError,
} from "./types.js";
export interface SetHpSuccess {
readonly encounter: Encounter;
readonly events: DomainEvent[];
}
/**
* Pure function that sets, updates, or clears a combatant's max HP.
*
* - Setting maxHp initializes currentHp to maxHp (full health).
* - Updating maxHp clamps currentHp to the new maxHp if needed.
* - Clearing maxHp (undefined) also clears currentHp.
*/
export function setHp(
encounter: Encounter,
combatantId: CombatantId,
maxHp: number | undefined,
): SetHpSuccess | DomainError {
const found = findCombatant(encounter, combatantId);
if (isDomainError(found)) return found;
if (maxHp !== undefined && (!Number.isInteger(maxHp) || maxHp < 1)) {
return {
kind: "domain-error",
code: "invalid-max-hp",
message: `Max HP must be a positive integer, got ${maxHp}`,
};
}
const previousMaxHp = found.combatant.maxHp;
const previousCurrentHp = found.combatant.currentHp;
let newMaxHp: number | undefined;
let newCurrentHp: number | undefined;
if (maxHp === undefined) {
newMaxHp = undefined;
newCurrentHp = undefined;
} else if (previousMaxHp === undefined) {
// First time setting HP — full health
newMaxHp = maxHp;
newCurrentHp = maxHp;
} else {
// Updating existing maxHp
newMaxHp = maxHp;
if (previousCurrentHp === previousMaxHp) {
// At full health — stay at full health
newCurrentHp = maxHp;
} else {
// Clamp currentHp to new max
newCurrentHp = Math.min(previousCurrentHp ?? maxHp, maxHp);
}
}
return {
encounter: {
combatants: encounter.combatants.map((c) =>
c.id === combatantId
? {
...c,
maxHp: newMaxHp,
currentHp: newCurrentHp,
tempHp: newMaxHp === undefined ? undefined : c.tempHp,
}
: c,
),
activeIndex: encounter.activeIndex,
roundNumber: encounter.roundNumber,
},
events: [
{
type: "MaxHpSet",
combatantId,
previousMaxHp,
newMaxHp,
previousCurrentHp,
newCurrentHp,
},
],
};
}