From ad474590bd5edb6eb476d5883e9513a20fc7f57d Mon Sep 17 00:00:00 2001 From: Thaum Date: Sun, 30 Nov 2014 10:38:44 +0000 Subject: [PATCH] Changed effect arrays to a single effect array array --- rpg-docs/Model/Character/Characters.js | 126 +++++++++--------- .../Model/Character/SubSchemas/Attribute.js | 6 +- .../Character/SubSchemas/Effect/Effect.js | 7 +- rpg-docs/Model/Character/SubSchemas/Skill.js | 24 +--- .../client/views/character/skills/skills.js | 29 +++- rpg-docs/lib/functions/evaluate.js | 6 +- 6 files changed, 97 insertions(+), 101 deletions(-) diff --git a/rpg-docs/Model/Character/Characters.js b/rpg-docs/Model/Character/Characters.js index 5bacf527..cc9c085e 100644 --- a/rpg-docs/Model/Character/Characters.js +++ b/rpg-docs/Model/Character/Characters.js @@ -26,18 +26,18 @@ Schemas.Character = new SimpleSchema({ //stats hitPoints: {type: Schemas.Attribute}, - "hitPoints.add": { + "hitPoints.effects": { type: [Schemas.Effect], defaultValue: [ - {name: "Constitution modifier for each level", calculation: "level * constitutionMod"} + {name: "Constitution modifier for each level", calculation: "level * constitutionMod", operation: "add", type: "inate"} ] }, experience: {type: Schemas.Attribute}, proficiencyBonus: {type: Schemas.Attribute}, - "proficiencyBonus.add": { + "proficiencyBonus.effects": { type: [Schemas.Effect], defaultValue: [ - {name: "Proficiency bonus by level", calculation: "floor(level / 4.1) + 2"} + {name: "Proficiency bonus by level", calculation: "floor(level / 4.1) + 2", operation: "add", type: "inate"} ] }, speed: {type: Schemas.Attribute}, @@ -45,10 +45,10 @@ Schemas.Character = new SimpleSchema({ age: {type: Schemas.Attribute}, ageRate: {type: Schemas.Attribute}, armor: {type: Schemas.Attribute}, - "armor.add": { + "armor.effects": { type: [Schemas.Effect], defaultValue: [ - {name: "Dexterity Modifier", calculation: "dexterityArmor"} + {name: "Dexterity Modifier", calculation: "dexterityArmor", operation: "add", type: "inate"} ] }, @@ -174,51 +174,51 @@ Schemas.Character = new SimpleSchema({ strengthAttack: {type: Schemas.Skill}, "strengthAttack.ability": {type: String,defaultValue: "strength"}, - "strengthAttack.proficiency": { + "strengthAttack.effects": { type: [Schemas.Effect], - defaultValue: [{_id: Random.id(),name: "Attack Proficiency",value: 1}] + defaultValue: [{name: "Attack Proficiency", value: 1, operation: "proficiency", type: "inate"}] }, dexterityAttack: {type: Schemas.Skill}, "dexterityAttack.ability": { type: String, defaultValue: "dexterity" }, "dexterityAttack.proficiency": { type: [Schemas.Effect], - defaultValue: [{_id: Random.id(),name: "Attack Proficiency",value: 1}] + defaultValue: [{name: "Attack Proficiency", value: 1, operation: "proficiency", type: "inate"}] }, constitutionAttack: {type: Schemas.Skill}, "constitutionAttack.ability":{ type: String, defaultValue: "constitution" }, "constitutionAttack.proficiency": { type: [Schemas.Effect], - defaultValue: [{_id: Random.id(),name: "Attack Proficiency",value: 1}] + defaultValue: [{name: "Attack Proficiency", value: 1, operation: "proficiency", type: "inate"}] }, intelligenceAttack: {type: Schemas.Skill}, "intelligenceAttack.ability":{ type: String, defaultValue: "intelligence" }, "intelligenceAttack.proficiency": { type: [Schemas.Effect], - defaultValue: [{_id: Random.id(),name: "Attack Proficiency",value: 1}] + defaultValue: [{name: "Attack Proficiency", value: 1, operation: "proficiency", type: "inate"}] }, wisdomAttack: {type: Schemas.Skill}, "wisdomAttack.ability": { type: String, defaultValue: "wisdom" }, "wisdomAttack.proficiency": { type: [Schemas.Effect], - defaultValue: [{_id: Random.id(),name: "Attack Proficiency",value: 1}] + defaultValue: [{name: "Attack Proficiency", value: 1, operation: "proficiency", type: "inate"}] }, charismaAttack: {type: Schemas.Skill}, "charismaAttack.ability": { type: String, defaultValue: "charisma" }, "charismaAttack.proficiency": { type: [Schemas.Effect], - defaultValue: [{_id: Random.id(),name: "Attack Proficiency",value: 1}] + defaultValue: [{name: "Attack Proficiency", value: 1, operation: "proficiency", type: "inate"}] }, rangedAttack: {type: Schemas.Skill}, "rangedAttack.ability": { type: String, defaultValue: "dexterity" }, "rangedAttack.proficiency": { type: [Schemas.Effect], - defaultValue: [{_id: Random.id(),name: "Attack Proficiency",value: 1}] + defaultValue: [{name: "Attack Proficiency", value: 1, operation: "proficiency", type: "inate"}] }, dexterityArmor: {type: Schemas.Skill}, @@ -277,26 +277,23 @@ Characters.find({},{fields: {time: 1, expirations: 1, features: 1}}).observe({ var attributeBase = function(charId, attribute){ var value = 0; - //add all values in add array - _.each(attribute.add, function(effect){ - value += evaluateEffect(charId, effect); - }); - - //multiply all values in mul array - _.each(attribute.mul, function(effect){ - value *= evaluateEffect(charId, effect); - }); - - //largest min - _.each(attribute.min, function(effect){ - var min = evaluateEffect(charId, effect); - value = value > min? value : min; - }); - - //smallest max - _.each(attribute.max, function(effect){ - var max = evaluateEffect(charId, effect); - value = value < max? value : max; + _.each(attribute.effects, function(effect){ + switch(effect.operation) { + case "add": + value += evaluateEffect(charId, effect); + break; + case "mul": + value *= evaluateEffect(charId, effect); + break; + case "min": + var min = evaluateEffect(charId, effect); + value = value > min? value : min; + break; + case "max": + var max = evaluateEffect(charId, effect); + value = value < max? value : max; + break; + } }); return value; } @@ -324,12 +321,12 @@ Characters.helpers({ throw new Meteor.Error("Field not found", "Character's schema does not contain a field called: " + fieldName); } //duck typing to get the right value function - //.proficiency implies skill - if (Schemas.Character.schema(fieldName + ".proficiency")){ + //.ability implies skill + if (Schemas.Character.schema(fieldName + ".ability")){ return this.skillMod(fieldName); } - //base without proficiency implies attribute - if (Schemas.Character.schema(fieldName + ".base")){ + //adjustment implies attribute + if (Schemas.Character.schema(fieldName + ".adjustment")){ return this.attributeValue(fieldName); } //fall back to just returning the field itself @@ -424,26 +421,23 @@ Characters.helpers({ //add multiplied proficiency bonus to modifier mod += prof * this.attributeValue("proficiencyBonus"); - //add all values in add array - _.each(skill.add, function(effect){ - mod += evaluateEffect(charId, effect); - }); - - //multiply all values in mul array - _.each(skill.mul, function(effect){ - mod *= evaluateEffect(charId, effect); - }); - - //largest min - _.each(skill.min, function(effect){ - var min = evaluateEffect(charId, effect); - mod = mod > min? mod : min; - }); - - //smallest max - _.each(skill.max, function(effect){ - var max = evaluateEffect(charId, effect); - mod = mod < max? mod : max; + _.each(skill.effects, function(effect){ + switch(effect.operation) { + case "add": + mod += evaluateEffect(charId, effect); + break; + case "mul": + mod *= evaluateEffect(charId, effect); + break; + case "min": + var min = evaluateEffect(charId, effect); + mod = mod > min? mod : min; + break; + case "max": + var max = evaluateEffect(charId, effect); + mod = mod < max? mod : max; + break; + } }); } finally{ //this skill returns or fails, pull it from the array @@ -460,10 +454,12 @@ Characters.helpers({ var charId = this._id; //return largest value in proficiency array var prof = 0; - _.each(skill.proficiency, function(effect){ - var newProf = evaluateEffect(charId, effect); - if (newProf > prof){ - prof = newProf; + _.each(skill.effects, function(effect){ + if(effect.operation === "proficiency"){ + var newProf = evaluateEffect(charId, effect); + if (newProf > prof){ + prof = newProf; + } } }); return prof; @@ -476,8 +472,10 @@ Characters.helpers({ var charId = this._id var mod = +this.skillMod(skillName); var value = 10 + mod; - _.each(skill.passiveAdd, function(effect){ - value += evaluateEffect(charId, effect); + _.each(skill.effects, function(effect){ + if(effect.operation === "passiveAdd"){ + value += evaluateEffect(charId, effect); + } }); return value; //TODO decide whether (dis)advantage gives (-)+5 to passive checks diff --git a/rpg-docs/Model/Character/SubSchemas/Attribute.js b/rpg-docs/Model/Character/SubSchemas/Attribute.js index bd99334f..031a061f 100644 --- a/rpg-docs/Model/Character/SubSchemas/Attribute.js +++ b/rpg-docs/Model/Character/SubSchemas/Attribute.js @@ -6,11 +6,7 @@ Schemas.Attribute = new SimpleSchema({ defaultValue: 0 }, //effect arrays - add: { type: [Schemas.Effect], defaultValue: [] }, - mul: { type: [Schemas.Effect], defaultValue: [] }, - min: { type: [Schemas.Effect], defaultValue: [] }, - max: { type: [Schemas.Effect], defaultValue: [] }, - conditional:{ type: [Schemas.Effect], defaultValue: [] }, + effects: { type: [Schemas.Effect], defaultValue: [] }, reset: { type: String, defaultValue: "longRest", diff --git a/rpg-docs/Model/Character/SubSchemas/Effect/Effect.js b/rpg-docs/Model/Character/SubSchemas/Effect/Effect.js index 6ae0c55a..ecca972e 100644 --- a/rpg-docs/Model/Character/SubSchemas/Effect/Effect.js +++ b/rpg-docs/Model/Character/SubSchemas/Effect/Effect.js @@ -13,6 +13,11 @@ Schemas.Effect = new SimpleSchema({ name: { type: String }, + operation: { + type: String, + defaultValue: "add", + allowedValues: ["proficiency","add","mul","min","max","advantage","disadvantage","passiveAdd","fail","conditional","passiveAdd"] + }, value: { type: Number, decimal: true, @@ -28,4 +33,4 @@ Schemas.Effect = new SimpleSchema({ defaultValue: "default", allowedValues: ["default", "inate", "class", "race", "feat", "equippedMagic", "equippedMundane", "external"] } -}); +}); \ No newline at end of file diff --git a/rpg-docs/Model/Character/SubSchemas/Skill.js b/rpg-docs/Model/Character/SubSchemas/Skill.js index d429631f..70f30320 100644 --- a/rpg-docs/Model/Character/SubSchemas/Skill.js +++ b/rpg-docs/Model/Character/SubSchemas/Skill.js @@ -1,25 +1,5 @@ Schemas.Skill = new SimpleSchema({ //attribute name that this skill used as base mod for roll - ability: { type: String, defaultValue: "" }, - //multiplied by profBonus and added to base mod - //only highest value proficiency is used - proficiency: { type: [Schemas.Effect], defaultValue: [] }, - //added to base mod - add: { type: [Schemas.Effect], defaultValue: [] }, - //multiplied by base + adds - mul: { type: [Schemas.Effect], defaultValue: [] }, - //lower bounds, highest used - min: { type: [Schemas.Effect], defaultValue: [] }, - //upper bounds, lowest used - max: { type: [Schemas.Effect], defaultValue: [] }, - //things giving advantage - advantage: { type: [Schemas.Effect], defaultValue: [] }, - //things giving disadvantage - disadvantage: { type: [Schemas.Effect], defaultValue: [] }, - //added to passive checks only - passiveAdd: { type: [Schemas.Effect], defaultValue: [] }, - //things causing all rolls to fail - fail: { type: [Schemas.Effect], defaultValue: [] }, - //things that only apply sometimes - conditional: { type: [Schemas.Effect], defaultValue: [] } + ability: { type: String, defaultValue: "" }, + effects: { type: [Schemas.Effect], defaultValue: [] }, }); \ No newline at end of file diff --git a/rpg-docs/client/views/character/skills/skills.js b/rpg-docs/client/views/character/skills/skills.js index 7e6e7442..fff61c9c 100644 --- a/rpg-docs/client/views/character/skills/skills.js +++ b/rpg-docs/client/views/character/skills/skills.js @@ -42,17 +42,34 @@ Template.skillRow.helpers({ return "profNone.png"; }, failSkill: function(){ - return Template.parentData(1).getField(this.skill).fail.length > 0; + var skill = Template.parentData(1).getField(this.skill); + _.each(skill.effets, function(effect){ + if (effect.operation === "fail"){ + return true; + } + }) + return false; }, advantage: function(){ - var adv = Template.parentData(1).getField(this.skill).advantage.length; - var disadv = Template.parentData(1).getField(this.skill).disadvantage.length; + var adv = 0; + var disadv = 0; + var skill = Template.parentData(1).getField(this.skill); + _.each(skill.effets, function(effect){ + if (effect.operation === "advantage"){ + adv ++; + } else if (effect.operation === "disadvantage") { + disadv ++; + } + }) if(adv > 0 && disadv === 0) return "advantage"; if(disadv > 0 && adv === 0) return "disadvantage"; }, conditionals: function(){ - if(Template.parentData(1).getField(this.skill).conditional.length > 0){ - return "conditionals"; - } + var skill = Template.parentData(1).getField(this.skill); + _.each(skill.effets, function(effect){ + if (effect.operation === "conditional"){ + return "conditionals"; + } + }) } }); \ No newline at end of file diff --git a/rpg-docs/lib/functions/evaluate.js b/rpg-docs/lib/functions/evaluate.js index d97495d6..7d1ae307 100644 --- a/rpg-docs/lib/functions/evaluate.js +++ b/rpg-docs/lib/functions/evaluate.js @@ -19,10 +19,10 @@ evaluate = function(charId, string){ return sub; }); try{ - result = math.eval(string); - return result + var result = math.eval(string); + return result; } catch(e){ - console.log(e) + console.log("Failed to evaluate ", string); return string; } }