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.
This commit is contained in:
@@ -1,22 +1,20 @@
|
||||
evaluate = function(character, string){
|
||||
//evaluates a calculation string
|
||||
evaluate = function(charId, string){
|
||||
if(!string) return string;
|
||||
string = string.replace(/\b[a-z]+\b/g, function(sub){
|
||||
//skill mods
|
||||
if(_.has(character.skills, sub)){
|
||||
return +character.skillMod(character.skills[sub]);
|
||||
}
|
||||
//attributes
|
||||
if(_.has(character.attributes, sub)){
|
||||
return +character.attributeValue(character.attributes[sub]);
|
||||
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 +character.abilityMod(character.attributes[slice]);
|
||||
return char.abilityMod(slice);
|
||||
}
|
||||
if(sub === "level"){
|
||||
return +character.level();
|
||||
return char.level();
|
||||
}
|
||||
return sub;
|
||||
});
|
||||
@@ -29,7 +27,9 @@ evaluate = function(character, string){
|
||||
}
|
||||
}
|
||||
|
||||
evaluateString = function(character, 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;
|
||||
@@ -37,7 +37,7 @@ evaluateString = function(character, string){
|
||||
var exp = exp.replace(/(\{|\})/g, "") //remove brackets
|
||||
var span = jQuery('<span/>', {
|
||||
title: exp,
|
||||
text: evaluate(character, exp),
|
||||
text: evaluate(charId, exp),
|
||||
class: "calculatedValue"
|
||||
});
|
||||
return span.prop('outerHTML');
|
||||
@@ -46,11 +46,14 @@ evaluateString = function(character, string){
|
||||
return result;
|
||||
}
|
||||
|
||||
evaluateEffect = function(character, effect){
|
||||
//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(character, effect.calculation);
|
||||
return +evaluate(charId, effect.calculation);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user