diff --git a/app/imports/api/creature/actions/applyProperties.js b/app/imports/api/creature/actions/applyProperties.js index 2326187e..aa6355eb 100644 --- a/app/imports/api/creature/actions/applyProperties.js +++ b/app/imports/api/creature/actions/applyProperties.js @@ -1,8 +1,9 @@ import applyAction from '/imports/api/creature/actions/applyAction.js'; 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 applyDamage from '/imports/api/creature/actions/applyDamage.js'; +import applyRoll from '/imports/api/creature/actions/applyRoll.js'; import applyToggle from '/imports/api/creature/actions/applyToggle.js'; function applyProperty(options){ @@ -42,7 +43,7 @@ function applyProperty(options){ case 'toggle': return applyToggle(options); case 'roll': - // applyRoll(options); + applyRoll(options); break; case 'savingThrow': // applySavingThrow(options); diff --git a/app/imports/api/creature/actions/applyRoll.js b/app/imports/api/creature/actions/applyRoll.js new file mode 100644 index 00000000..16921e63 --- /dev/null +++ b/app/imports/api/creature/actions/applyRoll.js @@ -0,0 +1,32 @@ +import evaluateString from '/imports/api/creature/computation/afterComputation/evaluateString.js'; + +export default function applyRoll({ + prop, + creature, + actionContext, + log, +}){ + let scope = { + ...creature.variables, + ...actionContext, + }; + try { + var {result, errors} = evaluateString(prop.roll, scope, 'reduce'); + actionContext[prop.variableName] = result; + log.content.push({ + name: prop.name, + resultPrefix: prop.variableName + ' = ' + prop.roll + ' = ', + result, + }); + if (errors.length) { + log.content.push({ + error: errors.join(', '), + }); + } + + } catch (e){ + log.content.push({ + error: e.toString(), + }); + } +} diff --git a/app/imports/api/creature/actions/applyToggle.js b/app/imports/api/creature/actions/applyToggle.js index a8fbc64c..efafc168 100644 --- a/app/imports/api/creature/actions/applyToggle.js +++ b/app/imports/api/creature/actions/applyToggle.js @@ -1,6 +1,6 @@ import evaluateString from '/imports/api/creature/computation/afterComputation/evaluateString.js'; -export default function applyDamage({ +export default function applyToggle({ prop, creature, actionContext, diff --git a/app/imports/api/properties/Rolls.js b/app/imports/api/properties/Rolls.js index ec2b1337..c68a40b8 100644 --- a/app/imports/api/properties/Rolls.js +++ b/app/imports/api/properties/Rolls.js @@ -1,5 +1,6 @@ import SimpleSchema from 'simpl-schema'; import ErrorSchema from '/imports/api/properties/subSchemas/ErrorSchema.js'; +import VARIABLE_NAME_REGEX from '/imports/constants/VARIABLE_NAME_REGEX.js'; /** * Rolls are children to actions or other rolls, they are triggered with 0 or @@ -20,6 +21,17 @@ import ErrorSchema from '/imports/api/properties/subSchemas/ErrorSchema.js'; * child rolls are applied */ let RollSchema = new SimpleSchema({ + name: { + type: String, + defaultValue: 'New Roll', + }, + // The technical, lowercase, single-word name used in formulae + variableName: { + type: String, + regEx: VARIABLE_NAME_REGEX, + min: 2, + defaultValue: 'newRoll', + }, // The roll, can be simplified, but only computed in context roll: { type: String, diff --git a/app/imports/ui/properties/forms/RollForm.vue b/app/imports/ui/properties/forms/RollForm.vue index 1118495c..eaa2e589 100644 --- a/app/imports/ui/properties/forms/RollForm.vue +++ b/app/imports/ui/properties/forms/RollForm.vue @@ -1,5 +1,21 @@