Advertisement
Guest User

Untitled

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