Dzejkobini

esim.spectators

Dec 7th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name ProductMarketExtension
  3. // @description  Adds the ability to buy goods from product market for gold.
  4. // @include http://*.e-sim.org/productMarket.html*
  5. // @grant none
  6. // ==/UserScript==
  7. var g_sServer = location.origin.match(/\/\/(.+?)\.e-sim\.org/)[1];
  8. var g_sCurrencyName;
  9. var g_nPlayerId;
  10. $(document).ready(function() {
  11.  
  12.     g_nPlayerId = parseInt($("[class='button foundation-style'][href*='profile']").attr('href').replace(/[^0-9]/g, ''));
  13.     var aObj = getMMcontent();
  14.     var nGoldRatio = getGoldRatio(aObj);
  15.     modifyPMtable(nGoldRatio);
  16. });
  17.  
  18. /**
  19.  * Events handlings.
  20.  *  
  21.  */
  22.  
  23. $(document).ready(function() {
  24.     $("[name='quantity']").change(function() {
  25.  
  26.         var aCell = $("td", $(this).parents()[2]);
  27.         var fPriceInCurrency = $("b", aCell[3])[0].title;
  28.         var fPriceInGold = $("b", aCell[3])[1].title;
  29.         var nQuantity = $(this).val();
  30.  
  31.         if (isInt(nQuantity) && !isNaN(parseInt(nQuantity))) {
  32.             var fTotalCurrency = Math.round(nQuantity * fPriceInCurrency * 100) / 100;
  33.             var fTotalGold = Math.round(nQuantity * fPriceInGold * 10000) / 10000;
  34.  
  35.             $("[name='totalCurrency']", $(this).parents()[1]).text(fTotalCurrency);
  36.             $("[name='totalGold']", $(this).parents()[1]).text(fTotalGold);
  37.         } else if (isNaN(parseInt(nQuantity))) {
  38.             $("[name='totalCurrency']", $(this).parents()[1]).text(0);
  39.             $("[name='totalGold']", $(this).parents()[1]).text(0);
  40.         }
  41.     });
  42.  
  43.  
  44.     $("[class='replacedButton']").click(function() {
  45.  
  46.         if (!isNaN(parseInt($("[name='quantity']", $(this).parent()).val())) && parseInt($("[name='quantity']", $(this).parent()).val()) !== 0) {
  47.  
  48.             var sBuyFor = $("[name='buyFor']", $(this).parent()).find(":selected").val();
  49.  
  50.             if (sBuyFor === "gold") {
  51.                 var nTotalCurrency = $("[name='totalCurrency']", $(this).parents()[1]).text();
  52.                 var sObj = getMMcontent();
  53.                 var aObj = JSON.parse(sObj);
  54.                 var nTotCurrOnMM = 0;
  55.                 var nSumToBuy = nTotalCurrency;
  56.  
  57.                 for (i = 0; i < aObj.length; i++) {
  58.                     if (g_nPlayerId !== aObj[i].nSellerId || (aObj[i].bIsStock && g_nPlayerId == aObj[i].nSellerId)) {
  59.                         //
  60.                         // The script will buy for you a currency which is not more expensive than 115% of the lowest offer!
  61.                         //
  62.                         if (aObj[i].fCurrencyPrice < 1.15 * aObj[0].fCurrencyPrice) {
  63.                             nTotCurrOnMM += aObj[i].fCurrencyAmount
  64.                         }
  65.                     }
  66.  
  67.                 }
  68.  
  69.  
  70.                 if (nTotCurrOnMM > nSumToBuy) {
  71.                     for (i = 0; i < aObj.length; i++) {
  72.                         if (g_nPlayerId !== aObj[i].nSellerId || (aObj[i].bIsStock && g_nPlayerId == aObj[i].nSellerId)) {
  73.                             if (aObj[i].fCurrencyAmount >= nSumToBuy) {
  74.                                 buyCurrency(nSumToBuy, aObj[i].nOfferId, aObj[i].bIsStock);
  75.                                 break;
  76.                             } else {
  77.                                 buyCurrency(aObj[i].fCurrencyAmount, aObj[i].nOfferId, aObj[i].bIsStock);
  78.                                 sleep(200);
  79.                                 nSumToBuy -= aObj[i].fCurrencyAmount;
  80.                             }
  81.                         }
  82.  
  83.                     }
  84.                 } else {
  85.                     location.href = location.origin + "/productMarket.html?citizenMessage=MM_POST_NOT_ENOUGH_MONEY";
  86.                 }
  87.             }
  88.  
  89.  
  90.             $(this).parent().submit()
  91.         } else {
  92.             location.href = location.origin + "/productMarket.html?citizenMessage=POST_PRODUCT_POSITIVE_NUMBER_REQUIRED";
  93.         }
  94.  
  95.     });
  96. });
  97.  
  98. /**
  99.  * @Return {String} JSON string of monetary market content.
  100.  *
  101.  */
  102. function getMMcontent() {
  103.  
  104.     var sHtml;
  105.  
  106.     var nCountryId = parseInt($('#countryId').find(":selected").attr('value'));
  107.  
  108.     $.ajax({
  109.         type: "GET",
  110.         dataType: "html",
  111.         async: false,
  112.         url: "http://" + g_sServer + ".e-sim.org/monetaryMarket.html?buyerCurrencyId=" + nCountryId + "&sellerCurrencyId=0",
  113.         success: function(html) {
  114.             sHtml = html;
  115.         }
  116.     });
  117.  
  118.  
  119.     var aHtmlDOM = $.parseHTML(sHtml);
  120.  
  121.     g_sCurrencyName = $('#buy', aHtmlDOM).find(":selected").text().split(" ")[0];
  122.  
  123.     var aMMtab = $("[class='dataTable']", aHtmlDOM)[0];
  124.     var aRows = $("tr", aMMtab);
  125.     aRows = aRows.slice(1, aRows.length);
  126.  
  127.     var aResult = [];
  128.  
  129.     for (i = 0; i < aRows.length; i++) {
  130.         aResult[i] = {};
  131.         var aCell = $("td", aRows[i]);
  132.  
  133.         var aSeller = $("[class='profileLink'], a[href*='stockCompany']", aCell[0]);
  134.         var bIsStock = false;
  135.  
  136.         if ($("[class='profileLink']", aCell[0]).length) {
  137.             var nSellerId = parseInt(aSeller.attr('href').match(/profile\.html\?id=([0-9]*)/)[1]);
  138.         } else if ($("a[href*='stockCompany']", aCell[0]).length) {
  139.             var bIsStock = true;
  140.             var nSellerId = parseInt(aSeller.attr('href').match(/stockCompany\.html\?id=([0-9]*)/)[1]);
  141.         }
  142.  
  143.         var fCurrencyAmount = parseFloat($("b", aCell[1]).attr('title'));
  144.         var fCurrencyPrice = parseFloat($("b", aCell[2]).html());
  145.         var nOfferId = parseInt($("[name='id']", aCell[3]).attr('value'));
  146.  
  147.         aResult[i]['nSellerId'] = nSellerId;
  148.         aResult[i]['fCurrencyAmount'] = fCurrencyAmount;
  149.         aResult[i]['fCurrencyPrice'] = fCurrencyPrice;
  150.         aResult[i]['nOfferId'] = nOfferId;
  151.         aResult[i]['bIsStock'] = bIsStock;
  152.     }
  153.  
  154.     return JSON.stringify(aResult);
  155. }
  156.  
  157. /**
  158.  * @Param {String} sObj JSON string of MM content.
  159.  * @Return {Number} gold ratio.
  160.  *
  161.  */
  162. function getGoldRatio(sObj) {
  163.     var aObj = JSON.parse(sObj);
  164.     if (aObj.length) {
  165.         for (i = 0; i < aObj.length; i++) {
  166.             if (aObj[i].fCurrencyAmount > 100) {
  167.                 return aObj[i].fCurrencyPrice;
  168.             }
  169.         }
  170.     }
  171.  
  172. }
  173.  
  174. /**
  175.  *  Add DOM elements to dataTable of product market.
  176.  *  @Param {Number} nGoldRatio gold ration
  177.  *
  178.  */
  179. function modifyPMtable(nGoldRatio) {
  180.     var sCountryName = $('#countryId').find(":selected").text();
  181.  
  182.     var aRows = $("[class='dataTable'] tr");
  183.     aRows = aRows.slice(1, aRows.length);
  184.  
  185.     for (i = 0; i < aRows.length; i++) {
  186.         var aCell = $("td", aRows[i]);
  187.         var fProductPrice = parseFloat($("b", aCell[3]).html());
  188.         var fPriceInGold = Math.round(fProductPrice * nGoldRatio * 10000) / 10000;
  189.         var nOfferId = parseInt($("[name='id']", aCell[4]).attr('value'));
  190.  
  191.         $("#command", aCell[4]).prepend("Buy for: <select name=\"buyFor\"><option value=\"currency\">" + g_sCurrencyName + "</option><option value=\"gold\">Gold</option></select><br/>");
  192.         $("[type='submit']", aCell[4]).remove();
  193.         $("#command", aCell[4]).append("<button type=\"button\" class=\"replacedButton\">Buy</button>");
  194.         aCell[3].innerHTML += "<br/><b title='" + fPriceInGold + "'>" + fPriceInGold + "</b> <div class='flags-small Gold'></div> Gold";
  195.         aCell[4].innerHTML += "<b>In total:</b><br/><div class=\"flags-small " + sCountryName + "\"></div> <b name=\"totalCurrency\">0</b> " + g_sCurrencyName + "<br/><b name=\"totalGold\">0</b> <div class='flags-small Gold'></div> Gold";
  196.  
  197.     }
  198.  
  199. }
  200.  
  201. /**
  202.  * Buy currency from monetary market.
  203.  * @Param {Number} nStock amount of currency.
  204.  * @Param {Number} nOfferId ID of offer.
  205.  *
  206.  */
  207. function buyCurrency(nStock, nOfferId) {
  208.     var sURL;
  209.     var sData;
  210.  
  211.  
  212.     sURL = "http://" + g_sServer + ".e-sim.org/monetaryMarket.html?stockCompanyId=&action=buy&id=" + nOfferId + "&ammount=" + nStock;
  213.  
  214.  
  215.     $.ajax({
  216.         type: "POST",
  217.         dataType: "html",
  218.         url: sURL,
  219.         async: false,
  220.         success: function(html, textStatus, request) {}
  221.     });
  222.  
  223. }
  224.  
  225. /**
  226.  * @Return {Boolean} check if input is int
  227.  * @Param {Int} n number
  228.  *
  229.  */
  230. function isInt(n) {
  231.     return n % 1 === 0;
  232. }
  233.  
  234. /**
  235.  * Delay code execution.
  236.  * @Param {Number} nMiliseconds time in ms.
  237.  */
  238. function sleep(nMiliseconds) {
  239.     var nCurrentTime = new Date().getTime();
  240.  
  241.     while (nCurrentTime + nMiliseconds >= new Date().getTime()) {}
  242. }
Advertisement
Add Comment
Please, Sign In to add comment