Overhauled data models to make actions and libraries more universal

This commit is contained in:
Stefan Zermatten
2019-03-12 16:47:20 +02:00
parent febb65a513
commit 94f6631a7d
62 changed files with 1076 additions and 1734 deletions

View File

@@ -1,57 +1,44 @@
import SimpleSchema from 'simpl-schema';
import schema from '/imports/api/schema.js';
import {makeParent} from "/imports/api/parenting.js";
import ColorSchema from "/imports/api/creature/subSchemas/ColorSchema.js";
import PropertySchema from '/imports/api/creature/subSchemas/PropertySchema.js';
import ChildSchema from '/imports/api/parenting/ChildSchema.js';
//set up the collection for containers
let Containers = new Mongo.Collection("containers");
let containerSchema = schema({
name: {type: String, optional: true, trim: false},
charId: {type: String, regEx: SimpleSchema.RegEx.Id, index: 1},
isCarried: {type: Boolean},
weight: {type: Number, min: 0, defaultValue: 0},
value: {type: Number, min: 0, defaultValue: 0},
description:{type: String, optional: true, trim: false},
});
Containers.attachSchema(containerSchema);
Containers.attachSchema(ColorSchema);
Containers.helpers({
contentsValue: function(){
var value = 0;
Items.find(
{"parent.id": this._id},
{fields: {quantity: 1, value: 1}}
).forEach(function(item){
value += item.totalValue();
});
return value;
let ContainerSchema = schema({
name: {
type: String,
optional: true,
trim: false
},
totalValue: function(){
return this.contentsValue() + this.value;
isCarried: {
type: Boolean,
defaultValue: true,
},
contentsWeight: function(){
var weight = 0;
Items.find(
{"parent.id": this._id},
{fields: {quantity: 1, weight: 1}}
).forEach(function(item){
weight += item.totalWeight();
});
return weight;
weight: {
type: Number,
min: 0,
defaultValue: 0
},
totalWeight: function(){
return this.contentsWeight() + this.weight;
value: {
type: Number,
min: 0,
defaultValue: 0
},
moveToCharacter: function(characterId){
if (this.charId === characterId) return;
Items.update(this._id, {$set: {charId: characterId}});
description: {
type: String,
optional: true,
trim: false
},
});
// Containers.attachBehaviour("softRemovable");
makeParent(Containers); //parents of items
ContainerSchema.extend(ColorSchema);
Containers.attachSchema(ContainerSchema);
Containers.attachSchema(PropertySchema);
Containers.attachSchema(ChildSchema);
export default Containers;
export { ContainerSchema };