From 1e67afbe6f60cdaf89aeb4c1704a3048d6feb255 Mon Sep 17 00:00:00 2001 From: Stefan Zermatten Date: Thu, 13 Jul 2017 10:29:17 +0200 Subject: [PATCH] Made DC available in spell descriptions as a variable closes #101 --- rpg-docs/client/globalHelpers/evaluate.js | 4 ++++ .../spells/spellDialog/spellDialog.html | 2 +- rpg-docs/lib/functions/evaluate.js | 20 ++++++++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/rpg-docs/client/globalHelpers/evaluate.js b/rpg-docs/client/globalHelpers/evaluate.js index d986b1f0..65ad0240 100644 --- a/rpg-docs/client/globalHelpers/evaluate.js +++ b/rpg-docs/client/globalHelpers/evaluate.js @@ -24,6 +24,10 @@ Template.registerHelper("evaluateString", function(charId, string) { return evaluateString(charId, string); }); +Template.registerHelper("evaluateSpellString", function(charId, spellListId, string) { + return evaluateSpellString(charId, spellListId, string); +}); + Template.registerHelper("evaluateShortString", function(charId, string) { if (_.isString(string)){ return evaluateString( diff --git a/rpg-docs/client/views/character/spells/spellDialog/spellDialog.html b/rpg-docs/client/views/character/spells/spellDialog/spellDialog.html index 07d9faa7..7de0fdd8 100644 --- a/rpg-docs/client/views/character/spells/spellDialog/spellDialog.html +++ b/rpg-docs/client/views/character/spells/spellDialog/spellDialog.html @@ -36,7 +36,7 @@ {{/if}} -
{{#markdown}}{{evaluateString charId description}}{{/markdown}}
+
{{#markdown}}{{evaluateSpellString charId parent.id description}}{{/markdown}}
{{> attacksViewList charId=charId parentId=_id}} diff --git a/rpg-docs/lib/functions/evaluate.js b/rpg-docs/lib/functions/evaluate.js index 9fce8c79..77cb34bb 100644 --- a/rpg-docs/lib/functions/evaluate.js +++ b/rpg-docs/lib/functions/evaluate.js @@ -8,7 +8,8 @@ })(); //evaluates a calculation string -evaluate = function(charId, string){ +evaluate = function(charId, string, opts){ + var spellListId = opts && opts.spellListId; if (!string) return string; string = string.replace(/\b[a-z]+\b/gi, function(sub){ //fields @@ -43,6 +44,12 @@ evaluate = function(charId, string){ if (sub.toUpperCase() === "LEVEL"){ return Characters.calculate.level(charId); } + if (spellListId && sub.toUpperCase() === "DC") { + var list = SpellLists.findOne(spellListId); + if (list && list.saveDC){ + return evaluate(charId, list.saveDC); + } + } return sub; }); try { @@ -66,6 +73,17 @@ evaluateString = function(charId, string){ return result; }; +evaluateSpellString = function (charId, spellListId, string) { + //define brackets as curly brackets around anything that isn't a curly bracket + if (!string) return string; + var brackets = /\{[^\{\}]*\}/g; + var result = string.replace(brackets, function(exp){ + exp = exp.replace(/(\{|\})/g, ""); //remove curly brackets + return evaluate(charId, exp, {spellListId}); + }); + return result; +} + //returns the value of the effect if it exists, //otherwise returns the result of the calculation if it exists, //otherwise returns 0