Advertisement
Guest User

trading.js

a guest
Aug 25th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(init);
  2.  
  3. var tradeID = '';
  4. var userID = 0;
  5. var value = 0;
  6. var max_value = 0;
  7. var other_value = 0;
  8.  
  9. function init() {
  10.     // Load values
  11.     userID = $('#trading_panel').data('userid');
  12.     tradeID = $('#trading_panel').data('tradeid');
  13.     other_value = parseFloat($('#other_trade_value_gold').data('gold'));
  14.  
  15.     // Stack items in all wrappers
  16.     stackItems("#main-items-wrapper");
  17.     stackItems("#my-items-wrapper-table");
  18.     stackItems("#other-items-wrapper-table");
  19.  
  20.     items = document.getElementsByClassName('items-wrapper');
  21.     Array.prototype.forEach.call(items, function (elem) {
  22.         Ps.initialize(elem);
  23.     });
  24.  
  25.     // Mark all my non-equippable items which are in others items to prevent them from putting to the right panel
  26.     $("#other-items-wrapper-table .item").each(function () {
  27.         if ($(this).data('id') == 0) {
  28.             $("#main-items-wrapper .item[data-item='" + $(this).data('item') + "']").addClass("exists-in-others-items");
  29.         }
  30.     });
  31.  
  32.     // Make items draggable
  33.     makeItemDraggable($(".draggable_item"));
  34.  
  35.     // Make items double clickable for quick move
  36.     makeItemDoubleClickable($(".draggable_item"));
  37.  
  38.     $(".items-wrapper:not(#other-items-wrapper-table)").disableSelection();
  39.  
  40.     makePanelDroppable("#main-items-wrapper", "#my-items-wrapper-table");
  41.     makePanelDroppable("#my-items-wrapper-table", "#main-items-wrapper");
  42.  
  43.     // Gold add/subtract buttons
  44.     $('#gold_trade_add').click(function () {
  45.         var curGold = parseFloat($('#my_gold_trade_amount').val());
  46.         var newGold = curGold + 100;
  47.         if (newGold > max_value) {
  48.             newGold = parseFloat(max_value);
  49.         }
  50.         $('#my_gold_trade_amount').val(newGold.toFixed(2));
  51.         calcValue();
  52.         return false;
  53.     });
  54.  
  55.     $('#gold_trade_subtract').click(function () {
  56.         var curGold = parseFloat($('#my_gold_trade_amount').val());
  57.         var newGold = curGold - 100;
  58.         if (newGold < 0) {
  59.             newGold = 0;
  60.         }
  61.         $('#my_gold_trade_amount').val(newGold.toFixed(2));
  62.         calcValue();
  63.         return false;
  64.     });
  65.  
  66.     $('#my_gold_trade_amount').bind('keyup mouseup mousewheel', function () {
  67.         if (parseInt($(this).val()) < 0) {
  68.             $(this).val(0);
  69.         } else {
  70.             var gold = parseFloat($(this).val());
  71.             calcValue();
  72.         }
  73.         return false;
  74.     });
  75.  
  76.     calcValue();
  77.  
  78.     // Search listener
  79.     $('#search_query').on('input', function () {
  80.         filterItems();
  81.     });
  82. }
  83.  
  84. function getItemCount(item) {
  85.     let elem = item.find('.item_count');
  86.     if (!elem.length) {
  87.         return 1;
  88.     }
  89.     return Number(elem.text());
  90. }
  91.  
  92. function setItemCount(item, count) {
  93.     let elem = item.find('.item_count');
  94.     if (!elem.length) {
  95.         elem = $('<span class="item_count"></span>').appendTo(item);
  96.     }
  97.     elem.text(count);
  98.     if (count > 1) {
  99.         elem.removeClass('hidden');
  100.     } else {
  101.         elem.addClass('hidden');
  102.     }
  103. }
  104.  
  105. function joinItem(baseItem, joinedItem) {
  106.     setItemCount(baseItem, getItemCount(baseItem) + getItemCount(joinedItem));
  107.     joinedItem.remove();
  108.     return baseItem;
  109. }
  110.  
  111. function splitItem(baseItem, count) {
  112.     let newItem = baseItem.clone();
  113.     setItemCount(baseItem, getItemCount(baseItem) - count);
  114.     setItemCount(newItem, count);
  115.     return newItem;
  116. }
  117.  
  118. function stackItems(container) {
  119.     let uniqueItems = {};
  120.     $(container).find('.item').each(function () {
  121.         // Do not stack equippable items
  122.         if ($(this).data('id') == 0) {
  123.             let id = $(this).data('item');
  124.             if (id in uniqueItems) {
  125.                 joinItem(uniqueItems[id], $(this));
  126.             } else {
  127.                 uniqueItems[id] = $(this);
  128.             }
  129.         }
  130.     });
  131. }
  132.  
  133. function makeItemDraggable(item) {
  134.     item.draggable({
  135.         containment: "document",
  136.         cursor: "move",
  137.         helper: function () {
  138.             let helper = $(this).clone();
  139.             setItemCount(helper, 1);
  140.             return helper;
  141.         },
  142.         appendTo: "#trading_panel",
  143.         revert: "invalid"
  144.     });
  145. }
  146.  
  147. function makeItemDoubleClickable(item) {
  148.     item.off("dblclick").dblclick(function() {
  149.         if (!$(this).hasClass("exists-in-others-items")) {
  150.             let dstPanel = $(this).parent("#my-items-wrapper-table").length ? "#main-items-wrapper" : "#my-items-wrapper-table";
  151.             moveItem($(this), 1, dstPanel, true);
  152.         }
  153.     });
  154. }
  155.  
  156. function makePanelDroppable(panel, srcPanel) {
  157.     $(panel).droppable({
  158.         // Only accept items from the source panel which are not in others items
  159.         accept: srcPanel + " .item:not(.exists-in-others-items)",
  160.         activeClass: "custom-state-active",
  161.         drop: function (event, ui) {
  162.             moveItem(ui.draggable, getItemCount(ui.helper), this);
  163.         }
  164.     });
  165. }
  166.  
  167. function moveItem(item, count, dstPanel, fast) {
  168.     let fadeDuration = fast ? 0 : 400;
  169.  
  170.     item.fadeOut(fadeDuration, function () {
  171.         // If not all quantity of the item is moved, split it into two items
  172.         if (count < getItemCount(item)) {
  173.             let newItem = splitItem(item, count);
  174.             item.show();
  175.             item = newItem;
  176.         }
  177.  
  178.         let existingItem = $(dstPanel).find(".item[data-item='" + item.data('item') + "']");
  179.  
  180.         if (existingItem.length && existingItem.data('id') == 0) {
  181.             // Item with the same ID already exists in the destination panel, join two items together
  182.             existingItem.hide();
  183.             joinItem(existingItem, item);
  184.             existingItem.fadeIn(fadeDuration);
  185.         } else {
  186.             // Item with such ID doesn't exist in the destination panel, put the dropped item at the end
  187.             item.appendTo(dstPanel);
  188.             makeItemDraggable(item);
  189.             makeItemDoubleClickable(item);
  190.             item.fadeIn(fadeDuration);
  191.         }
  192.  
  193.         calcValue(true);
  194.     });
  195. }
  196.  
  197. function calcValue(moved = false) {
  198.     var tempVal = 0;
  199.  
  200.     $('#my-items-wrapper-table li').each(function (i) {
  201.         // Add cost of all items. No need to check others items here because we do it when putting our items on the table.
  202.         tempVal += $(this).data('cost') * getItemCount($(this));
  203.     });
  204.     value = tempVal;
  205.     max_value = (value * 0.15);
  206.     $('#gold_trade_max_amount').text(max_value.toLocaleString('en-US', {minimumFractionDigits: 2}));
  207.  
  208.     if (parseFloat($('#my_gold_trade_amount').val()) > max_value) {
  209.         $('#my_gold_trade_amount').val(parseFloat(max_value).toFixed(2));
  210.     }
  211.  
  212.     var table_gold = parseFloat($('#my_gold_trade_amount').val());
  213.     var showVal = value + table_gold;
  214.     $('#my_trade_value_gold').text(showVal.toLocaleString('en-US'));
  215.  
  216.     if (moved && (value + table_gold) > 0) {
  217.         $('#propose_trade').prop('disabled', false);
  218.     }
  219.  
  220.     calcValuePercents();
  221. }
  222.  
  223. function calcValuePercents() {
  224.     var myPercent = 0;
  225.     var otherPercent = 0;
  226.  
  227.     if (value > 0 && other_value === 0) {
  228.         myPercent = 100;
  229.     } else if (other_value > 0 && value === 0) {
  230.         otherPercent = 100;
  231.     } else if (value > 0 && other_value > 0) {
  232.         myPercent = value / (value + other_value) * 100;
  233.         myPercent = myPercent.toFixed(2);
  234.         otherPercent = 100 - myPercent;
  235.         otherPercent = otherPercent.toFixed(2);
  236.         $('#accept_trade').css('visibility', 'visible');
  237.     }
  238.  
  239.     $('#my_trade_value_percent').text(myPercent + '%');
  240.     $('#other_trade_value_percent').text(otherPercent + '%');
  241.  
  242.     var difference = myPercent - otherPercent;
  243.     if (difference <= 20 && difference >= -20) {
  244.         $('#my_trade_value_percent').css('color', '#3B7E1A');
  245.         $('#other_trade_value_percent').css('color', '#3B7E1A');
  246.         $('#accept_trade').prop('disabled', false);
  247.     } else if (difference <= 30 && difference >= -30) {
  248.         $('#my_trade_value_percent').css('color', '#DDA82A');
  249.         $('#other_trade_value_percent').css('color', '#DDA82A');
  250.         $('#accept_trade').prop('disabled', true);
  251.     } else {
  252.         $('#my_trade_value_percent').css('color', '#B10000');
  253.         $('#other_trade_value_percent').css('color', '#B10000');
  254.         $('#accept_trade').prop('disabled', true);
  255.     }
  256. }
  257.  
  258. // Ajax functions / main buttons
  259. function cancelTrade() {
  260.     $.alertable.confirm('Are you sure you want to cancel this trade?').then(function () {
  261.         $('#cancel_trade').text('Cancelling...').prop('disabled', true);
  262.         $.get('user.php',
  263.             {action: 'ajax_trade', type: 'cancel', tradeid: tradeID},
  264.             function (response) {
  265.                 if (response == 'true') {
  266.                     location.replace('user.php?action=all_trades');
  267.                 } else {
  268.                     noty({
  269.                         text: 'An error occurred and the trade could not be cancelled.',
  270.                         type: 'error',
  271.                         timeout: 2500
  272.                     });
  273.                     $('#cancel_trade').text('Cancel Trade').prop('disabled', false);
  274.                 }
  275.             });
  276.     });
  277. }
  278.  
  279. function proposeTrade() {
  280.     if (tradeID == '') {
  281.         var message = "Are you sure you want to propose this new trade?";
  282.     } else {
  283.         var message = "Are you sure you want to propose a change to this trade?";
  284.     }
  285.  
  286.     $.alertable.confirm(message).then(function () {
  287.         var table_items = tableItems();
  288.         var gold = $('#my_gold_trade_amount').val();
  289.         var message = $('#message_body').val();
  290.  
  291.         console.log("table_items: " + JSON.stringify(table_items));
  292.  
  293.         var propose_text = $('#propose_trade').text();
  294.         $('#propose_trade').text('Sending...').prop('disabled', true);
  295.         $.get('user.php',
  296.             {
  297.                 action: 'ajax_trade',
  298.                 type: 'propose',
  299.                 userid: userID,
  300.                 tradeid: tradeID,
  301.                 gold: gold,
  302.                 items: JSON.stringify(table_items),
  303.                 message: message
  304.             },
  305.             function (response) {
  306.                 if (response != false) {
  307.                     location.replace('user.php?action=trade&tradeid=' + response);
  308.                 } else {
  309.                     $('#propose_trade').text(propose_text).prop('disabled', false);
  310.                     noty({
  311.                         text: 'Something went wrong and your trade proposal could not be sent.',
  312.                         type: 'error',
  313.                         timeout: 2500
  314.                     });
  315.                 }
  316.             });
  317.     });
  318. }
  319.  
  320. function acceptTrade() {
  321.     $.alertable.confirm('Are you sure you want to accept this trade?').then(function () {
  322.         var table_items = tableItems();
  323.         var gold = $('#my_gold_trade_amount').val();
  324.         var message = $('#message_body').val();
  325.  
  326.         $('#accept_trade').text('Accepting...').prop('disabled', true);
  327.         $.get('user.php',
  328.             {
  329.                 action: 'ajax_trade',
  330.                 type: 'accept',
  331.                 userid: userID,
  332.                 tradeid: tradeID,
  333.                 gold: gold,
  334.                 items: JSON.stringify(table_items),
  335.                 message: message,
  336.                 auth: authkey
  337.             },
  338.             function (response) {
  339.                 if (response != false) {
  340.                     location.replace('user.php?action=all_trades');
  341.                 } else {
  342.                     $('#accept_trade').text('Accept Trade').prop('disabled', false);
  343.                     noty({
  344.                         text: 'Something went wrong and your trade acceptance could not be sent.',
  345.                         type: 'error',
  346.                         timeout: 2500
  347.                     });
  348.                 }
  349.             });
  350.     });
  351. }
  352.  
  353. function sendPM() {
  354.     var message = $('#message_body').val();
  355.     var convid = $('.message_section').data('convid');
  356.  
  357.     if (convid > 0 && userID > 0 && message.length > 0) {
  358.         $('#send_message').val('Sending...').prop('disabled', true);
  359.         $.get('user.php',
  360.             {action: 'ajax_trade', type: 'send_pm', userid: userID, convid: convid, message: message},
  361.             function (response) {
  362.                 if (response == 'true') {
  363.                     noty({
  364.                         text: 'PM sent!',
  365.                         timeout: 3000
  366.                     });
  367.                     $('#message_body').val('');
  368.                     $('.message_replies').prepend($('<div class="message_reply message_reply_self"><span></span></div>').text(message));
  369.                     $('#send_message').val('Send message').prop('disabled', false);
  370.                 } else {
  371.                     $('#send_message').val('Send message').prop('disabled', false);
  372.                     noty({
  373.                         text: 'Something went wrong and your pm could not be sent.',
  374.                         type: 'error',
  375.                         timeout: 2500
  376.                     });
  377.                 }
  378.             });
  379.     } else {
  380.         noty({
  381.             text: 'Something went wrong and your pm could not be sent.',
  382.             type: 'error',
  383.             timeout: 2500
  384.         });
  385.     }
  386. }
  387.  
  388. // Returns all items on your table
  389. function tableItems() {
  390.     var table_items = [];
  391.  
  392.     $('#my-items-wrapper-table li').each(function (i) {
  393.         var item = {
  394.             itemid: $(this).data('item'),
  395.             equipid: $(this).data('id'),
  396.             amount: getItemCount($(this))
  397.         };
  398.         console.log("item: " + JSON.stringify(item));
  399.         table_items.push(item);
  400.     });
  401.  
  402.     return table_items;
  403. }
  404.  
  405. // Filter items in panel
  406. function filterItems() {
  407.     var query = $('#search_query').val();
  408.     if (query) {
  409.         // Hide all
  410.         $('#items li.item').each(function () {
  411.             $(this).addClass('hidden').removeAttr('style');
  412.         });
  413.  
  414.         // Get items to display
  415.         query.split('+').forEach(function (q) {
  416.             if (q) {
  417.                 q = encodeURIComponent(q).toLowerCase();
  418.                 $('#items li.item').each(function () {
  419.                     if ($(this).data("item-name").toLowerCase().indexOf(q) !== -1) {
  420.                         $(this).removeClass('hidden');
  421.                     }
  422.                 });
  423.             }
  424.         });
  425.     } else {
  426.         // Show all
  427.         $('#items li.item').each(function () {
  428.             $(this).removeClass('hidden').removeAttr('style');
  429.         });
  430.     }
  431.  
  432.     // Scroll to the top
  433.     $('#main-items-wrapper').scrollTop(0);
  434. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement