Browse Source

feat #1885: Refactored stash and stash events

tags/v0.10.6^2
Shaun 2 years ago
parent
commit
15489e24ae
5 changed files with 140 additions and 136 deletions
  1. +47
    -22
      src/client/ui/templates/stash/stash.js
  2. +2
    -44
      src/server/clientComponents/stash.js
  3. +0
    -1
      src/server/components/auth.js
  4. +1
    -3
      src/server/components/player.js
  5. +90
    -66
      src/server/components/stash.js

+ 47
- 22
src/client/ui/templates/stash/stash.js View File

@@ -22,33 +22,38 @@ define([
hoverItem: null,

items: [],
maxItems: null,

modal: true,
hasClose: true,

postRender: function () {
this.onEvent('onGetStashItems', this.onGetStashItems.bind(this));
this.onEvent('onDestroyStashItems', this.onDestroyStashItems.bind(this));
this.onEvent('onKeyDown', this.onKeyDown.bind(this));
this.onEvent('onKeyUp', this.onKeyUp.bind(this));
this.onEvent('onOpenStash', this.toggle.bind(this));
[
'onKeyUp',
'onKeyDown',
'onOpenStash',
'onAddStashItems',
'onRemoveStashItems'
]
.forEach(e => {
this.onEvent(e, this[e].bind(this));
});
},

build: function () {
this.el.removeClass('scrolls');
if (window.player.stash.maxItems > 50)
this.el.addClass('scrolls');
const { el, maxItems, items } = this;

let container = this.el.find('.grid')
.empty();
el.removeClass('scrolls');
if (maxItems > 50)
el.addClass('scrolls');

let items = this.items;
let iLen = Math.max(items.length, window.player.stash.maxItems);
const container = this.el.find('.grid').empty();

for (let i = 0; i < iLen; i++) {
let item = items[i];
const renderItemCount = Math.max(items.length, maxItems);

let itemEl = renderItem(container, item);
for (let i = 0; i < renderItemCount; i++) {
const item = items[i];
const itemEl = renderItem(container, item);

if (!item)
continue;
@@ -126,14 +131,30 @@ define([
this.build();
},

onDestroyStashItems: function (itemIds) {
itemIds.forEach(function (id) {
let item = this.items.find(i => i.id === id);
onAddStashItems: function (addItems) {
const { items } = this;

addItems.forEach(newItem => {
const existIndex = items.findIndex(i => i.id === newItem.id);
if (existIndex !== -1)
items.splice(existIndex, 1, newItem);
else
items.push(newItem);
});
},

onRemoveStashItems: function (removeItemIds) {
const { items } = this;

console.log(removeItemIds);

removeItemIds.forEach(id => {
const item = items.find(i => i.id === id);
if (item === this.hoverItem)
this.hideTooltip();

this.items.spliceWhere(i => i.id === id);
}, this);
items.spliceWhere(i => i.id === id);
});

if (this.shown)
this.build();
@@ -155,8 +176,12 @@ define([
events.emit('onHideContextMenu');
},

onOpenStash: function () {
this.build();
onOpenStash: function ({ items, maxItems }) {
this.maxItems = maxItems;

this.show();

this.onGetStashItems(items);
},

beforeDestroy: function () {


+ 2
- 44
src/server/clientComponents/stash.js View File

@@ -1,49 +1,7 @@
define([
'js/system/events'
], function (
events
) {
define([], function () {
return {
type: 'stash',

active: false,

items: null,

init: function () {
events.emit('onGetStashItems', this.items);
},

extend: function (blueprint) {
if (blueprint.has('active'))
this.active = blueprint.active;

if (blueprint.getItems) {
let items = this.items;
let newItems = blueprint.getItems || [];
let nLen = newItems.length;

for (let i = 0; i < nLen; i++) {
let nItem = newItems[i];
let nId = nItem.id;

let findItem = items.find(f => f.id === nId);
if (findItem) {
$.extend(true, findItem, nItem);

newItems.splice(i, 1);
i--;
nLen--;
}
}

this.items.push.apply(this.items, blueprint.getItems || []);

events.emit('onGetStashItems', this.items);
}

if (blueprint.destroyItems)
events.emit('onDestroyStashItems', blueprint.destroyItems);
}
init: function () {}
};
});

+ 0
- 1
src/server/components/auth.js View File

@@ -177,7 +177,6 @@ module.exports = {
this.characters[charName] = character;

await this.getCustomChannels(character);
await this.getStash();

await this.verifySkin(character);



+ 1
- 3
src/server/components/player.js View File

@@ -70,9 +70,7 @@ module.exports = {
faction: 'players'
});
obj.addComponent('gatherer');
obj.addComponent('stash', {
items: character.stash
});
obj.addComponent('stash');

let blueprintEffects = character.components.find(c => c.type === 'effects') || {};
if (blueprintEffects.effects) {


+ 90
- 66
src/server/components/stash.js View File

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



Loading…
Cancel
Save