Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

180 строки
4.2 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. 'ui/shared/renderItem'
  8. ], function (
  9. events,
  10. client,
  11. template,
  12. styles,
  13. tplItem,
  14. renderItem
  15. ) {
  16. return {
  17. tpl: template,
  18. centered: true,
  19. modal: true,
  20. hasClose: true,
  21. list: null,
  22. action: null,
  23. postRender: function () {
  24. this.onEvent('onGetTradeList', this.onGetTradeList.bind(this));
  25. this.onEvent('onCloseTrade', this.hide.bind(this));
  26. },
  27. onGetTradeList: function (itemList, action) {
  28. itemList = itemList || this.itemList;
  29. action = action || this.action;
  30. this.itemList = itemList;
  31. this.action = action;
  32. this.find('.heading-text').html(action);
  33. let uiInventory = $('.uiInventory').data('ui');
  34. let container = this.el.find('.grid')
  35. .empty();
  36. let buyItems = itemList.items;
  37. buyItems.forEach(function (item) {
  38. if ((item === this.hoverItem))
  39. this.onHover(null, item);
  40. }, this);
  41. let iLen = Math.max(buyItems.length, 50);
  42. for (let i = 0; i < iLen; i++) {
  43. let item = buyItems[i];
  44. if (action === 'sell') {
  45. item = buyItems.find(function (b) {
  46. return (b.pos === i);
  47. });
  48. }
  49. if (!item) {
  50. renderItem(container, null)
  51. .on('click', uiInventory.hideTooltip.bind(uiInventory));
  52. continue;
  53. }
  54. item = $.extend(true, {}, item);
  55. let itemEl = renderItem(container, item);
  56. itemEl
  57. .data('item', item)
  58. .find('.icon')
  59. .addClass(item.type);
  60. if (isMobile)
  61. itemEl.on('click', this.onHover.bind(this, itemEl, item, action));
  62. else {
  63. itemEl
  64. .on('click', this.onClick.bind(this, itemEl, item, action))
  65. .on('mousemove', this.onHover.bind(this, itemEl, item, action))
  66. .on('mouseleave', uiInventory.hideTooltip.bind(uiInventory, itemEl, item));
  67. }
  68. if (action === 'buy') {
  69. let noAfford = false;
  70. if (item.worth.currency) {
  71. let currencyItems = window.player.inventory.items.find(f => f.name === item.worth.currency);
  72. noAfford = ((!currencyItems) || (currencyItems.quantity < item.worth.amount));
  73. } else
  74. noAfford = (~~(item.worth * this.itemList.markup) > window.player.trade.gold);
  75. if (!noAfford && item.factions)
  76. noAfford = item.factions.some(f => f.noEquip);
  77. if (noAfford)
  78. $('<div class="no-afford"></div>').appendTo(itemEl);
  79. }
  80. let worth = item.worth;
  81. if (typeof(worth) === 'number')
  82. item.worthText = ~~(itemList.markup * item.worth);
  83. else {
  84. if (!worth.push)
  85. worth = [worth];
  86. item.worthText = worth
  87. .map(w => {
  88. const { currency, amount } = w;
  89. const result = `${amount}x ${currency}`;
  90. return result;
  91. })
  92. .join('<br />');
  93. item.worthText = '<br />' + item.worthText;
  94. }
  95. }
  96. this.center();
  97. this.show();
  98. events.emit('onShowOverlay', this.el);
  99. },
  100. onClick: function (el, item, action, e) {
  101. el.addClass('disabled');
  102. client.request({
  103. cpn: 'player',
  104. method: 'performAction',
  105. data: {
  106. cpn: 'trade',
  107. method: 'buySell',
  108. data: {
  109. itemId: item.id,
  110. action: action
  111. }
  112. },
  113. callback: this.onServerRespond.bind(this, el)
  114. });
  115. let uiInventory = $('.uiInventory').data('ui');
  116. uiInventory.hideTooltip(el, item, e);
  117. },
  118. onHover: function (el, item, action, e) {
  119. let uiInventory = $('.uiInventory').data('ui');
  120. uiInventory.onHover(el, item, e);
  121. let canAfford = true;
  122. if (action === 'buy') {
  123. if (item.worth.currency) {
  124. let currencyItems = window.player.inventory.items.find(i => i.name === item.worth.currency);
  125. canAfford = (currencyItems && currencyItems.quantity >= item.worth.amount);
  126. } else
  127. canAfford = (item.worth * this.itemList.markup <= window.player.trade.gold);
  128. }
  129. let uiTooltipItem = $('.uiTooltipItem').data('ui');
  130. uiTooltipItem.showWorth(canAfford);
  131. if (isMobile)
  132. uiTooltipItem.addButton(action, this.onClick.bind(this, el, item, action));
  133. },
  134. beforeHide: function () {
  135. events.emit('onHideOverlay', this.el);
  136. $('.uiInventory').data('ui').hideTooltip();
  137. },
  138. onServerRespond: function (el) {
  139. el.removeClass('disabled');
  140. }
  141. };
  142. });