|
- module.exports = {
- type: 'stash',
-
- active: false,
- items: [],
- changed: false,
-
- init: function (blueprint) {
- let items = blueprint.items || [];
- let iLen = items.length;
- for (let i = 0; i < iLen; i++)
- this.getItem(items[i]);
-
- delete blueprint.items;
-
- this.blueprint = blueprint;
- },
-
- getItem: function (item) {
- //Material?
- let exists = false;
- if (((item.material) || (item.quest) || (item.quantity)) && (!item.noStack) && (!item.uses)) {
- let existItem = this.items.find(i => i.name === item.name);
- if (existItem) {
- exists = true;
- if (!existItem.quantity)
- existItem.quantity = 1;
- existItem.quantity += item.quantity;
-
- //We modify the old object because it gets sent to the client
- item.id = existItem.id;
- item.quantity = existItem.quantity;
-
- item = existItem;
- }
- }
-
- //Get next id
- if (!exists) {
- let id = 0;
- let items = this.items;
- let iLen = items.length;
- for (let i = 0; i < iLen; i++) {
- let fItem = items[i];
- if (fItem.id >= id)
- id = fItem.id + 1;
- }
- item.id = id;
- }
-
- if (!exists)
- this.items.push(item);
- },
-
- deposit: function (item) {
- if (!this.active)
- return;
-
- this.getItem(item);
-
- this.obj.syncer.setArray(true, 'stash', 'getItems', item);
-
- this.changed = true;
- },
-
- destroyItem: function (id) {
- let item = this.items.find(i => i.id === id);
- if (!item)
- return;
-
- this.items.spliceWhere(i => i === item);
-
- this.obj.syncer.setArray(true, 'stash', 'destroyItems', id);
-
- this.changed = true;
- },
-
- withdraw: function (id) {
- if (!this.active)
- return;
-
- let item = this.items.find(i => i.id === id);
- if (!item)
- return;
-
- this.obj.inventory.getItem(item);
- this.items.spliceWhere(i => i === item);
-
- this.obj.syncer.setArray(true, 'stash', 'destroyItems', id);
-
- this.changed = true;
- },
-
- setActive: function (active) {
- this.active = active;
- this.obj.syncer.set(true, 'stash', 'active', this.active);
- },
-
- simplify: function (self) {
- if (!self)
- return null;
-
- return {
- type: 'stash',
- active: this.active,
- items: this.items
- };
- }
- };
|