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.
 
 
 

305 lines
7.0 KiB

  1. const { applyItemStats } = require('./equipment/helpers');
  2. module.exports = {
  3. type: 'equipment',
  4. eq: {},
  5. quickSlots: {},
  6. init: function (blueprint) {
  7. },
  8. transfer: function () {
  9. if (this.eqTransfer) {
  10. this.eq = this.eqTransfer;
  11. delete this.eqTransfer;
  12. }
  13. },
  14. simplify: function (self) {
  15. return {
  16. type: 'equipment',
  17. eq: {},
  18. quickSlots: this.quickSlots,
  19. eqTransfer: this.eq
  20. };
  21. },
  22. isSlotEmpty: function (slot) {
  23. return !this.eq.has(slot);
  24. },
  25. equip: function (itemId) {
  26. let slot = null;
  27. if (typeof (itemId) === 'object') {
  28. slot = itemId.slot;
  29. itemId = itemId.itemId;
  30. }
  31. let obj = this.obj;
  32. let inventory = obj.inventory;
  33. let item = inventory.findItem(itemId);
  34. if (!item || item.eq)
  35. return;
  36. else if ((!item.slot) || (item.material) || (item.quest) || (item.ability) || (!inventory.canEquipItem(item))) {
  37. item.eq = false;
  38. return;
  39. }
  40. if (!slot)
  41. slot = item.equipSlot || item.slot;
  42. if (slot === 'twoHanded') {
  43. if (this.eq.has('offHand'))
  44. this.unequip({ itemId: this.eq.offHand }, true);
  45. slot = 'oneHanded';
  46. } else if (slot === 'offHand') {
  47. if (this.eq.has('oneHanded')) {
  48. let oneHandedEq = inventory.findItem(this.eq.oneHanded);
  49. if (oneHandedEq.slot === 'twoHanded')
  50. this.unequip({ itemId: this.eq.oneHanded }, true);
  51. }
  52. }
  53. let equipMsg = {
  54. success: true,
  55. item: item
  56. };
  57. obj.fireEvent('beforeEquipItem', equipMsg);
  58. if (!equipMsg.success) {
  59. const message = equipMsg.msg || 'you cannot equip that item';
  60. obj.social.notifySelf({ message });
  61. return;
  62. }
  63. delete item.pos;
  64. if (slot === 'finger') {
  65. let f1 = (this.eq.has('finger-1'));
  66. let f2 = (this.eq.has('finger-2'));
  67. if ((f1) && (f2))
  68. slot = 'finger-1';
  69. else if (!f1)
  70. slot = 'finger-1';
  71. else if (!f2)
  72. slot = 'finger-2';
  73. }
  74. if (this.eq.has(slot)) {
  75. if (this.eq[slot] === item.id)
  76. return;
  77. this.unequip({ itemId: this.eq[slot] }, true);
  78. }
  79. applyItemStats(obj, item, true);
  80. item.eq = true;
  81. this.eq[slot] = itemId;
  82. item.equipSlot = slot;
  83. if (obj.spellbook)
  84. obj.spellbook.calcDps();
  85. if ((!obj.mob) || (item.ability)) {
  86. if (item.spell)
  87. inventory.learnAbility({ itemId, slot: item.runeSlot, bypassEqCheck: true });
  88. else
  89. obj.syncer.setArray(true, 'inventory', 'getItems', inventory.simplifyItem(item));
  90. }
  91. obj.fireEvent('afterEquipItem', item);
  92. },
  93. unequip: function (itemId, ignoreSpaceCheck) {
  94. let item = itemId;
  95. if (typeof (itemId) === 'object')
  96. itemId = itemId.itemId;
  97. let obj = this.obj;
  98. let inventory = obj.inventory;
  99. if (typeof(item) !== 'object' || !item.has('id'))
  100. item = inventory.findItem(itemId);
  101. if (!item || !item.eq)
  102. return;
  103. else if (!ignoreSpaceCheck && !inventory.hasSpace()) {
  104. const message = 'You do not have room in your inventory to unequip that item';
  105. obj.social.notifySelf({ message });
  106. return;
  107. }
  108. delete item.eq;
  109. delete this.eq[item.equipSlot];
  110. delete item.equipSlot;
  111. applyItemStats(obj, item, false);
  112. inventory.setItemPosition(itemId);
  113. if (item.spell) {
  114. item.eq = true;
  115. inventory.unlearnAbility({ itemId });
  116. } else
  117. obj.syncer.setArray(true, 'inventory', 'getItems', inventory.simplifyItem(item));
  118. obj.spellbook.calcDps();
  119. obj.fireEvent('afterUnequipItem', item);
  120. this.unequipAttrRqrGear();
  121. },
  122. unequipAll: function () {
  123. let eq = this.eq;
  124. Object.keys(this.eq).forEach(function (slot) {
  125. this.unequip({ itemId: eq[slot] });
  126. }, this);
  127. },
  128. setQuickSlot: function (msg) {
  129. let obj = this.obj;
  130. const inventory = obj.inventory;
  131. if (!msg.has('itemId') && this.quickSlots.has(msg.slot)) {
  132. let currentQuickItem = inventory.findItem(this.quickSlots[msg.slot]);
  133. if (!currentQuickItem)
  134. return;
  135. delete this.quickSlots[msg.slot];
  136. delete currentQuickItem.quickSlot;
  137. obj.syncer.setArray(true, 'inventory', 'getItems', currentQuickItem);
  138. return;
  139. }
  140. let item = inventory.findItem(msg.itemId);
  141. if (!item)
  142. return;
  143. let currentQuickId = this.quickSlots[msg.slot];
  144. if (currentQuickId) {
  145. let currentQuickItem = inventory.findItem(currentQuickId);
  146. if (currentQuickItem) {
  147. delete currentQuickItem.quickSlot;
  148. obj.syncer.setArray(true, 'inventory', 'getItems', currentQuickItem);
  149. }
  150. }
  151. this.quickSlots[msg.slot] = msg.itemId;
  152. item.quickSlot = msg.slot;
  153. obj.syncer.setArray(true, 'inventory', 'getItems', item);
  154. },
  155. useQuickSlot: function (msg) {
  156. if (!this.quickSlots.has(msg.slot))
  157. return;
  158. const inventory = this.obj.inventory;
  159. //If the item has been used up, find another one with the same name
  160. const item = inventory.findItem(this.quickSlots[0]);
  161. if (!item)
  162. return;
  163. inventory.useItem({ itemId: this.quickSlots[0] });
  164. if (item.uses <= 0 && !item.quantity)
  165. this.replaceQuickSlot(item);
  166. },
  167. replaceQuickSlot: function (item) {
  168. const newItem = this.obj.inventory.items.find(f => f.name === item.name);
  169. if (newItem) {
  170. newItem.quickSlot = 0;
  171. this.quickSlots[0] = newItem.id;
  172. this.obj.syncer.setArray(true, 'inventory', 'getItems', newItem);
  173. } else
  174. delete this.quickSlots[0];
  175. },
  176. unequipAttrRqrGear: function () {
  177. let inventory = this.obj.inventory;
  178. let eq = this.eq;
  179. Object.keys(this.eq).forEach(function (slot) {
  180. let itemId = eq[slot];
  181. let item = inventory.findItem(itemId);
  182. if (!item)
  183. return;
  184. let errors = inventory.equipItemErrors(item);
  185. if (errors.length > 0) {
  186. this.unequip({ itemId: itemId });
  187. let message = ({
  188. int: `You suddenly feel too stupid to wear your ${item.name}`,
  189. str: `Your weak body can no longer equip your ${item.name}`,
  190. dex: `Your sluggish physique cannot possibly equip your ${item.name}`,
  191. level: `Your level is too low to equip your ${item.name}`
  192. })[errors[0]];
  193. this.obj.social.notifySelf({
  194. message,
  195. type: 'rep'
  196. });
  197. }
  198. }, this);
  199. },
  200. unequipFactionGear: function (factionId, tier) {
  201. let inventory = this.obj.inventory;
  202. let eq = this.eq;
  203. Object.keys(this.eq).forEach(function (slot) {
  204. let itemId = eq[slot];
  205. let item = inventory.findItem(itemId);
  206. let factions = item.factions;
  207. if (!factions)
  208. return;
  209. let findFaction = factions.find(f => f.id === factionId);
  210. if (!findFaction)
  211. return;
  212. if (findFaction.tier > tier) {
  213. this.unequip({ itemId });
  214. const message = `You unequip your ${item.name} as it zaps you.`;
  215. this.obj.social.notifySelf({
  216. message,
  217. type: 'rep'
  218. });
  219. }
  220. }, this);
  221. },
  222. inspect: function (msg) {
  223. const targetPlayer = this.obj.instance.objects.find(o => o.id === msg.playerId);
  224. if (!targetPlayer || !targetPlayer.player)
  225. return;
  226. const targetEq = targetPlayer.inventory.items.filter(eq => eq.eq === true || eq.quickSlot === 0);
  227. const targetStats = targetPlayer.stats.values;
  228. const mappedEq = targetEq.map(m => targetPlayer.inventory.simplifyItem(m));
  229. const mappedStats = extend({}, targetStats);
  230. let result = {
  231. equipment: mappedEq,
  232. stats: mappedStats
  233. };
  234. this.obj.instance.syncer.queue('onInspectTarget', result, [this.obj.serverId]);
  235. }
  236. };