diff --git a/rpg-docs/Model/Inventory/Items.js b/rpg-docs/Model/Inventory/Items.js index 9287f807..f22bb8ef 100644 --- a/rpg-docs/Model/Inventory/Items.js +++ b/rpg-docs/Model/Inventory/Items.js @@ -47,6 +47,41 @@ Items.helpers({ if(!characterId || ! Characters.findOne(characterId)) throw "Invalid character"; if(this.parent.collection === "Characters" && this.parent.id === characterId && !this.enabled) return; Items.update(this._id, {$set: {"parent.collection": "Characters", "parent.id": characterId, charId: characterId, enabled: false}}); + }, + splitToParent: function(parent, moveQuantity){ + check(parent, {id: String, collection: String}); + check(moveQuantity, Number); + var parentCollection = Meteor.isClient? window[parent.collection] : global[parent.collection]; + if(!parent.id || !parentCollection.findOne(parent.id)) throw "Invalid parent"; + var oldStack = this; + //we can only move as much as we have, leaving 0 behind at worst + if(oldStack.quantity < moveQuantity) moveQuantity = oldStack.quantity; + var oldQuantity = oldStack.quantity - moveQuantity; + + var newStack = _.pick(oldStack, Schemas.Item.objectKeys()); + newStack.parent = parent; + newStack.quantity = moveQuantity; + + var existingStack = Items.findOne(_.omit(newStack, "quantity")); + var updateStackSize = function(){ + if(oldQuantity > 0){ + Items.update(oldStack._id, {$set: {quantity: oldQuantity}}); + } else { + Items.remove(oldStack._id); + } + }; + if(existingStack){ + Items.update(existingStack._id, {$inc: {quantity: moveQuantity}}, {}, function(){ + updateStackSize(); + }); + }else{ + Items.insert(newStack, function(err, id){ + if(err) throw err; + updateStackSize(); + //copy the children also + Meteor.call("cloneChildren", oldStack._id, {collection: "Items", id: id}); + }); + } } }); diff --git a/rpg-docs/client/views/character/inventory/inventory.js b/rpg-docs/client/views/character/inventory/inventory.js index 82311c84..4393567d 100644 --- a/rpg-docs/client/views/character/inventory/inventory.js +++ b/rpg-docs/client/views/character/inventory/inventory.js @@ -151,8 +151,20 @@ Template.layout.events({ }, "drop .itemContainer": function(event, instacne){ var item = Items.findOne(Session.get("inventory.dragItemId")); - //move item to the container - item.moveToContainer(this._id); + if(event.ctrlKey){ + //split the stack to the container + GlobalUI.showDialog({ + template: "splitStackDialog", + data: { + id: item._id, + parentCollection: "Containers", + parentId: this._id + } + }); + } else{ + //move item to the container + item.moveToContainer(this._id); + } resetInvetorySession(); }, "drop .equipmentContainer": function(event, instance){ @@ -164,7 +176,20 @@ Template.layout.events({ "drop .carriedContainer": function(event, instance){ var charId = Session.get("inventory.dragItemOriginalCharacter"); var item = Items.findOne(Session.get("inventory.dragItemId")); - item.moveToCharacter(this._id); + if(event.ctrlKey){ + //split the stack to the container + GlobalUI.showDialog({ + template: "splitStackDialog", + data: { + id: item._id, + parentCollection: "Characters", + parentId: this._id + } + }); + } else{ + //move item to the character + item.moveToCharacter(this._id); + } resetInvetorySession(); } }); diff --git a/rpg-docs/client/views/character/inventory/splitStackDialog/splitStackDialog.html b/rpg-docs/client/views/character/inventory/splitStackDialog/splitStackDialog.html new file mode 100644 index 00000000..114d91af --- /dev/null +++ b/rpg-docs/client/views/character/inventory/splitStackDialog/splitStackDialog.html @@ -0,0 +1,13 @@ + + diff --git a/rpg-docs/client/views/character/inventory/splitStackDialog/splitStackDialog.js b/rpg-docs/client/views/character/inventory/splitStackDialog/splitStackDialog.js new file mode 100644 index 00000000..84dbe9e6 --- /dev/null +++ b/rpg-docs/client/views/character/inventory/splitStackDialog/splitStackDialog.js @@ -0,0 +1,27 @@ +Template.splitStackDialog.helpers({ + quantity: function(){ + var item = Items.findOne(this.id); + if(item) return Math.round(item.quantity/2); + } +}); + +Template.splitStackDialog.events({ + 'tap #moveButton': function(event, instance){ + var item = Items.findOne(this.id); + if(item){ + item.splitToParent( + {collection: this.parentCollection , id: this.parentId}, + +instance.find('#quantityInput').value + ); + } + }, + 'tap #oneButton':function(event, instance){ + instance.find('#quantityInput').value = 1; + }, + 'tap #halfButton':function(event, instance){ + instance.find('#quantityInput').value = Math.round(Items.findOne(this.id).quantity/2); + }, + 'tap #allButton':function(event, instance){ + instance.find('#quantityInput').value = Items.findOne(this.id).quantity; + } +}); diff --git a/rpg-docs/lib/functions/parenting.js b/rpg-docs/lib/functions/parenting.js index 07842016..bd59feda 100644 --- a/rpg-docs/lib/functions/parenting.js +++ b/rpg-docs/lib/functions/parenting.js @@ -28,7 +28,7 @@ var limitModifierToKeys = function(modifier, keys){ var getParent = function(doc){ if(!doc || !doc.parent) return; - var parentCol = Meteor.isClient? + var parentCol = Meteor.isClient? window[doc.parent.collection] : global[doc.parent.collection]; if (parentCol) return parentCol.findOne(doc.parent.id, {removed: true}); @@ -128,8 +128,21 @@ Meteor.methods({ _.each(childCollections, function(collection){ collection.remove( - {charId: parent.charId, 'parent.id': parent._id} + {'parent.id': parent._id} ); }); + }, + cloneChildren: function (objectId, newParent){ + check(objectId, String); + check(newParent, {id: String, collection: String}); + + _.each(childCollections, function(collection){ + var keys = collection.simpleSchema().objectKeys(); + collection.find({'parent.id': objectId}).forEach(function(doc){ + var newDoc = _.pick( doc, keys); + newDoc.parent = newParent; + collection.insert(newDoc); + }); + }); } }); diff --git a/rpg-docs/tests/mocha/server/model/character/character.js b/rpg-docs/tests/mocha/server/model/character/character.js index 10c96732..f0f169e1 100644 --- a/rpg-docs/tests/mocha/server/model/character/character.js +++ b/rpg-docs/tests/mocha/server/model/character/character.js @@ -3,32 +3,23 @@ if (!(typeof MochaWeb === 'undefined')){ var charId; describe("Character", function(){ describe("insert", function(){ - it("should create a character", function(done){ - Characters.insert({owner: "FWeGYyDY5jc4HuTh8"}, function(err, id){ - charId = id; - done(err); - }); + it("should create a character", function(){ + charId = Characters.insert({owner: "FWeGYyDY5jc4HuTh8"}); }); - }); + }); describe("attribute.adjustment", function(){ - Characters.insert({owner: "FWeGYyDY5jc4HuTh8"}, function(err, id){ - charId = id; - done(err); - }); + Characters.insert({owner: "FWeGYyDY5jc4HuTh8"}); it("should track attribute adjustments", function(){ - Characters.update(charId, {$set: {"strength.adjustment": -12}},{},function(err, num){ - console.log(num); - done(err); - }); + Characters.update(charId, {$set: {"strength.adjustment": -12}}); }); it("should report the adjusted attribute correctly", function(){ var val = Characters.findOne(charId).attributeValue("strength"); chai.assert.equal(val, -12); - val = 0; val = Characters.findOne(charId).fieldValue("strength"); chai.assert.equal(val, -12); }); }); + Characters.remove({}); }); }); }