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.

166 lines
4.0 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(item => {
  38. if ((item === this.hoverItem))
  39. this.onHover(null, item);
  40. });
  41. const itemsHavePositions = action === 'sell' || buyItems.find(b => b.pos);
  42. let iLen = Math.max(buyItems.length, 50);
  43. for (let i = 0; i < iLen; i++) {
  44. let item = buyItems[i];
  45. if (itemsHavePositions)
  46. item = buyItems.find(b => b.pos === i);
  47. if (!item) {
  48. renderItem(container, null)
  49. .on('click', uiInventory.hideTooltip.bind(uiInventory));
  50. continue;
  51. }
  52. item = $.extend(true, {}, item);
  53. let itemEl = renderItem(container, item);
  54. itemEl
  55. .data('item', item)
  56. .find('.icon')
  57. .addClass(item.type);
  58. if (isMobile)
  59. itemEl.on('click', this.onHover.bind(this, itemEl, item, action));
  60. else {
  61. itemEl
  62. .on('click', this.onClick.bind(this, itemEl, item, action))
  63. .on('mousemove', this.onHover.bind(this, itemEl, item, action))
  64. .on('mouseleave', uiInventory.hideTooltip.bind(uiInventory, itemEl, item));
  65. }
  66. if (action === 'buy') {
  67. let noAfford = false;
  68. if (item.worth.currency) {
  69. let currencyItems = window.player.inventory.items.find(f => f.name === item.worth.currency);
  70. noAfford = ((!currencyItems) || (currencyItems.quantity < item.worth.amount));
  71. } else
  72. noAfford = (~~(item.worth * this.itemList.markup) > window.player.trade.gold);
  73. if (!noAfford && item.factions)
  74. noAfford = item.factions.some(f => f.tier > window.player.reputation.getTier(f.id));
  75. if (noAfford)
  76. $('<div class="no-afford"></div>').appendTo(itemEl);
  77. }
  78. if (item.worth.currency)
  79. item.worthText = item.worth.amount + 'x ' + item.worth.currency;
  80. else
  81. item.worthText = ~~(itemList.markup * item.worth);
  82. }
  83. this.center();
  84. this.show();
  85. events.emit('onShowOverlay', this.el);
  86. },
  87. onClick: function (el, item, action, e) {
  88. el.addClass('disabled');
  89. client.request({
  90. cpn: 'player',
  91. method: 'performAction',
  92. data: {
  93. cpn: 'trade',
  94. method: 'buySell',
  95. data: {
  96. itemId: item.id,
  97. action: action
  98. }
  99. },
  100. callback: this.onServerRespond.bind(this, el)
  101. });
  102. events.emit('onBuySellItem', this.el);
  103. let uiInventory = $('.uiInventory').data('ui');
  104. uiInventory.hideTooltip(el, item, e);
  105. },
  106. onHover: function (el, item, action, e) {
  107. let uiInventory = $('.uiInventory').data('ui');
  108. uiInventory.onHover(el, item, e);
  109. let canAfford = true;
  110. if (action === 'buy') {
  111. if (item.worth.currency) {
  112. let currencyItems = window.player.inventory.items.find(i => i.name === item.worth.currency);
  113. canAfford = (currencyItems && currencyItems.quantity >= item.worth.amount);
  114. } else
  115. canAfford = (item.worth * this.itemList.markup <= window.player.trade.gold);
  116. }
  117. let uiTooltipItem = $('.uiTooltipItem').data('ui');
  118. uiTooltipItem.showWorth(canAfford);
  119. if (isMobile)
  120. uiTooltipItem.addButton(action, this.onClick.bind(this, el, item, action));
  121. },
  122. beforeHide: function () {
  123. events.emit('onHideOverlay', this.el);
  124. $('.uiInventory').data('ui').hideTooltip();
  125. },
  126. onServerRespond: function (el) {
  127. el.removeClass('disabled');
  128. }
  129. };
  130. });