Effects targeting calculations by tag now work in the engine and actions

This commit is contained in:
Stefan Zermatten
2022-02-15 15:59:41 +02:00
parent e0f621cc44
commit 378da71f5d
19 changed files with 454 additions and 98 deletions

View File

@@ -1,4 +1,4 @@
import { get } from 'lodash';
import { get, intersection, difference } from 'lodash';
const linkDependenciesByType = {
action: linkAction,
@@ -127,11 +127,11 @@ function linkEffects(dependencyGraph, prop, computation){
dependOnCalc({dependencyGraph, prop, key: 'amount'});
// The stats depend on the effect
if (prop.targetByTags){
// TODO:
getEffectTagTargets(prop, computation).forEach(targetProp => {
getEffectTagTargets(prop, computation).forEach(targetId => {
const targetProp = computation.propsById[targetId];
const key = prop.targetField || getDefaultCalculationField(targetProp);
const calcObj = get(targetProp, key);
if (calcObj){
if (calcObj && calcObj.calculation){
dependencyGraph.addLink(`${targetProp._id}.${key}`, prop._id , 'effect');
}
});
@@ -143,6 +143,67 @@ function linkEffects(dependencyGraph, prop, computation){
}
}
// Returns an array of IDs of the properties the effect targets
function getEffectTagTargets(effect, computation){
const targets = getTargetListFromTags(effect.targetTags, computation);
const notIds = [];
if (effect.extraTags){
effect.extraTags.forEach(ex => {
if (ex.operation === 'OR'){
targets.push(...getTargetListFromTags(ex.tags, computation));
} else if (ex.operation === 'NOT'){
ex.tags.forEach(tag => {
const idList = computation.propsWithTag[tag];
if (idList) notIds.push(...computation.propsWithTag[tag])
});
}
});
}
return difference(targets, notIds);
}
function getTargetListFromTags(tags, computation){
const targetTagIdLists = [];
if (!tags) return [];
tags.forEach(tag => {
const idList = computation.propsWithTag[tag];
if (idList) targetTagIdLists.push(idList);
});
const targets = intersection(...targetTagIdLists);
return targets;
}
function getDefaultCalculationField(prop){
switch (prop.type){
case 'action': return 'attackRoll';
case 'adjustment': return 'amount';
case 'attribute': return 'baseValue';
case 'branch': return 'condition';
case 'buff': return 'duration';
case 'class': return null;
case 'classLevel': return null;
case 'constant': return null;
case 'container': return null;
case 'damageMultiplier': return null;
case 'damage': return 'amount';
case 'effect': return 'amount';
case 'feature': return null;
case 'folder': return null;
case 'item': return null;
case 'note': return null;
case 'proficiency': return null;
case 'reference': return null;
case 'roll': return 'roll';
case 'savingThrow': return 'dc';
case 'skill': return 'baseValue';
case 'slotFiller': return null;
case 'slot': return 'quantityExpected';
case 'spellList': return 'attackRollBonus';
case 'spell': return null;
case 'toggle': return 'condition';
}
}
function linkRoll(dependencyGraph, prop){
dependOnCalc({dependencyGraph, prop, key: 'roll'});
}

View File

@@ -29,7 +29,7 @@ function discoverInlineCalculationFields(prop, schemas){
// Set the value to the uncomputed string for use in calculations
inlineCalcObj.value = string;
// Has the text, if it matches the existing hash, stop
const inlineCalcHash = cyrb53(inlineCalcObj.text);
if (inlineCalcHash === inlineCalcObj.hash){
@@ -57,6 +57,9 @@ function parseAllCalculationFields(prop, schemas){
// Determine the level the calculation should compute down to
let parseLevel = schemas[prop.type].getDefinition(calcKey).parseLevel || 'reduce';
// Special case of effects, when targeting by tags compile
if (prop.type === 'effect' && prop.targetByTags) parseLevel = 'compile';
// For all fields matching they keys
// supports `keys.$.with.$.arrays`
applyFnToKey(prop, calcKey, (prop, key) => {