SHARE
TWEET

Untitled

a guest Nov 12th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Premium Exchange - Buy Resources
  3. // @description  Automatically buy resources up to a predefined amount of resources
  4. // @author       scriptspace
  5. // @version      2.1.2
  6. // @include      https://*/game.php*screen=market*
  7. // @namespace    scriptspace
  8. // ==/UserScript==
  9. const incoming = "Incoming"; // Change this accordingly to your language. In Portuguese it would be const incoming = "Entrada";
  10. const timeout = 9000; // Time in ms between transactions. Too low and the game won't allow it
  11. let topUp, price, stack;
  12. let start = false; // Start script or stop script, default is stop
  13.  
  14. createInput();
  15.  
  16. function createInput() {
  17.     "use strict";
  18.     const userInputParent = _ID("premium_exchange_form"); // Parent element
  19.  
  20.     // Create input for setting how much res should be bought
  21.     const divScript = document.createElement("div");
  22.     divScript.setAttribute("id", "divScript");
  23.     userInputParent.parentNode.insertBefore(divScript, userInputParent);
  24.     _ID("divScript").innerHTML = "<p>Top up warehouse to: <input id='topUpInput'> " +
  25.         "<button id='topUpOk' class='btn'>OK</button><span id='topUpText'></span></p><p>Buy when price above: <input id='priceInput'> " +
  26.         "<button id='priceOk' class='btn'>OK</button><span id='priceText'></span></p>" +
  27.         "<p>Buy max this much at once: <input id='stackInput'> <button id='stackOk' class='btn'>OK</button><span id='stackText'></span></p>" +
  28.         "<p>Buy the whole stock at once: <input type=\"checkbox\" name=\"buyStock\" id=\"buyStock\"></p><span style='color:red'>ATTENTION! This might deplete your Premium Points completely, as it buys everything that is available!</span>" +
  29.         "<p>Buy resources:</p><p><input type=\"checkbox\" name=\"wood\" id=\"woodCheck\"> Wood <input type=\"checkbox\" " +
  30.         "name=\"stone\" id=\"stoneCheck\"> Stone <input type=\"checkbox\" name=\"iron\" id=\"ironCheck\"> Iron</p>" +
  31.         "<p><button id='start' class='btn'></button></p>";
  32.     if (!start) {
  33.         _ID("start").innerHTML = "Start";
  34.     } else {
  35.         _ID("start").innerHTML = "Stop";
  36.     }
  37.     if (localStorage.topUp) {
  38.         _ID("topUpInput").value = localStorage.topUp;
  39.         topUp = localStorage.topUp;
  40.     }
  41.     if (localStorage.price) {
  42.         _ID("priceInput").value = localStorage.price;
  43.         price = localStorage.price;
  44.     }
  45.     if (localStorage.stack) {
  46.         _ID("stackInput").value = localStorage.stack;
  47.         stack = localStorage.stack;
  48.     }
  49. }
  50.  
  51. _ID("topUpOk").addEventListener("click", function() {
  52.     topUp = _ID("topUpInput").value;
  53.     localStorage.topUp = topUp;
  54.     _ID("topUpText").innerHTML = "Top up to " + topUp;
  55. });
  56. _ID("priceOk").addEventListener("click", function() {
  57.     price = _ID("priceInput").value;
  58.     localStorage.price = price;
  59.     _ID("priceText").innerHTML = "Buy when price above " + price;
  60. });
  61. _ID("stackOk").addEventListener("click", function() {
  62.     stack = _ID("stackInput").value;
  63.     localStorage.stack = stack;
  64.     _ID("stackText").innerHTML = "Buy only " + stack + " resources at once";
  65. });
  66. _ID("start").addEventListener("click", function() {
  67.     if (start) {
  68.         start = false;
  69.         _ID("start").innerHTML = "Start";
  70.     } else {
  71.         start = true;
  72.         _ID("start").innerHTML = "Stop";
  73.         buyRes();
  74.     }
  75. });
  76.  
  77. _ID("topUpInput").addEventListener("keydown", clickOnKeyPress.bind(this, 13, "#topUpOk"));
  78. _ID("priceInput").addEventListener("keydown", clickOnKeyPress.bind(this, 13, "#priceOk"));
  79. _ID("stackInput").addEventListener("keydown", clickOnKeyPress.bind(this, 13, "#stackOk"));
  80.  
  81. /**
  82.  *
  83.  * @param wh Amount of resources in the warehouse
  84.  * @param price Current price of the resource
  85.  * @param stock Amount of resources in the premium exchange stock
  86.  * @param inc Amount of incoming resources
  87.  * @param input DOM Element of the text box
  88.  * @param buy Amount of resources to buy
  89.  * @constructor
  90.  */
  91. function Resource(wh, price, stock, inc, input) {
  92.     this.wh = wh;
  93.     this.price = price;
  94.     this.stock = stock;
  95.     this.inc = inc;
  96.     this.inputBuy = input;
  97.     this.buy = 0;
  98. }
  99.  
  100. /**
  101.  * Get all the info of the resources
  102.  * @type {Resource}
  103.  */
  104. let wood = new Resource(game_data.village.wood, parseInt(__("#premium_exchange_rate_wood > div:nth-child(1)").innerText), parseInt(__("#premium_exchange_stock_wood").innerText), 0, __("#premium_exchange_buy_wood > div:nth-child(1) > input"));
  105. let iron = new Resource(game_data.village.iron, parseInt(__("#premium_exchange_rate_iron > div:nth-child(1)").innerText), parseInt(__("#premium_exchange_stock_iron").innerText), 0, __("#premium_exchange_buy_iron > div:nth-child(1) > input"));
  106. let stone = new Resource(game_data.village.stone, parseInt(__("#premium_exchange_rate_stone > div:nth-child(1)").innerText), parseInt(__("#premium_exchange_stock_stone").innerText), 0, __("#premium_exchange_buy_stone > div:nth-child(1) > input"));
  107. let warehouse = game_data.village.storage_max;
  108.  
  109.  
  110. if (start) {
  111.     buyRes();
  112. }
  113. const interval = setInterval(function() {
  114.     if (start && (!document.querySelector("#fader") || document.querySelector("#fader").style.display === "none")) {
  115.         buyRes();
  116.     }
  117. }, timeout + Math.random() * 890);
  118.  
  119. function buyRes() {
  120.     getRes();
  121.     // If buy everything is checked and warehouse + incoming resource of each resource is less than what the warehouse should be topped up to
  122.     if (__("#buyStock").checked || wood.wh + wood.inc < topUp || stone.wh + stone.inc < topUp || iron.wh + iron.inc < topUp) {
  123.         if ((__("#buyStock").checked && __("#woodCheck").checked || wood.price > price && wood.wh + wood.inc < topUp && __("#woodCheck").checked) && wood.stock > price) {
  124.             // Buy wood
  125.             wood.buy = topUp - wood.wh - wood.inc;
  126.             // If for some reason, which shouldn't occur, the amount to buy goes below 0, adjust the amount to buy
  127.             if (wood.buy <= 0) {
  128.                 wood.buy = wood.price - 2;
  129.             }
  130.             // Only buy a certain amount of resources (stack) at once so the price can be still seen
  131.             if(wood.buy > stack) {
  132.                 wood.buy = stack;
  133.             }
  134.             if(wood.buy > wood.stock || __("#buyStock").checked && wood.stock > 0) {
  135.                 wood.buy = wood.stock - 1;
  136.             }
  137.             stone.inputBuy.value = "";
  138.             iron.inputBuy.value = "";
  139.             //wood.buy = setZeroIfNaN(wood.buy);
  140.             if(wood.buy === 0) {
  141.                 clearInterval(interval);
  142.                 console.log("wood:");
  143.                 console.log(wood);
  144.                 alert("This error message shouldn't pop up. Please open the console with CTRL+Shift+J and send a message to the developer via Discord, FunnyPocketBook#9373");
  145.                 return;
  146.             }
  147.             wood.inputBuy.value = wood.buy;
  148.             clickBuy();
  149.         } else if ((__("#buyStock").checked && __("#stoneCheck").checked || stone.price > price && stone.wh + stone.inc < topUp && __("#stoneCheck").checked) && stone.stock > price) {
  150.             // Buy stone
  151.             stone.buy = topUp - stone.wh - stone.inc;
  152.             if (stone.buy <= 0) {
  153.                 stone.buy = stone.price - 2;
  154.             }
  155.             if(stone.buy > stack) {
  156.                 stone.buy = stack;
  157.             }
  158.             if(stone.buy > stone.stock || __("#buyStock").checked && stone.stock > 0) {
  159.                 stone.buy = stone.stock - 1;
  160.             }
  161.             wood.inputBuy.value = "";
  162.             iron.inputBuy.value = "";
  163.             //stone.buy = setZeroIfNaN(stone.buy);
  164.             if(stone.buy === 0) {
  165.                 clearInterval(interval);
  166.                 console.log("stone:");
  167.                 console.log(stone);
  168.                 alert("This error message shouldn't pop up. Please open the console with CTRL+Shift+J and send a message to the developer via Discord, FunnyPocketBook#9373");
  169.                 return;
  170.             }
  171.             stone.inputBuy.value = stone.buy;
  172.             clickBuy();
  173.         } else if ((__("#buyStock").checked && __("#ironCheck").checked || iron.price > price && iron.wh + iron.inc < topUp && __("#ironCheck").checked) && iron.stock > price) {
  174.             // Buy iron
  175.             iron.buy = topUp - iron.wh - iron.inc;
  176.             if (iron.buy <= 0) {
  177.                 iron.buy = iron.price - 2;
  178.             }
  179.             if(iron.buy > stack) {
  180.                 iron.buy = stack;
  181.             }
  182.             if(iron.buy > iron.stock || __("#buyStock").checked && iron.stock > 0) {
  183.                 iron.buy = iron.stock - 1;
  184.             }
  185.             wood.inputBuy.value = "";
  186.             stone.inputBuy.value = "";
  187.             //iron.buy = setZeroIfNaN(iron.buy);
  188.             if(iron.buy === 0) {
  189.                 clearInterval(interval);
  190.                 console.log("iron:");
  191.                 console.log(iron);
  192.                 alert("This error message shouldn't pop up. Please open the console with CTRL+Shift+J and send a message to the developer via Discord, FunnyPocketBook#9373");
  193.                 return;
  194.             }
  195.             iron.inputBuy.value = iron.buy;
  196.             clickBuy();
  197.         }
  198.     }
  199. }
  200.  
  201. function clickBuy() {
  202.     __("#premium_exchange_form > input").click();
  203.     setTimeout(function() {
  204.         __("#premium_exchange > div > div > div.confirmation-buttons > button.btn.evt-confirm-btn.btn-confirm-yes").click();
  205.     }, 1000);
  206. }
  207.  
  208. function _ID(selector) {
  209.     return document.getElementById(selector);
  210. }
  211.  
  212. function __(selector) {
  213.     return document.querySelector(selector);
  214. }
  215.  
  216. /**
  217.  * Update resource objects
  218.  */
  219. function getRes() {
  220.     let parentInc;
  221.     warehouse = game_data.village.storage_max;
  222.     wood.wh = game_data.village.wood;
  223.     stone.wh = game_data.village.stone;
  224.     iron.wh = game_data.village.iron;
  225.     wood.stock = parseInt(__("#premium_exchange_stock_wood").innerText);
  226.     iron.stock = parseInt(__("#premium_exchange_stock_iron").innerText);
  227.     stone.stock = parseInt(__("#premium_exchange_stock_stone").innerText);
  228.     wood.price = parseInt(__("#premium_exchange_rate_wood > div:nth-child(1)").innerText);
  229.     stone.price = parseInt(__("#premium_exchange_rate_stone > div:nth-child(1)").innerText);
  230.     iron.price = parseInt(__("#premium_exchange_rate_iron > div:nth-child(1)").innerText);
  231.     try {
  232.         if (__("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(1)").innerHTML.split(" ")[0].replace(":", "") === incoming) {
  233.             parentInc = __("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(1)");
  234.         }
  235.     } catch(e) {}
  236.     try {
  237.         if (__("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(2)").innerHTML.split(" ")[0].replace(":", "") === incoming) {
  238.             parentInc = __("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(2)");
  239.         }
  240.     } catch(e) {}
  241.  
  242.     try {
  243.         wood.inc = parseInt(setZeroIfNaN(parseInt(parentInc.querySelector(".wood").parentElement.innerText.replace(".", ""))));
  244.     } catch (e) {}
  245.     try {
  246.         stone.inc = parseInt(setZeroIfNaN(parseInt(parentInc.querySelector(".stone").parentElement.innerText.replace(".", ""))));
  247.     } catch (e) {}
  248.     try {
  249.         iron.inc = parseInt(setZeroIfNaN(parseInt(parentInc.querySelector(".iron").parentElement.innerText.replace(".", ""))));
  250.     } catch (e) {}
  251. }
  252.  
  253. function clickOnKeyPress(key, selector) {
  254.     "use strict";
  255.     if (event.defaultPrevented) {
  256.         return; // Should do nothing if the default action has been cancelled
  257.     }
  258.     let handled = false;
  259.     if (event.key === key) {
  260.         document.querySelector(selector).click();
  261.         handled = true;
  262.     } else if (event.keyIdentifier === key) {
  263.         document.querySelector(selector).click();
  264.         handled = true;
  265.     } else if (event.keyCode === key) {
  266.         document.querySelector(selector).click();
  267.         handled = true;
  268.     }
  269.     if (handled) {
  270.         event.preventDefault();
  271.     }
  272. }
  273.  
  274. function setZeroIfNaN(x) {
  275.     "use strict";
  276.     if ((typeof x === 'number') && (x % 1 === 0)) {
  277.         return x;
  278.     } else {
  279.         return 0;
  280.     };
  281. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top