Implemented item editing

This commit is contained in:
Thaum
2015-01-26 13:28:10 +00:00
parent 6a2e7f0832
commit dc6ea555e5
25 changed files with 331 additions and 133 deletions

View File

@@ -223,7 +223,7 @@ Characters.attachSchema(Schemas.Character);
var attributeBase = function(charId, statName){
check(statName, String);
var effects = Effects.find({charId: charId, stat: statName}).fetch();
var effects = Effects.find({charId: charId, stat: statName, enabled: true}).fetch();
effects = _.groupBy(effects, "operation");
var value = 0;
@@ -360,7 +360,7 @@ Characters.helpers({
//add multiplied proficiency bonus to modifier
mod += prof * this.attributeValue("proficiencyBonus");
Effects.find({charId: charId, stat: skillName}).forEach(function(effect){
Effects.find({charId: charId, stat: skillName, enabled: true}).forEach(function(effect){
switch(effect.operation) {
case "add":
mod += evaluateEffect(charId, effect);
@@ -390,7 +390,7 @@ Characters.helpers({
var charId = this._id;
//return largest value in proficiency array
var prof = 0;
Effects.find({charId: charId, stat: skillName}).forEach(function(effect){
Effects.find({charId: charId, stat: skillName, enabled: true}).forEach(function(effect){
if(effect.operation === "proficiency"){
var newProf = evaluateEffect(charId, effect);
if (newProf > prof){
@@ -408,7 +408,7 @@ Characters.helpers({
var charId = this._id
var mod = +this.skillMod(skillName);
var value = 10 + mod;
Effects.find({charId: charId, stat: skillName}).forEach(function(effect){
Effects.find({charId: charId, stat: skillName, enabled: true}).forEach(function(effect){
if(effect.operation === "passiveAdd"){
value += evaluateEffect(charId, effect);
}

View File

@@ -43,7 +43,27 @@ Schemas.Effect = new SimpleSchema({
stat: {
type: String,
optional: true
},
enabled: {
type: Boolean,
defaultValue: true
}
});
Effects.attachSchema(Schemas.Effect);
//Keep effects in-sync with items
Effects.find({type: "equipment"}, {fields: {type: 1, enabled: 1, sourceId: 1}}).observe({
added: function(newEffect){
var item = Items.findOne(newEffect.sourceId);
if(item && item.equipped !== newEffect.enabled){
Effects.update(newEffect._id, {$set: {enabled: item.equipped}})
}
},
changed: function(newEffect, oldEffect){
var item = Items.findOne(newEffect.sourceId);
if(item && item.equipped !== newEffect.enabled){
Effects.update(newEffect._id, {$set: {enabled: item.equipped}})
}
}
})

View File

@@ -1,41 +0,0 @@
/*
* Effects are reason-value attached to skills and abilities
* that modify their final value or presentation in some way
*/
Schemas.Effect = new SimpleSchema({
_id: {
type: String,
regEx: SimpleSchema.RegEx.Id,
autoValue: function(){
if(!this.isSet) return Random.id();
}
},
name: {
type: String
},
operation: {
type: String,
defaultValue: "add",
allowedValues: ["base", "proficiency","add","mul","min","max","advantage","disadvantage","passiveAdd","fail","conditional"]
},
value: {
type: Number,
decimal: true,
optional: true
},
calculation: {
type: String,
optional: true
},
//indicates what the effect originated from
type: {
type: String,
defaultValue: "editable",
allowedValues: ["editable", "feature", "buff", "equipment", "inate"]
},
//which stat the effect is applied to
stat: {
type: String,
optional: true
}
});

View File

@@ -4,13 +4,11 @@ Schemas.Item = new SimpleSchema({
name: {type: String, defaultValue: "New Item"},
plural: {type: String, optional: true},
description:{type: String, defaultValue: ""},
container: {type: String, regEx: SimpleSchema.RegEx.Id, optional: true}, //id of container it normally is stowed in
charId: {type: String, regEx: SimpleSchema.RegEx.Id, optional: true}, //id of owner
container: {type: String, regEx: SimpleSchema.RegEx.Id}, //id of container it is normally stowed in
charId: {type: String, regEx: SimpleSchema.RegEx.Id}, //id of owner
quantity: {type: Number, min: 0, defaultValue: 1},
weight: {type: Number, min: 0, defaultValue: 0, decimal: true},
value: {type: Number, min: 0, defaultValue: 0, decimal: true},
tradeGood: {type: Boolean, defaultValue: false},
stackable: {type: Boolean, defaultValue: false},
equipmentSlot: {
type: String,
defaultValue: "none",
@@ -29,10 +27,33 @@ Items.helpers({
return this.weight * this.quantity;
},
pluralName: function(){
if(this.stackable && this.plural && this.quantity > 1){
if(this.plural && this.quantity > 1){
return this.plural;
} else{
return this.name;
}
}
});
});
//keep effects sycned with items
Items.find({}, {fields: {equipped: 1}}).observeChanges({
added: function(id, fields){
Effects.find({type: "equipment", sourceId: id}, {fields: {enabled: 1} }).forEach(function(effect){
if(fields.equipped !== effect.enabled){
Effects.update(effect._id, {$set: {enabled: fields.equipped}})
}
});
},
changed: function(id, fields){
Effects.find({type: "equipment", sourceId: id}, {fields: {enabled: 1} }).forEach(function(effect){
if(fields.equipped !== effect.enabled){
Effects.update(effect._id, {$set: {enabled: fields.equipped}})
}
});
},
removed: function(id){
Effects.find({type: "equipment", sourceId: id}, {fields: {_id: 1} }).forEach(function(effect){
Effects.remove(effect._id);
});
}
});