Merge branch 'feature-soft-memoize' into develop

This commit is contained in:
Stefan Zermatten
2015-06-25 13:35:19 +02:00
27 changed files with 438 additions and 297 deletions

View File

@@ -3,16 +3,16 @@ Characters = new Mongo.Collection("characters");
Schemas.Character = new SimpleSchema({
//strings
name: {type: String, defaultValue: "", trim: false},
alignment: {type: String, defaultValue: "", trim: false},
gender: {type: String, defaultValue: "", trim: false},
race: {type: String, defaultValue: "", trim: false},
description: {type: String, defaultValue: "", trim: false},
personality: {type: String, defaultValue: "", trim: false},
ideals: {type: String, defaultValue: "", trim: false},
bonds: {type: String, defaultValue: "", trim: false},
flaws: {type: String, defaultValue: "", trim: false},
backstory: {type: String, defaultValue: "", trim: false},
name: {type: String, defaultValue: "", trim: false, optional: true},
alignment: {type: String, defaultValue: "", trim: false, optional: true},
gender: {type: String, defaultValue: "", trim: false, optional: true},
race: {type: String, defaultValue: "", trim: false, optional: true},
description: {type: String, defaultValue: "", trim: false, optional: true},
personality: {type: String, defaultValue: "", trim: false, optional: true},
ideals: {type: String, defaultValue: "", trim: false, optional: true},
bonds: {type: String, defaultValue: "", trim: false, optional: true},
flaws: {type: String, defaultValue: "", trim: false, optional: true},
backstory: {type: String, defaultValue: "", trim: false, optional: true},
//attributes
//ability scores
@@ -190,91 +190,101 @@ var attributeBase = function(charId, statName){
check(statName, String);
//if it's a damage multiplier, we treat it specially
if (_.contains(DAMAGE_MULTIPLIERS, statName)){
var effects = Effects.find(
{charId: charId, stat: statName, enabled: true, operation: "mul"}
).fetch();
var resistCount = 0;
var vulnCount = 0;
var multiplierEvaluationFail = false;
_.each(effects, function(effect){
var val = evaluateEffect(charId, effect);
if (val === 0.5){ //resistance
resistCount += 1;
} else if (val === 2){ //vulnerability
vulnCount += 1;
} else if (val === 0){ //imunity
return 0; //imunity is absolute
} else {
multiplierEvaluationFail = true;
}
});
if (multiplierEvaluationFail){
//we can't work it out correctly, set the value to 1
//and try work it out using regular maths below
value = 1;
} else if (resistCount && !vulnCount){
var invulnerabilityCount = Effects.find({
charId: charId,
stat: statName,
enabled: true,
operation: "mul",
value: 0,
}).count();
if (invulnerabilityCount) return 0;
var resistCount = Effects.find({
charId: charId,
stat: statName,
enabled: true,
operation: "mul",
value: 0.5,
}).count();
var vulnCount = Effects.find({
charId: charId,
stat: statName,
enabled: true,
operation: "mul",
value: 2,
}).count();
if (!resistCount && !vulnCount){
return 1;
} else if (resistCount && !vulnCount){
return 0.5;
} else if (!resistCount && vulnCount){
} else if (!resistCount && vulnCount){
return 2;
} else {
return 1;
}
}
var value;
var base = 0;
var add = 0;
var mul = 1;
var min = Math.NEGATIVE_INFINITY;
var max = Math.POSITIVE_INFINITY;
var value = 0;
//start with the highest base value
Effects.find(
{charId: charId, stat: statName, enabled: true, operation: "base"}
).forEach(function(effect){
var efv = evaluateEffect(charId, effect);
if (efv > value){
value = efv;
Effects.find({
charId: charId,
stat: statName,
enabled: true,
operation: {$in: ["base", "add", "mul", "min", "max"]},
}).forEach(function(effect) {
value = evaluateEffect(charId, effect);
if (effect.operation === "base"){
if (value > base) base = value;
} else if (effect.operation === "add"){
add += value;
} else if (effect.operation === "mul"){
mul *= value;
} else if (effect.operation === "min"){
if (value > min) min = value;
} else if (effect.operation === "max"){
if (value < max) max = value;
}
});
//add all the add values
Effects.find(
{charId: charId, stat: statName, enabled: true, operation: "add"}
).forEach(function(effect){
value += evaluateEffect(charId, effect);
});
var result = (base + add) * mul;
if (result < min) result = min;
if (result > max) result = max;
//multiply all the mul values
Effects.find(
{charId: charId, stat: statName, enabled: true, operation: "mul"}
).forEach(function(effect){
value *= evaluateEffect(charId, effect);
});
//ensure value is >= all mins
Effects.find(
{charId: charId, stat: statName, enabled: true, operation: "min"}
).forEach(function(effect){
var min = evaluateEffect(charId, effect);
value = value > min ? value : min;
});
//ensure value is <= all maxes
Effects.find(
{charId: charId, stat: statName, enabled: true, operation: "max"}
).forEach(function(effect){
var max = evaluateEffect(charId, effect);
value = value < max ? value : max;
});
return value;
return result;
};
//functions and calculated values.
//These functions can only rely on this._id since no other
//field is likely to be attached to all returned characters
Characters.helpers({
//returns the value stored in the field requested
//will set up dependencies on just that field
getField : function(fieldName){
if (Meteor.isClient) {
Template.registerHelper("characterCalculate", function(func, charId, input) {
try {
return Characters.calculate[func](charId, input);
} catch (e){
if (!Characters.calculate[func]){
throw new Error(func + "is not a function name");
} else {
throw e;
}
}
});
}
//create a local memoize with a argument concatenating hash function
var memoize = function(f) {
return Tracker.memoize(f, function() {
return _.reduce(arguments, function(memo, arg) {
return memo + arg;
}, "");
});
};
//memoize funcitons that have finds and slow loops
Characters.calculate = {
getField: function(charId, fieldName) {
var fieldSelector = {};
fieldSelector[fieldName] = 1;
var char = Characters.findOne(this._id, {fields: fieldSelector});
var char = Characters.findOne(charId, {fields: fieldSelector});
var field = char[fieldName];
if (field === undefined){
throw new Meteor.Error(
@@ -287,8 +297,7 @@ Characters.helpers({
}
return field;
},
//returns the value of a field
fieldValue : function(fieldName){
fieldValue: function(charId, fieldName) {
if (!Schemas.Character.schema(fieldName)){
throw new Meteor.Error(
"Field not found",
@@ -298,102 +307,92 @@ Characters.helpers({
//duck typing to get the right value function
//.ability implies skill
if (Schemas.Character.schema(fieldName + ".ability")){
return this.skillMod(fieldName);
return Characters.calculate.skillMod(charId, fieldName);
}
//adjustment implies attribute
if (Schemas.Character.schema(fieldName + ".adjustment")){
return this.attributeValue(fieldName);
return Characters.calculate.attributeValue(charId, fieldName);
}
//fall back to just returning the field itself
return this.getField(fieldName);
return Characters.calculate.getField(charId, fieldName);
},
attributeValue: function(attributeName){
var charId = this._id;
var attribute = this.getField(attributeName);
attributeValue: memoize(function(charId, attributeName){
var attribute = Characters.calculate.getField(charId, attributeName);
//base value
var value = this.attributeBase(attributeName);
var value = Characters.calculate.attributeBase(charId, attributeName);
//plus adjustment
value += attribute.adjustment;
return value;
},
attributeBase: preventLoop(function(attributeName){
var charId = this._id;
//base value
return attributeBase(charId, attributeName);
}),
skillMod: preventLoop(function(skillName){
var charId = this._id;
var skill = this.getField(skillName);
attributeBase: memoize(preventLoop(function(charId, attributeName){
return attributeBase(charId, attributeName);
})),
skillMod: memoize(preventLoop(function(charId, skillName){
var skill = Characters.calculate.getField(charId, skillName);
//get the final value of the ability score
var ability = this.attributeValue(skill.ability);
var ability = Characters.calculate.attributeValue(charId, skill.ability);
//base modifier
var mod = +getMod(ability);
//multiply proficiency bonus by largest value in proficiency array
var prof = this.proficiency(skillName);
var prof = Characters.calculate.proficiency(charId, skillName);
//add multiplied proficiency bonus to modifier
mod += prof * this.attributeValue("proficiencyBonus");
mod += prof * Characters.calculate.attributeValue(charId, "proficiencyBonus");
//apply all effects
var rawEffects = Effects.find(
{charId: charId, stat: skillName, enabled: true}
).fetch();
var effects = _.groupBy(rawEffects, "operation");
_.forEach(effects.add, function(effect){
mod += evaluateEffect(charId, effect);
});
_.forEach(effects.mul, function(effect){
mod *= evaluateEffect(charId, effect);
});
_.forEach(effects.min, function(effect){
var min = evaluateEffect(charId, effect);
mod = mod > min ? mod : min;
});
_.forEach(effects.max, function(effect){
var max = evaluateEffect(charId, effect);
mod = mod < max ? mod : max;
});
return signedString(mod);
}),
var value;
var add = 0;
var mul = 1;
var min = Math.NEGATIVE_INFINITY;
var max = Math.POSITIVE_INFINITY;
proficiency: function(skillName){
var charId = this._id;
//return largest value in proficiency array
var prof = 0;
Proficiencies.find(
{charId: charId, name: skillName, enabled: true}
).forEach(function(proficiency){
var newProf = proficiency.value;
if (newProf > prof){
prof = newProf;
Effects.find({
charId: charId,
stat: skillName,
enabled: true,
operation: {$in: ["base", "add", "mul", "min", "max"]},
}).forEach(function(effect) {
value = evaluateEffect(charId, effect);
if (effect.operation === "add"){
add += value;
} else if (effect.operation === "mul"){
mul *= value;
} else if (effect.operation === "min"){
if (value > min) min = value;
} else if (effect.operation === "max"){
if (value < max) max = value;
}
});
return prof;
},
var result = (mod + add) * mul;
if (result < min) result = min;
if (result > max) result = max;
passiveSkill: function(skillName){
if (_.isString(skillName)){
var skill = this.getField(skillName);
}
var charId = this._id;
var mod = +this.skillMod(skillName);
return result;
})),
proficiency: memoize(function(charId, skillName){
//return largest value in proficiency array
var prof = Proficiencies.findOne(
{charId: charId, name: skillName, enabled: true},
{sort: {value: -1}}
);
return prof && prof.value || 0;
}),
passiveSkill: memoize(function(charId, skillName){
var skill = Characters.calculate.getField(charId, skillName);
var mod = +Characters.calculate.skillMod(charId, skillName);
var value = 10 + mod;
Effects.find(
{charId: charId, stat: skillName, enabled: true, operation: "passiveAdd"}
).forEach(function(effect){
value += evaluateEffect(charId, effect);
});
var advantage = Characters.calculate.advantage(charId, skillName);
value += 5 * advantage;
return value;
//TODO decide whether (dis)advantage gives (-)+5 to passive checks
},
advantage: function(skillName){
var charId = this._id;
}),
advantage: memoize(function(charId, skillName){
var advantage = Effects.find(
{charId: charId, stat: skillName, enabled: true, operation: "advantage"}
).count();
@@ -403,19 +402,18 @@ Characters.helpers({
if (advantage && !disadvantage) return 1;
if (disadvantage && !advantage) return -1;
return 0;
}),
abilityMod: function(charId, attribute){
return getMod(
Characters.calculate.attributeValue(charId, attribute)
);
},
abilityMod: function(attribute){
return signedString(getMod(this.attributeValue(attribute)));
},
passiveAbility: function(attribute){
var mod = +getMod(this.attributeValue(attribute));
passiveAbility: function(charId, attribute){
var mod = +getMod(Characters.calculate.attributeValue(charId, attribute));
return 10 + mod;
},
xpLevel: function(){
var xp = this.experience();
xpLevel: function(charId){
var xp = Characters.calculate.experience(charId);
for (var i = 0; i < 19; i++){
if (xp < XP_TABLE[i]){
return i;
@@ -424,30 +422,103 @@ Characters.helpers({
if (xp > 355000) return 20;
return 0;
},
level: function(){
level: memoize(function(charId){
var level = 0;
Classes.find({charId: this._id}).forEach(function(cls){
Classes.find({charId: charId}).forEach(function(cls){
level += cls.level;
});
return level;
},
experience: function(){
}),
experience: memoize(function(charId){
var xp = 0;
Experiences.find(
{charId: this._id},
{charId: charId},
{fields: {value: 1}}
).forEach(function(e){
xp += e.value;
});
return xp;
}),
};
var depreciated = function() {
//var err = new Error("this function has been depreciated");
var name = "";
if (Template.instance()){
name = Template.instance().view.name;
}
var logString = "this function has been depreciated \n";
if (name){
logString += "View: " + name + "\n\n";
}
//logString += err.stack + "\n\n---------------------\n\n";
console.log(logString);
};
//functions and calculated values.
//These functions can only rely on this._id since no other
//field is likely to be attached to all returned characters
Characters.helpers({
//returns the value stored in the field requested
//will set up dependencies on just that field
getField : function(fieldName){
depreciated();
return Characters.calculate.getField(this._id, fieldName);
},
//returns the value of a field
fieldValue : function(fieldName){
depreciated();
return Characters.calculate.fieldValue(this._id, fieldName);
},
attributeValue: function(attributeName){
depreciated();
return Characters.calculate.attributeValue(this._id, attributeName);
},
attributeBase: function(attributeName){
depreciated();
return Characters.calculate.attributeBase(this._id, attributeName);
},
skillMod: function(skillName){
depreciated();
return Characters.calculate.skillMod(this._id, skillName);
},
proficiency: function(skillName){
depreciated();
return Characters.calculate.proficiency(this._id, skillName);
},
passiveSkill: function(skillName){
depreciated();
return Characters.calculate.passiveSkill(this._id, skillName);
},
advantage: function(skillName){
depreciated();
return Characters.calculate.advantage(this._id, skillName);
},
abilityMod: function(attribute){
depreciated();
return Characters.calculate.abilityMod(this._id, attribute);
},
passiveAbility: function(attribute){
depreciated();
return Characters.calculate.passiveAbility(this._id, attribute);
},
xpLevel: function(){
depreciated();
return Characters.calculate.xpLevel(this._id);
},
level: function(){
depreciated();
return Characters.calculate.level(this._id);
},
experience: function(){
depreciated();
return Characters.calculate.experience(this._id);
},
});
//clean up all data related to that character before removing it
Characters.after.remove(function(userId, character) {
if (Meteor.isServer){
if (Meteor.isServer){
Characters.after.remove(function(userId, character) {
Actions .remove({charId: character._id});
Attacks .remove({charId: character._id});
Buffs .remove({charId: character._id});
@@ -460,8 +531,8 @@ Characters.after.remove(function(userId, character) {
SpellLists .remove({charId: character._id});
Items .remove({charId: character._id});
Containers .remove({charId: character._id});
}
});
});
}
Characters.allow({
insert: function(userId, doc) {

View File

@@ -3,10 +3,6 @@
* Damage, healing and resource cost/recovery are all adjustments
*/
Schemas.Adjustment = new SimpleSchema({
name: {
type: String,
optional: true,
},
//which stat the adjustment is applied to
stat: {
type: String,

View File

@@ -20,7 +20,7 @@ Template.characterSheet.helpers({
hideSpellcasting: function() {
var char = Characters.findOne(this._id);
return char && char.settings.hideSpellcasting;
}
},
});
Template.characterSheet.events({

View File

@@ -135,7 +135,7 @@
</template>
<template name="resource">
{{#if char.attributeBase name}}
{{#if characterCalculate "attributeBase" char._id name}}
<paper-shadow class="card"
hero-id="main" {{detailHero name char._id}}
layout horizontal>
@@ -152,7 +152,7 @@
disabled={{cantDecrement}}>
</paper-icon-button>
</div>
<div>{{char.attributeValue name}}</div>
<div>{{characterCalculate "attributeValue" char._id name}}</div>
<!--<div>/{{char.attributeBase name}}</div>-->
</div>
<div class="right clickable"

View File

@@ -96,12 +96,14 @@ Template.features.events({
Template.resource.helpers({
cantIncrement: function(){
var baseBigger = this.char.attributeValue(this.name) <
this.char.attributeBase(this.name);
var value = Characters.calculate.attributeValue(this.char._id, this.name);
var base = Characters.calculate.attributeBase(this.char._id, this.name);
var baseBigger = value < base;
return !baseBigger;
},
cantDecrement: function(){
var valuePositive = this.char.attributeValue(this.name) > 0;
var value = Characters.calculate.attributeValue(this.char._id, this.name);
var valuePositive = value > 0;
return !valuePositive;
},
getColor: function(){
@@ -115,14 +117,17 @@ Template.resource.helpers({
Template.resource.events({
"tap .resourceUp": function(event){
if (this.char.attributeValue(this.name) < this.char.attributeBase(this.name)){
var value = Characters.calculate.attributeValue(this.char._id, this.name);
var base = Characters.calculate.attributeBase(this.char._id, this.name);
if (value < base){
var modifier = {$inc: {}};
modifier.$inc[this.name + ".adjustment"] = 1;
Characters.update(this.char._id, modifier, {validate: false});
}
},
"tap .resourceDown": function(event){
if (this.char.attributeValue(this.name) > 0){
var value = Characters.calculate.attributeValue(this.char._id, this.name);
if (value > 0){
var modifier = {$inc: {}};
modifier.$inc[this.name + ".adjustment"] = -1;
Characters.update(this.char._id, modifier, {validate: false});

View File

@@ -13,7 +13,7 @@ var getFractionCarried = function(char) {
weight += item.totalWeight();
});
//get strength
var strength = char.attributeValue("strength");
var strength = Characters.calculate.attributeValue(char._id, "strength");
var capacity = strength * 15;
return weight / capacity;
};

View File

@@ -9,7 +9,7 @@
hero-id="toolbar" {{detailHero}}
layout horizontal center>
<div flex>Experience</div>
<div >{{experience}} XP</div>
<div >{{characterCalculate "experience" _id}} XP</div>
<paper-icon-button class="black54" id="addXP" icon="add"></paper-icon-button>
</div>
<div class="bottom list">
@@ -45,7 +45,7 @@
layout horizontal center>
<div flex>
<div class="containerName subhead">
Level {{level}}
Level {{characterCalculate "level" _id}}
</div>
{{#if nextLevelXP}}
<div class="caption">

View File

@@ -41,7 +41,7 @@ Template.journal.helpers({
return Levels.find({charId: charId, classId: this._id}, {sort: {value: 1}});
},
nextLevelXP: function(){
var currentLevel = this.level();
var currentLevel = Characters.calculate.level(this._id);
if (currentLevel < 20){
return XP_TABLE[currentLevel];
}

View File

@@ -84,32 +84,28 @@ Template.spells.helpers({
},
cantCast: function(level, char){
for (var i = level; i <= 9; i++){
if (char.attributeValue("level" + i + "SpellSlots") > 0){
if (Characters.calculate.attributeValue(char._id, "level" + i + "SpellSlots") > 0){
return false;
}
}
return true;
},
baseSlots: function(char){
return char.attributeBase("level" + this.level + "SpellSlots");
},
slots: function(char){
return char.attributeValue("level" + this.level + "SpellSlots");
},
showSlots: function(char){
return this.level && char.attributeBase("level" + this.level + "SpellSlots");
return this.level && Characters.calculate.attributeBase(
char._id, "level" + this.level + "SpellSlots"
);
},
hasSlots: function(){
for (var i = 1; i <= 9; i += 1){
if (this.attributeBase("level" + i + "SpellSlots")){
if (Characters.calculate.attributeBase(this._id, "level" + i + "SpellSlots")){
return true;
}
}
return false;
},
slotBubbles: function(char){
var baseSlots = char.attributeBase("level" + this.level + "SpellSlots");
var currentSlots = char.attributeValue("level" + this.level + "SpellSlots");
var baseSlots = Characters.calculate.attributeBase(char._id, "level" + this.level + "SpellSlots");
var currentSlots = Characters.calculate.attributeValue(char._id, "level" + this.level + "SpellSlots");
var slotsUsed = baseSlots - currentSlots;
var bubbles = [];
var i;
@@ -143,15 +139,15 @@ Template.spells.events({
var char = Characters.findOne(this.charId);
if (event.currentTarget.icon === "radio-button-off"){
if (
char.attributeValue(this.attribute) <
char.attributeBase(this.attribute)
Characters.calculate.attributeValue(char._id, this.attribute) <
Characters.calculate.attributeBase(char._id, this.attribute)
){
modifier = {$inc: {}};
modifier.$inc[this.attribute + ".adjustment"] = 1;
Characters.update(this.charId, modifier, {validate: false});
}
} else {
if (char.attributeValue(this.attribute) > 0){
if (Characters.calculate.attributeValue(char._id, this.attribute) > 0){
modifier = {$inc: {}};
modifier.$inc[this.attribute + ".adjustment"] = -1;
Characters.update(this.charId, modifier, {validate: false});

View File

@@ -4,8 +4,8 @@
layout horizontal>
<div class="left white-text {{color}}"
hero-id="toolbar" {{detailHero ability ../_id}}>
<div class="display1">{{../attributeValue ability}}</div>
<div class="title">{{../abilityMod ability}}</div>
<div class="display1">{{characterCalculate "attributeValue" ../_id ability}}</div>
<div class="title">{{abilityMod}}</div>
</div>
<div class="right subhead" layout horizontal center>
{{title}}

View File

@@ -0,0 +1,9 @@
Template.abilityMiniCard.helpers({
abilityMod: function() {
return signedString(
Characters.calculate.abilityMod(
Template.parentData()._id, this.ability
)
);
}
});

View File

@@ -106,10 +106,8 @@ Template.attributeDialogView.helpers({
return a || b || c;
},
adjustment: function(){
var char = Characters.findOne(this.charId);
if (!char) return;
var value = char.attributeValue(this.statName);
var base = char.attributeBase(this.statName);
var value = Characters.calculate.attributeValue(this.charId, this.statName);
var base = Characters.calculate.attributeBase(this.charId, this.statName);
return value - base;
},
baseEffects: function(){
@@ -138,14 +136,10 @@ Template.attributeDialogView.helpers({
);
},
attributeBase: function(){
var char = Characters.findOne(this.charId);
if (!char) throw "character is " + char;
return char.attributeBase(this.statName);
return Characters.calculate.attributeBase(this.charId, this.statName);
},
attributeValue: function() {
var char = Characters.findOne(this.charId);
if (!char) throw "character is " + char;
return char.attributeValue(this.statName);
return Characters.calculate.attributeValue(this.charId, this.statName);
},
sourceName: function(){
if (this.parent.group === "racial"){

View File

@@ -11,11 +11,11 @@
<div class="right" flex layout vertical center-justified style="min-width: 180px;">
<div layout horizontal>
<paper-diff-slider id="hitPointSlider"
value={{attributeValue "hitPoints"}}
max={{attributeBase "hitPoints"}}
editable pin
role="slider"
></paper-diff-slider>
value={{characterCalculate "attributeValue" _id "hitPoints"}}
max={{characterCalculate "attributeBase" _id "hitPoints"}}
editable pin
role="slider">
</paper-diff-slider>
</div>
{{#each tempHitPoints}}
<div>
@@ -35,9 +35,21 @@
</div>
{{/each}}
<div class="caption">
{{#if multipliers.immunities.length}} <div>Immune: {{#each multipliers.immunities}} {{name}} {{/each}}</div>{{/if}}
{{#if multipliers.resistances.length}}<div>Resistance: {{#each multipliers.resistances}} {{name}} {{/each}}</div>{{/if}}
{{#if multipliers.weaknesses.length}} <div>Weakness: {{#each multipliers.weaknesses}} {{name}} {{/each}}</div>{{/if}}
{{#if multipliers.immunities.length}}
<div>
Immune: {{#each multipliers.immunities}} {{name}} {{/each}}
</div>
{{/if}}
{{#if multipliers.resistances.length}}
<div>
Resistance: {{#each multipliers.resistances}} {{name}} {{/each}}
</div>
{{/if}}
{{#if multipliers.weaknesses.length}}
<div>
Weakness: {{#each multipliers.weaknesses}} {{name}} {{/each}}
</div>
{{/if}}
</div>
{{#if showDeathSave}}
{{#with deathSaveObject}}

View File

@@ -3,7 +3,7 @@ Template.healthCard.helpers({
return TemporaryHitPoints.find({charId: this._id});
},
showDeathSave: function(){
return this.attributeValue("hitPoints") <= 0;
return Characters.calculate.attributeValue(this._id, "hitPoints") <= 0;
},
deathSaveObject: function(){
var char = Characters.findOne(this._id, {fields: {deathSave: 1}});
@@ -27,21 +27,20 @@ Template.healthCard.helpers({
return this.fail >= 3;
},
multipliers: function(){
var char = Characters.findOne(this._id, {fields: {_id: 1}});
var multipliers = [
{name: "Acid", value: char.attributeValue("acidMultiplier", 1)},
{name: "Bludgeoning", value: char.attributeValue("bludgeoningMultiplier", 1)},
{name: "Cold", value: char.attributeValue("coldMultiplier", 1)},
{name: "Fire", value: char.attributeValue("fireMultiplier", 1)},
{name: "Force", value: char.attributeValue("forceMultiplier", 1)},
{name: "Lightning", value: char.attributeValue("lightningMultiplier", 1)},
{name: "Necrotic", value: char.attributeValue("necroticMultiplier", 1)},
{name: "Piercing", value: char.attributeValue("piercingMultiplier", 1)},
{name: "Poison", value: char.attributeValue("poisonMultiplier", 1)},
{name: "Psychic", value: char.attributeValue("psychicMultiplier", 1)},
{name: "Radiant", value: char.attributeValue("radiantMultiplier", 1)},
{name: "Slashing", value: char.attributeValue("slashingMultiplier", 1)},
{name: "Thunder", value: char.attributeValue("thunderMultiplier", 1)},
{name: "Acid", value: Characters.calculate.attributeValue(this._id, "acidMultiplier")},
{name: "Bludgeoning", value: Characters.calculate.attributeValue(this._id, "bludgeoningMultiplier")},
{name: "Cold", value: Characters.calculate.attributeValue(this._id, "coldMultiplier")},
{name: "Fire", value: Characters.calculate.attributeValue(this._id, "fireMultiplier")},
{name: "Force", value: Characters.calculate.attributeValue(this._id, "forceMultiplier")},
{name: "Lightning", value: Characters.calculate.attributeValue(this._id, "lightningMultiplier")},
{name: "Necrotic", value: Characters.calculate.attributeValue(this._id, "necroticMultiplier")},
{name: "Piercing", value: Characters.calculate.attributeValue(this._id, "piercingMultiplier")},
{name: "Poison", value: Characters.calculate.attributeValue(this._id, "poisonMultiplier")},
{name: "Psychic", value: Characters.calculate.attributeValue(this._id, "psychicMultiplier")},
{name: "Radiant", value: Characters.calculate.attributeValue(this._id, "radiantMultiplier")},
{name: "Slashing", value: Characters.calculate.attributeValue(this._id, "slashingMultiplier")},
{name: "Thunder", value: Characters.calculate.attributeValue(this._id, "thunderMultiplier")},
];
multipliers = _.groupBy(multipliers, "value");
return {
@@ -55,7 +54,8 @@ Template.healthCard.helpers({
Template.healthCard.events({
"change #hitPointSlider": function(event){
var value = event.currentTarget.value;
var adjustment = value - this.attributeBase("hitPoints");
var base = Characters.calculate.attributeBase(this._id, "hitPoints")
var adjustment = value - base;
Characters.update(this._id, {$set: {"hitPoints.adjustment": adjustment}});
//reset the death saves if we are gaining HP
if (value > 0)

View File

@@ -1,5 +1,5 @@
<template name="hitDice">
{{#if ../attributeBase name}}
{{#if characterCalculate "attributeBase" ../_id name}}
<paper-shadow class="card hit-dice" hero-id="main"
{{detailHero name ../_id}}
layout horizontal>
@@ -18,10 +18,10 @@
</div>
<div class="resourceValue" layout vertical center>
<div>
{{../attributeValue name}}
{{characterCalculate "attributeValue" ../_id name}}
</div>
<div class="title white-text">
d{{diceNum}} {{../abilityMod "constitution"}}
d{{diceNum}} {{characterCalculate "abilityMod" ../_id "constitution"}}
</div>
</div>
</div>

View File

@@ -1,25 +1,28 @@
Template.hitDice.helpers({
cantIncrement: function(){
var valueSmallerThanBase = this.char.attributeValue(this.name) <
this.char.attributeBase(this.name);
return !valueSmallerThanBase;
var value = Characters.calculate.attributeValue(this.char._id, this.name);
var base = Characters.calculate.attributeBase(this.char._id, this.name);
return value >= base;
},
cantDecrement: function(){
var valuePositive = this.char.attributeValue(this.name) > 0;
return !valuePositive;
var value = Characters.calculate.attributeValue(this.char._id, this.name);
return value <= 0;
},
});
Template.hitDice.events({
"tap .resourceUp": function(event){
if (this.char.attributeValue(this.name) < this.char.attributeBase(this.name)){
var value = Characters.calculate.attributeValue(this.char._id, this.name);
var base = Characters.calculate.attributeBase(this.char._id, this.name);
if (value < base){
var modifier = {$inc: {}};
modifier.$inc[this.name + ".adjustment"] = 1;
Characters.update(this.char._id, modifier, {validate: false});
}
},
"tap .resourceDown": function(event){
if (this.char.attributeValue(this.name) > 0){
var value = Characters.calculate.attributeValue(this.char._id, this.name);
if (value > 0){
var modifier = {$inc: {}};
modifier.$inc[this.name + ".adjustment"] = -1;
Characters.update(this.char._id, modifier, {validate: false});

View File

@@ -1,4 +1,4 @@
<!-- needs name, char, and skillName -->
<!-- needs name, charId, and skillName -->
<template name="skillDialog">
{{#baseDialog title=name class=color hideEdit=true}}
{{> skillDialogView}}
@@ -8,7 +8,7 @@
<template name="skillDialogView">
<div layout vertical center>
<div class="display2">
{{char.skillMod skillName}}
{{characterCalculate "skillMod" charId skillName}}
</div>
<div class="subhead">
<core-icon icon="{{profIcon}}" class="black54"></core-icon>
@@ -25,9 +25,9 @@
<table class="summaryTable">
<tr>
<td>{{abilityName}}</td>
<td>{{char.abilityMod ability}}</td>
<td>{{characterCalculate "abilityMod" charId ability}}</td>
</tr>
{{#if char.proficiency skillName}}
{{#if characterCalculate "proficiency" charId skillName}}
<tr>
<td>{{proficiencyValue}}</td>
<td>{{signedString profBonus}}</td>
@@ -59,7 +59,7 @@
{{/each}}
<tr class="body2">
<td>Total</td>
<td>{{char.skillMod skillName}}</td>
<td>{{characterCalculate "skillMod" charId skillName}}</td>
</tr>
</table>

View File

@@ -106,9 +106,7 @@ Template.skillDialogView.helpers({
return a || b || c;
},
profIcon: function(){
var char = Characters.findOne(this.charId);
if (!char) return;
var prof = char.proficiency(this.skillName);
var prof = Characters.calculate.proficiency(this.charId, this.skillName);
if (prof > 0 && prof < 1) return "image:brightness-2";
if (prof === 1) return "image:brightness-1";
if (prof > 1) return "av:album";
@@ -123,13 +121,13 @@ Template.skillDialogView.helpers({
profBonus: function(){
var char = Characters.findOne(this.charId);
if (!char) return;
return char.proficiency(this.skillName) *
char.attributeValue("proficiencyBonus");
var prof = Characters.calculate.proficiency(this.charId, this.skillName);
var proficiencyBonus =
Characters.calculate.attributeValue(this.charId, "proficiencyBonus");
return prof * proficiencyBonus;
},
proficiencyValue: function(){
var char = Characters.findOne(this.charId);
if (!char) return;
var prof = char.proficiency(this.skillName);
var prof = Characters.calculate.proficiency(this.charId, this.skillName);
if (prof == 0.5) return "Half Proficiency";
if (prof == 1) return "Proficient";
if (prof == 2) return "Double Proficiency";
@@ -199,22 +197,15 @@ Template.skillDialogView.helpers({
return skill.ability;
},
abilityName: function(){
var opts = {fields: {}};
opts.fields[this.skillName] = 1;
var char = Characters.findOne(this.charId, opts);
if (!char) return;
var skill = char[this.skillName];
var skill = Characters.calculate.getField(this.charId, this.skillName);
if (!skill) return;
var ability = skill.ability;
return abilities[ability] && abilities[ability].name;
},
char: function(){
return Characters.findOne(this.charId, {fields:{_id: 1}});
},
sourceName: function(){
if (this.parent.collection === "Characters"){
if (this.parent.group === "racial"){
return Characters.findOne(this.charId, {fields:{race: 1}}).race || "Race";
return Characters.calculate.getField(this.charId, "race") || "Race";
}
if (this.parent.group === "background"){
return "Background";

View File

@@ -8,7 +8,9 @@
{{#if failSkill}}
<div class="fail skill-mod">fail</div>
{{else}}
<div class="{{advantage}} skill-mod">{{../skillMod skill}}</div>
<div class="{{advantage}} skill-mod">
{{skillMod}}
</div>
{{/if}}
<div flex>
{{name}}
@@ -16,7 +18,7 @@
*
{{/if}}
{{#if showPassive}}
({{../passiveSkill skill}})
({{characterCalculate "passiveSkill" ../_id skill}})
{{/if}}
</div>
</div>

View File

@@ -1,13 +1,21 @@
Template.skillRow.helpers({
skillMod: function() {
return signedString(
Characters.calculate.skillMod(
Template.parentData()._id, this.skill
)
);
},
profIcon: function(){
var prof = Template.parentData(1).proficiency(this.skill);
var charId = Template.parentData()._id;
var prof = Characters.calculate.proficiency(charId, this.skill);
if (prof > 0 && prof < 1) return "image:brightness-2";
if (prof === 1) return "image:brightness-1";
if (prof > 1) return "av:album";
return "radio-button-off";
},
failSkill: function(){
var charId = Template.parentData(1)._id;
var charId = Template.parentData()._id;
return Effects.find({
charId: charId,
stat: this.skill,
@@ -16,12 +24,13 @@ Template.skillRow.helpers({
}).count();
},
advantage: function(){
var advantage = Template.parentData(1).advantage(this.skill);
var charId = Template.parentData()._id;
var advantage = Characters.calculate.advantage(charId, this.skill);
if (advantage > 0) return "advantage";
if (advantage < 0) return "disadvantage";
},
conditionalCount: function(){
var charId = Template.parentData(1)._id;
var charId = Template.parentData()._id;
return Effects.find({
charId: charId,
stat: this.skill,

View File

@@ -75,9 +75,9 @@
<div class="left display1 white-text {{color}}"
hero-id="toolbar" {{detailHero stat ../_id}}>
{{#if isSkill}}
{{../skillMod stat}}
{{prefix}}{{skillMod}}
{{else}}
{{prefix}}{{../attributeValue stat}}
{{prefix}}{{characterCalculate "attributeValue" ../_id stat}}
{{/if}}
</div>
<div class="right subhead" flex horizontal layout center>

View File

@@ -67,6 +67,12 @@ Template.stats.events({
},
});
Template.stats.helpers({
Template.statCard.helpers({
skillMod: function() {
return signedString(
Characters.calculate.skillMod(
Template.parentData()._id, this.stat
)
);
},
});

View File

@@ -13,22 +13,22 @@
role="button"
tabindex="0"
icon="delete"
aria-label="Delete Feature"
noink></paper-icon-button>
aria-label="Delete Feature">
</paper-icon-button>
{{/unless}}
{{#unless hideColor}}
{{> colorDropdown}}
{{/unless}}
<paper-icon-button id="doneEditingButton"
icon="done"
aria-label="Delete Feature"
noink></paper-icon-button>
aria-label="Delete Feature">
</paper-icon-button>
{{else}}
{{#if showEdit}}
<paper-icon-button id="editButton"
icon="create"
aria-label="Delete Feature"
noink></paper-icon-button>
aria-label="Delete Feature">
</paper-icon-button>
{{/if}}
{{/if}}
</div>

View File

@@ -1,11 +1,10 @@
//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-z]+\b/gi, function(sub){
//fields
if (Schemas.Character.schema(sub)){
return char.fieldValue(sub);
return Characters.calculate.fieldValue(charId, sub);
}
//ability modifiers
var abilityMods = [
@@ -19,7 +18,7 @@ evaluate = function(charId, string){
if (_.contains(abilityMods, sub)){
var slice = sub.slice(0, -3);
try {
return char.abilityMod(slice);
return Characters.calculate.abilityMod(charId, slice);
} catch (e){
return sub;
}
@@ -33,7 +32,7 @@ evaluate = function(charId, string){
}
//character level
if (sub.toUpperCase() === "LEVEL"){
return char.level();
return Characters.calculate.level(charId);
}
return sub;
});
@@ -41,7 +40,6 @@ evaluate = function(charId, string){
var result = math.eval(string);
return result;
} catch (e){
console.log("Failed to evaluate ", string);
return string;
}
};

View File

@@ -1,4 +1,5 @@
preventLoop = function(inputFunction){
var self = this;
if (!_.isFunction(inputFunction)){
throw new Meteor.Error(
"Not a function",
@@ -9,23 +10,26 @@ preventLoop = function(inputFunction){
//if we try to visit the same argument twice before resolving its value
//we are in a dependency loop and need to GTFO
var visitedArgs = [];
return function(argument){
var value;
return function(){
var result;
var hash = _.reduce(arguments, function(memo, arg) {
return memo + arg;
}, "");
//we're still evaluating this attribute, must be in a loop
if (_.contains(visitedArgs, argument)) {
if (_.contains(visitedArgs, hash)) {
console.warn("dependency loop detected");
return NaN;
} else {
//push this skill to the list of visited skills
//push this hash to the list of visited hashes
//we can't visit it again unless it returns first
visitedArgs.push(argument);
visitedArgs.push(hash);
}
try {
value = inputFunction.call(this, argument);
result = inputFunction.apply(this, arguments);
} finally{
//this argument returns or fails, pull it from the array
visitedArgs = _.without(visitedArgs, argument);
//this hash returns or fails, pull it from the array
visitedArgs = _.without(visitedArgs, hash);
}
return value;
return result;
};
};

View File

@@ -0,0 +1,45 @@
Tracker.memoize = function(func, hasher){
var memoize = function(key) {
var cache = memoize.cache;
var address = "" + (hasher ? hasher.apply(this, arguments) : key);
if (!_.has(cache, address)) {
cache[address] = new CacheObject(func, address, arguments, cache, this);
}
return cache[address].get();
};
memoize.cache = {};
return memoize;
};
function CacheObject(func, address, args, cache, context){
var self = this;
self.currentValue = null;
self.dep = new Tracker.Dependency();
//spawn a new autorun that keeps the value up-to-date
Tracker.nonreactive(function() {
Tracker.autorun(function(computation) {
//if this isn't the first run and nobody is listening,
//delete itself from cache and stop the computation
if (!computation.firstRun && !self.dep.hasDependents()){
computation.stop();
delete cache[address];
return;
}
//call the expensive function
var newValue = func.apply(context, args);
//if the value changed, store the new value
if (self.currentValue !== newValue){
self.currentValue = newValue;
//tell the dependents that we've changed
self.dep.changed();
}
});
});
}
CacheObject.prototype.get = function() {
//if there is an active computation, track dependents
if (Tracker.active) this.dep.depend();
return this.currentValue;
};

View File

@@ -98,9 +98,9 @@ trackEncumbranceConditions = function(charId, templateInstance) {
});
var character = Characters.findOne(
charId,
{fields: {strength: 1, "settings": 1}}
{fields: {"settings": 1}}
);
var strength = character.attributeValue("strength");
var strength = Characters.calculate.attributeValue(charId, "strength");
var give = function(condition) {
Meteor.call("giveCondition", charId, condition);
};