Add manual CR assignment and difficulty breakdown panel
All checks were successful
CI / check (push) Successful in 2m20s
CI / build-image (push) Successful in 17s

Implement issue #21: custom combatants can now have a challenge rating
assigned via a new breakdown panel, opened by tapping the difficulty
indicator. Bestiary-linked combatants show read-only CR with source name;
custom combatants get a CR picker with all standard 5e values. CR persists
across reloads and round-trips through JSON export/import.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Lukas
2026-04-02 17:03:33 +02:00
parent 2c643cc98b
commit 1ae9e12cff
26 changed files with 1461 additions and 17 deletions

View File

@@ -0,0 +1,246 @@
// @vitest-environment jsdom
import type { Creature, CreatureId, PlayerCharacter } from "@initiative/domain";
import { combatantId, creatureId, playerCharacterId } from "@initiative/domain";
import { renderHook, waitFor } from "@testing-library/react";
import type { ReactNode } from "react";
import { beforeAll, describe, expect, it, vi } from "vitest";
import { createTestAdapters } from "../../__tests__/adapters/in-memory-adapters.js";
import {
buildCombatant,
buildCreature,
buildEncounter,
} from "../../__tests__/factories/index.js";
import { AllProviders } from "../../__tests__/test-providers.js";
import { useDifficultyBreakdown } from "../use-difficulty-breakdown.js";
beforeAll(() => {
Object.defineProperty(globalThis, "matchMedia", {
writable: true,
value: vi.fn().mockImplementation((query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
});
const pcId1 = playerCharacterId("pc-1");
const goblinCreature = buildCreature({
id: creatureId("srd:goblin"),
name: "Goblin",
cr: "1/4",
source: "srd",
sourceDisplayName: "SRD",
});
function makeWrapper(options: {
encounter: ReturnType<typeof buildEncounter>;
playerCharacters?: PlayerCharacter[];
creatures?: Map<CreatureId, Creature>;
}) {
const adapters = createTestAdapters({
encounter: options.encounter,
playerCharacters: options.playerCharacters ?? [],
creatures: options.creatures,
});
return ({ children }: { children: ReactNode }) => (
<AllProviders adapters={adapters}>{children}</AllProviders>
);
}
describe("useDifficultyBreakdown", () => {
it("returns null when no leveled PCs", () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c-1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c-2"),
name: "Goblin",
creatureId: goblinCreature.id,
}),
],
}),
playerCharacters: [{ id: pcId1, name: "Hero", ac: 15, maxHp: 30 }],
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
const { result } = renderHook(() => useDifficultyBreakdown(), {
wrapper,
});
expect(result.current).toBeNull();
});
it("returns null when no monsters with CR", () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c-1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c-2"),
name: "Custom",
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 5 },
],
});
const { result } = renderHook(() => useDifficultyBreakdown(), {
wrapper,
});
expect(result.current).toBeNull();
});
it("returns per-combatant entries with correct data", async () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c-1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c-2"),
name: "Goblin",
creatureId: goblinCreature.id,
}),
buildCombatant({
id: combatantId("c-3"),
name: "Custom Thug",
cr: "2",
}),
buildCombatant({
id: combatantId("c-4"),
name: "Bandit",
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 5 },
],
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
const { result } = renderHook(() => useDifficultyBreakdown(), {
wrapper,
});
await waitFor(() => {
const breakdown = result.current;
expect(breakdown).not.toBeNull();
expect(breakdown?.pcCount).toBe(1);
// CR 1/4 = 50 + CR 2 = 450 → total 500
expect(breakdown?.totalMonsterXp).toBe(500);
expect(breakdown?.combatants).toHaveLength(3);
// Bestiary combatant
const goblin = breakdown?.combatants[0];
expect(goblin?.cr).toBe("1/4");
expect(goblin?.xp).toBe(50);
expect(goblin?.source).toBe("SRD");
expect(goblin?.editable).toBe(false);
// Custom with CR
const thug = breakdown?.combatants[1];
expect(thug?.cr).toBe("2");
expect(thug?.xp).toBe(450);
expect(thug?.source).toBeNull();
expect(thug?.editable).toBe(true);
// Custom without CR
const bandit = breakdown?.combatants[2];
expect(bandit?.cr).toBeNull();
expect(bandit?.xp).toBeNull();
expect(bandit?.source).toBeNull();
expect(bandit?.editable).toBe(true);
});
});
it("bestiary combatant with missing creature is non-editable with null CR", () => {
const missingCreatureId = creatureId("creature-missing");
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c-1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c-2"),
name: "Ghost",
creatureId: missingCreatureId,
}),
buildCombatant({
id: combatantId("c-3"),
name: "Thug",
cr: "1",
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 5 },
],
});
const { result } = renderHook(() => useDifficultyBreakdown(), {
wrapper,
});
// With no bestiary creatures loaded, the Ghost has null CR
const breakdown = result.current;
expect(breakdown).not.toBeNull();
const ghost = breakdown?.combatants[0];
expect(ghost?.cr).toBeNull();
expect(ghost?.xp).toBeNull();
expect(ghost?.editable).toBe(false);
});
it("excludes PC combatants from breakdown entries", async () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c-1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c-2"),
name: "Goblin",
creatureId: goblinCreature.id,
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 1 },
],
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
const { result } = renderHook(() => useDifficultyBreakdown(), {
wrapper,
});
await waitFor(() => {
expect(result.current?.combatants).toHaveLength(1);
expect(result.current?.combatants[0].combatant.name).toBe("Goblin");
});
});
});

View File

@@ -0,0 +1,173 @@
// @vitest-environment jsdom
import type { Creature, CreatureId, PlayerCharacter } from "@initiative/domain";
import { combatantId, creatureId, playerCharacterId } from "@initiative/domain";
import { renderHook, waitFor } from "@testing-library/react";
import type { ReactNode } from "react";
import { beforeAll, describe, expect, it, vi } from "vitest";
import { createTestAdapters } from "../../__tests__/adapters/in-memory-adapters.js";
import {
buildCombatant,
buildCreature,
buildEncounter,
} from "../../__tests__/factories/index.js";
import { AllProviders } from "../../__tests__/test-providers.js";
import { useDifficulty } from "../use-difficulty.js";
beforeAll(() => {
Object.defineProperty(globalThis, "matchMedia", {
writable: true,
value: vi.fn().mockImplementation((query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
});
const pcId1 = playerCharacterId("pc-1");
const goblinCreature = buildCreature({
id: creatureId("srd:goblin"),
name: "Goblin",
cr: "1/4",
});
function makeWrapper(options: {
encounter: ReturnType<typeof buildEncounter>;
playerCharacters?: PlayerCharacter[];
creatures?: Map<CreatureId, Creature>;
}) {
const adapters = createTestAdapters({
encounter: options.encounter,
playerCharacters: options.playerCharacters ?? [],
creatures: options.creatures,
});
return ({ children }: { children: ReactNode }) => (
<AllProviders adapters={adapters}>{children}</AllProviders>
);
}
describe("useDifficulty with custom combatant CRs", () => {
it("includes custom combatant with cr field in monster XP", () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c-1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c-2"),
name: "Custom Thug",
cr: "2",
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 1 },
],
});
const { result } = renderHook(() => useDifficulty(), { wrapper });
expect(result.current).not.toBeNull();
expect(result.current?.totalMonsterXp).toBe(450);
});
it("uses bestiary CR when combatant has both creatureId and cr", async () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c-1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c-2"),
name: "Goblin",
creatureId: goblinCreature.id,
cr: "5",
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 1 },
],
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
const { result } = renderHook(() => useDifficulty(), { wrapper });
await waitFor(() => {
expect(result.current).not.toBeNull();
// Should use bestiary CR 1/4 (50 XP), not the manual cr "5" (1800 XP)
expect(result.current?.totalMonsterXp).toBe(50);
});
});
it("mixes bestiary and custom-with-CR combatants correctly", async () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c-1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c-2"),
name: "Goblin",
creatureId: goblinCreature.id,
}),
buildCombatant({
id: combatantId("c-3"),
name: "Custom",
cr: "1",
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 5 },
],
creatures: new Map([[goblinCreature.id, goblinCreature]]),
});
const { result } = renderHook(() => useDifficulty(), { wrapper });
await waitFor(() => {
expect(result.current).not.toBeNull();
// CR 1/4 = 50 XP, CR 1 = 200 XP → total 250
expect(result.current?.totalMonsterXp).toBe(250);
});
});
it("custom combatant without CR is still excluded", () => {
const wrapper = makeWrapper({
encounter: buildEncounter({
combatants: [
buildCombatant({
id: combatantId("c-1"),
name: "Hero",
playerCharacterId: pcId1,
}),
buildCombatant({
id: combatantId("c-2"),
name: "Custom Monster",
}),
],
}),
playerCharacters: [
{ id: pcId1, name: "Hero", ac: 15, maxHp: 30, level: 1 },
],
});
const { result } = renderHook(() => useDifficulty(), { wrapper });
expect(result.current).toBeNull();
});
});

View File

@@ -0,0 +1,140 @@
import type {
Combatant,
CreatureId,
DifficultyTier,
PlayerCharacter,
} from "@initiative/domain";
import { calculateEncounterDifficulty, crToXp } from "@initiative/domain";
import { useMemo } from "react";
import { useBestiaryContext } from "../contexts/bestiary-context.js";
import { useEncounterContext } from "../contexts/encounter-context.js";
import { usePlayerCharactersContext } from "../contexts/player-characters-context.js";
export interface BreakdownCombatant {
readonly combatant: Combatant;
readonly cr: string | null;
readonly xp: number | null;
readonly source: string | null;
readonly editable: boolean;
}
interface DifficultyBreakdown {
readonly tier: DifficultyTier;
readonly totalMonsterXp: number;
readonly partyBudget: {
readonly low: number;
readonly moderate: number;
readonly high: number;
};
readonly pcCount: number;
readonly combatants: readonly BreakdownCombatant[];
}
export function useDifficultyBreakdown(): DifficultyBreakdown | null {
const { encounter } = useEncounterContext();
const { characters } = usePlayerCharactersContext();
const { getCreature } = useBestiaryContext();
return useMemo(() => {
const partyLevels = derivePartyLevels(encounter.combatants, characters);
const { entries, crs } = classifyCombatants(
encounter.combatants,
getCreature,
);
if (partyLevels.length === 0 || crs.length === 0) {
return null;
}
const result = calculateEncounterDifficulty(partyLevels, crs);
return {
...result,
pcCount: partyLevels.length,
combatants: entries,
};
}, [encounter.combatants, characters, getCreature]);
}
function classifyBestiaryCombatant(
c: Combatant,
getCreature: (
id: CreatureId,
) => { cr: string; source: string; sourceDisplayName: string } | undefined,
): { entry: BreakdownCombatant; cr: string | null } {
const creature = c.creatureId ? getCreature(c.creatureId) : undefined;
if (creature) {
return {
entry: {
combatant: c,
cr: creature.cr,
xp: crToXp(creature.cr),
source: creature.sourceDisplayName ?? creature.source,
editable: false,
},
cr: creature.cr,
};
}
return {
entry: {
combatant: c,
cr: null,
xp: null,
source: null,
editable: false,
},
cr: null,
};
}
function classifyCombatants(
combatants: readonly Combatant[],
getCreature: (
id: CreatureId,
) => { cr: string; source: string; sourceDisplayName: string } | undefined,
): { entries: BreakdownCombatant[]; crs: string[] } {
const entries: BreakdownCombatant[] = [];
const crs: string[] = [];
for (const c of combatants) {
if (c.playerCharacterId) continue;
if (c.creatureId) {
const { entry, cr } = classifyBestiaryCombatant(c, getCreature);
entries.push(entry);
if (cr) crs.push(cr);
} else if (c.cr) {
crs.push(c.cr);
entries.push({
combatant: c,
cr: c.cr,
xp: crToXp(c.cr),
source: null,
editable: true,
});
} else {
entries.push({
combatant: c,
cr: null,
xp: null,
source: null,
editable: true,
});
}
}
return { entries, crs };
}
function derivePartyLevels(
combatants: readonly Combatant[],
characters: readonly PlayerCharacter[],
): number[] {
const levels: number[] = [];
for (const c of combatants) {
if (!c.playerCharacterId) continue;
const pc = characters.find((p) => p.id === c.playerCharacterId);
if (pc?.level !== undefined) levels.push(pc.level);
}
return levels;
}

View File

@@ -29,9 +29,12 @@ function deriveMonsterCrs(
): string[] {
const crs: string[] = [];
for (const c of combatants) {
if (!c.creatureId) continue;
const creature = getCreature(c.creatureId);
if (creature) crs.push(creature.cr);
if (c.creatureId) {
const creature = getCreature(c.creatureId);
if (creature) crs.push(creature.cr);
} else if (c.cr) {
crs.push(c.cr);
}
}
return crs;
}

View File

@@ -9,6 +9,7 @@ import {
removeCombatantUseCase,
retreatTurnUseCase,
setAcUseCase,
setCrUseCase,
setHpUseCase,
setInitiativeUseCase,
setTempHpUseCase,
@@ -52,6 +53,7 @@ type EncounterAction =
| { type: "adjust-hp"; id: CombatantId; delta: number }
| { type: "set-temp-hp"; id: CombatantId; tempHp: number | undefined }
| { type: "set-ac"; id: CombatantId; value: number | undefined }
| { type: "set-cr"; id: CombatantId; value: string | undefined }
| {
type: "toggle-condition";
id: CombatantId;
@@ -318,6 +320,7 @@ function dispatchEncounterAction(
| { type: "adjust-hp" }
| { type: "set-temp-hp" }
| { type: "set-ac" }
| { type: "set-cr" }
| { type: "toggle-condition" }
| { type: "toggle-concentration" }
>,
@@ -358,6 +361,9 @@ function dispatchEncounterAction(
case "set-ac":
result = setAcUseCase(store, action.id, action.value);
break;
case "set-cr":
result = setCrUseCase(store, action.id, action.value);
break;
case "toggle-condition":
result = toggleConditionUseCase(store, action.id, action.conditionId);
break;
@@ -495,6 +501,11 @@ export function useEncounter() {
dispatch({ type: "set-ac", id, value }),
[],
),
setCr: useCallback(
(id: CombatantId, value: string | undefined) =>
dispatch({ type: "set-cr", id, value }),
[],
),
toggleCondition: useCallback(
(id: CombatantId, conditionId: ConditionId) =>
dispatch({ type: "toggle-condition", id, conditionId }),