Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name DS 164
- // @version 1.2
- // @description Tribal wars bot
- // @match https://de164.die-staemme.de/*
- // @grant none
- // ==/UserScript==
- //____________________________________SETUP______________________________________
- //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
- const DEBUG = true;
- const URL_PREFIX = "de164"; //[[ANPASSEN]], Weltnummer (zB de164, de165 usw.)
- 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
- const MIN_WAIT_TIME = 8000;
- const MAX_WAIT_TIME = 15000;
- 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
- const IS_BUILDING_ACTIVE = true; // [[ANPASSEN]]Minenpushen aktivieren? BH/Speicher wird automatisch ausgebaut
- const IS_RECRUIT_ACTIVE = true; // [[ANPASSEN]] Truppen rekrutieren?
- 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!
- const IS_BUILDING_GOALS_ONLY = true;
- const TROOP_QUEUEPOOL = 5; //[[ANPASSEN]] Wieviele Einheiten sollen zu jeder Zeit in der Bauschleife sein. Je niedriger desto mehr Resis sind frei zum bauern.
- const USERNAME = "Zerker"; //[[ANPASSEN]] Wird nur im Fall eines autologouts benögit
- const PASSWORD = "asdf"; //[[ANPASSEN]] Wird nur im Fall eines autologouts benögit
- //Konfiguration der Ziel-Truppenstärke pro Dorf, für ein weiteres Dorf, alles von localStorage bis })); kopieren, einfügen und ändern
- //Dorf Id anpassen!
- localStorage.setItem("4411", JSON.stringify({ //[[ANPASSEN]] Dorf-ID (siehe Oben) einfügen!
- spear: 0,
- sword: 0,
- axe: 0,
- spy: 0,
- light: 0,
- heavy: 0,
- ram: 0,
- catapult: 0,
- snob: 0
- }));
- var goals = [];
- // Building Queue due to Quests
- queue.push("main_20");
- queue.push("barracks_15");
- queue.push("wall_10");
- //_________________________________________________________________________________
- //Mainfunction
- (function () {
- 'use strict';
- console.log("--Bob nimmt die Arbeit auf--");
- getCurrentView();
- setTimeout(function () {
- console.log("The end is nigh")
- goToOverview();
- }, getEndDelay());
- })();
- function logSteps() {
- if (DEBUG) {
- console.log("Step: " + localStorage.lastStep);
- }
- }
- function getCurrentView() {
- var item = OWN_VILLAGE_IDS[0];
- var village = JSON.parse(localStorage.getItem(item));
- village.id = item;
- localStorage.setItem(item, JSON.stringify(village));
- localStorage.setItem("currentVillageId", item)
- let currentUrl = window.location.href;
- if (currentUrl.endsWith("overview")) {
- console.log("CurrentView is overview");
- if (localStorage.lastStep == "garage") {
- goToMain();
- } else if (localStorage.lastStep == "main") {
- if (IS_RECRUIT_ACTIVE == true) {
- goToBarracks();
- } else {
- localStorage.lastStep = "barracks";
- goToOverview();
- }
- } else if (localStorage.lastStep == "barracks") {
- if (IS_RECRUIT_ACTIVE == true) {
- goToStable();
- } else {
- localStorage.lastStep = "stable";
- goToOverview();
- }
- } else if (localStorage.lastStep == "stable") {
- if (IS_RECRUIT_ACTIVE == true) {
- goToGarage();
- } else {
- localStorage.lastStep = "garage";
- goToOverview();
- }
- } else {
- localStorage.lastStep = "garage"
- goToOverview();
- }
- } else if (currentUrl.endsWith("main")) {
- localStorage.lastStep = "main";
- if (IS_BUILDING_ACTIVE) {
- logSteps()
- buildNextBuilding();
- }
- } else if (currentUrl.endsWith("barracks")) {
- localStorage.lastStep = "barracks";
- if (IS_RECRUIT_ACTIVE) {
- if (IS_RECRUIT_ACTIVE) {
- logSteps()
- recruitTroops();
- }
- }
- } else if (currentUrl.endsWith("stable")) {
- localStorage.lastStep = "stable";
- if (IS_RECRUIT_ACTIVE) {
- logSteps()
- recruitKavs();
- }
- } else if (currentUrl.endsWith("garage")) {
- localStorage.lastStep = "garage";
- if (IS_RECRUIT_ACTIVE) {
- logSteps()
- recruitCar();
- }
- } else if (currentUrl.endsWith("logout-successful")) {
- goToMain();
- } else if (currentUrl.endsWith("/")) {
- console.log("CurrentView is login");
- login();
- } else {
- goToOverview();
- }
- }
- //Goto Functions
- function goToOverview() {
- if (DEBUG) console.log("goToOverview");
- if (document.getElementsByClassName("btn - confirm - yes")[0] != undefined) {
- document.getElementsByClassName("btn - confirm - yes")[0].click();
- }
- changeUrl("overview");
- }
- function goToMain() {
- if (needToChangeUrl("main")) {
- changeUrl("main");
- }
- }
- function goToBarracks() {
- if (needToChangeUrl("barracks")) {
- changeUrl("barracks");
- }
- }
- function goToStable() {
- if (needToChangeUrl("stable")) {
- changeUrl("stable");
- }
- }
- function goToGarage() {
- if (needToChangeUrl("garage")) {
- changeUrl("garage");
- }
- }
- function needToChangeUrl(building) {
- if (window.location.href.endsWith(building)) {
- return false;
- }
- return true;
- }
- function changeUrl(building) {
- var url = "https://" + URL_PREFIX + ".die-staemme.de/game.php?village=" + localStorage.currentVillageId + "&screen=" + building;
- window.open(url, "_self");
- }
- //Build
- function buildNextBuilding() {
- if (DEBUG) console.log("buildNextBuilding");
- var nextBuildingElement = getNextBuildingElement();
- if (nextBuildingElement !== undefined) {
- nextBuildingElement.click();
- console.log("Now Building:" + nextBuildingElement);
- }
- setTimeout(console.log("wait time"), getDelay());
- }
- function getNextBuildingElement() {
- if (DEBUG) console.log("getNextBuildingElement");
- var buildableBuildings = document.getElementsByClassName("btn btn-build");
- //var buildingElementsQueue = getBuildingElementsQueue();
- var villageId = localStorage.currentVillageId;
- var villageObject = JSON.parse(localStorage.getItem(villageId));
- console.log(villageObject.id);
- for (let item of buildableBuildings) {
- if (item.id.indexOf("wood") != -1) {
- villageObject.woodLvl = item.id.replace(/^\D+/g, '');
- } else if (item.id.indexOf("stone") != -1) {
- villageObject.stoneLvl = item.id.replace(/^\D+/g, '');
- } else if (item.id.indexOf("iron") != -1) {
- villageObject.ironLvl = item.id.replace(/^\D+/g, '');
- } else if (item.id.indexOf("storage") != -1) {
- villageObject.storageLvl = item.id.replace(/^\D+/g, '');
- } else if (item.id.indexOf("farm") != -1) {
- villageObject.farmLvl = item.id.replace(/^\D+/g, '');
- }
- }
- getPopulation(villageObject);
- localStorage.setItem(localStorage.currentVillageId, villageObject);
- var next = false;
- if (IS_BUILDING_GOALS_ONLY) {
- let found = false;
- if (goals.length > 0) {
- while (goals.length > 0 || found) {
- let nextGoal = goals.shift();
- let goal = nextGoal.split('_');
- if (goal.length == 2) {
- let goal_building = goal[0];
- let goal_level = parseInt(goal[1]);
- let realBuildingLevl = getCurrentLevelOfBuilding(goal_building);
- //Ziel erreicht?
- if (realBuildingLevl >= goal_level) {
- continue;
- }
- let cont = false;
- //Suche in den baubaren Gebäuden
- for (let item of buildableBuildings) {
- if (item.id.indexOf("main_buildlink_" + goal_building) != -1) {
- if (item.id.replace(/^\D+/g, '') <= goal_level) {
- next = item.id;
- found = true;
- break;
- } else {
- cont = true;
- break;
- }
- }
- }
- if (cont) {
- continue;
- }
- if (found == false || next != false) {
- break;
- }
- }
- }
- }
- } else {
- if (((villageObject.populationCurrent / villageObject.populationMax) * 100) > 80) {
- let found = false;
- let buildingQueue = document.getElementsByClassName("buildorder_farm")
- if (buildingQueue.length != 0) {
- found = true;
- }
- if (found == false) {
- next = "main_buildlink_farm_" + villageObject.farmLvl;
- }
- }
- if (!IS_BUILDING_BH_ONLY) {
- if (next == false) {
- let found = false;
- let buildingQueue = document.getElementsByClassName("buildorder_storage")
- if (buildingQueue.length != 0) {
- found = true;
- }
- if (found == false) {
- next = getStorageBuilding(villageObject.ironLvl, villageObject.storageLvl);
- }
- }
- if (next == false) {
- next = getMineBuilding(villageObject.stoneLvl, villageObject.woodLvl, villageObject.ironLvl);
- }
- }
- }
- var found;
- console.log("Choose next Building:" + next)
- if (next != "") {
- var nextBuilding = document.getElementById(next);
- if (nextBuilding != null) {
- var isVisible = nextBuilding.offsetWidth > 0 || nextBuilding.offsetHeight > 0;
- if (isVisible) {
- found = nextBuilding;
- }
- }
- }
- localStorage.setItem(villageObject.id, JSON.stringify(villageObject));
- return found;
- }
- function getCurrentLevelOfBuilding(buildingString) {
- let element = document.getElementById("main_buildrow_" + buildingString).children[0].children[3].innerText;
- return parseInt(element.replace(/^\D+/g, ''));
- }
- function getPopulation(villageObject) {
- villageObject.populationCurrent = parseInt(document.getElementById("pop_current_label").innerText);
- villageObject.populationMax = parseInt(document.getElementById("pop_max_label").innerText);
- }
- function getStorageBuilding(iron, store) {
- iron = iron - 1;
- store = store - 1;
- if (iron < 12) var build = false;
- else if (iron >= 12 && iron <= 18 && (iron - store) >= 7) {
- build = true;
- } else if (iron >= 19 && iron <= 24 && (iron - store) >= 6) {
- build = true;
- } else if (iron >= 25 && iron <= 27 && (iron - store) >= 5) {
- build = true;
- }
- if (build) {
- return "main_buildlink_storage_" + (store + 1);
- }
- return false;
- }
- function getMineBuilding(stone, wood, iron) {
- if (stone > wood) {
- var next = "main_buildlink_wood_" + wood;
- } else if (iron + 3 < stone) {
- next = "main_buildlink_iron_" + iron;
- } else {
- next = "main_buildlink_stone_" + stone;
- }
- return next;
- }
- //Recruit
- function recruitTroops() {
- var village = JSON.parse(localStorage.getItem(localStorage.currentVillageId));
- if (village.spear > 0) {
- if (DEBUG) console.log("Recruit spear")
- doRecruitTroops("spear");
- }
- if (village.sword > 0) {
- if (DEBUG) console.log("Recruit sword")
- doRecruitTroops("sword");
- }
- if (village.axe > 0) {
- if (DEBUG) console.log("Recruit axe")
- doRecruitTroops("axe");
- logSteps();
- }
- var button = document.getElementsByClassName("btn-recruit")[0];
- button.click();
- }
- function doRecruitTroops(troopstring) {
- var str = "unit_sprite unit_sprite_smaller " + troopstring;
- var village = JSON.parse(localStorage.getItem(localStorage.currentVillageId));
- var existingTroop = document.getElementById(troopstring + "_0_interaction").parentNode.previousElementSibling.innerText;
- if (parseInt(existingTroop.split('/')[1]) > Reflect.get(village, troopstring)) {
- return;
- }
- var queue = document.getElementsByClassName(str);
- var htmlelement = document.getElementById(troopstring + "_0_a");
- if (htmlelement != null) {
- var maxTroop = parseInt(htmlelement.innerHTML.replace(/^\D+/g, ''));
- var recruitTotal = 0;
- for (var item of queue) {
- recruitTotal = recruitTotal + parseInt(item.parentElement.innerText.replace(/^\D+/g, ''));
- }
- if (recruitTotal < TROOP_QUEUEPOOL) {
- var recruitCount = TROOP_QUEUEPOOL - recruitTotal;
- if (maxTroop > recruitCount) {
- document.getElementById(troopstring + "_0").value = recruitCount;
- }
- }
- }
- }
- function recruitKavs() {
- var village = JSON.parse(localStorage.getItem(localStorage.currentVillageId));
- if (village.spy > 0) {
- if (DEBUG) console.log("Recruit spy")
- doRecruitTroops("spy");
- }
- if (village.light > 0) {
- if (DEBUG) console.log("Recruit light")
- doRecruitTroops("light");
- }
- if (village.heavy > 0) {
- if (DEBUG) console.log("Recruit heavy")
- doRecruitTroops("heavy");
- logSteps();
- }
- var button = document.getElementsByClassName("btn-recruit")[0];
- button.click();
- }
- function recruitCar() {
- var village = JSON.parse(localStorage.getItem(localStorage.currentVillageId));
- if (village.ram > 0) {
- if (DEBUG) console.log("Recruit ram")
- doRecruitTroops("ram");
- }
- if (village.catapult > 0) {
- if (DEBUG) console.log("Recruit catapult")
- doRecruitTroops("catapult");
- }
- var button = document.getElementsByClassName("btn-recruit")[0];
- button.click();
- }
- //Util
- function login() {
- if (document.getElementById("login_form")) {
- document.getElementById("user").value = USERNAME;
- document.getElementById("password").valie = PASSWORD;
- if (document.getElementById("remember-me").checked == false) {
- document.getElementById("remember-me").checked = true;
- }
- document.getElementsByClassName("btn-login")[0].click();
- } else {
- var links = document.getElementsByClassName("world-select");
- for (let i = 0; links.length - 1; i++) {
- if (links[i].href == "https://www.die-staemme.de/page/play/" + URL_PREFIX) {
- links[i].click();
- }
- }
- }
- }
- function getDelay() {
- return Math.floor(Math.random() * (MAX_WAIT_TIME - MIN_WAIT_TIME) + MIN_WAIT_TIME);
- }
- function getEndDelay() {
- return Math.floor(Math.random() * (10 * 1000) + SCRIPT_FINAL_PAUSE_TIME);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement