Added stack splitting and merging to inventory

This commit is contained in:
Thaum
2015-03-25 12:09:24 +00:00
parent 6b14b820a6
commit cb2d1ab958
6 changed files with 124 additions and 20 deletions

View File

@@ -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});
});
}
}
});

View File

@@ -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();
}
});

View File

@@ -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>

View File

@@ -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;
}
});

View File

@@ -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);
});
});
}
});

View File

@@ -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({});
});
});
}