diff --git a/rpg-docs/Model/Character/Attacks.js b/rpg-docs/Model/Character/Attacks.js index 57020e02..aceb8c6c 100644 --- a/rpg-docs/Model/Character/Attacks.js +++ b/rpg-docs/Model/Character/Attacks.js @@ -69,5 +69,17 @@ Attacks.attachSchema(Schemas.Attack); Attacks.attachBehaviour("softRemovable"); makeChild(Attacks, ["name", "enabled"]); //children of lots of things +Attacks.after.insert(function (userId, attack) { + //Check to see if this attack's parent is a spell, if so, mirror prepared state to enabled + if (attack.parent.collection === "Spells") { + var parentSpell = Spells.findOne(attack.parentId); + if (parentSpell.prepared === "unprepared") { + Attacks.update(attack._id, {$set: {enabled: false}}); + } else if (parentSpell.prepared === "prepared" || "always") { + Attacks.update(attack._id, {$set: {enabled: true}}); + } + } +}); + Attacks.allow(CHARACTER_SUBSCHEMA_ALLOW); Attacks.deny(CHARACTER_SUBSCHEMA_DENY); diff --git a/rpg-docs/Model/Character/Spells.js b/rpg-docs/Model/Character/Spells.js index f4759512..43e5e388 100644 --- a/rpg-docs/Model/Character/Spells.js +++ b/rpg-docs/Model/Character/Spells.js @@ -64,5 +64,21 @@ Spells.attachBehaviour("softRemovable"); makeChild(Spells); //children of spell lists makeParent(Spells, ["name", "enabled"]); //parents of attacks +Spells.after.update(function (userId, spell, fieldNames) { + //Update prepared state of spell and child attacks to be enabled or not + if (_.contains(fieldNames, "prepared")) { + var childAttacks = Attacks.find({"parent.id": spell._id}).fetch(); + if (spell.prepared === "unprepared") { + _.each(childAttacks, function(attack){ + Attacks.update(attack._id, {$set: {enabled: false}}); + }); + } else if (spell.prepared === "prepared" || "always") { + _.each(childAttacks, function(attack){ + Attacks.update(attack._id, {$set: {enabled: true}}); + }); + } + } +}); + Spells.allow(CHARACTER_SUBSCHEMA_ALLOW); Spells.deny(CHARACTER_SUBSCHEMA_DENY);