Advertisement
Dzejkobini

esim.productmarket.extension

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