Premium Exchange - Buy Resources

Automatically buy resources up to a predefined amount of resources

2018-11-14 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

  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.3
  6. // @include https://*/game.php*screen=market*
  7. // @namespace https://greasyfork.org/users/151096
  8. // ==/UserScript==
  9. const incoming = "Incoming";
  10. const outgoing = "Outgoing";
  11. const timeout = 9000;
  12. let topUp, price, stack;
  13. let start = false;
  14.  
  15. createInput();
  16.  
  17. function createInput() {
  18. "use strict";
  19. const userInputParent = _ID("premium_exchange_form"); // Parent element
  20.  
  21. // Create input for setting how much res should be bought
  22. const divScript = document.createElement("div");
  23. divScript.setAttribute("id", "divScript");
  24. userInputParent.parentNode.insertBefore(divScript, userInputParent);
  25. _ID("divScript").innerHTML = "<p>Top up warehouse to: <input id='topUpInput'> " +
  26. "<button id='topUpOk' class='btn'>OK</button><span id='topUpText'></span></p><p>Buy when price above: <input id='priceInput'> " +
  27. "<button id='priceOk' class='btn'>OK</button><span id='priceText'></span></p>" +
  28. "<p>Buy max this much at once: <input id='stackInput'> <button id='stackOk' class='btn'>OK</button><span id='stackText'></span></p>" +
  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. let warehouse = game_data.village.res[6];
  82. let wood = game_data.village.wood;
  83. let stone = game_data.village.stone;
  84. let iron = game_data.village.iron;
  85. let woodPrice = parseInt(__("#premium_exchange_rate_wood > div:nth-child(1)").innerText);
  86. let stonePrice = parseInt(__("#premium_exchange_rate_stone > div:nth-child(1)").innerText);
  87. let ironPrice = parseInt(__("#premium_exchange_rate_iron > div:nth-child(1)").innerText);
  88. let woodInc = 0;
  89. let stoneInc = 0;
  90. let ironInc = 0;
  91. const buyWoodInput = __("#premium_exchange_buy_wood > div:nth-child(1) > input");
  92. const buyStoneInput = __("#premium_exchange_buy_stone > div:nth-child(1) > input");
  93. const buyIronInput = __("#premium_exchange_buy_iron > div:nth-child(1) > input");
  94.  
  95. if (start) {
  96. buyRes();
  97. }
  98. const interval = setInterval(function() {
  99. if (start && (!document.querySelector("#fader") || document.querySelector("#fader").style.display === "none")) {
  100. buyRes();
  101. }
  102. }, timeout);
  103.  
  104. function buyRes() {
  105. getRes();
  106. if (wood + woodInc < topUp || stone + stoneInc < topUp || iron + ironInc < topUp) {
  107. if (woodPrice > price && wood + woodInc < topUp && __("#woodCheck").checked) {
  108. // Buy wood
  109. let buyWoodAmnt = topUp - wood - woodInc;
  110. if (buyWoodAmnt <= 0) {
  111. buyWoodAmnt = woodPrice - 2;
  112. }
  113. if(buyWoodAmnt > stack) {
  114. buyWoodAmnt = stack;
  115. }
  116. buyStoneInput.value = "";
  117. buyIronInput.value = "";
  118. buyWoodInput.value = buyWoodAmnt;
  119. clickBuy();
  120. } else if (stonePrice > price && stone + stoneInc < topUp && __("#stoneCheck").checked) {
  121. // Buy stone
  122. let buyStoneAmnt = topUp - stone - stoneInc;
  123. if (buyStoneAmnt <= 0) {
  124. buyStoneAmnt = stonePrice - 2;
  125. }
  126. if(buyStoneAmnt > stack) {
  127. buyStoneAmnt = stack;
  128. }
  129. buyWoodInput.value = "";
  130. buyIronInput.value = "";
  131. buyStoneInput.value = buyStoneAmnt;
  132. clickBuy();
  133. } else if (ironPrice > price && iron + ironInc < topUp && __("#ironCheck").checked) {
  134. // Buy iron
  135. let buyIronAmnt = topUp - iron - ironInc;
  136. if (buyIronAmnt <= 0) {
  137. buyIronAmnt = ironPrice - 2;
  138. }
  139. if(buyIronAmnt > stack) {
  140. buyIronAmnt = stack;
  141. }
  142. buyWoodInput.value = "";
  143. buyStoneInput.value = "";
  144. buyIronInput.value = buyIronAmnt;
  145. clickBuy();
  146. }
  147. }
  148. }
  149.  
  150. function clickBuy() {
  151. __("#premium_exchange_form > input").click();
  152. setTimeout(function() {
  153. __("#premium_exchange > div > div > div.confirmation-buttons > button.btn.evt-confirm-btn.btn-confirm-yes").click();
  154. }, 1000);
  155. }
  156.  
  157. function _ID(selector) {
  158. return document.getElementById(selector);
  159. }
  160.  
  161. function __(selector) {
  162. return document.querySelector(selector);
  163. }
  164.  
  165. function getRes() {
  166. let parentInc;
  167. warehouse = game_data.village.res[6];
  168. wood = game_data.village.wood;
  169. stone = game_data.village.stone;
  170. iron = game_data.village.iron;
  171. woodPrice = parseInt(__("#premium_exchange_rate_wood > div:nth-child(1)").innerText);
  172. stonePrice = parseInt(__("#premium_exchange_rate_stone > div:nth-child(1)").innerText);
  173. ironPrice = parseInt(__("#premium_exchange_rate_iron > div:nth-child(1)").innerText);
  174. try {
  175. if (__("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(1)").innerHTML.split(" ")[0].replace(":", "") === incoming) {
  176. parentInc = __("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(1)");
  177. }
  178. } catch(e) {}
  179. try {
  180. if (__("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(2)").innerHTML.split(" ")[0].replace(":", "") === incoming) {
  181. parentInc = __("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(2)");
  182. }
  183. } catch(e) {}
  184.  
  185. try {
  186. woodInc = setZeroIfNaN(parseInt(parentInc.querySelector(".wood").parentElement.innerText.replace(".", "")));
  187. } catch (e) {}
  188. try {
  189. stoneInc = setZeroIfNaN(parseInt(parentInc.querySelector(".stone").parentElement.innerText.replace(".", "")));
  190. } catch (e) {}
  191. try {
  192. ironInc = setZeroIfNaN(parseInt(parentInc.querySelector(".iron").parentElement.innerText.replace(".", "")));
  193. } catch (e) {}
  194. }
  195.  
  196. function clickOnKeyPress(key, selector) {
  197. "use strict";
  198. if (event.defaultPrevented) {
  199. return; // Should do nothing if the default action has been cancelled
  200. }
  201. let handled = false;
  202. if (event.key === key) {
  203. document.querySelector(selector).click();
  204. handled = true;
  205. } else if (event.keyIdentifier === key) {
  206. document.querySelector(selector).click();
  207. handled = true;
  208. } else if (event.keyCode === key) {
  209. document.querySelector(selector).click();
  210. handled = true;
  211. }
  212. if (handled) {
  213. event.preventDefault();
  214. }
  215. }
  216.  
  217. function setZeroIfNaN(x) {
  218. "use strict";
  219. if ((typeof x === 'number') && (x % 1 === 0)) {
  220. return x;
  221. } else {
  222. return 0;
  223. };
  224. }