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.
 
 
 

323 lines
7.3 KiB

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