From 7daab97297b8181a130c0a58508de2f907922b05 Mon Sep 17 00:00:00 2001 From: Stefan Zermatten Date: Mon, 1 Mar 2021 11:55:43 +0200 Subject: [PATCH] Made toggles function properly when nested under inactive properties and each other --- .../creature/computation/engine/applyToggles.js | 6 ++++++ .../engine/getComputationProperties.js | 17 +++-------------- .../creatureProperties/methods/equipItem.js | 2 ++ 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/app/imports/api/creature/computation/engine/applyToggles.js b/app/imports/api/creature/computation/engine/applyToggles.js index d1e53233..74839c35 100644 --- a/app/imports/api/creature/computation/engine/applyToggles.js +++ b/app/imports/api/creature/computation/engine/applyToggles.js @@ -2,6 +2,11 @@ import computeToggle from '/imports/api/creature/computation/engine/computeToggl import { union } from 'lodash'; export default function applyToggles(prop, memo){ + // If it used to be inactive delete those fields + if (prop.inactive) prop.inactive = undefined; + if (prop.deactivatedByAncestor) prop.deactivatedByAncestor = undefined; + if (prop.deactivatedByToggle) prop.deactivatedByToggle = undefined; + // Iterate through the toggle ancestors from oldest to nearest prop.computationDetails.toggleAncestors.forEach(toggleId => { let toggle = memo.togglesById[toggleId]; computeToggle(toggle, memo); @@ -10,6 +15,7 @@ export default function applyToggles(prop, memo){ [toggle._id], toggle.dependencies, ); + // Deactivate if the toggle is false if (!toggle.toggleResult){ prop.inactive = true; prop.deactivatedByAncestor = true; diff --git a/app/imports/api/creature/computation/engine/getComputationProperties.js b/app/imports/api/creature/computation/engine/getComputationProperties.js index 96a75b76..aee7575e 100644 --- a/app/imports/api/creature/computation/engine/getComputationProperties.js +++ b/app/imports/api/creature/computation/engine/getComputationProperties.js @@ -1,15 +1,6 @@ import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js'; export default function getComputationProperties(creatureId){ - // find ids of all toggles that have conditions, even if they are inactive - let toggleIds = CreatureProperties.find({ - 'ancestors.id': creatureId, - type: 'toggle', - removed: {$ne: true}, - condition: { $exists: true }, - }, { - fields: {_id: 1}, - }).map(t => t._id); // Find all the relevant properties return CreatureProperties.find({ 'ancestors.id': creatureId, @@ -17,17 +8,15 @@ export default function getComputationProperties(creatureId){ $or: [ // All active properties {inactive: {$ne: true}}, - // All active and inactive toggles with conditions - // Same as {$in: toggleIds}, but should be slightly faster - {type: 'toggle', condition: { $exists: true }}, - // All decendents of the above toggles - {'ancestors.id': {$in: toggleIds}}, + // Unless they were deactivated because of a toggle + {deactivatedByToggle: true}, ] }, { // Filter out fields never used by calculations fields: { icon: 0, }, + // Obey tree order sort: { order: 1, } diff --git a/app/imports/api/creature/creatureProperties/methods/equipItem.js b/app/imports/api/creature/creatureProperties/methods/equipItem.js index 4e043908..ab396c9f 100644 --- a/app/imports/api/creature/creatureProperties/methods/equipItem.js +++ b/app/imports/api/creature/creatureProperties/methods/equipItem.js @@ -5,6 +5,7 @@ import { assertEditPermission } from '/imports/api/sharing/sharingPermissions.js import { organizeDoc } from '/imports/api/parenting/organizeMethods.js'; import getRootCreatureAncestor from '/imports/api/creature/creatureProperties/getRootCreatureAncestor.js'; import { recomputeCreatureByDoc } from '/imports/api/creature/computation/methods/recomputeCreature.js'; +import recomputeInactiveProperties from '/imports/api/creature/denormalise/recomputeInactiveProperties.js'; import recomputeInventory from '/imports/api/creature/denormalise/recomputeInventory.js'; import INVENTORY_TAGS from '/imports/constants/INVENTORY_TAGS.js'; @@ -62,6 +63,7 @@ const equipItem = new ValidatedMethod({ skipRecompute: true, }); + recomputeInactiveProperties(creature._id); recomputeInventory(creature._id); recomputeCreatureByDoc(creature); },