Premium Exchange - Buy Resources

Automatically buy resources up to a predefined amount of resources

2018-11-15 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

  1. // ==UserScript==
  2. // @name Premium Exchange - Buy Resources
  3. // @description Automatically buy resources up to a predefined amount of resources
  4. // @author FunnyPocketBook
  5. // @version 2.0.5
  6. // @include https://*/game.php*screen=market*
  7. // @namespace https://greasyfork.org/users/151096
  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 one: <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.res[6];
  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);
  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 > 0) {
  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(__("#buyStock").checked && wood.stock > 0) {
  135. console.log("wood.stock: " + wood.stock);
  136. wood.buy = wood.stock - 1;
  137. }
  138. stone.inputBuy.value = "";
  139. iron.inputBuy.value = "";
  140. wood.buy = setZeroIfNaN(wood.buy);
  141. if(wood.buy === 0) {
  142. clearInterval(interval);
  143. console.log("wood:");
  144. console.log(wood);
  145. 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");
  146. return;
  147. }
  148. wood.inputBuy.value = wood.buy;
  149. clickBuy();
  150. } else if ((__("#buyStock").checked && __("#stoneCheck").checked || stone.price > price && stone.wh + stone.inc < topUp && __("#stoneCheck").checked) && stone.stock > 0) {
  151. // Buy stone
  152. stone.buy = topUp - stone.wh - stone.inc;
  153. if (stone.buy <= 0) {
  154. stone.buy = stone.price - 2;
  155. }
  156. if(stone.buy > stack) {
  157. stone.buy = stack;
  158. }
  159. if(__("#buyStock").checked && stone.stock > 0) {
  160. stone.buy = stone.stock - 1;
  161. }
  162. wood.inputBuy.value = "";
  163. iron.inputBuy.value = "";
  164. stone.buy = setZeroIfNaN(stone.buy);
  165. if(stone.buy === 0) {
  166. clearInterval(interval);
  167. console.log("stone:");
  168. console.log(stone);
  169. 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");
  170. return;
  171. }
  172. stone.inputBuy.value = stone.buy;
  173. clickBuy();
  174. } else if ((__("#buyStock").checked && __("#ironCheck").checked || iron.price > price && iron.wh + iron.inc < topUp && __("#ironCheck").checked) && iron.stock > 0) {
  175. // Buy iron
  176. iron.buy = topUp - iron.wh - iron.inc;
  177. if (iron.buy <= 0) {
  178. iron.buy = iron.price - 2;
  179. }
  180. if(iron.buy > stack) {
  181. iron.buy = stack;
  182. }
  183. if(__("#buyStock").checked && iron.stock > 0) {
  184. iron.buy = iron.stock - 1;
  185. }
  186. wood.inputBuy.value = "";
  187. stone.inputBuy.value = "";
  188. iron.buy = setZeroIfNaN(iron.buy);
  189. if(iron.buy === 0) {
  190. clearInterval(interval);
  191. console.log("iron:");
  192. console.log(iron);
  193. 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");
  194. return;
  195. }
  196. iron.inputBuy.value = iron.buy;
  197. clickBuy();
  198. }
  199. }
  200. }
  201.  
  202. function clickBuy() {
  203. __("#premium_exchange_form > input").click();
  204. setTimeout(function() {
  205. __("#premium_exchange > div > div > div.confirmation-buttons > button.btn.evt-confirm-btn.btn-confirm-yes").click();
  206. }, 1000);
  207. }
  208.  
  209. function _ID(selector) {
  210. return document.getElementById(selector);
  211. }
  212.  
  213. function __(selector) {
  214. return document.querySelector(selector);
  215. }
  216.  
  217. /**
  218. * Update resource objects
  219. */
  220. function getRes() {
  221. let parentInc;
  222. warehouse = game_data.village.res[6];
  223. wood.wh = game_data.village.wood;
  224. stone.wh = game_data.village.stone;
  225. iron.wh = game_data.village.iron;
  226. wood.price = parseInt(__("#premium_exchange_rate_wood > div:nth-child(1)").innerText);
  227. stone.price = parseInt(__("#premium_exchange_rate_stone > div:nth-child(1)").innerText);
  228. iron.price = parseInt(__("#premium_exchange_rate_iron > div:nth-child(1)").innerText);
  229. console.log(wood.buy);
  230. try {
  231. if (__("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(1)").innerHTML.split(" ")[0].replace(":", "") === incoming) {
  232. parentInc = __("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(1)");
  233. }
  234. } catch(e) {}
  235. try {
  236. if (__("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(2)").innerHTML.split(" ")[0].replace(":", "") === incoming) {
  237. parentInc = __("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(2)");
  238. }
  239. } catch(e) {}
  240.  
  241. try {
  242. wood.inc = parseInt(setZeroIfNaN(parseInt(parentInc.querySelector(".wood").parentElement.innerText.replace(".", ""))));
  243. } catch (e) {}
  244. try {
  245. stone.inc = parseInt(setZeroIfNaN(parseInt(parentInc.querySelector(".stone").parentElement.innerText.replace(".", ""))));
  246. } catch (e) {}
  247. try {
  248. iron.inc = parseInt(setZeroIfNaN(parseInt(parentInc.querySelector(".iron").parentElement.innerText.replace(".", ""))));
  249. } catch (e) {}
  250. }
  251.  
  252. function clickOnKeyPress(key, selector) {
  253. "use strict";
  254. if (event.defaultPrevented) {
  255. return; // Should do nothing if the default action has been cancelled
  256. }
  257. let handled = false;
  258. if (event.key === key) {
  259. document.querySelector(selector).click();
  260. handled = true;
  261. } else if (event.keyIdentifier === key) {
  262. document.querySelector(selector).click();
  263. handled = true;
  264. } else if (event.keyCode === key) {
  265. document.querySelector(selector).click();
  266. handled = true;
  267. }
  268. if (handled) {
  269. event.preventDefault();
  270. }
  271. }
  272.  
  273. function setZeroIfNaN(x) {
  274. "use strict";
  275. if ((typeof x === 'number') && (x % 1 === 0)) {
  276. return x;
  277. } else {
  278. return 0;
  279. };
  280. }