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.
 
 
 

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