Changed how effects are applied and removed to enable effects expiring after a set duration.
This commit is contained in:
@@ -9,31 +9,26 @@ Schemas.Character = new SimpleSchema({
|
|||||||
proficiencies: { type: Schemas.Proficiencies },
|
proficiencies: { type: Schemas.Proficiencies },
|
||||||
features: { type: [Schemas.Feature]},
|
features: { type: [Schemas.Feature]},
|
||||||
time: { type: Number, min: 0, decimal: true},
|
time: { type: Number, min: 0, decimal: true},
|
||||||
initiativeOrder:{ type: Number, min: 0, max: 1, decimal: true}
|
initiativeOrder:{ type: Number, min: 0, max: 1, decimal: true},
|
||||||
|
expirations: { type: [Schemas.Expiration]}
|
||||||
//TODO add permission stuff for owner, readers and writers
|
//TODO add permission stuff for owner, readers and writers
|
||||||
//TODO hit dice
|
//TODO hit dice
|
||||||
|
//TODO spells
|
||||||
});
|
});
|
||||||
|
|
||||||
Characters.attachSchema(Schemas.Character);
|
Characters.attachSchema(Schemas.Character);
|
||||||
|
|
||||||
//react to time changing
|
//reactively remove expired effects
|
||||||
Characters.find({fields: {time: 1}}).observeChanges({
|
//this can be optimised a lot once clients can do projections
|
||||||
changed: function(id, fields){
|
Characters.find({},{fields: {time: 1, expirations: 1}}).observe({
|
||||||
var currentTime = fields.time;
|
changed: function(character, oldCharacter){
|
||||||
console.log(id + "time changed to " + currentTime)
|
var currentTime = character.time;
|
||||||
var features = Characters.findOne(id, { fields: {features: 1} }).features;
|
_.each(character.expirations, function(expiration){
|
||||||
_.each(features, function(feature){
|
if(expiration.expiry <= currentTime){
|
||||||
//expired features, if no expiry time is set, this is always false
|
pullEffect(character._id, expiration.stat, expiration.effectId);
|
||||||
if(feature.expires >= currentTime){
|
pullExpiry(character._id, expiration._id);
|
||||||
//remove buffs
|
|
||||||
pullBuffs(id, feature.buffs);
|
|
||||||
//disable feature
|
|
||||||
Characters.update(
|
|
||||||
{_id: id, "features._id": feature.id},
|
|
||||||
{$set: {"features.$.enabled": false}}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -45,28 +40,26 @@ Characters.helpers({
|
|||||||
var value = attribute.base;
|
var value = attribute.base;
|
||||||
|
|
||||||
//add all values in add array
|
//add all values in add array
|
||||||
for(var i = 0, l = attribute.add.length; i < l; i++){
|
_.each(attribute.add, function(effect){
|
||||||
var add = pop(attribute.add[i].value, this);
|
value += pop(effect.value, this)
|
||||||
value += add ;
|
});
|
||||||
}
|
|
||||||
|
|
||||||
//multiply all values in mul array
|
//multiply all values in mul array
|
||||||
for(var i = 0, l = attribute.mul.length; i < l; i++){
|
_.each(attribute.mul, function(effect){
|
||||||
var mul = pop(attribute.mul[i], this);
|
value *= pop(effect.value, this)
|
||||||
value *= mul;
|
});
|
||||||
}
|
|
||||||
|
|
||||||
//largest min
|
//largest min
|
||||||
for(var i = 0, l = attribute.min.length; i < l; i++){
|
_.each(attribute.min, function(effect){
|
||||||
var min = pop(attribute.min[i], this);
|
var min = pop(effect.value, this);
|
||||||
value = value > min? value : min;
|
value = value > min? value : min;
|
||||||
}
|
});
|
||||||
|
|
||||||
//smallest max
|
//smallest max
|
||||||
for(var i = 0, l = attribute.max.length; i < l; i++){
|
_.each(attribute.max, function(effect){
|
||||||
var max = pop(attribute.max[i], this);
|
var max = pop(effect.value, this);
|
||||||
value = value < max? value : max;
|
value = value < max? value : max;
|
||||||
}
|
});
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
},
|
},
|
||||||
@@ -101,26 +94,26 @@ Characters.helpers({
|
|||||||
mod += prof * this.attributeValue(this.attributes.proficiencyBonus);
|
mod += prof * this.attributeValue(this.attributes.proficiencyBonus);
|
||||||
|
|
||||||
//add all values in add array
|
//add all values in add array
|
||||||
for(var i = 0, l = skill.add.length; i < l; i++){
|
_.each(skill.add, function(effect){
|
||||||
mod += pop(skill.add[i].value, this);
|
mod += pop(effect.value, this)
|
||||||
}
|
});
|
||||||
|
|
||||||
//multiply all values in mul array
|
//multiply all values in mul array
|
||||||
for(var i = 0, l = skill.mul.length; i < l; i++){
|
_.each(skill.mul, function(effect){
|
||||||
mod *= pop(skill.mul[i].value, this);
|
mod *= pop(effect.value, this)
|
||||||
}
|
});
|
||||||
|
|
||||||
//largest min
|
//largest min
|
||||||
for(var i = 0, l = skill.min.length; i < l; i++){
|
_.each(skill.min, function(effect){
|
||||||
var min = pop(skill.min[i], this);
|
var min = pop(effect.value, this);
|
||||||
mod = mod > min? mod : min;
|
mod = mod > min? mod : min;
|
||||||
}
|
});
|
||||||
|
|
||||||
//smallest max
|
//smallest max
|
||||||
for(var i = 0, l = skill.max.length; i < l; i++){
|
_.each(skill.max, function(effect){
|
||||||
var max = pop(skill.max[i], this);
|
var max = pop(effect.value, this);
|
||||||
mod = mod < max? mod : max;
|
mod = mod < max? mod : max;
|
||||||
}
|
});
|
||||||
|
|
||||||
return signedString(mod);
|
return signedString(mod);
|
||||||
},
|
},
|
||||||
@@ -128,9 +121,9 @@ Characters.helpers({
|
|||||||
passiveSkill: function(skill){
|
passiveSkill: function(skill){
|
||||||
var mod = +this.skillMod(skill);
|
var mod = +this.skillMod(skill);
|
||||||
var value = 10 + mod;
|
var value = 10 + mod;
|
||||||
for(var i = 0, l = skill.passiveAdd.length; i < l; i++){
|
_.each(skill.passiveAdd, function(effect){
|
||||||
value += pop(skill.passiveAdd[i].value, this);
|
value += pop(effect.value, this);
|
||||||
}
|
});
|
||||||
return value;
|
return value;
|
||||||
//TODO decide whether (dis)advantage gives (-)+5 to passive checks
|
//TODO decide whether (dis)advantage gives (-)+5 to passive checks
|
||||||
},
|
},
|
||||||
@@ -156,12 +149,4 @@ Characters.helpers({
|
|||||||
});
|
});
|
||||||
return 20;
|
return 20;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
getMod = function(score){
|
|
||||||
return Math.floor((score-10)/2);
|
|
||||||
}
|
|
||||||
|
|
||||||
signedString = function(number){
|
|
||||||
return number > 0? "+" + number : "" + number;
|
|
||||||
}
|
|
||||||
@@ -44,7 +44,7 @@ Schemas.Attributes = new SimpleSchema({
|
|||||||
"armor.add": {
|
"armor.add": {
|
||||||
type: [Schemas.Effect],
|
type: [Schemas.Effect],
|
||||||
defaultValue: [
|
defaultValue: [
|
||||||
new Effect("Dexterity Modifier", "skillMod skills.dexterityArmor")
|
{name: "Dexterity Modifier", value: "skillMod skills.dexterityArmor"}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -9,9 +9,4 @@ Schemas.Buff = new SimpleSchema({
|
|||||||
effect: {
|
effect: {
|
||||||
type: Schemas.Effect
|
type: Schemas.Effect
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Buff = function(name, stat, value){
|
|
||||||
this.stat = stat;
|
|
||||||
this.effect = new Effect(name, value);
|
|
||||||
};
|
|
||||||
@@ -7,7 +7,7 @@ Schemas.Effect = new SimpleSchema({
|
|||||||
type: String,
|
type: String,
|
||||||
regEx: SimpleSchema.RegEx.Id,
|
regEx: SimpleSchema.RegEx.Id,
|
||||||
autoValue: function(){
|
autoValue: function(){
|
||||||
if(!isSet) return Random.id();
|
if(!this.isSet) return Random.id();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
name: {
|
name: {
|
||||||
@@ -22,14 +22,4 @@ Schemas.Effect = new SimpleSchema({
|
|||||||
type: String,
|
type: String,
|
||||||
optional: true
|
optional: true
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Effect = function(name, value){
|
|
||||||
this._id = Random.id();
|
|
||||||
this.name = name;
|
|
||||||
if (typeof value === "string"){
|
|
||||||
this.calculation = value;
|
|
||||||
} else if (typeof valye === "number"){
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
12
rpg-docs/Model/Character/SubSchemas/Expiration.js
Normal file
12
rpg-docs/Model/Character/SubSchemas/Expiration.js
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
//schema to store all effects which expire and their expiry dates
|
||||||
|
Schemas.Expiration = new SimpleSchema({
|
||||||
|
_id: {
|
||||||
|
type: String,
|
||||||
|
regEx: SimpleSchema.RegEx.Id,
|
||||||
|
autoValue: function(){
|
||||||
|
if(!this.isSet) return Random.id();
|
||||||
|
}},
|
||||||
|
stat: { type: String },
|
||||||
|
effectId: { type: String, regEx: SimpleSchema.RegEx.Id },
|
||||||
|
expiry: { type: Number }
|
||||||
|
});
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
Schemas.Feature = new SimpleSchema({
|
Schemas.Feature = new SimpleSchema({
|
||||||
_id: {type: String, regEx: SimpleSchema.RegEx.Id},
|
_id: {type: String, regEx: SimpleSchema.RegEx.Id},
|
||||||
character: {type: String, regEx: SimpleSchema.RegEx.Id},
|
|
||||||
name: {type: String},
|
name: {type: String},
|
||||||
description:{type: String},
|
description:{type: String},
|
||||||
buffs: {type: [Schemas.Buff], optional: true},
|
buffs: {type: [Schemas.Buff], optional: true},
|
||||||
@@ -8,4 +7,4 @@ Schemas.Feature = new SimpleSchema({
|
|||||||
expires: {type: Number, optional: true},
|
expires: {type: Number, optional: true},
|
||||||
duration: {type: Number, optional: true},
|
duration: {type: Number, optional: true},
|
||||||
uses: {type: Number, min: 0, optional: true},
|
uses: {type: Number, min: 0, optional: true},
|
||||||
})
|
});
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
Schemas.Vulnerability = _.extend({
|
Schemas.Vulnerability = _.extend({
|
||||||
"min.defaultValue": [
|
"min.defaultValue": [
|
||||||
new Effect("Resistance doesn't stack", 0.5)
|
{name: "Resistance doesn't stack", value: 0.5}
|
||||||
],
|
],
|
||||||
"max.defaultValue": [
|
"max.defaultValue": [
|
||||||
new Effect("Vulnerability doesn't stack", 2)
|
{name: "Vulnerability doesn't stack", value: 2}
|
||||||
]
|
]
|
||||||
}, Schemas.Attribute);
|
}, Schemas.Attribute);
|
||||||
|
|
||||||
|
|||||||
@@ -6,5 +6,4 @@
|
|||||||
{{/each}}
|
{{/each}}
|
||||||
</div>
|
</div>
|
||||||
<input id="addCharacter" type="button" value="Add Character">
|
<input id="addCharacter" type="button" value="Add Character">
|
||||||
{{> quickForm collection="Characters" id="insertCharacterForm" type="insert"}}
|
|
||||||
</template>
|
</template>
|
||||||
@@ -1,11 +1,42 @@
|
|||||||
pushBuffs = function(id, buffArray){
|
//give a character a set of buffs that expire after [duration]
|
||||||
|
pushBuffs = function(id, buffArray, duration){
|
||||||
_.each(buffArray, function(buff){
|
_.each(buffArray, function(buff){
|
||||||
Characters.update(id, {$push: {"buff.stat": buff.effect}});
|
var pushObject = {};
|
||||||
|
if(duration > 0){
|
||||||
|
//expiry time is now plus duration
|
||||||
|
var expiry = Characters.findOne(id, {fields: {time: 1}}).time + duration;
|
||||||
|
//ensure the effect has an id
|
||||||
|
buff.effect._id = buff.effect._id || Random.id();
|
||||||
|
//build the expiration object
|
||||||
|
var expiration = {
|
||||||
|
stat: buff.stat,
|
||||||
|
effectId: buff.effect._id,
|
||||||
|
expiry: expiry
|
||||||
|
};
|
||||||
|
//push expiration object to character
|
||||||
|
Characters.update(id, {$push: {expirations: expiration } });
|
||||||
|
}
|
||||||
|
//push the effect to the character
|
||||||
|
pushObject[buff.stat] = buff.effect;
|
||||||
|
Characters.update(id, {$push: pushObject});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//pull all the buffs listed in the buffArray
|
||||||
pullBuffs = function(id, buffArray){
|
pullBuffs = function(id, buffArray){
|
||||||
_.each(buffArray, function(buff){
|
_.each(buffArray, function(buff){
|
||||||
Characters.update(id, {$pull: {"buff.stat": {_id: buff.effect._id} } });
|
pullEffect(id, buff.effect._id);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//pull a single effect by stat and id
|
||||||
|
pullEffect = function(id, stat, effectId){
|
||||||
|
var pullObject = {};
|
||||||
|
pullObject[stat] = {_id: effectId};
|
||||||
|
Characters.update(id, {$pull: pullObject });
|
||||||
|
}
|
||||||
|
|
||||||
|
//pull an expiry by id
|
||||||
|
pullExpiry = function(id, expiryId){
|
||||||
|
Characters.update(id, {$pull: {expirations: {_id: expiryId} } });
|
||||||
|
}
|
||||||
7
rpg-docs/lib/functions/characterUtility.js
Normal file
7
rpg-docs/lib/functions/characterUtility.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
getMod = function(score){
|
||||||
|
return Math.floor((score-10)/2);
|
||||||
|
}
|
||||||
|
|
||||||
|
signedString = function(number){
|
||||||
|
return number > 0? "+" + number : "" + number;
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
// turns dot notation strings into keys of root
|
// turns dot notation strings into keys of root
|
||||||
// argument formats:
|
// argument formats:
|
||||||
// 157, anything -> 157
|
// 157, object -> 157
|
||||||
// "some.number", object -> object.some.number
|
// "some.number", object -> object.some.number
|
||||||
// "some.function", object -> object.some.function()
|
// "some.function", object -> object.some.function()
|
||||||
// "some.function arg1 arg2", object -> object.some.function(arg1, arg2)
|
// "some.function arg1 arg2", object -> object.some.function(arg1, arg2)
|
||||||
|
|||||||
Reference in New Issue
Block a user