Implemented a javascript code style
This commit is contained in:
@@ -2,13 +2,17 @@
|
||||
Containers = new Mongo.Collection("containers");
|
||||
|
||||
Schemas.Container = new SimpleSchema({
|
||||
name: { type: String, trim: false },
|
||||
charId: { type: String, regEx: SimpleSchema.RegEx.Id},
|
||||
isCarried: { type: Boolean },
|
||||
name: {type: String, trim: false},
|
||||
charId: {type: String, regEx: SimpleSchema.RegEx.Id},
|
||||
isCarried: {type: Boolean},
|
||||
weight: {type: Number, min: 0, defaultValue: 0, decimal: true},
|
||||
value: {type: Number, min: 0, defaultValue: 0, decimal: true},
|
||||
description:{type: String, optional: true, trim: false},
|
||||
color: {type: String, allowedValues: _.pluck(colorOptions, "key"), defaultValue: "q"}
|
||||
color: {
|
||||
type: String,
|
||||
allowedValues: _.pluck(colorOptions, "key"),
|
||||
defaultValue: "q",
|
||||
},
|
||||
});
|
||||
|
||||
Containers.attachSchema(Schemas.Container);
|
||||
@@ -16,7 +20,10 @@ Containers.attachSchema(Schemas.Container);
|
||||
Containers.helpers({
|
||||
contentsValue: function(){
|
||||
var value = 0;
|
||||
Items.find({"parent.id": this._id}, {fields: {quantity: 1, value: 1}}).forEach(function(item){
|
||||
Items.find(
|
||||
{"parent.id": this._id},
|
||||
{fields: {quantity: 1, value: 1}}
|
||||
).forEach(function(item){
|
||||
value += item.totalValue();
|
||||
});
|
||||
return value;
|
||||
@@ -26,7 +33,10 @@ Containers.helpers({
|
||||
},
|
||||
contentsWeight: function(){
|
||||
var weight = 0;
|
||||
Items.find({"parent.id": this._id}, {fields: {quantity: 1, weight: 1}}).forEach(function(item){
|
||||
Items.find(
|
||||
{"parent.id": this._id},
|
||||
{fields: {quantity: 1, weight: 1}}
|
||||
).forEach(function(item){
|
||||
weight += item.totalWeight();
|
||||
});
|
||||
return weight;
|
||||
@@ -35,12 +45,12 @@ Containers.helpers({
|
||||
return this.contentsWeight() + this.weight;
|
||||
},
|
||||
moveToCharacter: function(characterId){
|
||||
if(this.charId === characterId) return;
|
||||
if (this.charId === characterId) return;
|
||||
Items.update(this._id, {$set: {charId: characterId}});
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
Containers.attachBehaviour('softRemovable');
|
||||
Containers.attachBehaviour("softRemovable");
|
||||
makeParent(Containers); //parents of items
|
||||
|
||||
Containers.allow(CHARACTER_SUBSCHEMA_ALLOW);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
Items = new Mongo.Collection('items');
|
||||
Items = new Mongo.Collection("items");
|
||||
|
||||
Schemas.Item = new SimpleSchema({
|
||||
name: {type: String, defaultValue: "New Item", trim: false},
|
||||
@@ -10,7 +10,11 @@ Schemas.Item = new SimpleSchema({
|
||||
value: {type: Number, min: 0, defaultValue: 0, decimal: true},
|
||||
enabled: {type: Boolean, defaultValue: false},
|
||||
requiresAttunement: {type: Boolean, defaultValue: false},
|
||||
color: {type: String, allowedValues: _.pluck(colorOptions, "key"), defaultValue: "q"}
|
||||
color: {
|
||||
type: String,
|
||||
allowedValues: _.pluck(colorOptions, "key"),
|
||||
defaultValue: "q",
|
||||
},
|
||||
});
|
||||
|
||||
Items.attachSchema(Schemas.Item);
|
||||
@@ -23,40 +27,77 @@ Items.helpers({
|
||||
return this.weight * this.quantity;
|
||||
},
|
||||
pluralName: function(){
|
||||
if(this.plural && this.quantity !== 1){
|
||||
if (this.plural && this.quantity !== 1){
|
||||
return this.plural;
|
||||
} else{
|
||||
} else {
|
||||
return this.name;
|
||||
}
|
||||
},
|
||||
equip: function(characterId){
|
||||
var charId = characterId || this.charId;
|
||||
if(!charId || ! Characters.findOne(charId)) throw "Invalid character";
|
||||
if(this.parent.collection === "Characters" && this.parent.id === charId && this.enabled) return;
|
||||
Items.update(this._id, {$set: {"parent.collection": "Characters", "parent.id": charId, enabled: true}});
|
||||
if (!charId || !Characters.findOne(charId)) throw "Invalid character";
|
||||
if (this.parent.collection === "Characters" &&
|
||||
this.parent.id === charId &&
|
||||
this.enabled) {
|
||||
return;
|
||||
}
|
||||
Items.update(
|
||||
this._id,
|
||||
{$set: {
|
||||
"parent.collection": "Characters",
|
||||
"parent.id": charId,
|
||||
enabled: true,
|
||||
}}
|
||||
);
|
||||
},
|
||||
unequip: function(){
|
||||
if(!this.enabled) return;
|
||||
if (!this.enabled) return;
|
||||
Items.update(this._id, {$set: {enabled: false}});
|
||||
},
|
||||
moveToContainer: function(containerId){
|
||||
if( !containerId || !Containers.findOne(containerId) ) throw "Invalid container";
|
||||
if(this.parent.collection === "Containers" && this.parent.id === containerId && !this.enabled) return;
|
||||
Items.update(this._id, {$set: {"parent.collection": "Containers", "parent.id": containerId, enabled: false}});
|
||||
if (!containerId || !Containers.findOne(containerId)){
|
||||
throw "Invalid container";
|
||||
}
|
||||
if (this.parent.collection === "Containers" &&
|
||||
this.parent.id === containerId &&
|
||||
!this.enabled) return;
|
||||
Items.update(
|
||||
this._id,
|
||||
{$set: {
|
||||
"parent.collection": "Containers",
|
||||
"parent.id": containerId,
|
||||
enabled: false,
|
||||
}}
|
||||
);
|
||||
},
|
||||
moveToCharacter: function(characterId){
|
||||
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}});
|
||||
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 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;
|
||||
if (oldStack.quantity < moveQuantity) moveQuantity = oldStack.quantity;
|
||||
var oldQuantity = oldStack.quantity - moveQuantity;
|
||||
|
||||
var newStack = _.pick(oldStack, Schemas.Item.objectKeys());
|
||||
@@ -65,31 +106,39 @@ Items.helpers({
|
||||
|
||||
var existingStack = Items.findOne(_.omit(newStack, "quantity"));
|
||||
var updateStackSize = function(){
|
||||
if(oldQuantity > 0){
|
||||
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{
|
||||
if (existingStack){
|
||||
Items.update(
|
||||
existingStack._id,
|
||||
{$inc: {quantity: moveQuantity}},
|
||||
{},
|
||||
function(){
|
||||
updateStackSize();
|
||||
}
|
||||
);
|
||||
} else {
|
||||
Items.insert(newStack, function(err, id){
|
||||
if(err) throw err;
|
||||
if (err) throw err;
|
||||
updateStackSize();
|
||||
//copy the children also
|
||||
Meteor.call("cloneChildren", oldStack._id, {collection: "Items", id: id});
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
Items.before.update(function(userId, doc, fieldNames, modifier, options){
|
||||
if(
|
||||
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
|
||||
!(
|
||||
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";
|
||||
@@ -97,21 +146,21 @@ Items.before.update(function(userId, doc, fieldNames, modifier, options){
|
||||
}
|
||||
});
|
||||
|
||||
Items.attachBehaviour('softRemovable');
|
||||
Items.attachBehaviour("softRemovable");
|
||||
makeChild(Items); //children of containers
|
||||
makeParent(Items, ['name', 'enabled']); //parents of effects and attacks
|
||||
makeParent(Items, ["name", "enabled"]); //parents of effects and attacks
|
||||
|
||||
Items.allow(CHARACTER_SUBSCHEMA_ALLOW);
|
||||
|
||||
//give characters default items
|
||||
Characters.after.insert(function (userId, char) {
|
||||
if(Meteor.isServer){
|
||||
Characters.after.insert(function(userId, char) {
|
||||
if (Meteor.isServer){
|
||||
var containerId = Containers.insert({
|
||||
name: "Coin Pouch",
|
||||
charId: char._id,
|
||||
isCarried: true,
|
||||
description: "A sturdy pouch for coins",
|
||||
color: "d"
|
||||
color: "d",
|
||||
});
|
||||
Items.insert({
|
||||
name: "Gold piece",
|
||||
@@ -123,8 +172,8 @@ Characters.after.insert(function (userId, char) {
|
||||
color: "n",
|
||||
parent: {
|
||||
id: containerId,
|
||||
collection: "Containers"
|
||||
}
|
||||
collection: "Containers",
|
||||
},
|
||||
});
|
||||
Items.insert({
|
||||
name: "Silver piece",
|
||||
@@ -136,8 +185,8 @@ Characters.after.insert(function (userId, char) {
|
||||
color: "q",
|
||||
parent: {
|
||||
id: containerId,
|
||||
collection: "Containers"
|
||||
}
|
||||
collection: "Containers",
|
||||
},
|
||||
});
|
||||
Items.insert({
|
||||
name: "Copper piece",
|
||||
@@ -149,8 +198,8 @@ Characters.after.insert(function (userId, char) {
|
||||
color: "s",
|
||||
parent: {
|
||||
id: containerId,
|
||||
collection: "Containers"
|
||||
}
|
||||
collection: "Containers",
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user