Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.79 KB | None | 0 0
  1. // ==UserScript==
  2. // @name DS 164
  3. // @version 1.3
  4. // @description Tribal wars bot
  5. // @match https://de164.die-staemme.de/*
  6. // @grant none
  7. // ==/UserScript==
  8.  
  9.  
  10.  
  11. //____________________________________SETUP______________________________________
  12. //WICHTIG: mit z.B: "// @match https://de165.die-staemme.de/*", [[ANPASSEN]] am Anfang der Datei, legt ihr die Welt fest auf dem das Script arbeitet, sonst läuft es überall
  13. const DEBUG = true;
  14. const URL_PREFIX = "de164"; //[[ANPASSEN]], Weltnummer (zB de164, de165 usw.)
  15. const OWN_VILLAGE_IDS = ["4411"]; //[[ANPASSEN]] die ID die in der URL eures Dorfes steht (zB "4662) für den Dorflink https://de164.die-staemme.de/game.php?village=4662
  16. const MIN_WAIT_TIME = 8000;
  17. const MAX_WAIT_TIME = 15000;
  18. const SCRIPT_FINAL_PAUSE_TIME = 120 * 1 * 1000; // Wie lange in Millisekunden wartet das Skript bis es wieder aktiviert wird, je weniger desto besser um Captchas zu vermeiden. Hier: 120 * 1 * 1000 = 120 Sekunden
  19. const IS_BUILDING_ACTIVE = true; // [[ANPASSEN]]Minenpushen aktivieren? BH/Speicher wird automatisch ausgebaut
  20. const IS_RECRUIT_ACTIVE = true; // [[ANPASSEN]] Truppen rekrutieren?
  21. const IS_BUILDING_BH_ONLY = false; //[[ANPASSEN]] Dorf ist fertig sausgebaut (oder wird manuell gebaut) aber wenn der BH wegen der automatischen Rekrutierung knapp wird soll dieser gebaut werden!
  22. const IS_BUILDING_GOALS_ONLY = true; // [[ANPASSEN]] Anstatt den Minenpushalgorithmus zu fahren, baut Bob die vorgegebenen Ziele von oben nach unten ab. Es ist hier nur notwendig die Entstufen anzugeben. BH/Speicher wird automatisch ausgebaut
  23. const TROOP_QUEUEPOOL = 5; //[[ANPASSEN]] Wieviele Einheiten sollen zu jeder Zeit in der Bauschleife sein. Je niedriger desto mehr Resis sind frei zum bauern.
  24. const USERNAME = "Zerker"; //[[ANPASSEN]] Wird nur im Fall eines autologouts benögit
  25. const PASSWORD = "asdf"; //[[ANPASSEN]] Wird nur im Fall eines autologouts benögit
  26.  
  27. //Konfiguration der Ziel-Truppenstärke pro Dorf, für ein weiteres Dorf, alles von localStorage bis })); kopieren, einfügen und ändern
  28. //Dorf Id anpassen!
  29. localStorage.setItem("4411", JSON.stringify({ //[[ANPASSEN]] Dorf-ID (siehe Oben) einfügen!
  30. spear: 0,
  31. sword: 0,
  32. axe: 0,
  33. spy: 0,
  34. light: 0,
  35. heavy: 0,
  36. ram: 0,
  37. catapult: 0,
  38. snob: 0
  39. }));
  40.  
  41.  
  42.  
  43.  
  44. var goals = [];
  45. // Building Queue due to Quests
  46. goals.push("main_20");
  47. goals.push("wall_15");
  48. goals.push("barracks_18");
  49. goals.push("stable_10");
  50. goals.push("stone_24");
  51. goals.push("wood_24");
  52. goals.push("iron_21");
  53. goals.push("storage_19");
  54.  
  55.  
  56. //_________________________________________________________________________________
  57.  
  58.  
  59. //Mainfunction
  60. (function () {
  61. 'use strict';
  62. console.log("--Bob nimmt die Arbeit auf--");
  63. getCurrentView();
  64. setTimeout(function () {
  65. console.log("The end is nigh")
  66. goToOverview();
  67. }, getEndDelay());
  68. })();
  69.  
  70. function logSteps() {
  71. if (DEBUG) {
  72. console.log("Step: " + localStorage.lastStep);
  73. }
  74. }
  75.  
  76. function getCurrentView() {
  77. var item = OWN_VILLAGE_IDS[0];
  78. var village = JSON.parse(localStorage.getItem(item));
  79. village.id = item;
  80. localStorage.setItem(item, JSON.stringify(village));
  81. localStorage.setItem("currentVillageId", item)
  82.  
  83. let currentUrl = window.location.href;
  84. if (currentUrl.endsWith("overview")) {
  85. console.log("CurrentView is overview");
  86. if (localStorage.lastStep == "garage") {
  87. goToMain();
  88. } else if (localStorage.lastStep == "main") {
  89. if (IS_RECRUIT_ACTIVE == true) {
  90. goToBarracks();
  91. } else {
  92. localStorage.lastStep = "barracks";
  93. goToOverview();
  94. }
  95. } else if (localStorage.lastStep == "barracks") {
  96. if (IS_RECRUIT_ACTIVE == true) {
  97. goToStable();
  98. } else {
  99. localStorage.lastStep = "stable";
  100. goToOverview();
  101. }
  102.  
  103. } else if (localStorage.lastStep == "stable") {
  104. if (IS_RECRUIT_ACTIVE == true) {
  105. goToGarage();
  106. } else {
  107. localStorage.lastStep = "garage";
  108. goToOverview();
  109. }
  110. } else {
  111. localStorage.lastStep = "garage"
  112. goToOverview();
  113. }
  114. } else if (currentUrl.endsWith("main")) {
  115. localStorage.lastStep = "main";
  116. if (IS_BUILDING_ACTIVE) {
  117. logSteps()
  118. buildNextBuilding();
  119. }
  120. } else if (currentUrl.endsWith("barracks")) {
  121. localStorage.lastStep = "barracks";
  122. if (IS_RECRUIT_ACTIVE) {
  123. if (IS_RECRUIT_ACTIVE) {
  124. logSteps()
  125. recruitTroops();
  126. }
  127. }
  128. } else if (currentUrl.endsWith("stable")) {
  129. localStorage.lastStep = "stable";
  130. if (IS_RECRUIT_ACTIVE) {
  131. logSteps()
  132. recruitKavs();
  133. }
  134. } else if (currentUrl.endsWith("garage")) {
  135. localStorage.lastStep = "garage";
  136. if (IS_RECRUIT_ACTIVE) {
  137. logSteps()
  138. recruitCar();
  139. }
  140. } else if (currentUrl.endsWith("logout-successful")) {
  141. goToMain();
  142. } else if (currentUrl.endsWith("/")) {
  143. console.log("CurrentView is login");
  144. login();
  145. } else {
  146. goToOverview();
  147. }
  148. }
  149.  
  150. //Goto Functions
  151. function goToOverview() {
  152. if (DEBUG) console.log("goToOverview");
  153. if (document.getElementsByClassName("btn - confirm - yes")[0] != undefined) {
  154. document.getElementsByClassName("btn - confirm - yes")[0].click();
  155. }
  156. changeUrl("overview");
  157. }
  158.  
  159. function goToMain() {
  160. if (needToChangeUrl("main")) {
  161. changeUrl("main");
  162. }
  163. }
  164.  
  165. function goToBarracks() {
  166. if (needToChangeUrl("barracks")) {
  167. changeUrl("barracks");
  168. }
  169. }
  170.  
  171. function goToStable() {
  172. if (needToChangeUrl("stable")) {
  173. changeUrl("stable");
  174. }
  175. }
  176.  
  177. function goToGarage() {
  178. if (needToChangeUrl("garage")) {
  179. changeUrl("garage");
  180. }
  181. }
  182.  
  183. function needToChangeUrl(building) {
  184. if (window.location.href.endsWith(building)) {
  185. return false;
  186. }
  187. return true;
  188. }
  189.  
  190. function changeUrl(building) {
  191. var url = "https://" + URL_PREFIX + ".die-staemme.de/game.php?village=" + localStorage.currentVillageId + "&screen=" + building;
  192. window.open(url, "_self");
  193. }
  194.  
  195.  
  196.  
  197. //Build
  198. function buildNextBuilding() {
  199. if (DEBUG) console.log("buildNextBuilding");
  200.  
  201. var nextBuildingElement = getNextBuildingElement();
  202. if (nextBuildingElement !== undefined) {
  203. nextBuildingElement.click();
  204. console.log("Now Building:" + nextBuildingElement);
  205. }
  206.  
  207. setTimeout(console.log("wait time"), getDelay());
  208. }
  209.  
  210. function getNextBuildingElement() {
  211. if (DEBUG) console.log("getNextBuildingElement");
  212. var buildableBuildings = document.getElementsByClassName("btn btn-build");
  213. //var buildingElementsQueue = getBuildingElementsQueue();
  214. var villageId = localStorage.currentVillageId;
  215. var villageObject = JSON.parse(localStorage.getItem(villageId));
  216. console.log(villageObject.id);
  217. for (let item of buildableBuildings) {
  218. if (item.id.indexOf("wood") != -1) {
  219. villageObject.woodLvl = item.id.replace(/^\D+/g, '');
  220. } else if (item.id.indexOf("stone") != -1) {
  221. villageObject.stoneLvl = item.id.replace(/^\D+/g, '');
  222. } else if (item.id.indexOf("iron") != -1) {
  223. villageObject.ironLvl = item.id.replace(/^\D+/g, '');
  224. } else if (item.id.indexOf("storage") != -1) {
  225. villageObject.storageLvl = item.id.replace(/^\D+/g, '');
  226. } else if (item.id.indexOf("farm") != -1) {
  227. villageObject.farmLvl = item.id.replace(/^\D+/g, '');
  228. }
  229. }
  230. getPopulation(villageObject);
  231.  
  232. localStorage.setItem(localStorage.currentVillageId, villageObject);
  233. var next = false;
  234. if (((villageObject.populationCurrent / villageObject.populationMax) * 100) > 80) {
  235. let found = false;
  236. let buildingQueue = document.getElementsByClassName("buildorder_farm")
  237. if (buildingQueue.length != 0) {
  238. found = true;
  239. }
  240. if (found == false) {
  241. next = "main_buildlink_farm_" + villageObject.farmLvl;
  242. }
  243. }
  244. if (IS_BUILDING_GOALS_ONLY && next == false) {
  245. let found = false;
  246. if (goals.length > 0) {
  247. while (goals.length > 0 || found) {
  248. let nextGoal = goals.shift();
  249. let goal = nextGoal.split('_');
  250. if (goal.length == 2) {
  251. let goal_building = goal[0];
  252. let goal_level = parseInt(goal[1]);
  253. let realBuildingLevl = getCurrentLevelOfBuilding(goal_building);
  254.  
  255. //Ziel erreicht?
  256. if (realBuildingLevl >= goal_level) {
  257. continue;
  258. }
  259. let cont = false;
  260. //Suche in den baubaren Gebäuden
  261. for (let item of buildableBuildings) {
  262. if (item.id.indexOf("main_buildlink_" + goal_building) != -1) {
  263. if (item.id.replace(/^\D+/g, '') <= goal_level) {
  264. next = item.id;
  265. found = true;
  266. break;
  267. } else {
  268. cont = true;
  269. break;
  270. }
  271. }
  272. }
  273. if (cont) {
  274. continue;
  275. }
  276. if (found == false || next != false) {
  277. break;
  278. }
  279. }
  280. }
  281. }
  282.  
  283. } else {
  284.  
  285. if (!IS_BUILDING_BH_ONLY) {
  286. if (next == false) {
  287. let found = false;
  288. let buildingQueue = document.getElementsByClassName("buildorder_storage")
  289. if (buildingQueue.length != 0) {
  290. found = true;
  291. }
  292. if (found == false) {
  293. next = getStorageBuilding(villageObject.ironLvl, villageObject.storageLvl);
  294. }
  295. }
  296. if (next == false) {
  297. next = getMineBuilding(villageObject.stoneLvl, villageObject.woodLvl, villageObject.ironLvl);
  298. }
  299. }
  300. }
  301. var found;
  302. console.log("Choose next Building:" + next)
  303. if (next != "") {
  304. var nextBuilding = document.getElementById(next);
  305.  
  306. if (nextBuilding != null) {
  307. var isVisible = nextBuilding.offsetWidth > 0 || nextBuilding.offsetHeight > 0;
  308. if (isVisible) {
  309. found = nextBuilding;
  310. }
  311. }
  312. }
  313. localStorage.setItem(villageObject.id, JSON.stringify(villageObject));
  314. return found;
  315. }
  316.  
  317. function getCurrentLevelOfBuilding(buildingString) {
  318. let element = document.getElementById("main_buildrow_" + buildingString).children[0].children[3].innerText;
  319. return parseInt(element.replace(/^\D+/g, ''));
  320. }
  321.  
  322.  
  323.  
  324. function getPopulation(villageObject) {
  325. villageObject.populationCurrent = parseInt(document.getElementById("pop_current_label").innerText);
  326. villageObject.populationMax = parseInt(document.getElementById("pop_max_label").innerText);
  327. }
  328.  
  329. function getStorageBuilding(iron, store) {
  330. iron = iron - 1;
  331. store = store - 1;
  332. if (iron < 12) var build = false;
  333. else if (iron >= 12 && iron <= 18 && (iron - store) >= 7) {
  334. build = true;
  335. } else if (iron >= 19 && iron <= 24 && (iron - store) >= 6) {
  336. build = true;
  337. } else if (iron >= 25 && iron <= 27 && (iron - store) >= 5) {
  338. build = true;
  339. }
  340. if (build) {
  341. return "main_buildlink_storage_" + (store + 1);
  342. }
  343. return false;
  344. }
  345.  
  346. function getMineBuilding(stone, wood, iron) {
  347. if (stone > wood) {
  348. var next = "main_buildlink_wood_" + wood;
  349. } else if (iron + 3 < stone) {
  350. next = "main_buildlink_iron_" + iron;
  351. } else {
  352. next = "main_buildlink_stone_" + stone;
  353. }
  354. return next;
  355. }
  356.  
  357. //Recruit
  358.  
  359. function recruitTroops() {
  360. var village = JSON.parse(localStorage.getItem(localStorage.currentVillageId));
  361. if (village.spear > 0) {
  362. if (DEBUG) console.log("Recruit spear")
  363. doRecruitTroops("spear");
  364. }
  365. if (village.sword > 0) {
  366. if (DEBUG) console.log("Recruit sword")
  367. doRecruitTroops("sword");
  368. }
  369. if (village.axe > 0) {
  370. if (DEBUG) console.log("Recruit axe")
  371. doRecruitTroops("axe");
  372. logSteps();
  373. }
  374. var button = document.getElementsByClassName("btn-recruit")[0];
  375. button.click();
  376. }
  377.  
  378. function doRecruitTroops(troopstring) {
  379. var str = "unit_sprite unit_sprite_smaller " + troopstring;
  380. var village = JSON.parse(localStorage.getItem(localStorage.currentVillageId));
  381. var existingTroop = document.getElementById(troopstring + "_0_interaction").parentNode.previousElementSibling.innerText;
  382. if (parseInt(existingTroop.split('/')[1]) > Reflect.get(village, troopstring)) {
  383. return;
  384. }
  385. var queue = document.getElementsByClassName(str);
  386. var htmlelement = document.getElementById(troopstring + "_0_a");
  387. if (htmlelement != null) {
  388. var maxTroop = parseInt(htmlelement.innerHTML.replace(/^\D+/g, ''));
  389. var recruitTotal = 0;
  390. for (var item of queue) {
  391. recruitTotal = recruitTotal + parseInt(item.parentElement.innerText.replace(/^\D+/g, ''));
  392. }
  393. if (recruitTotal < TROOP_QUEUEPOOL) {
  394. var recruitCount = TROOP_QUEUEPOOL - recruitTotal;
  395. if (maxTroop > recruitCount) {
  396. document.getElementById(troopstring + "_0").value = recruitCount;
  397.  
  398. }
  399.  
  400. }
  401. }
  402. }
  403.  
  404. function recruitKavs() {
  405. var village = JSON.parse(localStorage.getItem(localStorage.currentVillageId));
  406. if (village.spy > 0) {
  407. if (DEBUG) console.log("Recruit spy")
  408. doRecruitTroops("spy");
  409. }
  410. if (village.light > 0) {
  411. if (DEBUG) console.log("Recruit light")
  412. doRecruitTroops("light");
  413. }
  414. if (village.heavy > 0) {
  415. if (DEBUG) console.log("Recruit heavy")
  416. doRecruitTroops("heavy");
  417. logSteps();
  418. }
  419. var button = document.getElementsByClassName("btn-recruit")[0];
  420. button.click();
  421. }
  422.  
  423. function recruitCar() {
  424. var village = JSON.parse(localStorage.getItem(localStorage.currentVillageId));
  425. if (village.ram > 0) {
  426. if (DEBUG) console.log("Recruit ram")
  427. doRecruitTroops("ram");
  428. }
  429. if (village.catapult > 0) {
  430. if (DEBUG) console.log("Recruit catapult")
  431. doRecruitTroops("catapult");
  432. }
  433.  
  434. var button = document.getElementsByClassName("btn-recruit")[0];
  435. button.click();
  436. }
  437.  
  438. //Util
  439. function login() {
  440. if (document.getElementById("login_form")) {
  441. document.getElementById("user").value = USERNAME;
  442. document.getElementById("password").valie = PASSWORD;
  443. if (document.getElementById("remember-me").checked == false) {
  444. document.getElementById("remember-me").checked = true;
  445. }
  446. document.getElementsByClassName("btn-login")[0].click();
  447. } else {
  448. var links = document.getElementsByClassName("world-select");
  449. for (let i = 0; links.length - 1; i++) {
  450. if (links[i].href == "https://www.die-staemme.de/page/play/" + URL_PREFIX) {
  451. links[i].click();
  452. }
  453. }
  454. }
  455. }
  456.  
  457. function getDelay() {
  458. return Math.floor(Math.random() * (MAX_WAIT_TIME - MIN_WAIT_TIME) + MIN_WAIT_TIME);
  459. }
  460.  
  461. function getEndDelay() {
  462. return Math.floor(Math.random() * (10 * 1000) + SCRIPT_FINAL_PAUSE_TIME);
  463. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement