Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

299 rindas
6.6 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. var el = $(e.currentTarget);
  36. this.find('.col-btn').removeClass('selected');
  37. var action = el.attr('action');
  38. var 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. var msg = {
  63. msg: 'Item Enhancement Succeeded',
  64. type: 'success',
  65. zIndex: 9999999,
  66. top: 100
  67. };
  68. if (this.action == 'scour')
  69. msg.msg = 'Item Scouring Succeeded';
  70. if (!result.success) {
  71. msg.msg = 'Item Enhancement Failed';
  72. msg.type = 'failure';
  73. }
  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. },
  82. //Something needs to listen to events or they'll be queued
  83. hackMethod: function() {
  84. },
  85. openInventory: function() {
  86. this.eventCloseInv = this.onEvent('onHideInventory', this.onHideInventory.bind(this));
  87. this.eventClickInv = this.onEvent('beforeInventoryClickItem', this.onHideInventory.bind(this));
  88. events.emit('onShowInventory');
  89. this.el.hide();
  90. },
  91. onHideInventory: function(msg) {
  92. if (msg)
  93. msg.success = false;
  94. if ((!msg) || (!msg.item)) {
  95. this.offEvent(this.eventCloseInv);
  96. this.offEvent(this.eventClickInv);
  97. return;
  98. }
  99. else if (!msg.item.slot) {
  100. var msg = {
  101. msg: 'Incorrect Item Type',
  102. type: 'failure',
  103. zIndex: 9999999,
  104. top: 180
  105. };
  106. events.emit('onGetAnnouncement', msg);
  107. return;
  108. }
  109. else if (msg.item.eq) {
  110. var msg = {
  111. msg: 'Cannot augment equipped items',
  112. type: 'failure',
  113. zIndex: 9999999,
  114. top: 180
  115. };
  116. events.emit('onGetAnnouncement', msg);
  117. return;
  118. }
  119. this.offEvent(this.eventClickInv);
  120. $('.uiInventory').data('ui').toggle();
  121. this.el.show();
  122. events.emit('onShowOverlay', this.el);
  123. msg.success = false;
  124. if ((!msg) || (!msg.item) || (!msg.item.slot) || (msg.item.eq))
  125. return;
  126. this.item = msg.item;
  127. this.getMaterials(msg.item);
  128. },
  129. getMaterials: function(item) {
  130. this.setDisabled(true);
  131. client.request({
  132. cpn: 'player',
  133. method: 'performAction',
  134. data: {
  135. cpn: 'inventory',
  136. method: 'getEnchantMaterials',
  137. data: {
  138. itemId: item.id,
  139. action: this.action
  140. }
  141. },
  142. callback: this.onGetMaterials.bind(this, item)
  143. });
  144. },
  145. onGetMaterials: function(item, result) {
  146. this.find('.item').remove();
  147. this.drawItem(this.find('.item-picker'), item);
  148. this.find('.actionButton').removeClass('disabled').addClass('disabled');
  149. this.find('.chance').html('');
  150. var material = result.materials[0];
  151. if (material) {
  152. var hasMaterials = window.player.inventory.items.find(function(i) {
  153. return (i.name == material.name);
  154. });
  155. if (hasMaterials) {
  156. material.quantityText = hasMaterials.quantity + '/' + material.quantity;
  157. hasMaterials = hasMaterials.quantity >= material.quantity;
  158. }
  159. else {
  160. if (!material.quantityText)
  161. material.quantityText = '';
  162. material.quantityText += '0/' + material.quantity;
  163. }
  164. if (hasMaterials)
  165. this.find('.actionButton').removeClass('disabled');
  166. this.find('.chance').html(result.successChance + '%');
  167. this.drawItem(this.find('.material'), material, !hasMaterials);
  168. }
  169. this.setDisabled(false);
  170. },
  171. drawItem: function(container, item, redQuantity) {
  172. container.find('.icon').hide();
  173. var imgX = -item.sprite[0] * 64;
  174. var imgY = -item.sprite[1] * 64;
  175. var spritesheet = item.spritesheet || 'items';
  176. if (item.material)
  177. spritesheet = 'materials';
  178. else if (item.quest)
  179. spritesheet = 'questItems';
  180. var el = $(templateItem)
  181. .appendTo(container);
  182. el
  183. .data('item', item)
  184. .on('mousemove', this.onHover.bind(this, el, item))
  185. .on('mouseleave', this.hideTooltip.bind(this, el, item))
  186. .find('.icon')
  187. .css('background', 'url(../../../images/' + spritesheet + '.png) ' + imgX + 'px ' + imgY + 'px');
  188. if (item.quantity) {
  189. var quantityText = item.quantityText;
  190. el.find('.quantity').html(quantityText);
  191. if (redQuantity)
  192. el.find('.quantity').addClass('red');
  193. }
  194. },
  195. onHover: function(el, item, e) {
  196. if (item)
  197. this.hoverItem = item;
  198. else
  199. item = this.hoverItem;
  200. var ttPos = null;
  201. if (el) {
  202. var elOffset = el.offset();
  203. ttPos = {
  204. x: ~~(e.clientX + 32),
  205. y: ~~(e.clientY)
  206. };
  207. }
  208. events.emit('onShowItemTooltip', item, ttPos);
  209. },
  210. hideTooltip: function(el, item, e) {
  211. events.emit('onHideItemTooltip', this.hoverItem);
  212. this.hoverItem = null;
  213. },
  214. beforeHide: function() {
  215. this.offEvent(this.eventCloseInv);
  216. this.offEvent(this.eventClickInv);
  217. },
  218. toggle: function() {
  219. this.shown = !this.el.is(':visible');
  220. if (this.shown) {
  221. this.find('.item').remove();
  222. this.find('.icon').show();
  223. this.find('.actionButton').removeClass('disabled').addClass('disabled');
  224. this.find('.chance').html('');
  225. this.show();
  226. //this.build();
  227. events.emit('onShowOverlay', this.el);
  228. } else {
  229. this.hide();
  230. events.emit('onHideOverlay', this.el);
  231. }
  232. },
  233. onKeyDown: function(key) {
  234. if (key == 'm')
  235. this.toggle();
  236. }
  237. };
  238. });