From 2c0496b44bb622d945f9fbaa6dfb47f4549b0475 Mon Sep 17 00:00:00 2001 From: Stefan Zermatten Date: Tue, 2 Mar 2021 13:56:53 +0200 Subject: [PATCH] Fixed properties not being made inactive by toggles --- .../computation/engine/ComputationMemo.js | 7 +--- .../computation/engine/computeConstant.js | 6 +++ .../engine/computeEndStepProperty.js | 3 ++ .../computation/engine/computeLevels.js | 5 ++- .../computation/engine/computeMemo.js | 2 +- .../computation/engine/computeStat.js | 37 +++++++++---------- .../engine/writeAlteredProperties.js | 5 ++- .../computedOnlyPropertySchemasIndex.js | 21 ++++++----- 8 files changed, 49 insertions(+), 37 deletions(-) diff --git a/app/imports/api/creature/computation/engine/ComputationMemo.js b/app/imports/api/creature/computation/engine/ComputationMemo.js index a907fce7..25782d0f 100644 --- a/app/imports/api/creature/computation/engine/ComputationMemo.js +++ b/app/imports/api/creature/computation/engine/ComputationMemo.js @@ -7,6 +7,7 @@ export default class ComputationMemo { constructor(props, creature){ this.statsByVariableName = {}; this.constantsByVariableName = {}; + this.constantsById = {}; this.extraStatsByVariableName = {}; this.statsById = {}; this.originalPropsById = {}; @@ -77,11 +78,7 @@ export default class ComputationMemo { } addConstant(prop){ prop = this.registerProperty(prop); - if ( - !this.constantsByVariableName[prop.variableName] - ){ - this.constantsByVariableName[prop.variableName] = prop - } + this.constantsById[prop._id] = prop; } registerProperty(prop){ this.originalPropsById[prop._id] = cloneDeep(prop); diff --git a/app/imports/api/creature/computation/engine/computeConstant.js b/app/imports/api/creature/computation/engine/computeConstant.js index bacece4c..4d972c57 100644 --- a/app/imports/api/creature/computation/engine/computeConstant.js +++ b/app/imports/api/creature/computation/engine/computeConstant.js @@ -3,4 +3,10 @@ import applyToggles from '/imports/api/creature/computation/engine/applyToggles. export default function computeConstant(constant, memo){ // Apply any toggles applyToggles(constant, memo); + if (constant.deactivatedByToggle) return; + if ( + !memo.constantsByVariableName[constant.variableName] + ){ + memo.constantsByVariableName[constant.variableName] = constant + } } diff --git a/app/imports/api/creature/computation/engine/computeEndStepProperty.js b/app/imports/api/creature/computation/engine/computeEndStepProperty.js index 58d0a525..fad2afd4 100644 --- a/app/imports/api/creature/computation/engine/computeEndStepProperty.js +++ b/app/imports/api/creature/computation/engine/computeEndStepProperty.js @@ -1,8 +1,11 @@ import evaluateCalculation from '/imports/api/creature/computation/engine/evaluateCalculation.js'; import ConstantNode from '/imports/parser/parseTree/ConstantNode.js'; +import applyToggles from '/imports/api/creature/computation/engine/applyToggles.js'; import { union } from 'lodash'; export default function computeEndStepProperty(prop, memo){ + applyToggles(prop, memo); + switch (prop.type){ case 'action': case 'spell': diff --git a/app/imports/api/creature/computation/engine/computeLevels.js b/app/imports/api/creature/computation/engine/computeLevels.js index f07f4adf..21757025 100644 --- a/app/imports/api/creature/computation/engine/computeLevels.js +++ b/app/imports/api/creature/computation/engine/computeLevels.js @@ -1,4 +1,5 @@ import { forOwn, has, union } from 'lodash'; +import applyToggles from '/imports/api/creature/computation/engine/applyToggles.js'; export default function computeLevels(memo){ computeClassLevels(memo); @@ -7,11 +8,13 @@ export default function computeLevels(memo){ function computeClassLevels(memo){ forOwn(memo.classLevelsById, classLevel => { + applyToggles(classLevel, memo); // class levels are mutually dependent classLevel.dependencies = union( classLevel.dependencies, Object.keys(memo.classLevelsById) ); + if (classLevel.deactivatedByToggle) return; let name = classLevel.variableName; let stat = memo.statsByVariableName[name]; if (!stat){ @@ -29,7 +32,7 @@ function computeClassLevels(memo){ function computeTotalLevel(memo){ let currentLevel = memo.statsByVariableName['level']; - if (!currentLevel){ + if (!currentLevel || currentLevel.deactivatedByToggle){ currentLevel = { value: 0, dependencies: [], diff --git a/app/imports/api/creature/computation/engine/computeMemo.js b/app/imports/api/creature/computation/engine/computeMemo.js index 39a4c555..5eb3b27e 100644 --- a/app/imports/api/creature/computation/engine/computeMemo.js +++ b/app/imports/api/creature/computation/engine/computeMemo.js @@ -9,7 +9,7 @@ import computeConstant from '/imports/api/creature/computation/engine/computeCon export default function computeMemo(memo){ // Compute all constants that could be used - forOwn(memo.constantsByVariableName, constant => { + forOwn(memo.constantsById, constant => { computeConstant (constant, memo); }); // Compute level diff --git a/app/imports/api/creature/computation/engine/computeStat.js b/app/imports/api/creature/computation/engine/computeStat.js index 2444c316..fcd912c8 100644 --- a/app/imports/api/creature/computation/engine/computeStat.js +++ b/app/imports/api/creature/computation/engine/computeStat.js @@ -22,28 +22,25 @@ export default function computeStat(stat, memo){ // Apply any toggles applyToggles(stat, memo); - if (!stat.deactivatedByToggle){ - // Compute and aggregate all the effects - let aggregator = new EffectAggregator(stat, memo) - each(stat.computationDetails.effects, (effect) => { - computeEffect(effect, memo); - if (effect._id){ - stat.dependencies = union( - stat.dependencies, - [effect._id] - ); - } + // Compute and aggregate all the effects + let aggregator = new EffectAggregator(stat, memo) + each(stat.computationDetails.effects, (effect) => { + computeEffect(effect, memo); + if (effect.deactivatedByToggle) return; + if (effect._id){ stat.dependencies = union( stat.dependencies, - effect.dependencies - ) - if (!effect.deactivatedByToggle){ - aggregator.addEffect(effect); - } - }); - // Conglomerate all the effects to compute the final stat values - combineStat(stat, aggregator, memo); - } + [effect._id] + ); + } + stat.dependencies = union( + stat.dependencies, + effect.dependencies + ) + aggregator.addEffect(effect); + }); + // Conglomerate all the effects to compute the final stat values + combineStat(stat, aggregator, memo); // Mark the attribute as computed stat.computationDetails.computed = true; stat.computationDetails.busyComputing = false; diff --git a/app/imports/api/creature/computation/engine/writeAlteredProperties.js b/app/imports/api/creature/computation/engine/writeAlteredProperties.js index 7f61ef58..3a91c4ba 100644 --- a/app/imports/api/creature/computation/engine/writeAlteredProperties.js +++ b/app/imports/api/creature/computation/engine/writeAlteredProperties.js @@ -8,7 +8,10 @@ export default function writeAlteredProperties(memo){ // Loop through all properties on the memo forOwn(memo.propsById, changed => { let schema = propertySchemasIndex[changed.type]; - if (!schema) return; + if (!schema){ + console.warn('No schema for ' + changed.type); + return; + } let extraIds = changed.computationDetails.idsOfSameName; let ids; if (extraIds && extraIds.length){ diff --git a/app/imports/api/properties/computedOnlyPropertySchemasIndex.js b/app/imports/api/properties/computedOnlyPropertySchemasIndex.js index c97f62f3..0cf8afcc 100644 --- a/app/imports/api/properties/computedOnlyPropertySchemasIndex.js +++ b/app/imports/api/properties/computedOnlyPropertySchemasIndex.js @@ -4,21 +4,22 @@ import { ComputedOnlyAdjustmentSchema } from '/imports/api/properties/Adjustment import { ComputedOnlyAttackSchema } from '/imports/api/properties/Attacks.js'; import { ComputedOnlyAttributeSchema } from '/imports/api/properties/Attributes.js'; import { ComputedOnlyBuffSchema } from '/imports/api/properties/Buffs.js'; -// import { ClassLevelSchema } from '/imports/api/properties/ClassLevels.js'; +import { ClassLevelSchema } from '/imports/api/properties/ClassLevels.js'; +import { ConstantSchema } from '/imports/api/properties/Constants.js'; import { ComputedOnlyContainerSchema } from '/imports/api/properties/Containers.js'; import { ComputedOnlyDamageSchema } from '/imports/api/properties/Damages.js'; import { DamageMultiplierSchema } from '/imports/api/properties/DamageMultipliers.js'; import { ComputedOnlyEffectSchema } from '/imports/api/properties/Effects.js'; import { ComputedOnlyFeatureSchema } from '/imports/api/properties/Features.js'; -// import { FolderSchema } from '/imports/api/properties/Folders.js'; +import { FolderSchema } from '/imports/api/properties/Folders.js'; import { ComputedOnlyItemSchema } from '/imports/api/properties/Items.js'; import { ComputedOnlyNoteSchema } from '/imports/api/properties/Notes.js'; -// import { ProficiencySchema } from '/imports/api/properties/Proficiencies.js'; +import { ProficiencySchema } from '/imports/api/properties/Proficiencies.js'; import { ComputedOnlyRollSchema } from '/imports/api/properties/Rolls.js'; import { ComputedOnlySavingThrowSchema } from '/imports/api/properties/SavingThrows.js'; import { ComputedOnlySkillSchema } from '/imports/api/properties/Skills.js'; import { ComputedOnlySlotSchema } from '/imports/api/properties/Slots.js'; -// import { SlotFillerSchema } from '/imports/api/properties/SlotFillers.js'; +import { SlotFillerSchema } from '/imports/api/properties/SlotFillers.js'; import { ComputedOnlySpellSchema } from '/imports/api/properties/Spells.js'; import { ComputedOnlySpellListSchema } from '/imports/api/properties/SpellLists.js'; import { ComputedOnlyToggleSchema } from '/imports/api/properties/Toggles.js'; @@ -29,23 +30,25 @@ const propertySchemasIndex = { attack: ComputedOnlyAttackSchema, attribute: ComputedOnlyAttributeSchema, buff: ComputedOnlyBuffSchema, - // classLevel: ClassLevelSchema, + classLevel: ClassLevelSchema, + constant: ConstantSchema, + container: ComputedOnlyContainerSchema, damage: ComputedOnlyDamageSchema, damageMultiplier: DamageMultiplierSchema, effect: ComputedOnlyEffectSchema, feature: ComputedOnlyFeatureSchema, - // folder: FolderSchema, + folder: FolderSchema, + item: ComputedOnlyItemSchema, note: ComputedOnlyNoteSchema, - // proficiency: ProficiencySchema, + proficiency: ProficiencySchema, propertySlot: ComputedOnlySlotSchema, roll: ComputedOnlyRollSchema, savingThrow: ComputedOnlySavingThrowSchema, skill: ComputedOnlySkillSchema, + slotFiller: SlotFillerSchema, spellList: ComputedOnlySpellListSchema, spell: ComputedOnlySpellSchema, toggle: ComputedOnlyToggleSchema, - container: ComputedOnlyContainerSchema, - item: ComputedOnlyItemSchema, any: new SimpleSchema({}), };