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.
 
 
 

317 lines
7.5 KiB

  1. define([
  2. 'js/system/events',
  3. 'js/system/client',
  4. 'html!ui/templates/smithing/template',
  5. 'css!ui/templates/smithing/styles',
  6. 'html!/ui/templates/smithing/templateItem',
  7. 'js/misc/statTranslations'
  8. ], function (
  9. events,
  10. client,
  11. template,
  12. styles,
  13. templateItem,
  14. statTranslations
  15. ) {
  16. return {
  17. tpl: template,
  18. centered: true,
  19. modal: true,
  20. eventCloseInv: null,
  21. hoverItem: null,
  22. item: null,
  23. action: 'augment',
  24. postRender: function () {
  25. this.onEvent('onShowSmithing', this.toggle.bind(this));
  26. this.onEvent('onKeyDown', this.onKeyDown.bind(this));
  27. this.find('.item-picker').on('click', this.openInventory.bind(this));
  28. this.find('.actionButton').on('click', this.smith.bind(this));
  29. this.onEvent('onHideInventory', this.hackMethod.bind(this));
  30. this.onEvent('beforeInventoryClickItem', this.hackMethod.bind(this));
  31. this.onEvent('onSetSmithItem', this.onHideInventory.bind(this));
  32. this.find('.col-btn').on('click', this.clickAction.bind(this));
  33. },
  34. clickAction: function (e) {
  35. let el = $(e.currentTarget);
  36. this.find('.col-btn').removeClass('selected');
  37. let action = el.attr('action');
  38. let changed = (action != this.action);
  39. this.action = action;
  40. el.addClass('selected');
  41. if ((this.item) && (changed))
  42. this.getMaterials(this.item);
  43. },
  44. smith: function () {
  45. this.setDisabled(true);
  46. client.request({
  47. cpn: 'player',
  48. method: 'performAction',
  49. data: {
  50. cpn: 'inventory',
  51. method: 'enchantItem',
  52. data: {
  53. itemId: this.item.id,
  54. action: this.action
  55. }
  56. },
  57. callback: this.onSmith.bind(this, this.item)
  58. });
  59. },
  60. onSmith: function (item, result) {
  61. this.setDisabled(false);
  62. let msg = {
  63. msg: 'Item Enhancement Succeeded',
  64. type: 'success',
  65. zIndex: 9999999,
  66. top: 100
  67. };
  68. if (this.action == 'reroll')
  69. msg.msg = 'Item Reroll Succeeded';
  70. else if (this.action == 'relevel')
  71. msg.msg = 'Item Relevel Succeeded';
  72. else if (this.action == 'reslot')
  73. msg.msg = 'Item Reslot Succeeded';
  74. result.addStatMsgs.forEach(function (a) {
  75. msg.msg += '<br /> ' + ((a.value > 0) ? '+' : '') + a.value + ' ' + statTranslations.translate(a.stat);
  76. });
  77. events.emit('onGetAnnouncement', msg);
  78. if (result.item)
  79. this.item = result.item;
  80. this.getMaterials(this.item);
  81. let augment = this.find('[action="augment"]').addClass('disabled');
  82. if ((result.item.power || 0) < 3)
  83. augment.removeClass('disabled');
  84. else
  85. this.find('[action="reroll"]').click();
  86. },
  87. //Something needs to listen to events or they'll be queued
  88. hackMethod: function () {
  89. },
  90. openInventory: function () {
  91. this.eventCloseInv = this.onEvent('onHideInventory', this.onHideInventory.bind(this));
  92. this.eventClickInv = this.onEvent('beforeInventoryClickItem', this.onHideInventory.bind(this));
  93. events.emit('onShowInventory');
  94. this.el.hide();
  95. },
  96. onHideInventory: function (msg) {
  97. if (msg)
  98. msg.success = false;
  99. if ((!msg) || (!msg.item)) {
  100. this.offEvent(this.eventCloseInv);
  101. this.offEvent(this.eventClickInv);
  102. return;
  103. } else if ((!msg.item.slot) || (msg.item.noAugment)) {
  104. var msg = {
  105. msg: 'Incorrect Item Type',
  106. type: 'failure',
  107. zIndex: 9999999,
  108. top: 180
  109. };
  110. events.emit('onGetAnnouncement', msg);
  111. return;
  112. } else if (msg.item.eq) {
  113. var msg = {
  114. msg: 'Cannot augment equipped items',
  115. type: 'failure',
  116. zIndex: 9999999,
  117. top: 180
  118. };
  119. events.emit('onGetAnnouncement', msg);
  120. return;
  121. }
  122. this.find('.selected').removeClass('selected');
  123. this.find('[action="augment"]').addClass('selected');
  124. this.action = 'augment';
  125. let augment = this.find('[action="augment"]').addClass('disabled');
  126. if ((msg.item.power || 0) < 3)
  127. augment.removeClass('disabled');
  128. let reforge = this.find('[action="reforge"]').addClass('disabled');
  129. if (msg.item.spell)
  130. reforge.removeClass('disabled');
  131. let reslot = this.find('[action="reslot"]').addClass('disabled');
  132. if (!msg.item.effects)
  133. reslot.removeClass('disabled');
  134. let relevel = this.find('[action="relevel"]').addClass('disabled');
  135. if (msg.item.slot == 'tool')
  136. relevel.removeClass('disabled');
  137. this.offEvent(this.eventClickInv);
  138. $('.uiInventory').data('ui').toggle();
  139. this.el.show();
  140. msg.success = false;
  141. if ((!msg) || (!msg.item) || (!msg.item.slot) || (msg.item.eq))
  142. return;
  143. this.item = msg.item;
  144. this.getMaterials(msg.item);
  145. },
  146. getMaterials: function (item) {
  147. this.setDisabled(true);
  148. client.request({
  149. cpn: 'player',
  150. method: 'performAction',
  151. data: {
  152. cpn: 'inventory',
  153. method: 'getEnchantMaterials',
  154. data: {
  155. itemId: item.id,
  156. action: this.action
  157. }
  158. },
  159. callback: this.onGetMaterials.bind(this, item)
  160. });
  161. },
  162. onGetMaterials: function (item, result) {
  163. this.find('.item').remove();
  164. this.drawItem(this.find('.item-picker'), item);
  165. this.find('.actionButton').removeClass('disabled').addClass('disabled');
  166. if (result.materials) {
  167. let material = result.materials[0];
  168. if (material) {
  169. let hasMaterials = window.player.inventory.items.find(function (i) {
  170. return (i.name == material.name);
  171. });
  172. if (hasMaterials) {
  173. material.quantityText = hasMaterials.quantity + '/' + material.quantity;
  174. hasMaterials = hasMaterials.quantity >= material.quantity;
  175. } else {
  176. if (!material.quantityText)
  177. material.quantityText = '';
  178. material.quantityText += '0/' + material.quantity;
  179. }
  180. if (hasMaterials)
  181. this.find('.actionButton').removeClass('disabled');
  182. this.drawItem(this.find('.material'), material, !hasMaterials);
  183. }
  184. }
  185. this.setDisabled(false);
  186. },
  187. drawItem: function (container, item, redQuantity) {
  188. container.find('.icon').hide();
  189. let imgX = -item.sprite[0] * 64;
  190. let imgY = -item.sprite[1] * 64;
  191. let spritesheet = item.spritesheet || '../../../images/items.png';
  192. if (item.material)
  193. spritesheet = '../../../images/materials.png';
  194. else if (item.quest)
  195. spritesheet = '../../../images/questItems.png';
  196. else if (item.type == 'consumable')
  197. spritesheet = '../../../images/consumables.png';
  198. let el = $(templateItem)
  199. .appendTo(container);
  200. el
  201. .data('item', item)
  202. .on('mousemove', this.onHover.bind(this, el, item))
  203. .on('mouseleave', this.hideTooltip.bind(this, el, item))
  204. .find('.icon')
  205. .css('background', 'url(' + spritesheet + ') ' + imgX + 'px ' + imgY + 'px');
  206. if (item.quantity) {
  207. let quantityText = item.quantityText;
  208. el.find('.quantity').html(quantityText);
  209. if (redQuantity)
  210. el.find('.quantity').addClass('red');
  211. }
  212. },
  213. onHover: function (el, item, e) {
  214. if (item)
  215. this.hoverItem = item;
  216. else
  217. item = this.hoverItem;
  218. let ttPos = null;
  219. if (el) {
  220. let elOffset = el.offset();
  221. ttPos = {
  222. x: ~~(e.clientX + 32),
  223. y: ~~(e.clientY)
  224. };
  225. }
  226. events.emit('onShowItemTooltip', item, ttPos);
  227. },
  228. hideTooltip: function (el, item, e) {
  229. events.emit('onHideItemTooltip', this.hoverItem);
  230. this.hoverItem = null;
  231. },
  232. beforeHide: function () {
  233. this.offEvent(this.eventCloseInv);
  234. this.offEvent(this.eventClickInv);
  235. },
  236. toggle: function () {
  237. this.shown = !this.el.is(':visible');
  238. if (this.shown) {
  239. this.find('.item').remove();
  240. this.find('.icon').show();
  241. this.find('.actionButton').removeClass('disabled').addClass('disabled');
  242. this.show();
  243. } else
  244. this.hide();
  245. },
  246. onKeyDown: function (key) {
  247. if (key == 'm')
  248. this.toggle();
  249. }
  250. };
  251. });