Refactoring of Character class to add effect appliers and removers

This commit is contained in:
Thaum
2014-11-03 11:30:14 +00:00
parent e7f7f75436
commit 25b2a95f14
9 changed files with 139 additions and 93 deletions

View File

@@ -10,17 +10,6 @@ Characters = new Meteor.Collection("characters", {
}
});
//Attributes are numerical values
Attribute = function(base){
this.base = base; //the unmodified value of the attribute
//effects of the form {name: "Ring of Protection", value: 1}
this.add = []; //bonuses added to the attribute
this.mul = []; //multipliers to the attribute (after adding bonuses)
this.min = []; //effects setting the minimum value of the attribute
this.max = []; //effects setting the maximum value of the attribute
this.conditional = []; //conditional modifiers
}
var attributes = [
{name: "strength"},
{name: "dexterity"},
@@ -32,18 +21,18 @@ var attributes = [
{name: "experience"},
{name: "proficiencyBonus",
add: [
{name: "Level 1", value: 2}
new Effect("Level 1", 2)
]
},
{name: "speed",
add: [
{name: "Base Speed", value: 30}
new Effect("Base Speed", 30)
]
},
{name: "armor",
add: [
{name: "Base Armor Class", value: 10},
{name: "Dexterity Modifier", value: "skillMod skills.dexterityArmor"}
new Effect("Base Armor Class", 10),
new Effect("Dexterity Modifier", "skillMod skills.dexterityArmor")
]
},
{name: "weight"},
@@ -64,25 +53,6 @@ var attributes = [
{name: "rages"}
];
//Skills are bonuses to rolls: "+2" etc.
//They are based off of some ability
Skill = function(ability){
//proficiencies of the form {name: "Jack of all Trades", value: 0.5}
//only the highest is used
this.proficiency = [];
//ability name that this skill uses as base for roll
this.ability = ability;
this.add = [];
this.mul = [];
this.min = [];
this.max = [];
this.advantage = []; //effects granting advantage
this.disadvantage = [];
this.passiveAdd = []; //only added to passive checks
this.fail = []; //all checks are failed
this.conditional = []; //conditional modifiers
}
var skills = [
{skill: "strengthSave", ability: "strength"},
{skill: "dexteritySave", ability: "dexterity"},
@@ -292,53 +262,3 @@ getMod = function(score){
signedString = function(number){
return number > 0? "+" + number : "" + number;
}
// turns dot notation strings into keys of root
// argument formats:
// 157, anything -> 157
// "some.number", object -> object.some.number
// "some.function", object -> object.some.function()
// "some.function arg1 arg2", object -> object.some.function(arg1, arg2)
pop = function(input, root){
if(typeof(input) === "string"){
//we need root for this part
if(root === undefined) return;
//this is a likely to fail if the string is malformed
try{
//split over spaces
var parts = input.split(" ");
//for each word
for (var i = 0; i < parts.length; i++){
//split over dots
var str = parts[i].split(".");
//start at root
parts[i] = root;
//for each word between dots
for (var j = 0; j < str.length; j++){
parts[i] = parts[i][str[j]];
}
}
//pull the first word out, might be a function
var func = parts.splice(0, 1)[0];
//if it's a function, apply the arguments to it
if(_.isFunction(func)) return +func.apply(root, parts);
//if it's a number, return it
if(!isNaN(func)) return +func;
} catch (err) {
//TODO pokemon catch statement is bad
//"gotta catch em all"
console.log(err);
return;
}
}
return +input;
}

View File

@@ -0,0 +1,12 @@
//Attributes are numerical values
Attribute = function(base){
//the unmodified value of the attribute
//should be zero for most attributes after a long rest
this.base = base;
//effects of the form {name: "Ring of Protection", value: 1}
this.add = []; //bonuses added to the attribute
this.mul = []; //multipliers to the attribute (after adding bonuses)
this.min = []; //effects setting the minimum value of the attribute
this.max = []; //effects setting the maximum value of the attribute
this.conditional = []; //conditional modifiers
}

View File

@@ -33,15 +33,15 @@ effect, such as when a creature is hurled\
away by the thunderwave spell."
],
effects: [
{stat: "attributes.speed.maximum", value: 0}
{stat: "attributes.speed.max", value: 0}
]
}
Conditions.Incapacitated = {
effects: [
{stat: "attributes.actions.maximum", value: 0},
{stat: "attributes.reactions.maximum", value: 0},
{stat: "attributes.bonusActions.maximum", value: 0}
{stat: "attributes.actions.max", value: 0},
{stat: "attributes.reactions.max", value: 0},
{stat: "attributes.bonusActions.max", value: 0}
]
}
@@ -54,7 +54,7 @@ Conditions.Paralyzed = {
effects: [
{stat: "skills.strengthSave.fail", value: 1},
{stat: "skills.dexteritySave.fail", value: 1},
{stat: "attributes.speed.maximum", value: 0}
{stat: "attributes.speed.max", value: 0}
]
}
_.extend(Conditions.Paralyzed, Conditions.Incapacitated);
@@ -67,7 +67,7 @@ Conditions.Petrified = {
{stat: "attributes.ageRate.min", value: 0},
{stat: "skills.strengthSave.fail", value: 1},
{stat: "skills.dexteritySave.fail", value: 1},
{stat: "attributes.speed.maximum", value: 0}
{stat: "attributes.speed.max", value: 0}
]
}
for(var i = 0, l = DamageTypes.length; i < l; i++){
@@ -91,14 +91,14 @@ Conditions.Prone = {
Conditions.Restrained = {
effects: [
{stat: "attributes.speed.maximum", value: 0}
{stat: "attributes.speed.max", value: 0}
]
}
Conditions.Stunned = {
//implies incapacitated
effects: [
{stat: "attributes.speed.maximum", value: 0},
{stat: "attributes.speed.max", value: 0},
{stat: "skills.strengthSave.fail", value: 1},
{stat: "skills.dexteritySave.fail", value: 1}
]
@@ -109,7 +109,7 @@ Conditions.Unconscious = {
//implies incapacitated
//implies prone
effects: [
{stat: "attributes.speed.maximum", value: 0},
{stat: "attributes.speed.max", value: 0},
{stat: "skills.strengthSave.fail", value: 1},
{stat: "skills.dexteritySave.fail", value: 1}
]

View File

@@ -0,0 +1,47 @@
Effect = function(stat, value){
this.stat = stat;
this.value = value;
this._id = new Mongo.ObjectID()._str;
}
pushEffects = function(characterId, effectName, effectsArray){
//check that the arguments are of the right form
check(characterId, String);
check(effectName, String);
check(effectsArray, [{ _id: String, stat: String, value: Number}]);
for(var i = 0; i < effectsArray.length; i++){
var effect = effectsArray[i];
//check if the character exists with the field we are changing
var chk = {_id: characterId}; //right id
chk[effect.stat] = {$exists: true}; //has a field for the stat already
if(Characters.findOne(chk)){
var newEffect = {};
newEffect[effect.stat] = {_id: effect.id, name: effectName, value: effect.value};
//update the field
Characters.update(characterId, {$push: newEffect});
}
}
}
pullEffects = function(characterId, effectsArray){
//check that the arguments are of the right form
check(characterId, String);
check(effectsArray, [{ _id: String, stat: String, value: Number}]);
for(var i = 0; i < effectsArray.length; i++){
var effect = effectsArray[i];
//check if the character exists with the field we are changing
var chk = {_id: characterId}; //right id
chk[effect.stat] = {$exists: true}; //has a field for the stat already
if(Characters.findOne(chk)){
var effectToPull = {};
effectToPull[effect.stat] = {_id: effect.id};
//update the field
Characters.update(characterId, {$pull: effectToPull});
}
}
}

View File

@@ -0,0 +1,18 @@
//Skills are bonuses to rolls: "+2" etc.
//They are based off of some ability
Skill = function(ability){
//proficiencies of the form {name: "Jack of all Trades", value: 0.5}
//only the highest is used
this.proficiency = [];
//ability name that this skill uses as base for roll
this.ability = ability;
this.add = [];
this.mul = [];
this.min = [];
this.max = [];
this.advantage = []; //effects granting advantage
this.disadvantage = [];
this.passiveAdd = []; //only added to passive checks
this.fail = []; //all checks are failed
this.conditional = []; //conditional modifiers
}

View File

@@ -0,0 +1,49 @@
// turns dot notation strings into keys of root
// argument formats:
// 157, anything -> 157
// "some.number", object -> object.some.number
// "some.function", object -> object.some.function()
// "some.function arg1 arg2", object -> object.some.function(arg1, arg2)
pop = function(input, root){
if(typeof(input) === "string"){
//we need root for this part
if(root === undefined) return;
//this is a likely to fail if the string is malformed
try{
//split over spaces
var parts = input.split(" ");
//for each word
for (var i = 0; i < parts.length; i++){
//split over dots
var str = parts[i].split(".");
//start at root
parts[i] = root;
//for each word between dots
for (var j = 0; j < str.length; j++){
parts[i] = parts[i][str[j]];
}
}
//pull the first word out, might be a function
var func = parts.splice(0, 1)[0];
//if it's a function, apply the arguments to it
if(_.isFunction(func)) return +func.apply(root, parts);
//if it's a number, return it
if(!isNaN(func)) return +func;
} catch (err) {
//TODO pokemon catch statement is bad
//"gotta catch em all"
console.log(err);
return;
}
}
return +input;
}