From 91acc1d3b7875d108b3ef2fd5056b4aa7e4b6b30 Mon Sep 17 00:00:00 2001 From: Thaum Date: Wed, 25 Mar 2015 09:41:00 +0000 Subject: [PATCH] Added "Carried" pseudo-container --- rpg-docs/Model/Inventory/Items.js | 6 +- .../views/character/inventory/inventory.html | 15 ++++ .../views/character/inventory/inventory.js | 71 ++++++++++++------- rpg-docs/lib/functions/parenting.js | 5 +- 4 files changed, 68 insertions(+), 29 deletions(-) diff --git a/rpg-docs/Model/Inventory/Items.js b/rpg-docs/Model/Inventory/Items.js index 44407b5e..a0bd679c 100644 --- a/rpg-docs/Model/Inventory/Items.js +++ b/rpg-docs/Model/Inventory/Items.js @@ -51,7 +51,11 @@ Items.helpers({ }); Items.before.update(function(userId, doc, fieldNames, modifier, options){ - if(modifier && modifier.$set && modifier.$set.enabled){ + if( + modifier && modifier.$set && modifier.$set.enabled && //we are equipping this item + !(modifier.$set["parent.collection"] === "Characters" && modifier.$set["parent.id"]) //and we haven't specified a character to equip to + ){ + //equip it to the current character modifier.$set["parent.collection"] = "Characters"; modifier.$set["parent.id"] = doc.charId; } diff --git a/rpg-docs/client/views/character/inventory/inventory.html b/rpg-docs/client/views/character/inventory/inventory.html index dc1fbdd5..7400496a 100644 --- a/rpg-docs/client/views/character/inventory/inventory.html +++ b/rpg-docs/client/views/character/inventory/inventory.html @@ -35,6 +35,21 @@ {{/each}} + + +
+
+ Carried +
+
{{valueString carriedValue}}
+
{{round carriedWeight}}lbs
+
+
+ {{#each carriedItems}} + {{>inventoryItem}} + {{/each}} +
+
{{#each containers}}
diff --git a/rpg-docs/client/views/character/inventory/inventory.js b/rpg-docs/client/views/character/inventory/inventory.js index 9cba6291..82311c84 100644 --- a/rpg-docs/client/views/character/inventory/inventory.js +++ b/rpg-docs/client/views/character/inventory/inventory.js @@ -1,10 +1,10 @@ Template.inventory.created = function(){ this.showAddButtons = new ReactiveVar(false); -} +}; Template.inventory.helpers({ containers: function(){ - return Containers.find({charId: this._id}, {sort: {color: 1, name: 1}}) + return Containers.find({charId: this._id}, {sort: {color: 1, name: 1}}); }, items: function(charId, containerId){ return Items.find({charId: charId, "parent.id": containerId }, {sort: {color: 1, name: 1}}); @@ -12,11 +12,14 @@ Template.inventory.helpers({ equipment: function(){ return Items.find({ charId: this._id, enabled: true }, {sort: {color: 1, name: 1}}); }, + carriedItems: function(){ + return Items.find({charId: this._id, enabled: false, "parent.id": this._id}, {sort: {color: 1, name: 1}}); + }, showAddButtons: function(){ return Template.instance().showAddButtons.get(); }, colorClass: function(){ - return getColorClass(this.color) + return getColorClass(this.color); }, netWorth: function(){ var worth = 0; @@ -48,28 +51,35 @@ Template.inventory.helpers({ weight += item.totalWeight(); }); return weight; - } + }, + carriedValue: function(){ + var value = 0; + Items.find({charId: this._id, enabled: false, "parent.id": this._id}, {fields: {value : 1, quantity: 1}}).forEach(function(item){ + value += item.totalValue(); + }); + return value; + }, + carriedWeight: function(){ + var weight = 0; + Items.find({charId: this._id, enabled: false, "parent.id": this._id}, {fields: {weight : 1, quantity: 1}}).forEach(function(item){ + weight += item.totalWeight(); + }); + return weight; + }, }); Template.inventory.events({ "tap #addItem": function(event){ var charId = this._id; - var container = Containers.findOne({charId: charId}, {sort: {name: 1, _id: 1}, fields: {_id: 1}}); - var containerId; - if(container){ - containerId = container._id; - } else{ - console.log("no container was found for new item, adding a new container"); - containerId = Containers.insert({name: "New Container", isCarried: true, charId: this._id}); - } - _.defer(function(){ - var itemId = Items.insert({ - charId: charId, - parent:{ - id: containerId, - collection: "Containers" - } - }); + console.log(charId); + Items.insert({ + charId: charId, + parent:{ + id: charId, + collection: "Characters" + } + }, function(err, itemId){ + if(err) console.log(err); GlobalUI.setDetail({ template: "itemDialog", data: {itemId: itemId, charId: charId}, @@ -128,18 +138,21 @@ Template.layout.events({ Session.set("inventory.dragItemOriginalCharacter", this.charId); }, "dragend .inventoryItem": function(event, instance){ - resetInvetorySession(); + resetInvetorySession(); //this is a valid drop zone }, "dragover .itemContainer": function(event, instance){ event.preventDefault(); }, "dragover .equipmentContainer": function(event, instance){ - event.preventDefault(); //this is a valid drop zone + event.preventDefault(); + }, + "dragover .carriedContainer": function(event, instance){ + event.preventDefault(); }, "drop .itemContainer": function(event, instacne){ var item = Items.findOne(Session.get("inventory.dragItemId")); //move item to the container - item.moveToContainer(this._id) + item.moveToContainer(this._id); resetInvetorySession(); }, "drop .equipmentContainer": function(event, instance){ @@ -147,13 +160,19 @@ Template.layout.events({ var item = Items.findOne(Session.get("inventory.dragItemId")); item.equip(charId); resetInvetorySession(); + }, + "drop .carriedContainer": function(event, instance){ + var charId = Session.get("inventory.dragItemOriginalCharacter"); + var item = Items.findOne(Session.get("inventory.dragItemId")); + item.moveToCharacter(this._id); + resetInvetorySession(); } -}) +}); var resetInvetorySession = function(){ _.defer(function(){ Session.set("inventory.dragItemId", null); Session.set("inventory.dragItemOriginalContainer", null); Session.set("inventory.dragItemOriginalCharacter", null); - }) -} + }); +}; diff --git a/rpg-docs/lib/functions/parenting.js b/rpg-docs/lib/functions/parenting.js index 1cec4733..07842016 100644 --- a/rpg-docs/lib/functions/parenting.js +++ b/rpg-docs/lib/functions/parenting.js @@ -39,6 +39,7 @@ var inheritParentProperties = function(doc, collection){ if(!parent) throw new Meteor.Error('Parenting Error', 'Document\'s parent does not exist'); var handMeDowns = _.pick(parent, collection.inheritedKeys); + if(_.isEmpty(handMeDowns)) return; collection.update(doc._id, {$set: handMeDowns}); }; @@ -57,7 +58,7 @@ makeChild = function(collection, inheritedKeys){ window[this.parent.collection] : global[this.parent.collection]; } }); - + //when created, inherit parent properties collection.after.insert(function(userId, doc){ inheritParentProperties(doc, collection); @@ -96,7 +97,7 @@ var checkPermission = function(userId, charId){ if(!char) throw new Meteor.Error('Access Denied', 'Character '+charId+' does not exist'); - if (!userId) + if (!userId) throw new Meteor.Error('Access Denied', 'No UserId set when trying to update character asset.'); if (char.owner !== userId && !_.contains(char.writers, userId))