f4fb69dbc7
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>
104 lines
2.2 KiB
TypeScript
104 lines
2.2 KiB
TypeScript
import type { DomainEvent } from "./events.js";
|
|
import {
|
|
type CombatantId,
|
|
type DomainError,
|
|
type Encounter,
|
|
findCombatant,
|
|
isDomainError,
|
|
} from "./types.js";
|
|
|
|
export interface AdjustHpSuccess {
|
|
readonly encounter: Encounter;
|
|
readonly events: DomainEvent[];
|
|
}
|
|
|
|
/**
|
|
* Pure function that adjusts a combatant's current HP by a delta.
|
|
*
|
|
* The result is clamped to [0, maxHp]. Requires the combatant to have
|
|
* HP tracking enabled (maxHp must be set).
|
|
*/
|
|
export function adjustHp(
|
|
encounter: Encounter,
|
|
combatantId: CombatantId,
|
|
delta: number,
|
|
): AdjustHpSuccess | DomainError {
|
|
const found = findCombatant(encounter, combatantId);
|
|
if (isDomainError(found)) return found;
|
|
const { combatant: target } = found;
|
|
|
|
if (target.maxHp === undefined || target.currentHp === undefined) {
|
|
return {
|
|
kind: "domain-error",
|
|
code: "no-hp-tracking",
|
|
message: `Combatant "${combatantId}" does not have HP tracking enabled`,
|
|
};
|
|
}
|
|
|
|
if (delta === 0) {
|
|
return {
|
|
kind: "domain-error",
|
|
code: "zero-delta",
|
|
message: "Delta must not be zero",
|
|
};
|
|
}
|
|
|
|
if (!Number.isInteger(delta)) {
|
|
return {
|
|
kind: "domain-error",
|
|
code: "invalid-delta",
|
|
message: `Delta must be an integer, got ${delta}`,
|
|
};
|
|
}
|
|
|
|
const previousHp = target.currentHp;
|
|
const previousTempHp = target.tempHp ?? 0;
|
|
let newTempHp = previousTempHp;
|
|
let effectiveDelta = delta;
|
|
|
|
if (delta < 0 && previousTempHp > 0) {
|
|
const absorbed = Math.min(previousTempHp, Math.abs(delta));
|
|
newTempHp = previousTempHp - absorbed;
|
|
effectiveDelta = delta + absorbed;
|
|
}
|
|
|
|
const newHp = Math.max(
|
|
0,
|
|
Math.min(target.maxHp, previousHp + effectiveDelta),
|
|
);
|
|
|
|
const events: DomainEvent[] = [];
|
|
|
|
if (newTempHp !== previousTempHp) {
|
|
events.push({
|
|
type: "TempHpSet",
|
|
combatantId,
|
|
previousTempHp: previousTempHp || undefined,
|
|
newTempHp: newTempHp || undefined,
|
|
});
|
|
}
|
|
|
|
if (newHp !== previousHp) {
|
|
events.push({
|
|
type: "CurrentHpAdjusted",
|
|
combatantId,
|
|
previousHp,
|
|
newHp,
|
|
delta,
|
|
});
|
|
}
|
|
|
|
return {
|
|
encounter: {
|
|
combatants: encounter.combatants.map((c) =>
|
|
c.id === combatantId
|
|
? { ...c, currentHp: newHp, tempHp: newTempHp || undefined }
|
|
: c,
|
|
),
|
|
activeIndex: encounter.activeIndex,
|
|
roundNumber: encounter.roundNumber,
|
|
},
|
|
events,
|
|
};
|
|
}
|