Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

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