Add Pathfinder 2e game system mode
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:
@@ -1,23 +1,25 @@
|
||||
import type { Creature, CreatureId } from "@initiative/domain";
|
||||
import type { AnyCreature, CreatureId } from "@initiative/domain";
|
||||
import { type IDBPDatabase, openDB } from "idb";
|
||||
|
||||
const DB_NAME = "initiative-bestiary";
|
||||
const STORE_NAME = "sources";
|
||||
const DB_VERSION = 3;
|
||||
const DB_VERSION = 4;
|
||||
|
||||
interface CachedSourceInfo {
|
||||
readonly sourceCode: string;
|
||||
readonly displayName: string;
|
||||
readonly creatureCount: number;
|
||||
readonly cachedAt: number;
|
||||
readonly system?: string;
|
||||
}
|
||||
|
||||
interface CachedSourceRecord {
|
||||
sourceCode: string;
|
||||
displayName: string;
|
||||
creatures: Creature[];
|
||||
creatures: AnyCreature[];
|
||||
cachedAt: number;
|
||||
creatureCount: number;
|
||||
system?: string;
|
||||
}
|
||||
|
||||
let db: IDBPDatabase | null = null;
|
||||
@@ -26,6 +28,10 @@ let dbFailed = false;
|
||||
// In-memory fallback when IndexedDB is unavailable
|
||||
const memoryStore = new Map<string, CachedSourceRecord>();
|
||||
|
||||
function scopedKey(system: string, sourceCode: string): string {
|
||||
return `${system}:${sourceCode}`;
|
||||
}
|
||||
|
||||
async function getDb(): Promise<IDBPDatabase | null> {
|
||||
if (db) return db;
|
||||
if (dbFailed) return null;
|
||||
@@ -58,60 +64,77 @@ async function getDb(): Promise<IDBPDatabase | null> {
|
||||
}
|
||||
|
||||
export async function cacheSource(
|
||||
system: string,
|
||||
sourceCode: string,
|
||||
displayName: string,
|
||||
creatures: Creature[],
|
||||
creatures: AnyCreature[],
|
||||
): Promise<void> {
|
||||
const key = scopedKey(system, sourceCode);
|
||||
const record: CachedSourceRecord = {
|
||||
sourceCode,
|
||||
sourceCode: key,
|
||||
displayName,
|
||||
creatures,
|
||||
cachedAt: Date.now(),
|
||||
creatureCount: creatures.length,
|
||||
system,
|
||||
};
|
||||
|
||||
const database = await getDb();
|
||||
if (database) {
|
||||
await database.put(STORE_NAME, record);
|
||||
} else {
|
||||
memoryStore.set(sourceCode, record);
|
||||
memoryStore.set(key, record);
|
||||
}
|
||||
}
|
||||
|
||||
export async function isSourceCached(sourceCode: string): Promise<boolean> {
|
||||
export async function isSourceCached(
|
||||
system: string,
|
||||
sourceCode: string,
|
||||
): Promise<boolean> {
|
||||
const key = scopedKey(system, sourceCode);
|
||||
const database = await getDb();
|
||||
if (database) {
|
||||
const record = await database.get(STORE_NAME, sourceCode);
|
||||
const record = await database.get(STORE_NAME, key);
|
||||
return record !== undefined;
|
||||
}
|
||||
return memoryStore.has(sourceCode);
|
||||
return memoryStore.has(key);
|
||||
}
|
||||
|
||||
export async function getCachedSources(): Promise<CachedSourceInfo[]> {
|
||||
export async function getCachedSources(
|
||||
system?: string,
|
||||
): Promise<CachedSourceInfo[]> {
|
||||
const database = await getDb();
|
||||
let records: CachedSourceRecord[];
|
||||
if (database) {
|
||||
const all: CachedSourceRecord[] = await database.getAll(STORE_NAME);
|
||||
return all.map((r) => ({
|
||||
sourceCode: r.sourceCode,
|
||||
displayName: r.displayName,
|
||||
creatureCount: r.creatureCount,
|
||||
cachedAt: r.cachedAt,
|
||||
}));
|
||||
records = await database.getAll(STORE_NAME);
|
||||
} else {
|
||||
records = [...memoryStore.values()];
|
||||
}
|
||||
return [...memoryStore.values()].map((r) => ({
|
||||
sourceCode: r.sourceCode,
|
||||
|
||||
const filtered = system
|
||||
? records.filter((r) => r.system === system)
|
||||
: records;
|
||||
return filtered.map((r) => ({
|
||||
sourceCode: r.system
|
||||
? r.sourceCode.slice(r.system.length + 1)
|
||||
: r.sourceCode,
|
||||
displayName: r.displayName,
|
||||
creatureCount: r.creatureCount,
|
||||
cachedAt: r.cachedAt,
|
||||
system: r.system,
|
||||
}));
|
||||
}
|
||||
|
||||
export async function clearSource(sourceCode: string): Promise<void> {
|
||||
export async function clearSource(
|
||||
system: string,
|
||||
sourceCode: string,
|
||||
): Promise<void> {
|
||||
const key = scopedKey(system, sourceCode);
|
||||
const database = await getDb();
|
||||
if (database) {
|
||||
await database.delete(STORE_NAME, sourceCode);
|
||||
await database.delete(STORE_NAME, key);
|
||||
} else {
|
||||
memoryStore.delete(sourceCode);
|
||||
memoryStore.delete(key);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,9 +148,9 @@ export async function clearAll(): Promise<void> {
|
||||
}
|
||||
|
||||
export async function loadAllCachedCreatures(): Promise<
|
||||
Map<CreatureId, Creature>
|
||||
Map<CreatureId, AnyCreature>
|
||||
> {
|
||||
const map = new Map<CreatureId, Creature>();
|
||||
const map = new Map<CreatureId, AnyCreature>();
|
||||
const database = await getDb();
|
||||
|
||||
let records: CachedSourceRecord[];
|
||||
|
||||
Reference in New Issue
Block a user