Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name ProductMarketExtension
- // @description Adds the ability to buy goods from product market for gold.
- // @include http://*.e-sim.org/productMarket.html*
- // @grant none
- // ==/UserScript==
- var g_sServer = location.origin.match(/\/\/(.+?)\.e-sim\.org/)[1];
- var g_sCurrencyName;
- var g_nPlayerId;
- /**
- * Init block.
- *
- */
- $(document).ready(function() {
- g_nPlayerId = parseInt($("[class='button foundation-style'][href*='profile']").attr('href').replace(/[^0-9]/g, ''));
- var aObj = getMMcontent();
- var nGoldRatio = getGoldRatio(aObj);
- modifyPMtable(nGoldRatio);
- });
- /**
- * Events handlings.
- *
- */
- $(document).ready(function() {
- $("[name='quantity']").change(function() {
- var aCell = $("td", $(this).parents()[2]);
- var fPriceInCurrency = $("b", aCell[3])[0].title;
- var fPriceInGold = $("b", aCell[3])[1].title;
- var nQuantity = $(this).val();
- if (isInt(nQuantity) && !isNaN(parseInt(nQuantity))) {
- var fTotalCurrency = Math.round(nQuantity * fPriceInCurrency * 100) / 100;
- var fTotalGold = Math.round(nQuantity * fPriceInGold * 10000) / 10000;
- $("[name='totalCurrency']", $(this).parents()[1]).text(fTotalCurrency);
- $("[name='totalGold']", $(this).parents()[1]).text(fTotalGold);
- } else if (isNaN(parseInt(nQuantity))) {
- $("[name='totalCurrency']", $(this).parents()[1]).text(0);
- $("[name='totalGold']", $(this).parents()[1]).text(0);
- }
- });
- $("[class='replacedButton']").click(function() {
- if (!isNaN(parseInt($("[name='quantity']", $(this).parent()).val())) && parseInt($("[name='quantity']", $(this).parent()).val()) !== 0) {
- var sBuyFor = $("[name='buyFor']", $(this).parent()).find(":selected").val();
- var sLocation = $("[class*='flags-small']", $("#stats"))[0].className.replace("flags-small ", "");
- var aRows = $("[class='dataTable'] tr");
- aRows = aRows.slice(1, aRows.length);
- var aCell = $("td", aRows[0]);
- var sCountryName = $("[class*='flags-small']", aCell[3])[0].className.replace("flags-small ", "");
- if (sBuyFor === "gold") {
- if (sLocation === sCountryName) {
- var nTotalCurrency = $("[name='totalCurrency']", $(this).parents()[1]).text();
- var sObj = getMMcontent();
- var aObj = JSON.parse(sObj);
- var nTotCurrOnMM = 0;
- var nSumToBuy = nTotalCurrency;
- for (i = 0; i < aObj.length; i++) {
- if (g_nPlayerId !== aObj[i].nSellerId || (aObj[i].bIsStock && g_nPlayerId == aObj[i].nSellerId)) {
- //
- // The script will buy for you a currency which is not more expensive than 115% of the lowest offer!
- //
- if (aObj[i].fCurrencyPrice < 1.15 * aObj[0].fCurrencyPrice) {
- nTotCurrOnMM += aObj[i].fCurrencyAmount
- }
- }
- }
- if (nTotCurrOnMM > nSumToBuy) {
- for (i = 0; i < aObj.length; i++) {
- if (g_nPlayerId !== aObj[i].nSellerId || (aObj[i].bIsStock && g_nPlayerId == aObj[i].nSellerId)) {
- if (aObj[i].fCurrencyAmount >= nSumToBuy) {
- buyCurrency(nSumToBuy, aObj[i].nOfferId, aObj[i].bIsStock);
- break;
- } else {
- buyCurrency(aObj[i].fCurrencyAmount, aObj[i].nOfferId, aObj[i].bIsStock);
- sleep(200);
- nSumToBuy -= aObj[i].fCurrencyAmount;
- }
- }
- }
- } else {
- location.href = location.origin + "/productMarket.html?citizenMessage=MM_POST_NOT_ENOUGH_MONEY";
- }
- } else {
- location.href = location.origin + "/productMarket.html?citizenMessage=POST_PRODUCT_WRONG_LOCATION";
- }
- }
- $(this).parent().submit()
- } else {
- location.href = location.origin + "/productMarket.html?citizenMessage=POST_PRODUCT_POSITIVE_NUMBER_REQUIRED";
- }
- });
- });
- /**
- * @Return {String} JSON string of monetary market content.
- *
- */
- function getMMcontent() {
- var sHtml;
- var nCountryId = parseInt($('#countryId').find(":selected").attr('value'));
- $.ajax({
- type: "GET",
- dataType: "html",
- async: false,
- url: "http://" + g_sServer + ".e-sim.org/monetaryMarket.html?buyerCurrencyId=" + nCountryId + "&sellerCurrencyId=0",
- success: function(html) {
- sHtml = html;
- }
- });
- var aHtmlDOM = $.parseHTML(sHtml);
- g_sCurrencyName = $('#buy', aHtmlDOM).find(":selected").text().split(" ")[0];
- var aMMtab = $("[class='dataTable']", aHtmlDOM)[0];
- var aRows = $("tr", aMMtab);
- aRows = aRows.slice(1, aRows.length);
- var aResult = [];
- for (i = 0; i < aRows.length; i++) {
- aResult[i] = {};
- var aCell = $("td", aRows[i]);
- var aSeller = $("[class='profileLink'], a[href*='stockCompany']", aCell[0]);
- var bIsStock = false;
- if ($("[class='profileLink']", aCell[0]).length) {
- var nSellerId = parseInt(aSeller.attr('href').match(/profile\.html\?id=([0-9]*)/)[1]);
- } else if ($("a[href*='stockCompany']", aCell[0]).length) {
- var bIsStock = true;
- var nSellerId = parseInt(aSeller.attr('href').match(/stockCompany\.html\?id=([0-9]*)/)[1]);
- }
- var fCurrencyAmount = parseFloat($("b", aCell[1]).attr('title'));
- var fCurrencyPrice = parseFloat($("b", aCell[2]).html());
- var nOfferId = parseInt($("[name='id']", aCell[3]).attr('value'));
- aResult[i]['nSellerId'] = nSellerId;
- aResult[i]['fCurrencyAmount'] = fCurrencyAmount;
- aResult[i]['fCurrencyPrice'] = fCurrencyPrice;
- aResult[i]['nOfferId'] = nOfferId;
- aResult[i]['bIsStock'] = bIsStock;
- }
- return JSON.stringify(aResult);
- }
- /**
- * @Param {String} sObj JSON string of MM content.
- * @Return {Number} gold ratio.
- *
- */
- function getGoldRatio(sObj) {
- var aObj = JSON.parse(sObj);
- if (aObj.length) {
- for (i = 0; i < aObj.length; i++) {
- if (aObj[i].fCurrencyAmount > 100) {
- return aObj[i].fCurrencyPrice;
- }
- }
- }
- }
- /**
- * Add DOM elements to dataTable of product market.
- * @Param {Number} nGoldRatio gold ration
- *
- */
- function modifyPMtable(nGoldRatio) {
- var sCountryId = parseInt($('#countryId').find(":selected").val());
- if (sCountryId !== -1) {
- var aRows = $("[class='dataTable'] tr");
- aRows = aRows.slice(1, aRows.length);
- for (i = 0; i < aRows.length; i++) {
- var aCell = $("td", aRows[i]);
- var fProductPrice = parseFloat($("b", aCell[3]).html());
- var fPriceInGold = Math.round(fProductPrice * nGoldRatio * 10000) / 10000;
- var nOfferId = parseInt($("[name='id']", aCell[4]).attr('value'));
- var sCountryName = $("[class*='flags-small']", aCell[3])[0].className.replace("flags-small ", "");
- $("#command", aCell[4]).prepend("Buy for: <select name=\"buyFor\"><option value=\"currency\">" + g_sCurrencyName + "</option><option value=\"gold\">Gold</option></select><br/>");
- if (typeof aCell[4] != 'undefined')
- $("[type='submit']", aCell[4]).remove();
- $("#command", aCell[4]).append("<button type=\"button\" class=\"replacedButton\">Buy</button>");
- try {
- aCell[3].innerHTML += "<br/><b title='" + fPriceInGold + "'>" + fPriceInGold + "</b> <div class='flags-small Gold'></div> Gold";
- 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";
- } catch (e) {}
- }
- }
- }
- /**
- * Buy currency from monetary market.
- * @Param {Number} nStock amount of currency.
- * @Param {Number} nOfferId ID of offer.
- *
- */
- function buyCurrency(nStock, nOfferId) {
- var sURL;
- var sData;
- sURL = "http://" + g_sServer + ".e-sim.org/monetaryMarket.html?stockCompanyId=&action=buy&id=" + nOfferId + "&ammount=" + nStock;
- $.ajax({
- type: "POST",
- dataType: "html",
- url: sURL,
- async: false,
- success: function(html, textStatus, request) {}
- });
- }
- /**
- * @Return {Boolean} check if input is int
- * @Param {Int} n number
- *
- */
- function isInt(n) {
- return n % 1 === 0;
- }
- /**
- * Delay code execution.
- * @Param {Number} nMiliseconds time in ms.
- */
- function sleep(nMiliseconds) {
- var nCurrentTime = new Date().getTime();
- while (nCurrentTime + nMiliseconds >= new Date().getTime()) {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement