Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name DS Bob
- // @version 1.0
- // @description Tribal wars bot
- // @match https://de164.die-staemme.de/*
- // @grant none
- // ==/UserScript==
- //____________________________________SETUP______________________________________
- //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
- const DEBUG = true;
- const URL_PREFIX = "de164";
- const OWN_VILLAGE_IDS = ["4411"]; //Es können mehrere Dörfer abgearbeitet werden = ["12464","123123","38045"];
- const MIN_WAIT_TIME = 8000;
- const MAX_WAIT_TIME = 15000;
- const SCRIPT_FINAL_PAUSE_TIME = 1 * 1000;
- const IS_BUILDING_ACTIVE = true;
- const IS_RECRUIT_ACTIVE = true;
- const IS_BUILDING_BH_ONLY = true;
- const TROOP_QUEUEPOOL = 5;
- const USERNAME = "Legoslav"; //Wird nur im Fall eines autologouts benögit
- const PASSWORD = "k4ntholz"; //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({
- spear: 0,
- sword: 0,
- axe: 1000,
- spy: 5,
- light: 5,
- heavy: 0,
- ram: 0,
- catapult: 0,
- snob: 0
- }));
- //_________________________________________________________________________________
- //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 == "stable") {
- if (IS_BUILDING_ACTIVE) {
- goToMain();
- } else {
- goToOverview();
- }
- } 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 {
- localStorage.lastStep = "stable"
- 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("logout-successful")) {
- goToMain();
- } else if (currentUrl.endsWith("/")) {
- console.log("CurrentView is login");
- login();
- } else {
- goToOverview();
- }
- //______
- /* OWN_VILLAGE_IDS.forEach(function (item, index, array) {
- var village = JSON.parse(localStorage.getItem(item));
- village.id = item;
- localStorage.setItem(item, JSON.stringify(village));
- localStorage.setItem("currentVillageId", item)
- console.log("Current Village:" + item);
- var currentUrl = window.location.href;
- if (IS_BUILDING_ACTIVE) {
- goToMain();
- buildNextBuilding();
- localStorage.lastStep = "Build";
- logSteps();
- } else {
- localStorage.lastStep = "Build";
- logSteps();
- }
- if (localStorage.lastStep == "Build") {
- if (IS_RECRUIT_ACTIVE) {
- localStorage.lastStep = "Barracks_Start";
- logSteps();
- goToBarracks();
- recruitTroops();
- } else {
- localStorage.lastStep = "Barracks_End";
- logSteps();
- }
- }
- if (localStorage.lastStep == "Barracks_End") {
- if (IS_RECRUIT_ACTIVE) {
- localStorage.lastStep = "Stable_Start";
- logSteps();
- goToStable();
- recruitKavs();
- } else {
- localStorage.lastStep = "Stable_End";
- logSteps();
- }
- }
- });*/
- }
- //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.villageId + "&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 (var 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 (((villageObject.populationCurrent / villageObject.populationMax) * 100) > 80) {
- let found = false;
- var 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;
- var 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 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")
- doRecruitKav("spy");
- }
- if (village.light > 0) {
- if (DEBUG) console.log("Recruit light")
- doRecruitKav("light");
- }
- if (village.heavy > 0) {
- if (DEBUG) console.log("Recruit heavy")
- doRecruitKav("heavy");
- logSteps();
- }
- var button = document.getElementsByClassName("btn-recruit")[0];
- button.click();
- }
- function doRecruitKav(troopstring) {
- var str = "unit_sprite unit_sprite_smaller " + troopstring;
- var queue = document.getElementsByClassName(str);
- 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 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;
- }
- }
- }
- }
- //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