|
|
@@ -1,121 +1,129 @@ |
|
|
|
//System |
|
|
|
const eventEmitter = require('../misc/events'); |
|
|
|
|
|
|
|
//Helpers |
|
|
|
const fixes = require('../fixes/fixes'); |
|
|
|
const cpnInventory = require('./inventory'); |
|
|
|
const { isItemStackable } = require('./inventory/helpers'); |
|
|
|
|
|
|
|
//Config |
|
|
|
const maxItemsBase = 50; |
|
|
|
|
|
|
|
//Component |
|
|
|
module.exports = { |
|
|
|
type: 'stash', |
|
|
|
|
|
|
|
active: false, |
|
|
|
items: [], |
|
|
|
items: null, |
|
|
|
changed: false, |
|
|
|
|
|
|
|
maxItems: maxItemsBase, |
|
|
|
|
|
|
|
init: function (blueprint) { |
|
|
|
let items = blueprint.items || []; |
|
|
|
let iLen = items.length; |
|
|
|
for (let i = 0; i < iLen; i++) |
|
|
|
this.getItem(items[i]); |
|
|
|
init: function (blueprint) {}, |
|
|
|
|
|
|
|
getItemsFromDb: async function () { |
|
|
|
const { obj } = this; |
|
|
|
|
|
|
|
this.items = await io.getAsync({ |
|
|
|
key: obj.account, |
|
|
|
table: 'stash', |
|
|
|
isArray: true, |
|
|
|
clean: true |
|
|
|
}); |
|
|
|
|
|
|
|
delete blueprint.items; |
|
|
|
fixes.fixStash(this.items); |
|
|
|
|
|
|
|
this.blueprint = blueprint; |
|
|
|
await eventEmitter.emit('onAfterGetStash', { |
|
|
|
obj: obj, |
|
|
|
stash: this.items |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
getItem: function (item) { |
|
|
|
//Material? |
|
|
|
let exists = false; |
|
|
|
const { items } = this; |
|
|
|
|
|
|
|
if (isItemStackable(item)) { |
|
|
|
let existItem = this.items.find(i => i.name === item.name); |
|
|
|
const existItem = items.find(i => i.name === item.name); |
|
|
|
if (existItem) { |
|
|
|
exists = true; |
|
|
|
if (!existItem.quantity) |
|
|
|
existItem.quantity = 1; |
|
|
|
existItem.quantity += (item.quantity || 1); |
|
|
|
|
|
|
|
existItem.quantity += (+item.quantity || 1); |
|
|
|
|
|
|
|
//We modify the old object because it gets sent to the client |
|
|
|
item.id = existItem.id; |
|
|
|
item.quantity = existItem.quantity; |
|
|
|
|
|
|
|
item = existItem; |
|
|
|
|
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//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; |
|
|
|
} |
|
|
|
let id = 0; |
|
|
|
items.forEach(i => { |
|
|
|
if (i.id >= id) |
|
|
|
id = i.id + 1; |
|
|
|
}); |
|
|
|
item.id = id; |
|
|
|
|
|
|
|
if (!exists) |
|
|
|
this.items.push(item); |
|
|
|
items.push(item); |
|
|
|
}, |
|
|
|
|
|
|
|
deposit: function (item) { |
|
|
|
if (!this.active) |
|
|
|
const { active, items, maxItems, obj } = this; |
|
|
|
|
|
|
|
if (!active) |
|
|
|
return; |
|
|
|
else if (this.items.length >= this.maxItems) { |
|
|
|
let isStackable = this.items.some(stashedItem => item.name === stashedItem.name && (isItemStackable(stashedItem))); |
|
|
|
|
|
|
|
else if (items.length >= maxItems) { |
|
|
|
const isStackable = items.some(stashedItem => item.name === stashedItem.name && isItemStackable(stashedItem)); |
|
|
|
if (!isStackable) { |
|
|
|
const message = 'You do not have room in your stash to deposit that item'; |
|
|
|
this.obj.social.notifySelf({ message }); |
|
|
|
obj.social.notifySelf({ message }); |
|
|
|
|
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
this.getItem(item); |
|
|
|
|
|
|
|
this.obj.syncer.setArray(true, 'stash', 'getItems', item); |
|
|
|
const sendItem = cpnInventory.simplifyItem.call({ obj: {} }, item); |
|
|
|
|
|
|
|
obj.instance.syncer.queue('onAddStashItems', [sendItem], [obj.serverId]); |
|
|
|
|
|
|
|
this.changed = true; |
|
|
|
|
|
|
|
return 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) |
|
|
|
const { active, items, obj } = this; |
|
|
|
|
|
|
|
if (!active) |
|
|
|
return; |
|
|
|
|
|
|
|
let item = this.items.find(i => i.id === id); |
|
|
|
let item = items.find(i => i.id === id); |
|
|
|
if (!item) |
|
|
|
return; |
|
|
|
else if (!this.obj.inventory.hasSpace(item)) { |
|
|
|
else if (!obj.inventory.hasSpace(item)) { |
|
|
|
const message = 'You do not have room in your inventory to withdraw that item'; |
|
|
|
this.obj.social.notifySelf({ message }); |
|
|
|
obj.social.notifySelf({ message }); |
|
|
|
|
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
this.obj.inventory.getItem(item); |
|
|
|
this.items.spliceWhere(i => i === item); |
|
|
|
obj.inventory.getItem(item); |
|
|
|
items.spliceWhere(i => i === item); |
|
|
|
|
|
|
|
this.obj.syncer.setArray(true, 'stash', 'destroyItems', id); |
|
|
|
obj.instance.syncer.queue('onRemoveStashItems', [id], [obj.serverId]); |
|
|
|
|
|
|
|
this.changed = true; |
|
|
|
}, |
|
|
|
|
|
|
|
setActive: function (active) { |
|
|
|
let obj = this.obj; |
|
|
|
const { obj } = this; |
|
|
|
|
|
|
|
this.active = active; |
|
|
|
obj.syncer.set(true, 'stash', 'active', this.active); |
|
|
@@ -131,33 +139,49 @@ module.exports = { |
|
|
|
}); |
|
|
|
|
|
|
|
let msg = 'Press U to access your Shared Stash'; |
|
|
|
this.obj.instance.syncer.queue('onGetAnnouncement', { |
|
|
|
src: this.obj.id, |
|
|
|
obj.instance.syncer.queue('onGetAnnouncement', { |
|
|
|
src: obj.id, |
|
|
|
msg: msg |
|
|
|
}, [obj.serverId]); |
|
|
|
|
|
|
|
if (this.active && this.items.length > this.maxItems) { |
|
|
|
const message = `You have more than ${this.maxItems} items in your stash. In the future, these items will be lost.`; |
|
|
|
obj.social.notifySelf({ message }); |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
open: function () { |
|
|
|
const { active, obj } = this; |
|
|
|
open: async function () { |
|
|
|
if (!this.items) |
|
|
|
await this.getItemsFromDb(); |
|
|
|
|
|
|
|
const { obj, active, maxItems, items } = this; |
|
|
|
|
|
|
|
if (!active) |
|
|
|
return; |
|
|
|
|
|
|
|
const sendItems = items.map(i => cpnInventory.simplifyItem.call({ obj: {} }, i)); |
|
|
|
|
|
|
|
const msg = { |
|
|
|
maxItems, |
|
|
|
items: sendItems |
|
|
|
}; |
|
|
|
|
|
|
|
obj.instance.syncer.queue('onOpenStash', msg, [obj.serverId]); |
|
|
|
|
|
|
|
if (active) |
|
|
|
obj.instance.syncer.queue('onOpenStash', {}, [obj.serverId]); |
|
|
|
if (items.length > maxItems) { |
|
|
|
const message = `You have more than ${maxItems} items in your stash. In the future, these items will be lost.`; |
|
|
|
obj.social.notifySelf({ message }); |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
simplify: function (self) { |
|
|
|
if (!self) |
|
|
|
return null; |
|
|
|
|
|
|
|
return { type: 'stash' }; |
|
|
|
}, |
|
|
|
|
|
|
|
simplifyTransfer: function () { |
|
|
|
const { type, items } = this; |
|
|
|
|
|
|
|
return { |
|
|
|
type: 'stash', |
|
|
|
active: this.active, |
|
|
|
items: this.items, |
|
|
|
maxItems: this.maxItems |
|
|
|
type, |
|
|
|
items |
|
|
|
}; |
|
|
|
}, |
|
|
|
|
|
|
|