ChatGPT dd1b08ee21
All checks were successful
Sonarqube Scanner / Build and analyze (push) Successful in 51s
cleanup and fix sonarqube findings
2026-06-04 23:54:22 +02:00

46 lines
1.7 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) {
await handleAssignedReaction(triggerType, context, reaction);
}
}
async function handleAssignedReaction(triggerType, context, reaction) {
try {
if (!await shouldExecuteReaction(triggerType, context, reaction)) return;
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);
}
}
async function shouldExecuteReaction(triggerType, context, reaction) {
if (!reaction?.enabled) return false;
if (reaction.trigger?.type !== triggerType) return false;
if (!matchesTriggerSpellFilter(reaction.trigger, context)) return false;
if (!await checkConsumptionAvailable(reaction, context)) return false;
return checkConditions(reaction, context);
}
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();
}