Files
initiative/packages/domain/src/set-initiative.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

78 lines
1.9 KiB
TypeScript

import type { DomainEvent } from "./events.js";
import { sortByInitiative } from "./initiative-sort.js";
import {
type CombatantId,
type DomainError,
type Encounter,
findCombatant,
isDomainError,
} from "./types.js";
export interface SetInitiativeSuccess {
readonly encounter: Encounter;
readonly events: DomainEvent[];
}
/**
* Pure function that sets, changes, or clears a combatant's initiative value.
*
* After updating the value, combatants are stable-sorted:
* 1. Combatants with initiative — descending by value
* 2. Combatants without initiative — preserve relative order
*
* The active combatant's turn is preserved through the reorder
* by tracking identity (CombatantId) rather than position.
*
* roundNumber is never changed.
*/
export function setInitiative(
encounter: Encounter,
combatantId: CombatantId,
value: number | undefined,
): SetInitiativeSuccess | DomainError {
const found = findCombatant(encounter, combatantId);
if (isDomainError(found)) return found;
if (value !== undefined && !Number.isInteger(value)) {
return {
kind: "domain-error",
code: "invalid-initiative",
message: `Initiative must be an integer, got ${value}`,
};
}
const previousValue = found.combatant.initiative;
// Create new combatants array with updated initiative
const updated = encounter.combatants.map((c) =>
c.id === combatantId ? { ...c, initiative: value } : c,
);
// Record active combatant's id before reorder
const activeCombatantId =
encounter.combatants.length > 0
? encounter.combatants[encounter.activeIndex].id
: combatantId;
const { sorted, activeIndex: newActiveIndex } = sortByInitiative(
updated,
activeCombatantId,
);
return {
encounter: {
combatants: sorted,
activeIndex: newActiveIndex,
roundNumber: encounter.roundNumber,
},
events: [
{
type: "InitiativeSet",
combatantId,
previousValue,
newValue: value,
},
],
};
}