Automate buying resources in Tribal Wars
当前为
// ==UserScript==
// @name Tribal Wars Resource Buyer
// @namespace http://tampermonkey.net/
// @version 0.8
// @description Automate buying resources in Tribal Wars
// @author ricardofauch
// @match https://*.die-staemme.de/game.php?village=*&screen=market&mode=exchange
// @license MIT
// ==/UserScript==
(function() {
'use strict';
let isReloadNeeded = false;
function checkForUsageWarningAndReload() {
const warningText = "Es sieht so aus, als würdest du die Premium-Börse im Moment zu stark nutzen. Bitte versuche es in Kürze erneut.";
const pageText = document.body.textContent || document.body.innerText;
if (pageText.includes(warningText)) {
console.log("Usage warning detected. Reloading page.");
window.location.reload();
}
}
function checkForErrorAndReload() {
const errorImageSrc = 'https://dsde.innogamescdn.com/asset/525cce89/graphic/error.png';
const errorImages = document.querySelectorAll(`img[src="${errorImageSrc}"]`);
if (errorImages.length > 0) {
//console.log("Error detected ('Kein ausreichender Vorrat im Depot'). Reloading page.");
window.location.reload();
}
}
function clickBuyButton() {
const buyButton = document.querySelector('.btn-premium-exchange-buy');
if (buyButton) {
//console.log("Clicking the 'Bestes Angebot berechnen' button.");
buyButton.click();
} else {
//console.log("'Bestes Angebot berechnen' button not found.");
}
}
function clickConfirmButton() {
let counter = 0;
const checkDialog = setInterval(function() {
const confirmDialog = document.querySelector('.confirmation-box');
if (confirmDialog && confirmDialog.style.display !== 'none') {
clearInterval(checkDialog);
//console.log("Confirmation dialog detected.");
const confirmButton = document.querySelector('.btn-confirm-yes');
if (confirmButton) {
//console.log("Clicking the 'Bestätigen' button.");
confirmButton.click();
setTimeout(function(){checkAndBuyResources();}, 100)
} else {
//console.log("'Bestätigen' button not found.");
window.location.reload();
}
}
}, 20); // Check every 500ms
}
function checkAndBuyResources() {
checkForUsageWarningAndReload(); // Check for the warning before executing main logic
// checkForErrorAndReload();
console.log("ckecking...")
//if (isReloadNeeded) {
//console.log("Reloading the page for fresh data...");
//window.location.reload();
//}
const purchasePercentage = 0.7; // 50% for example
const resources = ['wood', 'stone', 'iron'];
let anyResourceAvailable = false;
for (let i = 0; i < resources.length; i++) {
const resource = resources[i];
const stock = parseInt(document.getElementById(`premium_exchange_stock_${resource}`).innerText, 10);
console.log(`Current stock for ${resource}: ${stock}`);
if (stock > 50) {
anyResourceAvailable = true;
const amountToBuy = Math.floor(stock * purchasePercentage);
console.log(`Calculated amount to buy for ${resource}: ${amountToBuy}`);
const buyInputField = document.querySelector(`input[name="buy_${resource}"]`);
if (buyInputField) {
buyInputField.value = amountToBuy;
console.log(`Set value for ${resource}: ${amountToBuy}`);
isReloadNeeded = false;
clickBuyButton();
clickConfirmButton();
break; // Exit the loop as we've found a resource to buy
} else {
console.log(`Input field for buying ${resource} not found.`);
}
}
}
isReloadNeeded = !anyResourceAvailable; // Set the flag based on resource availability
//console.log("Check if reload needed2", isReloadNeeded)
if (isReloadNeeded) {
//console.log("Reloading the page for fresh data...");
const randomInterval = Math.random() * (4000 - 50) + 50;
setTimeout(function(){window.location.reload();
}, randomInterval);
}
}
const randomIntervalCheck = Math.random() * (300 - 50) + 50;
setTimeout(function(){checkAndBuyResources();}, randomIntervalCheck)
setInterval(function() {
//console.log("Reloading page every 20 seconds.");
window.location.reload();
}, 20000); // 20000 milliseconds = 20 seconds
})();