ef76b9c90b
Live 3-bar difficulty indicator in the top bar showing encounter difficulty (Trivial/Low/Moderate/High) based on the 2024 5.5e XP budget system. Automatically derived from PC levels and bestiary creature CRs. - Add optional level field (1-20) to PlayerCharacter - Add CR-to-XP and XP Budget per Character lookup tables in domain - Add calculateEncounterDifficulty pure function - Add DifficultyIndicator component with color-coded bars and tooltip - Add useDifficulty hook composing encounter, PC, and bestiary contexts - Indicator hidden when no PCs with levels or no bestiary-linked monsters - Level field in PC create/edit forms, persisted in storage Closes #18 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
161 lines
3.6 KiB
TypeScript
161 lines
3.6 KiB
TypeScript
import type { DomainEvent } from "./events.js";
|
|
import type {
|
|
PlayerCharacter,
|
|
PlayerCharacterId,
|
|
} from "./player-character-types.js";
|
|
import {
|
|
VALID_PLAYER_COLORS,
|
|
VALID_PLAYER_ICONS,
|
|
} from "./player-character-types.js";
|
|
import type { DomainError } from "./types.js";
|
|
|
|
export interface EditPlayerCharacterSuccess {
|
|
readonly characters: readonly PlayerCharacter[];
|
|
readonly events: DomainEvent[];
|
|
}
|
|
|
|
interface EditFields {
|
|
readonly name?: string;
|
|
readonly ac?: number;
|
|
readonly maxHp?: number;
|
|
readonly color?: string | null;
|
|
readonly icon?: string | null;
|
|
readonly level?: number | null;
|
|
}
|
|
|
|
function validateFields(fields: EditFields): DomainError | null {
|
|
if (fields.name?.trim() === "") {
|
|
return {
|
|
kind: "domain-error",
|
|
code: "invalid-name",
|
|
message: "Player character name must not be empty",
|
|
};
|
|
}
|
|
if (
|
|
fields.ac !== undefined &&
|
|
(!Number.isInteger(fields.ac) || fields.ac < 0)
|
|
) {
|
|
return {
|
|
kind: "domain-error",
|
|
code: "invalid-ac",
|
|
message: "AC must be a non-negative integer",
|
|
};
|
|
}
|
|
if (
|
|
fields.maxHp !== undefined &&
|
|
(!Number.isInteger(fields.maxHp) || fields.maxHp < 1)
|
|
) {
|
|
return {
|
|
kind: "domain-error",
|
|
code: "invalid-max-hp",
|
|
message: "Max HP must be a positive integer",
|
|
};
|
|
}
|
|
if (
|
|
fields.color !== undefined &&
|
|
fields.color !== null &&
|
|
!VALID_PLAYER_COLORS.has(fields.color)
|
|
) {
|
|
return {
|
|
kind: "domain-error",
|
|
code: "invalid-color",
|
|
message: `Invalid color: ${fields.color}`,
|
|
};
|
|
}
|
|
if (
|
|
fields.icon !== undefined &&
|
|
fields.icon !== null &&
|
|
!VALID_PLAYER_ICONS.has(fields.icon)
|
|
) {
|
|
return {
|
|
kind: "domain-error",
|
|
code: "invalid-icon",
|
|
message: `Invalid icon: ${fields.icon}`,
|
|
};
|
|
}
|
|
if (
|
|
fields.level !== undefined &&
|
|
fields.level !== null &&
|
|
(!Number.isInteger(fields.level) || fields.level < 1 || fields.level > 20)
|
|
) {
|
|
return {
|
|
kind: "domain-error",
|
|
code: "invalid-level",
|
|
message: "Level must be an integer between 1 and 20",
|
|
};
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function applyFields(
|
|
existing: PlayerCharacter,
|
|
fields: EditFields,
|
|
): PlayerCharacter {
|
|
return {
|
|
id: existing.id,
|
|
name: fields.name?.trim() ?? existing.name,
|
|
ac: fields.ac ?? existing.ac,
|
|
maxHp: fields.maxHp ?? existing.maxHp,
|
|
color:
|
|
fields.color === undefined
|
|
? existing.color
|
|
: ((fields.color as PlayerCharacter["color"]) ?? undefined),
|
|
icon:
|
|
fields.icon === undefined
|
|
? existing.icon
|
|
: ((fields.icon as PlayerCharacter["icon"]) ?? undefined),
|
|
level:
|
|
fields.level === undefined ? existing.level : (fields.level ?? undefined),
|
|
};
|
|
}
|
|
|
|
export function editPlayerCharacter(
|
|
characters: readonly PlayerCharacter[],
|
|
id: PlayerCharacterId,
|
|
fields: EditFields,
|
|
): EditPlayerCharacterSuccess | DomainError {
|
|
const index = characters.findIndex((c) => c.id === id);
|
|
if (index === -1) {
|
|
return {
|
|
kind: "domain-error",
|
|
code: "player-character-not-found",
|
|
message: `Player character not found: ${id}`,
|
|
};
|
|
}
|
|
|
|
const validationError = validateFields(fields);
|
|
if (validationError) return validationError;
|
|
|
|
const existing = characters[index];
|
|
const updated = applyFields(existing, fields);
|
|
|
|
if (
|
|
updated.name === existing.name &&
|
|
updated.ac === existing.ac &&
|
|
updated.maxHp === existing.maxHp &&
|
|
updated.color === existing.color &&
|
|
updated.icon === existing.icon &&
|
|
updated.level === existing.level
|
|
) {
|
|
return {
|
|
kind: "domain-error",
|
|
code: "no-changes",
|
|
message: "No fields changed",
|
|
};
|
|
}
|
|
|
|
const newList = characters.map((c, i) => (i === index ? updated : c));
|
|
|
|
return {
|
|
characters: newList,
|
|
events: [
|
|
{
|
|
type: "PlayerCharacterUpdated",
|
|
playerCharacterId: id,
|
|
oldName: existing.name,
|
|
newName: updated.name,
|
|
},
|
|
],
|
|
};
|
|
}
|