Implement the 016-combatant-ac feature that adds an optional Armor Class field to combatants with shield icon display and inline editing in the encounter tracker
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
56
packages/domain/src/set-ac.ts
Normal file
56
packages/domain/src/set-ac.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import type { DomainEvent } from "./events.js";
|
||||
import type { CombatantId, DomainError, Encounter } from "./types.js";
|
||||
|
||||
export interface SetAcSuccess {
|
||||
readonly encounter: Encounter;
|
||||
readonly events: DomainEvent[];
|
||||
}
|
||||
|
||||
export function setAc(
|
||||
encounter: Encounter,
|
||||
combatantId: CombatantId,
|
||||
value: number | undefined,
|
||||
): SetAcSuccess | DomainError {
|
||||
const targetIdx = encounter.combatants.findIndex((c) => c.id === combatantId);
|
||||
|
||||
if (targetIdx === -1) {
|
||||
return {
|
||||
kind: "domain-error",
|
||||
code: "combatant-not-found",
|
||||
message: `No combatant found with ID "${combatantId}"`,
|
||||
};
|
||||
}
|
||||
|
||||
if (value !== undefined) {
|
||||
if (!Number.isInteger(value) || value < 0) {
|
||||
return {
|
||||
kind: "domain-error",
|
||||
code: "invalid-ac",
|
||||
message: `AC must be a non-negative integer, got ${value}`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const target = encounter.combatants[targetIdx];
|
||||
const previousAc = target.ac;
|
||||
|
||||
const updatedCombatants = encounter.combatants.map((c) =>
|
||||
c.id === combatantId ? { ...c, ac: value } : c,
|
||||
);
|
||||
|
||||
return {
|
||||
encounter: {
|
||||
combatants: updatedCombatants,
|
||||
activeIndex: encounter.activeIndex,
|
||||
roundNumber: encounter.roundNumber,
|
||||
},
|
||||
events: [
|
||||
{
|
||||
type: "AcSet",
|
||||
combatantId,
|
||||
previousAc,
|
||||
newAc: value,
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user