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.
 
 
 

191 lines
4.6 KiB

  1. define([
  2. 'js/system/events',
  3. 'js/system/client',
  4. 'html!ui/templates/trade/template',
  5. 'css!ui/templates/trade/styles',
  6. 'html!ui/templates/inventory/templateItem'
  7. ], function (
  8. events,
  9. client,
  10. template,
  11. styles,
  12. tplItem
  13. ) {
  14. return {
  15. tpl: template,
  16. centered: true,
  17. modal: true,
  18. list: null,
  19. action: null,
  20. postRender: function () {
  21. this.onEvent('onGetTradeList', this.onGetTradeList.bind(this));
  22. this.onEvent('onCloseTrade', this.hide.bind(this));
  23. },
  24. onGetTradeList: function (itemList, action) {
  25. itemList = itemList || this.itemList;
  26. action = action || this.action;
  27. this.itemList = itemList;
  28. this.action = action;
  29. this.find('.heading-text').html(action);
  30. let uiInventory = $('.uiInventory').data('ui');
  31. let container = this.el.find('.grid')
  32. .empty();
  33. let buyItems = itemList.items;
  34. buyItems.forEach(function (item) {
  35. if ((item == this.hoverItem))
  36. this.onHover(null, item);
  37. }, this);
  38. let iLen = Math.max(buyItems.length, 50);
  39. for (var i = 0; i < iLen; i++) {
  40. var item = buyItems[i];
  41. if (action == 'sell') {
  42. item = buyItems.find(function (b) {
  43. return (b.pos == i);
  44. });
  45. }
  46. if (!item) {
  47. $(tplItem).appendTo(container);
  48. continue;
  49. }
  50. item = $.extend(true, {}, item);
  51. let size = 64;
  52. let offset = 0;
  53. let itemEl = $(tplItem)
  54. .appendTo(container);
  55. let spritesheet = item.spritesheet || '../../../images/items.png';
  56. if (item.material)
  57. spritesheet = '../../../images/materials.png';
  58. else if (item.quest)
  59. spritesheet = '../../../images/questItems.png';
  60. else if (item.type == 'consumable')
  61. spritesheet = '../../../images/consumables.png';
  62. else if (item.type == 'skin') {
  63. offset = 4;
  64. size = 8;
  65. if (!item.spritesheet)
  66. spritesheet = '../../../images/characters.png';
  67. }
  68. let imgX = (-item.sprite[0] * size) + offset;
  69. let imgY = (-item.sprite[1] * size) + offset;
  70. itemEl
  71. .data('item', item)
  72. .on('click', this.onClick.bind(this, itemEl, item, action))
  73. .on('mousemove', this.onHover.bind(this, itemEl, item, action))
  74. .on('mouseleave', uiInventory.hideTooltip.bind(uiInventory, itemEl, item))
  75. .find('.icon')
  76. .css('background', 'url(' + spritesheet + ') ' + imgX + 'px ' + imgY + 'px')
  77. .addClass(item.type);
  78. if (item.quantity)
  79. itemEl.find('.quantity').html(item.quantity);
  80. else if (item.eq)
  81. itemEl.find('.quantity').html('EQ');
  82. if (action == 'buy') {
  83. let noAfford = false;
  84. if (item.worth.currency) {
  85. let currencyItems = window.player.inventory.items.find(function (i) {
  86. return (i.name == item.worth.currency);
  87. });
  88. noAfford = ((!currencyItems) || (currencyItems.quantity < item.worth.amount));
  89. } else
  90. noAfford = (item.worth * this.itemList.markup > window.player.trade.gold);
  91. if ((!noAfford) && (item.factions)) {
  92. noAfford = item.factions.some(function (f) {
  93. return f.noEquip;
  94. });
  95. }
  96. if (noAfford)
  97. $('<div class="no-afford"></div>').appendTo(itemEl);
  98. }
  99. if (item.worth.currency)
  100. item.worthText = item.worth.amount + 'x ' + item.worth.currency;
  101. else
  102. item.worthText = ~~(itemList.markup * item.worth);
  103. if (item.eq)
  104. itemEl.addClass('eq');
  105. else if (item.isNew) {
  106. itemEl.addClass('new');
  107. itemEl.find('.quantity').html('NEW');
  108. }
  109. }
  110. this.center();
  111. this.show();
  112. events.emit('onShowOverlay', this.el);
  113. },
  114. onClick: function (el, item, action, e) {
  115. el.addClass('disabled');
  116. client.request({
  117. cpn: 'player',
  118. method: 'performAction',
  119. data: {
  120. cpn: 'trade',
  121. method: 'buySell',
  122. data: {
  123. itemId: item.id,
  124. action: action
  125. }
  126. },
  127. callback: this.onServerRespond.bind(this, el)
  128. });
  129. let uiInventory = $('.uiInventory').data('ui');
  130. uiInventory.hideTooltip(el, item, e);
  131. },
  132. onHover: function (el, item, action, e) {
  133. let uiInventory = $('.uiInventory').data('ui');
  134. uiInventory.onHover(el, item, e);
  135. let canAfford = true;
  136. if (action == 'buy') {
  137. if (item.worth.currency) {
  138. let currencyItems = window.player.inventory.items.find(function (i) {
  139. return (i.name == item.worth.currency);
  140. });
  141. canAfford = ((currencyItems) && (currencyItems.quantity >= item.worth.amount));
  142. } else
  143. canAfford = (item.worth * this.itemList.markup <= window.player.trade.gold);
  144. }
  145. let uiTooltipItem = $('.uiTooltipItem').data('ui');
  146. uiTooltipItem.showWorth(canAfford);
  147. },
  148. beforeHide: function () {
  149. events.emit('onHideOverlay', this.el);
  150. },
  151. onServerRespond: function (el) {
  152. el.removeClass('disabled');
  153. }
  154. };
  155. });