From 6571fb860a279d35cd609558b5ae1b7158f6c21e Mon Sep 17 00:00:00 2001 From: Stefan Zermatten Date: Mon, 22 Feb 2021 11:36:30 +0200 Subject: [PATCH] Toggles now work in actions to make choices based on action context --- .../api/creature/actions/applyAttack.js | 1 + .../api/creature/actions/applyProperties.js | 10 +++++- .../api/creature/actions/applyToggle.js | 35 +++++++++++++++++++ .../creatureProperties/CreatureProperties.js | 7 ++++ .../recomputeInactiveProperties.js | 21 ++++++++--- 5 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 app/imports/api/creature/actions/applyToggle.js diff --git a/app/imports/api/creature/actions/applyAttack.js b/app/imports/api/creature/actions/applyAttack.js index d933976e..977ccc67 100644 --- a/app/imports/api/creature/actions/applyAttack.js +++ b/app/imports/api/creature/actions/applyAttack.js @@ -8,6 +8,7 @@ export default function applyAttack({ let value = roll(1, 20)[0]; actionContext.attackRoll = {value}; let result = value + prop.rollBonusResult; + actionContext.toHit = {value: result}; log.content.push({ name: 'To Hit', resultPrefix: `1d20 [${value}] + ${prop.rollBonusResult} = `, diff --git a/app/imports/api/creature/actions/applyProperties.js b/app/imports/api/creature/actions/applyProperties.js index ece1aee3..2326187e 100644 --- a/app/imports/api/creature/actions/applyProperties.js +++ b/app/imports/api/creature/actions/applyProperties.js @@ -3,6 +3,7 @@ import applyAdjustment from '/imports/api/creature/actions/applyAdjustment.js'; import applyAttack from '/imports/api/creature/actions/applyAttack.js'; import applyDamage from '/imports/api/creature/actions/applyDamage.js'; import applyBuff from '/imports/api/creature/actions/applyBuff.js'; +import applyToggle from '/imports/api/creature/actions/applyToggle.js'; function applyProperty(options){ let prop = options.prop; @@ -11,8 +12,13 @@ function applyProperty(options){ if (prop.applied === true){ return false; } + // Only ignore toggles if they wont be computed + } else if (prop.type === 'toggle') { + if (prop.disabled) return false; + if (prop.enabled) return true; + if (!prop.condition) return false; // Ignore inactive props of other types - } else if (prop.inactive === true){ + } else if (prop.deactivatedBySelf === true){ return false; } switch (prop.type){ @@ -33,6 +39,8 @@ function applyProperty(options){ case 'buff': applyBuff(options); break; + case 'toggle': + return applyToggle(options); case 'roll': // applyRoll(options); break; diff --git a/app/imports/api/creature/actions/applyToggle.js b/app/imports/api/creature/actions/applyToggle.js new file mode 100644 index 00000000..a8fbc64c --- /dev/null +++ b/app/imports/api/creature/actions/applyToggle.js @@ -0,0 +1,35 @@ +import evaluateString from '/imports/api/creature/computation/afterComputation/evaluateString.js'; + +export default function applyDamage({ + prop, + creature, + actionContext, + log, +}){ + let scope = { + ...creature.variables, + ...actionContext, + }; + if (Number.isFinite(+prop.condition)){ + return !!+prop.condition; + } + try { + var {result, errors} = evaluateString(prop.condition, scope, 'reduce'); + if (typeof result !== 'number' && typeof result !== 'boolean') { + log.content.push({ + error: errors.join(', '), + }); + return false; + } + log.content.push({ + name: prop.name, + resultPrefix: prop.condition + ' = ', + result, + }); + return !!result; + } catch (e){ + log.content.push({ + error: e.toString(), + }); + } +} diff --git a/app/imports/api/creature/creatureProperties/CreatureProperties.js b/app/imports/api/creature/creatureProperties/CreatureProperties.js index aa20f7df..f12916c9 100644 --- a/app/imports/api/creature/creatureProperties/CreatureProperties.js +++ b/app/imports/api/creature/creatureProperties/CreatureProperties.js @@ -43,6 +43,13 @@ let CreaturePropertySchema = new SimpleSchema({ optional: true, index: 1, }, + // Denormalised flag if this property was made inactive because of its own + // state + deactivatedBySelf: { + type: Boolean, + optional: true, + index: 1, + }, // Denormalised list of all properties or creatures this property depends on dependencies: { type: Array, diff --git a/app/imports/api/creature/denormalise/recomputeInactiveProperties.js b/app/imports/api/creature/denormalise/recomputeInactiveProperties.js index e53f05ee..d719e096 100644 --- a/app/imports/api/creature/denormalise/recomputeInactiveProperties.js +++ b/app/imports/api/creature/denormalise/recomputeInactiveProperties.js @@ -20,9 +20,16 @@ export default function recomputeInactiveProperties(ancestorId){ CreatureProperties.update({ 'ancestors.id': ancestorId, '_id': {$in: disabledIds}, - $or: [{inactive: {$ne: true}}, {deactivatedByAncestor: true}], + $or: [ + {inactive: {$ne: true}}, + {deactivatedBySelf: {$ne: true}}, + {deactivatedByAncestor: true}, + ], }, { - $set: {inactive: true}, + $set: { + inactive: true, + deactivatedBySelf: true, + }, $unset: {deactivatedByAncestor: 1}, }, { multi: true, @@ -31,7 +38,10 @@ export default function recomputeInactiveProperties(ancestorId){ // Decendants of inactive properties CreatureProperties.update({ 'ancestors.id': {$eq: ancestorId, $in: disabledIds}, - $or: [{inactive: {$ne: true}}, {deactivatedByAncestor: {$ne: true}}], + $or: [ + {inactive: {$ne: true}}, + {deactivatedByAncestor: {$ne: true}}, + ], }, { $set: { inactive: true, @@ -46,7 +56,10 @@ export default function recomputeInactiveProperties(ancestorId){ CreatureProperties.update({ 'ancestors.id': {$eq: ancestorId, $nin: disabledIds}, '_id': {$nin: disabledIds}, - $or: [{inactive: true}, {deactivatedByAncestor: true}], + $or: [ + {inactive: true}, + {deactivatedByAncestor: true}, + ], }, { $unset: { inactive: 1,