You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

110 lines
2.0 KiB

  1. module.exports = {
  2. type: 'stash',
  3. active: false,
  4. items: [],
  5. changed: false,
  6. init: function (blueprint) {
  7. let items = blueprint.items || [];
  8. let iLen = items.length;
  9. for (let i = 0; i < iLen; i++)
  10. this.getItem(items[i]);
  11. delete blueprint.items;
  12. this.blueprint = blueprint;
  13. },
  14. getItem: function (item) {
  15. //Material?
  16. let exists = false;
  17. if (((item.material) || (item.quest) || (item.quantity)) && (!item.noStack) && (!item.uses)) {
  18. let existItem = this.items.find(i => i.name === item.name);
  19. if (existItem) {
  20. exists = true;
  21. if (!existItem.quantity)
  22. existItem.quantity = 1;
  23. existItem.quantity += item.quantity;
  24. //We modify the old object because it gets sent to the client
  25. item.id = existItem.id;
  26. item.quantity = existItem.quantity;
  27. item = existItem;
  28. }
  29. }
  30. //Get next id
  31. if (!exists) {
  32. let id = 0;
  33. let items = this.items;
  34. let iLen = items.length;
  35. for (let i = 0; i < iLen; i++) {
  36. let fItem = items[i];
  37. if (fItem.id >= id)
  38. id = fItem.id + 1;
  39. }
  40. item.id = id;
  41. }
  42. if (!exists)
  43. this.items.push(item);
  44. },
  45. deposit: function (item) {
  46. if (!this.active)
  47. return;
  48. this.getItem(item);
  49. this.obj.syncer.setArray(true, 'stash', 'getItems', item);
  50. this.changed = true;
  51. },
  52. destroyItem: function (id) {
  53. let item = this.items.find(i => i.id === id);
  54. if (!item)
  55. return;
  56. this.items.spliceWhere(i => i === item);
  57. this.obj.syncer.setArray(true, 'stash', 'destroyItems', id);
  58. this.changed = true;
  59. },
  60. withdraw: function (id) {
  61. if (!this.active)
  62. return;
  63. let item = this.items.find(i => i.id === id);
  64. if (!item)
  65. return;
  66. this.obj.inventory.getItem(item);
  67. this.items.spliceWhere(i => i === item);
  68. this.obj.syncer.setArray(true, 'stash', 'destroyItems', id);
  69. this.changed = true;
  70. },
  71. setActive: function (active) {
  72. this.active = active;
  73. this.obj.syncer.set(true, 'stash', 'active', this.active);
  74. },
  75. simplify: function (self) {
  76. if (!self)
  77. return null;
  78. return {
  79. type: 'stash',
  80. active: this.active,
  81. items: this.items
  82. };
  83. }
  84. };