45 lines
1.4 KiB
JavaScript

import { executeApplyStatusAction } from "../actions/apply-status-action.js";
import { executeTeleportAction } from "../actions/teleport-action.js";
import { executeCastSpellAction } from "../actions/cast-spell-action.js";
import { executeUseInventoryItemAction } from "../actions/use-inventory-item-action.js";
const ACTION_HANDLERS = Object.freeze({
applyStatus: executeApplyStatusAction,
teleport: executeTeleportAction,
castSpellFromToken: executeCastSpellAction,
useInventoryItem: executeUseInventoryItemAction
});
export async function executeReactionActions(reaction, context) {
let anySuccess = false;
let anyFailureConsumes = false;
for (const action of reaction.actions ?? []) {
if (!action?.enabled) continue;
const handler = ACTION_HANDLERS[action.type];
if (!handler) continue;
const result = await handler(action, {
...context,
reaction,
reactionOrigin: `Module.configurable-reactions.Reaction.${reaction.id}`
});
if (result.success) anySuccess = true;
else if (shouldConsumeOnFailure(reaction, action)) anyFailureConsumes = true;
if (result.stopProcessing) break;
}
return {
success: anySuccess,
shouldConsume: anySuccess || anyFailureConsumes
};
}
function shouldConsumeOnFailure(reaction, action) {
if (typeof action.consumeOnFailure === "boolean") return action.consumeOnFailure;
return reaction.consumption?.consumeOnFailure === true;
}