37 lines
1.5 KiB
JavaScript
37 lines
1.5 KiB
JavaScript
import { MODULE_ID } from "../constants.js";
|
|
import { getAssignedReactionsForContext } from "./assignment-resolver.js";
|
|
import { checkConditions } from "./condition-runner.js";
|
|
import { checkConsumptionAvailable, consumeReactionUse } from "./consumption-manager.js";
|
|
import { executeReactionActions } from "./action-runner.js";
|
|
|
|
export async function handleTrigger(triggerType, context) {
|
|
if (!game.user.isGM) return;
|
|
|
|
const assignedReactions = await getAssignedReactionsForContext(context);
|
|
|
|
for (const reaction of assignedReactions) {
|
|
try {
|
|
if (!reaction?.enabled) continue;
|
|
if (reaction.trigger?.type !== triggerType) continue;
|
|
if (!matchesTriggerSpellFilter(reaction.trigger, context)) continue;
|
|
if (!await checkConsumptionAvailable(reaction, context)) continue;
|
|
if (!await checkConditions(reaction, context)) continue;
|
|
|
|
const result = await executeReactionActions(reaction, context);
|
|
if (result.shouldConsume) await consumeReactionUse(reaction, context);
|
|
} catch (error) {
|
|
console.error(`${MODULE_ID} | Failed to handle reaction`, reaction, error);
|
|
}
|
|
}
|
|
}
|
|
|
|
function matchesTriggerSpellFilter(trigger, context) {
|
|
const expectedName = trigger?.spell?.itemName;
|
|
if (!expectedName) return true;
|
|
|
|
const actualName = context?.spellItem?.name ?? context?.item?.name ?? context?.itemName ?? context?.spellName ?? null;
|
|
if (!actualName) return false;
|
|
|
|
return actualName.toLocaleLowerCase() === expectedName.toLocaleLowerCase();
|
|
}
|