Add PF2e persistent damage condition tags
Persistent damage displayed as compact tags with damage type icon and formula (e.g., Flame + "2d6"). Supports fire, bleed, acid, cold, electricity, poison, and mental types. One instance per type, added via sub-picker in the condition picker. PF2e only, persists across reload. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
import type { DomainEvent } from "./events.js";
|
||||
import {
|
||||
type CombatantId,
|
||||
type DomainError,
|
||||
type Encounter,
|
||||
findCombatant,
|
||||
isDomainError,
|
||||
} from "./types.js";
|
||||
|
||||
export const PERSISTENT_DAMAGE_TYPES = [
|
||||
"fire",
|
||||
"bleed",
|
||||
"acid",
|
||||
"cold",
|
||||
"electricity",
|
||||
"poison",
|
||||
"mental",
|
||||
] as const;
|
||||
|
||||
export type PersistentDamageType = (typeof PERSISTENT_DAMAGE_TYPES)[number];
|
||||
|
||||
export const VALID_PERSISTENT_DAMAGE_TYPES: ReadonlySet<string> = new Set(
|
||||
PERSISTENT_DAMAGE_TYPES,
|
||||
);
|
||||
|
||||
export interface PersistentDamageEntry {
|
||||
readonly type: PersistentDamageType;
|
||||
readonly formula: string;
|
||||
}
|
||||
|
||||
export interface PersistentDamageDefinition {
|
||||
readonly type: PersistentDamageType;
|
||||
readonly label: string;
|
||||
readonly iconName: string;
|
||||
readonly color: string;
|
||||
}
|
||||
|
||||
export const PERSISTENT_DAMAGE_DEFINITIONS: readonly PersistentDamageDefinition[] =
|
||||
[
|
||||
{ type: "fire", label: "Fire", iconName: "Flame", color: "orange" },
|
||||
{ type: "bleed", label: "Bleed", iconName: "Droplets", color: "red" },
|
||||
{
|
||||
type: "acid",
|
||||
label: "Acid",
|
||||
iconName: "FlaskConical",
|
||||
color: "lime",
|
||||
},
|
||||
{ type: "cold", label: "Cold", iconName: "Snowflake", color: "sky" },
|
||||
{
|
||||
type: "electricity",
|
||||
label: "Electricity",
|
||||
iconName: "Zap",
|
||||
color: "yellow",
|
||||
},
|
||||
{
|
||||
type: "poison",
|
||||
label: "Poison",
|
||||
iconName: "Droplet",
|
||||
color: "green",
|
||||
},
|
||||
{
|
||||
type: "mental",
|
||||
label: "Mental",
|
||||
iconName: "BrainCog",
|
||||
color: "pink",
|
||||
},
|
||||
];
|
||||
|
||||
export interface PersistentDamageSuccess {
|
||||
readonly encounter: Encounter;
|
||||
readonly events: DomainEvent[];
|
||||
}
|
||||
|
||||
function applyPersistentDamage(
|
||||
encounter: Encounter,
|
||||
combatantId: CombatantId,
|
||||
newEntries: readonly PersistentDamageEntry[] | undefined,
|
||||
): Encounter {
|
||||
return {
|
||||
combatants: encounter.combatants.map((c) =>
|
||||
c.id === combatantId ? { ...c, persistentDamage: newEntries } : c,
|
||||
),
|
||||
activeIndex: encounter.activeIndex,
|
||||
roundNumber: encounter.roundNumber,
|
||||
};
|
||||
}
|
||||
|
||||
export function addPersistentDamage(
|
||||
encounter: Encounter,
|
||||
combatantId: CombatantId,
|
||||
damageType: PersistentDamageType,
|
||||
formula: string,
|
||||
): PersistentDamageSuccess | DomainError {
|
||||
if (!VALID_PERSISTENT_DAMAGE_TYPES.has(damageType)) {
|
||||
return {
|
||||
kind: "domain-error",
|
||||
code: "unknown-damage-type",
|
||||
message: `Unknown persistent damage type "${damageType}"`,
|
||||
};
|
||||
}
|
||||
if (formula.trim().length === 0) {
|
||||
return {
|
||||
kind: "domain-error",
|
||||
code: "empty-formula",
|
||||
message: "Persistent damage formula must not be empty",
|
||||
};
|
||||
}
|
||||
|
||||
const found = findCombatant(encounter, combatantId);
|
||||
if (isDomainError(found)) return found;
|
||||
const { combatant: target } = found;
|
||||
const current = target.persistentDamage ?? [];
|
||||
|
||||
// Replace existing entry of same type, or append
|
||||
const filtered = current.filter((e) => e.type !== damageType);
|
||||
const newEntries = [
|
||||
...filtered,
|
||||
{ type: damageType, formula: formula.trim() },
|
||||
];
|
||||
|
||||
// Sort by definition order
|
||||
const order = PERSISTENT_DAMAGE_DEFINITIONS.map((d) => d.type);
|
||||
newEntries.sort((a, b) => order.indexOf(a.type) - order.indexOf(b.type));
|
||||
|
||||
return {
|
||||
encounter: applyPersistentDamage(encounter, combatantId, newEntries),
|
||||
events: [
|
||||
{
|
||||
type: "PersistentDamageAdded",
|
||||
combatantId,
|
||||
damageType,
|
||||
formula: formula.trim(),
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
export function removePersistentDamage(
|
||||
encounter: Encounter,
|
||||
combatantId: CombatantId,
|
||||
damageType: PersistentDamageType,
|
||||
): PersistentDamageSuccess | DomainError {
|
||||
const found = findCombatant(encounter, combatantId);
|
||||
if (isDomainError(found)) return found;
|
||||
const { combatant: target } = found;
|
||||
const current = target.persistentDamage ?? [];
|
||||
|
||||
if (!current.some((e) => e.type === damageType)) {
|
||||
return {
|
||||
kind: "domain-error",
|
||||
code: "persistent-damage-not-active",
|
||||
message: `Persistent ${damageType} damage is not active`,
|
||||
};
|
||||
}
|
||||
|
||||
const filtered = current.filter((e) => e.type !== damageType);
|
||||
return {
|
||||
encounter: applyPersistentDamage(
|
||||
encounter,
|
||||
combatantId,
|
||||
filtered.length > 0 ? filtered : undefined,
|
||||
),
|
||||
events: [{ type: "PersistentDamageRemoved", combatantId, damageType }],
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user