25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

168 lines
3.6 KiB

  1. const cpnInventory = require('./inventory');
  2. const { isItemStackable } = require('./inventory/helpers');
  3. const maxItemsBase = 50;
  4. module.exports = {
  5. type: 'stash',
  6. active: false,
  7. items: [],
  8. changed: false,
  9. maxItems: maxItemsBase,
  10. init: function (blueprint) {
  11. let items = blueprint.items || [];
  12. let iLen = items.length;
  13. for (let i = 0; i < iLen; i++)
  14. this.getItem(items[i]);
  15. delete blueprint.items;
  16. this.blueprint = blueprint;
  17. },
  18. getItem: function (item) {
  19. //Material?
  20. let exists = false;
  21. if (isItemStackable(item)) {
  22. let existItem = this.items.find(i => i.name === item.name);
  23. if (existItem) {
  24. exists = true;
  25. if (!existItem.quantity)
  26. existItem.quantity = 1;
  27. existItem.quantity += (item.quantity || 1);
  28. //We modify the old object because it gets sent to the client
  29. item.id = existItem.id;
  30. item.quantity = existItem.quantity;
  31. item = existItem;
  32. }
  33. }
  34. //Get next id
  35. if (!exists) {
  36. let id = 0;
  37. let items = this.items;
  38. let iLen = items.length;
  39. for (let i = 0; i < iLen; i++) {
  40. let fItem = items[i];
  41. if (fItem.id >= id)
  42. id = fItem.id + 1;
  43. }
  44. item.id = id;
  45. }
  46. if (!exists)
  47. this.items.push(item);
  48. },
  49. deposit: function (item) {
  50. if (!this.active)
  51. return;
  52. else if (this.items.length >= this.maxItems) {
  53. let isStackable = this.items.some(stashedItem => item.name === stashedItem.name && (isItemStackable(stashedItem)));
  54. if (!isStackable) {
  55. const message = 'You do not have room in your stash to deposit that item';
  56. this.obj.social.notifySelf({ message });
  57. return;
  58. }
  59. }
  60. this.getItem(item);
  61. this.obj.syncer.setArray(true, 'stash', 'getItems', item);
  62. this.changed = true;
  63. return true;
  64. },
  65. destroyItem: function (id) {
  66. let item = this.items.find(i => i.id === id);
  67. if (!item)
  68. return;
  69. this.items.spliceWhere(i => i === item);
  70. this.obj.syncer.setArray(true, 'stash', 'destroyItems', id);
  71. this.changed = true;
  72. },
  73. withdraw: function (id) {
  74. if (!this.active)
  75. return;
  76. let item = this.items.find(i => i.id === id);
  77. if (!item)
  78. return;
  79. else if (!this.obj.inventory.hasSpace(item)) {
  80. const message = 'You do not have room in your inventory to withdraw that item';
  81. this.obj.social.notifySelf({ message });
  82. return;
  83. }
  84. this.obj.inventory.getItem(item);
  85. this.items.spliceWhere(i => i === item);
  86. this.obj.syncer.setArray(true, 'stash', 'destroyItems', id);
  87. this.changed = true;
  88. },
  89. setActive: function (active) {
  90. let obj = this.obj;
  91. this.active = active;
  92. obj.syncer.set(true, 'stash', 'active', this.active);
  93. const actionType = active ? 'addActions' : 'removeActions';
  94. obj.syncer.setArray(true, 'serverActions', actionType, {
  95. key: 'u',
  96. action: {
  97. targetId: obj.id,
  98. cpn: 'stash',
  99. method: 'open'
  100. }
  101. });
  102. let msg = 'Press U to access your Shared Stash';
  103. this.obj.instance.syncer.queue('onGetAnnouncement', {
  104. src: this.obj.id,
  105. msg: msg
  106. }, [obj.serverId]);
  107. if (this.active && this.items.length > this.maxItems) {
  108. const message = `You have more than ${this.maxItems} items in your stash. In the future, these items will be lost.`;
  109. obj.social.notifySelf({ message });
  110. }
  111. },
  112. open: function () {
  113. const { active, obj } = this;
  114. if (active)
  115. obj.instance.syncer.queue('onOpenStash', {}, [obj.serverId]);
  116. },
  117. simplify: function (self) {
  118. if (!self)
  119. return null;
  120. return {
  121. type: 'stash',
  122. active: this.active,
  123. items: this.items,
  124. maxItems: this.maxItems
  125. };
  126. },
  127. serialize: function () {
  128. return this.items.map(i => cpnInventory.simplifyItem.call({ obj: {} }, i));
  129. }
  130. };