All checks were successful
Sonarqube Scanner / Build and analyze (push) Successful in 50s
32 lines
1.3 KiB
JavaScript
32 lines
1.3 KiB
JavaScript
export async function checkDamageCondition(config, context) {
|
|
const amount = Number(context.amount ?? context.damageAmount ?? 0);
|
|
const absoluteAmount = Math.abs(amount);
|
|
|
|
if (absoluteAmount < (Number(config.minAmount) || 0)) return false;
|
|
if (config.amountMode === "damageOnly" && amount <= 0) return false;
|
|
if (config.amountMode === "healingOnly" && amount >= 0) return false;
|
|
if (config.amountMode === "healingOnly") return true;
|
|
|
|
const configuredTypes = config.types ?? [];
|
|
if (!configuredTypes.length) return true;
|
|
|
|
const eventTypes = new Set(context.damageTypes ?? []);
|
|
if (!eventTypes.size) return configuredTypesCoverAllKnownTypes(configuredTypes);
|
|
|
|
if (config.typeMode === "all") return configuredTypes.every(type => eventTypes.has(type));
|
|
if (config.typeMode === "none") return configuredTypes.every(type => !eventTypes.has(type));
|
|
return configuredTypes.some(type => eventTypes.has(type));
|
|
}
|
|
|
|
function configuredTypesCoverAllKnownTypes(configuredTypes) {
|
|
const knownTypes = getKnownDamageTypes();
|
|
if (!knownTypes.size) return false;
|
|
return Array.from(knownTypes).every(type => configuredTypes.includes(type));
|
|
}
|
|
|
|
function getKnownDamageTypes() {
|
|
const damageTypes = CONFIG.DND5E?.damageTypes ?? {};
|
|
if (damageTypes instanceof Map) return new Set(damageTypes.keys());
|
|
return new Set(Object.keys(damageTypes));
|
|
}
|