Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.50 KB | None | 0 0
  1. // ==UserScript==
  2. // @name DS 164
  3. // @version 1.2
  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;
  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. var goals = [];
  44. // Building Queue due to Quests
  45. queue.push("main_20");
  46. queue.push("barracks_15");
  47. queue.push("wall_10");
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. //_________________________________________________________________________________
  55.  
  56.  
  57. //Mainfunction
  58. (function () {
  59. 'use strict';
  60. console.log("--Bob nimmt die Arbeit auf--");
  61. getCurrentView();
  62. setTimeout(function () {
  63. console.log("The end is nigh")
  64. goToOverview();
  65. }, getEndDelay());
  66. })();
  67.  
  68. function logSteps() {
  69. if (DEBUG) {
  70. console.log("Step: " + localStorage.lastStep);
  71. }
  72. }
  73.  
  74. function getCurrentView() {
  75. var item = OWN_VILLAGE_IDS[0];
  76. var village = JSON.parse(localStorage.getItem(item));
  77. village.id = item;
  78. localStorage.setItem(item, JSON.stringify(village));
  79. localStorage.setItem("currentVillageId", item)
  80.  
  81. let currentUrl = window.location.href;
  82. if (currentUrl.endsWith("overview")) {
  83. console.log("CurrentView is overview");
  84. if (localStorage.lastStep == "garage") {
  85. goToMain();
  86. } else if (localStorage.lastStep == "main") {
  87. if (IS_RECRUIT_ACTIVE == true) {
  88. goToBarracks();
  89. } else {
  90. localStorage.lastStep = "barracks";
  91. goToOverview();
  92. }
  93. } else if (localStorage.lastStep == "barracks") {
  94. if (IS_RECRUIT_ACTIVE == true) {
  95. goToStable();
  96. } else {
  97. localStorage.lastStep = "stable";
  98. goToOverview();
  99. }
  100.  
  101. } else if (localStorage.lastStep == "stable") {
  102. if (IS_RECRUIT_ACTIVE == true) {
  103. goToGarage();
  104. } else {
  105. localStorage.lastStep = "garage";
  106. goToOverview();
  107. }
  108. } else {
  109. localStorage.lastStep = "garage"
  110. goToOverview();
  111. }
  112. } else if (currentUrl.endsWith("main")) {
  113. localStorage.lastStep = "main";
  114. if (IS_BUILDING_ACTIVE) {
  115. logSteps()
  116. buildNextBuilding();
  117. }
  118. } else if (currentUrl.endsWith("barracks")) {
  119. localStorage.lastStep = "barracks";
  120. if (IS_RECRUIT_ACTIVE) {
  121. if (IS_RECRUIT_ACTIVE) {
  122. logSteps()
  123. recruitTroops();
  124. }
  125. }
  126. } else if (currentUrl.endsWith("stable")) {
  127. localStorage.lastStep = "stable";
  128. if (IS_RECRUIT_ACTIVE) {
  129. logSteps()
  130. recruitKavs();
  131. }
  132. } else if (currentUrl.endsWith("garage")) {
  133. localStorage.lastStep = "garage";
  134. if (IS_RECRUIT_ACTIVE) {
  135. logSteps()
  136. recruitCar();
  137. }
  138. } else if (currentUrl.endsWith("logout-successful")) {
  139. goToMain();
  140. } else if (currentUrl.endsWith("/")) {
  141. console.log("CurrentView is login");
  142. login();
  143. } else {
  144. goToOverview();
  145. }
  146. }
  147.  
  148. //Goto Functions
  149. function goToOverview() {
  150. if (DEBUG) console.log("goToOverview");
  151. if (document.getElementsByClassName("btn - confirm - yes")[0] != undefined) {
  152. document.getElementsByClassName("btn - confirm - yes")[0].click();
  153. }
  154. changeUrl("overview");
  155. }
  156.  
  157. function goToMain() {
  158. if (needToChangeUrl("main")) {
  159. changeUrl("main");
  160. }
  161. }
  162.  
  163. function goToBarracks() {
  164. if (needToChangeUrl("barracks")) {
  165. changeUrl("barracks");
  166. }
  167. }
  168.  
  169. function goToStable() {
  170. if (needToChangeUrl("stable")) {
  171. changeUrl("stable");
  172. }
  173. }
  174.  
  175. function goToGarage() {
  176. if (needToChangeUrl("garage")) {
  177. changeUrl("garage");
  178. }
  179. }
  180.  
  181. function needToChangeUrl(building) {
  182. if (window.location.href.endsWith(building)) {
  183. return false;
  184. }
  185. return true;
  186. }
  187.  
  188. function changeUrl(building) {
  189. var url = "https://" + URL_PREFIX + ".die-staemme.de/game.php?village=" + localStorage.currentVillageId + "&screen=" + building;
  190. window.open(url, "_self");
  191. }
  192.  
  193.  
  194.  
  195. //Build
  196. function buildNextBuilding() {
  197. if (DEBUG) console.log("buildNextBuilding");
  198.  
  199. var nextBuildingElement = getNextBuildingElement();
  200. if (nextBuildingElement !== undefined) {
  201. nextBuildingElement.click();
  202. console.log("Now Building:" + nextBuildingElement);
  203. }
  204.  
  205. setTimeout(console.log("wait time"), getDelay());
  206. }
  207.  
  208. function getNextBuildingElement() {
  209. if (DEBUG) console.log("getNextBuildingElement");
  210. var buildableBuildings = document.getElementsByClassName("btn btn-build");
  211. //var buildingElementsQueue = getBuildingElementsQueue();
  212. var villageId = localStorage.currentVillageId;
  213. var villageObject = JSON.parse(localStorage.getItem(villageId));
  214. console.log(villageObject.id);
  215. for (let item of buildableBuildings) {
  216. if (item.id.indexOf("wood") != -1) {
  217. villageObject.woodLvl = item.id.replace(/^\D+/g, '');
  218. } else if (item.id.indexOf("stone") != -1) {
  219. villageObject.stoneLvl = item.id.replace(/^\D+/g, '');
  220. } else if (item.id.indexOf("iron") != -1) {
  221. villageObject.ironLvl = item.id.replace(/^\D+/g, '');
  222. } else if (item.id.indexOf("storage") != -1) {
  223. villageObject.storageLvl = item.id.replace(/^\D+/g, '');
  224. } else if (item.id.indexOf("farm") != -1) {
  225. villageObject.farmLvl = item.id.replace(/^\D+/g, '');
  226. }
  227. }
  228. getPopulation(villageObject);
  229.  
  230. localStorage.setItem(localStorage.currentVillageId, villageObject);
  231. var next = false;
  232. if (IS_BUILDING_GOALS_ONLY) {
  233. let found = false;
  234. if (goals.length > 0) {
  235. while (goals.length > 0 || found) {
  236. let nextGoal = goals.shift();
  237. let goal = nextGoal.split('_');
  238. if (goal.length == 2) {
  239. let goal_building = goal[0];
  240. let goal_level = parseInt(goal[1]);
  241. let realBuildingLevl = getCurrentLevelOfBuilding(goal_building);
  242.  
  243. //Ziel erreicht?
  244. if (realBuildingLevl >= goal_level) {
  245. continue;
  246. }
  247. let cont = false;
  248. //Suche in den baubaren Gebäuden
  249. for (let item of buildableBuildings) {
  250. if (item.id.indexOf("main_buildlink_" + goal_building) != -1) {
  251. if (item.id.replace(/^\D+/g, '') <= goal_level) {
  252. next = item.id;
  253. found = true;
  254. break;
  255. } else {
  256. cont = true;
  257. break;
  258. }
  259. }
  260. }
  261. if (cont) {
  262. continue;
  263. }
  264. if (found == false || next != false) {
  265. break;
  266. }
  267. }
  268. }
  269. }
  270.  
  271. } else {
  272.  
  273. if (((villageObject.populationCurrent / villageObject.populationMax) * 100) > 80) {
  274. let found = false;
  275. let buildingQueue = document.getElementsByClassName("buildorder_farm")
  276. if (buildingQueue.length != 0) {
  277. found = true;
  278. }
  279. if (found == false) {
  280. next = "main_buildlink_farm_" + villageObject.farmLvl;
  281. }
  282. }
  283. if (!IS_BUILDING_BH_ONLY) {
  284. if (next == false) {
  285. let found = false;
  286. let buildingQueue = document.getElementsByClassName("buildorder_storage")
  287. if (buildingQueue.length != 0) {
  288. found = true;
  289. }
  290. if (found == false) {
  291. next = getStorageBuilding(villageObject.ironLvl, villageObject.storageLvl);
  292. }
  293. }
  294. if (next == false) {
  295. next = getMineBuilding(villageObject.stoneLvl, villageObject.woodLvl, villageObject.ironLvl);
  296. }
  297. }
  298. }
  299. var found;
  300. console.log("Choose next Building:" + next)
  301. if (next != "") {
  302. var nextBuilding = document.getElementById(next);
  303.  
  304. if (nextBuilding != null) {
  305. var isVisible = nextBuilding.offsetWidth > 0 || nextBuilding.offsetHeight > 0;
  306. if (isVisible) {
  307. found = nextBuilding;
  308. }
  309. }
  310. }
  311. localStorage.setItem(villageObject.id, JSON.stringify(villageObject));
  312. return found;
  313. }
  314.  
  315. function getCurrentLevelOfBuilding(buildingString) {
  316. let element = document.getElementById("main_buildrow_" + buildingString).children[0].children[3].innerText;
  317. return parseInt(element.replace(/^\D+/g, ''));
  318. }
  319.  
  320.  
  321.  
  322. function getPopulation(villageObject) {
  323. villageObject.populationCurrent = parseInt(document.getElementById("pop_current_label").innerText);
  324. villageObject.populationMax = parseInt(document.getElementById("pop_max_label").innerText);
  325. }
  326.  
  327. function getStorageBuilding(iron, store) {
  328. iron = iron - 1;
  329. store = store - 1;
  330. if (iron < 12) var build = false;
  331. else if (iron >= 12 && iron <= 18 && (iron - store) >= 7) {
  332. build = true;
  333. } else if (iron >= 19 && iron <= 24 && (iron - store) >= 6) {
  334. build = true;
  335. } else if (iron >= 25 && iron <= 27 && (iron - store) >= 5) {
  336. build = true;
  337. }
  338. if (build) {
  339. return "main_buildlink_storage_" + (store + 1);
  340. }
  341. return false;
  342. }
  343.  
  344. function getMineBuilding(stone, wood, iron) {
  345. if (stone > wood) {
  346. var next = "main_buildlink_wood_" + wood;
  347. } else if (iron + 3 < stone) {
  348. next = "main_buildlink_iron_" + iron;
  349. } else {
  350. next = "main_buildlink_stone_" + stone;
  351. }
  352. return next;
  353. }
  354.  
  355. //Recruit
  356.  
  357. function recruitTroops() {
  358. var village = JSON.parse(localStorage.getItem(localStorage.currentVillageId));
  359. if (village.spear > 0) {
  360. if (DEBUG) console.log("Recruit spear")
  361. doRecruitTroops("spear");
  362. }
  363. if (village.sword > 0) {
  364. if (DEBUG) console.log("Recruit sword")
  365. doRecruitTroops("sword");
  366. }
  367. if (village.axe > 0) {
  368. if (DEBUG) console.log("Recruit axe")
  369. doRecruitTroops("axe");
  370. logSteps();
  371. }
  372. var button = document.getElementsByClassName("btn-recruit")[0];
  373. button.click();
  374. }
  375.  
  376. function doRecruitTroops(troopstring) {
  377. var str = "unit_sprite unit_sprite_smaller " + troopstring;
  378. var village = JSON.parse(localStorage.getItem(localStorage.currentVillageId));
  379. var existingTroop = document.getElementById(troopstring + "_0_interaction").parentNode.previousElementSibling.innerText;
  380. if (parseInt(existingTroop.split('/')[1]) > Reflect.get(village, troopstring)) {
  381. return;
  382. }
  383. var queue = document.getElementsByClassName(str);
  384. var htmlelement = document.getElementById(troopstring + "_0_a");
  385. if (htmlelement != null) {
  386. var maxTroop = parseInt(htmlelement.innerHTML.replace(/^\D+/g, ''));
  387. var recruitTotal = 0;
  388. for (var item of queue) {
  389. recruitTotal = recruitTotal + parseInt(item.parentElement.innerText.replace(/^\D+/g, ''));
  390. }
  391. if (recruitTotal < TROOP_QUEUEPOOL) {
  392. var recruitCount = TROOP_QUEUEPOOL - recruitTotal;
  393. if (maxTroop > recruitCount) {
  394. document.getElementById(troopstring + "_0").value = recruitCount;
  395.  
  396. }
  397.  
  398. }
  399. }
  400. }
  401.  
  402. function recruitKavs() {
  403. var village = JSON.parse(localStorage.getItem(localStorage.currentVillageId));
  404. if (village.spy > 0) {
  405. if (DEBUG) console.log("Recruit spy")
  406. doRecruitTroops("spy");
  407. }
  408. if (village.light > 0) {
  409. if (DEBUG) console.log("Recruit light")
  410. doRecruitTroops("light");
  411. }
  412. if (village.heavy > 0) {
  413. if (DEBUG) console.log("Recruit heavy")
  414. doRecruitTroops("heavy");
  415. logSteps();
  416. }
  417. var button = document.getElementsByClassName("btn-recruit")[0];
  418. button.click();
  419. }
  420.  
  421. function recruitCar() {
  422. var village = JSON.parse(localStorage.getItem(localStorage.currentVillageId));
  423. if (village.ram > 0) {
  424. if (DEBUG) console.log("Recruit ram")
  425. doRecruitTroops("ram");
  426. }
  427. if (village.catapult > 0) {
  428. if (DEBUG) console.log("Recruit catapult")
  429. doRecruitTroops("catapult");
  430. }
  431.  
  432. var button = document.getElementsByClassName("btn-recruit")[0];
  433. button.click();
  434. }
  435.  
  436. //Util
  437. function login() {
  438. if (document.getElementById("login_form")) {
  439. document.getElementById("user").value = USERNAME;
  440. document.getElementById("password").valie = PASSWORD;
  441. if (document.getElementById("remember-me").checked == false) {
  442. document.getElementById("remember-me").checked = true;
  443. }
  444. document.getElementsByClassName("btn-login")[0].click();
  445. } else {
  446. var links = document.getElementsByClassName("world-select");
  447. for (let i = 0; links.length - 1; i++) {
  448. if (links[i].href == "https://www.die-staemme.de/page/play/" + URL_PREFIX) {
  449. links[i].click();
  450. }
  451. }
  452. }
  453. }
  454.  
  455. function getDelay() {
  456. return Math.floor(Math.random() * (MAX_WAIT_TIME - MIN_WAIT_TIME) + MIN_WAIT_TIME);
  457. }
  458.  
  459. function getEndDelay() {
  460. return Math.floor(Math.random() * (10 * 1000) + SCRIPT_FINAL_PAUSE_TIME);
  461. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement