Add PF2e attack effects, ability frequency, and perception details

Show inline on-hit effects on attack lines (e.g., "plus Grab"), frequency
limits on abilities (e.g., "(1/day)"), and perception details text alongside
senses. Strip redundant frequency lines from Foundry descriptions.

Also add resilient PF2e source fetching: batched requests with retry,
graceful handling of ad-blocker-blocked creature files (partial success
with toast warning and re-fetch prompt for missing creatures).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Lukas
2026-04-10 23:37:03 +02:00
parent 1eaeecad32
commit c3707cf0b6
16 changed files with 488 additions and 55 deletions

View File

@@ -63,6 +63,7 @@ interface MeleeSystem {
bonus?: { value: number };
damageRolls?: Record<string, { damage: string; damageType: string }>;
traits?: { value: string[] };
attackEffects?: { value: string[] };
}
interface ActionSystem {
@@ -71,6 +72,7 @@ interface ActionSystem {
actions?: { value: number | null };
traits?: { value: string[] };
description?: { value: string };
frequency?: { max: number; per: string };
}
interface SpellcastingEntrySystem {
@@ -342,7 +344,17 @@ function formatSpeed(speed: {
// -- Attack normalization --
function normalizeAttack(item: RawFoundryItem): TraitBlock {
/** Format an attack effect slug to display text: "grab" → "Grab", "lich-siphon-life" → "Siphon Life". */
function formatAttackEffect(slug: string, creatureName: string): string {
const prefix = `${creatureName.toLowerCase().replaceAll(/[^a-z0-9]+/g, "-")}-`;
const stripped = slug.startsWith(prefix) ? slug.slice(prefix.length) : slug;
return stripped.split("-").map(capitalize).join(" ");
}
function normalizeAttack(
item: RawFoundryItem,
creatureName: string,
): TraitBlock {
const sys = item.system as unknown as MeleeSystem;
const bonus = sys.bonus?.value ?? 0;
const traits = sys.traits?.value ?? [];
@@ -352,13 +364,18 @@ function normalizeAttack(item: RawFoundryItem): TraitBlock {
.join(" plus ");
const traitStr =
traits.length > 0 ? ` (${traits.map(formatTrait).join(", ")})` : "";
const effects = sys.attackEffects?.value ?? [];
const effectStr =
effects.length > 0
? ` plus ${effects.map((e) => formatAttackEffect(e, creatureName)).join(" and ")}`
: "";
return {
name: capitalize(item.name),
activity: { number: 1, unit: "action" },
segments: [
{
type: "text",
value: `+${bonus}${traitStr}, ${damage}`,
value: `+${bonus}${traitStr}, ${damage}${effectStr}`,
},
],
};
@@ -382,15 +399,31 @@ function parseActivity(
// -- Ability normalization --
const FREQUENCY_LINE = /(<strong>)?Frequency(<\/strong>)?\s+[^\n]+\n*/i;
/** Strip the "Frequency once per day" line from ability descriptions when structured frequency data exists. */
function stripFrequencyLine(text: string): string {
return text.replace(FREQUENCY_LINE, "").trimStart();
}
function normalizeAbility(item: RawFoundryItem): TraitBlock {
const sys = item.system as unknown as ActionSystem;
const actionType = sys.actionType?.value;
const actionCount = sys.actions?.value;
const description = stripFoundryTags(sys.description?.value ?? "");
let description = stripFoundryTags(sys.description?.value ?? "");
const traits = sys.traits?.value ?? [];
const activity = parseActivity(actionType, actionCount);
const frequency =
sys.frequency?.max != null && sys.frequency.per
? `${sys.frequency.max}/${sys.frequency.per}`
: undefined;
if (frequency) {
description = stripFrequencyLine(description);
}
const traitStr =
traits.length > 0
? `(${traits.map((t) => capitalize(formatTrait(t))).join(", ")}) `
@@ -401,7 +434,7 @@ function normalizeAbility(item: RawFoundryItem): TraitBlock {
? [{ type: "text", value: text }]
: [];
return { name: item.name, activity, segments };
return { name: item.name, activity, frequency, segments };
}
// -- Spellcasting normalization --
@@ -684,6 +717,7 @@ export function normalizeFoundryCreature(
level: sys.details?.level?.value ?? 0,
traits: buildTraits(sys.traits),
perception: sys.perception?.mod ?? 0,
perceptionDetails: sys.perception?.details || undefined,
senses: formatSenses(sys.perception?.senses),
languages: formatLanguages(sys.details?.languages),
skills: formatSkills(sys.skills),
@@ -701,7 +735,9 @@ export function normalizeFoundryCreature(
weaknesses: formatWeaknesses(sys.attributes.weaknesses),
speed: formatSpeed(sys.attributes.speed),
attacks: orUndefined(
items.filter((i) => i.type === "melee").map(normalizeAttack),
items
.filter((i) => i.type === "melee")
.map((i) => normalizeAttack(i, r.name)),
),
abilitiesTop: orUndefined(actionsByCategory(items, "interaction")),
abilitiesMid: orUndefined(