Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

198 lignes
3.8 KiB

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