Advertisement
Guest User

Untitled

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