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.4
  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 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>" +
  30. "<p>Buy resources:</p><p><input type=\"checkbox\" name=\"wood\" id=\"woodCheck\"> Wood <input type=\"checkbox\" " +
  31. "name=\"stone\" id=\"stoneCheck\"> Stone <input type=\"checkbox\" name=\"iron\" id=\"ironCheck\"> Iron</p>" +
  32. "<p><button id='start' class='btn'></button></p>";
  33. if (!start) {
  34. _ID("start").innerHTML = "Start";
  35. } else {
  36. _ID("start").innerHTML = "Stop";
  37. }
  38. if (localStorage.topUp) {
  39. _ID("topUpInput").value = localStorage.topUp;
  40. topUp = localStorage.topUp;
  41. }
  42. if (localStorage.price) {
  43. _ID("priceInput").value = localStorage.price;
  44. price = localStorage.price;
  45. }
  46. if (localStorage.stack) {
  47. _ID("stackInput").value = localStorage.stack;
  48. stack = localStorage.stack;
  49. }
  50. }
  51.  
  52. _ID("topUpOk").addEventListener("click", function() {
  53. topUp = _ID("topUpInput").value;
  54. localStorage.topUp = topUp;
  55. _ID("topUpText").innerHTML = "Top up to " + topUp;
  56. });
  57. _ID("priceOk").addEventListener("click", function() {
  58. price = _ID("priceInput").value;
  59. localStorage.price = price;
  60. _ID("priceText").innerHTML = "Buy when price above " + price;
  61. });
  62. _ID("stackOk").addEventListener("click", function() {
  63. stack = _ID("stackInput").value;
  64. localStorage.stack = stack;
  65. _ID("stackText").innerHTML = "Buy only " + stack + " resources at once";
  66. });
  67. _ID("start").addEventListener("click", function() {
  68. if (start) {
  69. start = false;
  70. _ID("start").innerHTML = "Start";
  71. } else {
  72. start = true;
  73. _ID("start").innerHTML = "Stop";
  74. buyRes();
  75. }
  76. });
  77.  
  78. _ID("topUpInput").addEventListener("keydown", clickOnKeyPress.bind(this, 13, "#topUpOk"));
  79. _ID("priceInput").addEventListener("keydown", clickOnKeyPress.bind(this, 13, "#priceOk"));
  80. _ID("stackInput").addEventListener("keydown", clickOnKeyPress.bind(this, 13, "#stackOk"));
  81.  
  82. let warehouse = game_data.village.res[6];
  83. let wood = game_data.village.wood;
  84. let stone = game_data.village.stone;
  85. let iron = game_data.village.iron;
  86. let woodPrice = parseInt(__("#premium_exchange_rate_wood > div:nth-child(1)").innerText);
  87. let stonePrice = parseInt(__("#premium_exchange_rate_stone > div:nth-child(1)").innerText);
  88. let ironPrice = parseInt(__("#premium_exchange_rate_iron > div:nth-child(1)").innerText);
  89. let woodInc = 0;
  90. let stoneInc = 0;
  91. let ironInc = 0;
  92. const buyWoodInput = __("#premium_exchange_buy_wood > div:nth-child(1) > input");
  93. const buyStoneInput = __("#premium_exchange_buy_stone > div:nth-child(1) > input");
  94. const buyIronInput = __("#premium_exchange_buy_iron > div:nth-child(1) > input");
  95.  
  96. if (start) {
  97. buyRes();
  98. }
  99. const interval = setInterval(function() {
  100. if (start && (!document.querySelector("#fader") || document.querySelector("#fader").style.display === "none")) {
  101. buyRes();
  102. }
  103. }, timeout);
  104.  
  105. function buyRes() {
  106. getRes();
  107. if (__("#buyStock").checked || wood + woodInc < topUp || stone + stoneInc < topUp || iron + ironInc < topUp) {
  108. if (__("#buyStock").checked && __("#woodCheck").checked || woodPrice > price && wood + woodInc < topUp && __("#woodCheck").checked) {
  109. // Buy wood
  110. let buyWoodAmnt = topUp - wood - woodInc;
  111. if (buyWoodAmnt <= 0) {
  112. buyWoodAmnt = woodPrice - 2;
  113. }
  114. if(buyWoodAmnt > stack) {
  115. buyWoodAmnt = stack;
  116. }
  117. if(__("#buyStock").checked) {
  118. buyWoodAmnt = parseInt(document.querySelector("#premium_exchange_stock_wood").innerText) - 1;
  119. }
  120. buyStoneInput.value = "";
  121. buyIronInput.value = "";
  122. buyWoodInput.value = buyWoodAmnt;
  123. clickBuy();
  124. } else if (__("#buyStock").checked && __("#stoneCheck").checked || stonePrice > price && stone + stoneInc < topUp && __("#stoneCheck").checked) {
  125. // Buy stone
  126. let buyStoneAmnt = topUp - stone - stoneInc;
  127. if (buyStoneAmnt <= 0) {
  128. buyStoneAmnt = stonePrice - 2;
  129. }
  130. if(buyStoneAmnt > stack) {
  131. buyStoneAmnt = stack;
  132. }
  133. if(__("#buyStock").checked) {
  134. buyStoneAmnt = parseInt(document.querySelector("#premium_exchange_stock_stone").innerText) - 1;
  135. }
  136. buyWoodInput.value = "";
  137. buyIronInput.value = "";
  138. buyStoneInput.value = buyStoneAmnt;
  139. clickBuy();
  140. } else if (__("#buyStock").checked && __("#ironCheck").checked || ironPrice > price && iron + ironInc < topUp && __("#ironCheck").checked) {
  141. // Buy iron
  142. let buyIronAmnt = topUp - iron - ironInc;
  143. if (buyIronAmnt <= 0) {
  144. buyIronAmnt = ironPrice - 2;
  145. }
  146. if(buyIronAmnt > stack) {
  147. buyIronAmnt = stack;
  148. }
  149. if(__("#buyStock").checked) {
  150. buyIronAmnt = parseInt(document.querySelector("#premium_exchange_stock_iron").innerText) - 1;
  151. }
  152. buyWoodInput.value = "";
  153. buyStoneInput.value = "";
  154. buyIronInput.value = buyIronAmnt;
  155. clickBuy();
  156. }
  157. }
  158. }
  159.  
  160. function clickBuy() {
  161. __("#premium_exchange_form > input").click();
  162. setTimeout(function() {
  163. __("#premium_exchange > div > div > div.confirmation-buttons > button.btn.evt-confirm-btn.btn-confirm-yes").click();
  164. }, 1000);
  165. }
  166.  
  167. function _ID(selector) {
  168. return document.getElementById(selector);
  169. }
  170.  
  171. function __(selector) {
  172. return document.querySelector(selector);
  173. }
  174.  
  175. function getRes() {
  176. let parentInc;
  177. warehouse = game_data.village.res[6];
  178. wood = game_data.village.wood;
  179. stone = game_data.village.stone;
  180. iron = game_data.village.iron;
  181. woodPrice = parseInt(__("#premium_exchange_rate_wood > div:nth-child(1)").innerText);
  182. stonePrice = parseInt(__("#premium_exchange_rate_stone > div:nth-child(1)").innerText);
  183. ironPrice = parseInt(__("#premium_exchange_rate_iron > div:nth-child(1)").innerText);
  184. try {
  185. if (__("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(1)").innerHTML.split(" ")[0].replace(":", "") === incoming) {
  186. parentInc = __("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(1)");
  187. }
  188. } catch(e) {}
  189. try {
  190. if (__("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(2)").innerHTML.split(" ")[0].replace(":", "") === incoming) {
  191. parentInc = __("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(2)");
  192. }
  193. } catch(e) {}
  194.  
  195. try {
  196. woodInc = setZeroIfNaN(parseInt(parentInc.querySelector(".wood").parentElement.innerText.replace(".", "")));
  197. } catch (e) {}
  198. try {
  199. stoneInc = setZeroIfNaN(parseInt(parentInc.querySelector(".stone").parentElement.innerText.replace(".", "")));
  200. } catch (e) {}
  201. try {
  202. ironInc = setZeroIfNaN(parseInt(parentInc.querySelector(".iron").parentElement.innerText.replace(".", "")));
  203. } catch (e) {}
  204. }
  205.  
  206. function clickOnKeyPress(key, selector) {
  207. "use strict";
  208. if (event.defaultPrevented) {
  209. return; // Should do nothing if the default action has been cancelled
  210. }
  211. let handled = false;
  212. if (event.key === key) {
  213. document.querySelector(selector).click();
  214. handled = true;
  215. } else if (event.keyIdentifier === key) {
  216. document.querySelector(selector).click();
  217. handled = true;
  218. } else if (event.keyCode === key) {
  219. document.querySelector(selector).click();
  220. handled = true;
  221. }
  222. if (handled) {
  223. event.preventDefault();
  224. }
  225. }
  226.  
  227. function setZeroIfNaN(x) {
  228. "use strict";
  229. if ((typeof x === 'number') && (x % 1 === 0)) {
  230. return x;
  231. } else {
  232. return 0;
  233. };
  234. }