Files
DiceCloud/rpg-docs/lib/functions/evaluate.js
Thaum ca7a625534 Consolidated all sub-documents into character document to improve granularity significantly
Because just the top-level field changing invalidates everything in the sub-document, a single attribute change would trigger a re-calculation on all attributes and their dependents. This change should significantly improve performance.
2014-11-20 09:02:10 +00:00

60 lines
1.7 KiB
JavaScript

//evaluates a calculation string
evaluate = function(charId, string){
if(!string) return string;
var char = Characters.findOne(charId, {fields: {_id: 1}});
string = string.replace(/\b[a-zA-Z]+\b/g, function(sub){
//fields
if(Schemas.Character.schema(sub)){
return char.fieldValue(sub)
}
//ability modifiers
var abilityMods = ["strengthMod", "dexterityMod", "constitutionMod", "intelligenceMod", "wisdomMod", "charismaMod"]
if( _.contains(abilityMods, sub) ){
var slice = sub.slice(0, - 3);
return char.abilityMod(slice);
}
if(sub === "level"){
return char.level();
}
return sub;
});
try{
result = math.eval(string);
return result
} catch(e){
console.log(e)
return string;
}
}
//takes a string with {calculations} and returns it with the results
//of the calculations returned in place
evaluateString = function(charId, 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){
var exp = exp.replace(/(\{|\})/g, "") //remove brackets
var span = jQuery('<span/>', {
title: exp,
text: evaluate(charId, exp),
class: "calculatedValue"
});
return span.prop('outerHTML');
});
//this is going to return HTML, ensure it is santized!
return result;
}
//returns the value of the effect if it exists,
//otherwise returns the result of the calculation if it exists,
//otherwise returns 0
evaluateEffect = function(charId, effect){
if(_.isFinite(effect.value)){
return effect.value;
} else if(_.isString(effect.calculation)){
return +evaluate(charId, effect.calculation);
} else {
return 0;
}
}