Added stack splitting and merging to inventory
This commit is contained in:
@@ -47,6 +47,41 @@ Items.helpers({
|
|||||||
if(!characterId || ! Characters.findOne(characterId)) throw "Invalid character";
|
if(!characterId || ! Characters.findOne(characterId)) throw "Invalid character";
|
||||||
if(this.parent.collection === "Characters" && this.parent.id === characterId && !this.enabled) return;
|
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}});
|
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});
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -151,8 +151,20 @@ Template.layout.events({
|
|||||||
},
|
},
|
||||||
"drop .itemContainer": function(event, instacne){
|
"drop .itemContainer": function(event, instacne){
|
||||||
var item = Items.findOne(Session.get("inventory.dragItemId"));
|
var item = Items.findOne(Session.get("inventory.dragItemId"));
|
||||||
//move item to the container
|
if(event.ctrlKey){
|
||||||
item.moveToContainer(this._id);
|
//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();
|
resetInvetorySession();
|
||||||
},
|
},
|
||||||
"drop .equipmentContainer": function(event, instance){
|
"drop .equipmentContainer": function(event, instance){
|
||||||
@@ -164,7 +176,20 @@ Template.layout.events({
|
|||||||
"drop .carriedContainer": function(event, instance){
|
"drop .carriedContainer": function(event, instance){
|
||||||
var charId = Session.get("inventory.dragItemOriginalCharacter");
|
var charId = Session.get("inventory.dragItemOriginalCharacter");
|
||||||
var item = Items.findOne(Session.get("inventory.dragItemId"));
|
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();
|
resetInvetorySession();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<!-- data needs to include id of item, parentCollection, parentId -->
|
||||||
|
<template name="splitStackDialog">
|
||||||
|
<div style="width: 300px; height: 110px;">
|
||||||
|
<paper-input-decorator label="Quantity" floatinglabel>
|
||||||
|
<input id="quantityInput" type="number" value={{quantity}}>
|
||||||
|
</paper-input-decorator>
|
||||||
|
<paper-button id="oneButton"> One </paper-button>
|
||||||
|
<paper-button id="halfButton"> Half </paper-button>
|
||||||
|
<paper-button id="allButton"> All </paper-button>
|
||||||
|
</div>
|
||||||
|
<paper-button id="cancelButton" affirmative> Cancel </paper-button>
|
||||||
|
<paper-button id="moveButton" affirmative> Move </paper-button>
|
||||||
|
</template>
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -128,8 +128,21 @@ Meteor.methods({
|
|||||||
|
|
||||||
_.each(childCollections, function(collection){
|
_.each(childCollections, function(collection){
|
||||||
collection.remove(
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -3,32 +3,23 @@ if (!(typeof MochaWeb === 'undefined')){
|
|||||||
var charId;
|
var charId;
|
||||||
describe("Character", function(){
|
describe("Character", function(){
|
||||||
describe("insert", function(){
|
describe("insert", function(){
|
||||||
it("should create a character", function(done){
|
it("should create a character", function(){
|
||||||
Characters.insert({owner: "FWeGYyDY5jc4HuTh8"}, function(err, id){
|
charId = Characters.insert({owner: "FWeGYyDY5jc4HuTh8"});
|
||||||
charId = id;
|
|
||||||
done(err);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe("attribute.adjustment", function(){
|
describe("attribute.adjustment", function(){
|
||||||
Characters.insert({owner: "FWeGYyDY5jc4HuTh8"}, function(err, id){
|
Characters.insert({owner: "FWeGYyDY5jc4HuTh8"});
|
||||||
charId = id;
|
|
||||||
done(err);
|
|
||||||
});
|
|
||||||
it("should track attribute adjustments", function(){
|
it("should track attribute adjustments", function(){
|
||||||
Characters.update(charId, {$set: {"strength.adjustment": -12}},{},function(err, num){
|
Characters.update(charId, {$set: {"strength.adjustment": -12}});
|
||||||
console.log(num);
|
|
||||||
done(err);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
it("should report the adjusted attribute correctly", function(){
|
it("should report the adjusted attribute correctly", function(){
|
||||||
var val = Characters.findOne(charId).attributeValue("strength");
|
var val = Characters.findOne(charId).attributeValue("strength");
|
||||||
chai.assert.equal(val, -12);
|
chai.assert.equal(val, -12);
|
||||||
val = 0;
|
|
||||||
val = Characters.findOne(charId).fieldValue("strength");
|
val = Characters.findOne(charId).fieldValue("strength");
|
||||||
chai.assert.equal(val, -12);
|
chai.assert.equal(val, -12);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Characters.remove({});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user