Files
initiative/packages/domain/src/remove-combatant.ts
T
Lukas f4fb69dbc7
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

65 lines
1.6 KiB
TypeScript

import type { DomainEvent } from "./events.js";
import {
type CombatantId,
type DomainError,
type Encounter,
findCombatant,
isDomainError,
} from "./types.js";
export interface RemoveCombatantSuccess {
readonly encounter: Encounter;
readonly events: DomainEvent[];
}
/**
* Pure function that removes a combatant from an encounter by ID.
*
* Adjusts activeIndex to preserve turn integrity:
* - Removed after active → unchanged
* - Removed before active → decrement
* - Removed is active, mid-list → same index (next slides in)
* - Removed is active, at end → wrap to 0
* - Only combatant removed → 0
*
* roundNumber is never changed.
*/
export function removeCombatant(
encounter: Encounter,
id: CombatantId,
): RemoveCombatantSuccess | DomainError {
const found = findCombatant(encounter, id);
if (isDomainError(found)) return found;
const { index: removedIdx, combatant: removed } = found;
const newCombatants = encounter.combatants.filter((_, i) => i !== removedIdx);
let newActiveIndex: number;
if (newCombatants.length === 0) {
newActiveIndex = 0;
} else if (removedIdx < encounter.activeIndex) {
newActiveIndex = encounter.activeIndex - 1;
} else if (removedIdx > encounter.activeIndex) {
newActiveIndex = encounter.activeIndex;
} else {
// removedIdx === activeIndex
newActiveIndex =
removedIdx >= newCombatants.length ? 0 : encounter.activeIndex;
}
return {
encounter: {
combatants: newCombatants,
activeIndex: newActiveIndex,
roundNumber: encounter.roundNumber,
},
events: [
{
type: "CombatantRemoved",
combatantId: removed.id,
name: removed.name,
},
],
};
}