Add Pathfinder 2e game system mode
All checks were successful
CI / check (push) Successful in 2m21s
CI / build-image (push) Successful in 24s

Implements PF2e as an alternative game system alongside D&D 5e/5.5e.
Settings modal "Game System" selector switches conditions, bestiary,
stat block layout, and initiative calculation between systems.

- Valued conditions with increment/decrement UX (Clumsy 2, Frightened 3)
- 2,502 PF2e creatures from bundled search index (77 sources)
- PF2e stat block: level, traits, Perception, Fort/Ref/Will, ability mods
- Perception-based initiative rolling
- System-scoped source cache (D&D and PF2e sources don't collide)
- Backwards-compatible condition rehydration (ConditionId[] → ConditionEntry[])
- Difficulty indicator hidden in PF2e mode (excluded from MVP)

Closes dostulata/initiative#19

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Lukas
2026-04-07 01:26:22 +02:00
parent 8f6eebc43b
commit e62c49434c
67 changed files with 27758 additions and 527 deletions

View File

@@ -1,8 +1,4 @@
import type {
BestiaryIndexEntry,
ConditionId,
PlayerCharacter,
} from "@initiative/domain";
import type { ConditionId, PlayerCharacter } from "@initiative/domain";
import {
combatantId,
createEncounter,
@@ -11,6 +7,7 @@ import {
playerCharacterId,
} from "@initiative/domain";
import { describe, expect, it } from "vitest";
import type { SearchResult } from "../use-bestiary.js";
import { type EncounterState, encounterReducer } from "../use-encounter.js";
function emptyState(): EncounterState {
@@ -45,9 +42,11 @@ function stateWithHp(name: string, maxHp: number): EncounterState {
});
}
const BESTIARY_ENTRY: BestiaryIndexEntry = {
const BESTIARY_ENTRY: SearchResult = {
system: "dnd",
name: "Goblin",
source: "MM",
sourceDisplayName: "Monster Manual",
ac: 15,
hp: 7,
dex: 14,
@@ -57,6 +56,19 @@ const BESTIARY_ENTRY: BestiaryIndexEntry = {
type: "humanoid",
};
const PF2E_BESTIARY_ENTRY: SearchResult = {
system: "pf2e",
name: "Goblin Warrior",
source: "B1",
sourceDisplayName: "Bestiary",
level: -1,
ac: 16,
hp: 6,
perception: 5,
size: "small",
type: "humanoid",
};
describe("encounterReducer", () => {
describe("add-combatant", () => {
it("adds a combatant and pushes undo", () => {
@@ -236,7 +248,9 @@ describe("encounterReducer", () => {
conditionId: "blinded" as ConditionId,
});
expect(next.encounter.combatants[0].conditions).toContain("blinded");
expect(next.encounter.combatants[0].conditions).toContainEqual({
id: "blinded",
});
});
it("toggles concentration", () => {
@@ -327,6 +341,19 @@ describe("encounterReducer", () => {
expect(names).toContain("Goblin 1");
expect(names).toContain("Goblin 2");
});
it("adds PF2e creature with HP, AC, and creatureId", () => {
const next = encounterReducer(emptyState(), {
type: "add-from-bestiary",
entry: PF2E_BESTIARY_ENTRY,
});
const c = next.encounter.combatants[0];
expect(c.name).toBe("Goblin Warrior");
expect(c.maxHp).toBe(6);
expect(c.ac).toBe(16);
expect(c.creatureId).toBe("b1:goblin-warrior");
});
});
describe("add-multiple-from-bestiary", () => {

View File

@@ -1,11 +1,12 @@
// @vitest-environment jsdom
import type { BestiaryIndexEntry, PlayerCharacter } from "@initiative/domain";
import type { PlayerCharacter } from "@initiative/domain";
import { combatantId, creatureId, playerCharacterId } from "@initiative/domain";
import { act, renderHook } 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 { AllProviders } from "../../__tests__/test-providers.js";
import type { SearchResult } from "../use-bestiary.js";
import { useEncounter } from "../use-encounter.js";
beforeAll(() => {
@@ -152,9 +153,11 @@ describe("useEncounter", () => {
expect(result.current.canRollAllInitiative).toBe(false);
// Add from bestiary to get a creature combatant
const entry: BestiaryIndexEntry = {
const entry: SearchResult = {
system: "dnd",
name: "Goblin",
source: "MM",
sourceDisplayName: "Monster Manual",
ac: 15,
hp: 7,
dex: 14,
@@ -175,9 +178,11 @@ describe("useEncounter", () => {
it("addFromBestiary adds combatant with HP, AC, creatureId", () => {
const { result } = renderHook(() => useEncounter(), { wrapper });
const entry: BestiaryIndexEntry = {
const entry: SearchResult = {
system: "dnd",
name: "Goblin",
source: "MM",
sourceDisplayName: "Monster Manual",
ac: 15,
hp: 7,
dex: 14,
@@ -202,9 +207,11 @@ describe("useEncounter", () => {
it("addFromBestiary auto-numbers duplicate names", () => {
const { result } = renderHook(() => useEncounter(), { wrapper });
const entry: BestiaryIndexEntry = {
const entry: SearchResult = {
system: "dnd",
name: "Goblin",
source: "MM",
sourceDisplayName: "Monster Manual",
ac: 15,
hp: 7,
dex: 14,

View File

@@ -1,9 +1,10 @@
// @vitest-environment jsdom
import { act, renderHook } from "@testing-library/react";
import { afterEach, describe, expect, it } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";
import { useRulesEdition } from "../use-rules-edition.js";
const STORAGE_KEY = "initiative:rules-edition";
const STORAGE_KEY = "initiative:game-system";
const OLD_STORAGE_KEY = "initiative:rules-edition";
describe("useRulesEdition", () => {
afterEach(() => {
@@ -11,6 +12,7 @@ describe("useRulesEdition", () => {
const { result } = renderHook(() => useRulesEdition());
act(() => result.current.setEdition("5.5e"));
localStorage.removeItem(STORAGE_KEY);
localStorage.removeItem(OLD_STORAGE_KEY);
});
it("defaults to 5.5e", () => {
@@ -42,4 +44,31 @@ describe("useRulesEdition", () => {
expect(r2.current.edition).toBe("5e");
});
it("accepts pf2e as a valid game system", () => {
const { result } = renderHook(() => useRulesEdition());
act(() => result.current.setEdition("pf2e"));
expect(result.current.edition).toBe("pf2e");
expect(localStorage.getItem(STORAGE_KEY)).toBe("pf2e");
});
it("migrates from old storage key on fresh module load", async () => {
// Set up old key before re-importing the module
localStorage.setItem(OLD_STORAGE_KEY, "5e");
localStorage.removeItem(STORAGE_KEY);
// Force a fresh module so loadEdition() re-runs at init time
vi.resetModules();
const { useRulesEdition: freshHook } = await import(
"../use-rules-edition.js"
);
const { result } = renderHook(() => freshHook());
expect(result.current.edition).toBe("5e");
expect(localStorage.getItem(STORAGE_KEY)).toBe("5e");
expect(localStorage.getItem(OLD_STORAGE_KEY)).toBeNull();
});
});