Travian marketplace calculator

This script is used for calculating incoming grain in Travian Legends game

当前为 2024-03-27 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Travian marketplace calculator
// @namespace    https://*.travian.com/*
// @version      1.005
// @description  This script is used for calculating incoming grain in Travian Legends game
// @author       Marko Miljkovic
// @match        https://*.travian.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

var parentDiv = document.createElement("div")
parentDiv.style.position = "fixed"
parentDiv.style.top = "50px"
parentDiv.style.left = "50px"
parentDiv.style.zIndex = "9999"
document.body.append(parentDiv)
 
 
var button = document.createElement("button");
button.textContent = "Calculate incoming crops";
 
// Set button styles
button.style.position = "fixed";
button.style.top = "50px";
button.style.left = "50px";
button.style.padding = "10px";
button.style.backgroundColor = "blue";
button.style.color = "white";
button.style.border = "none";
button.style.cursor = "pointer";
 
button.addEventListener("click", function() {
    calculateIncomingCrops(); // Don't forget to call the function
});

var button = document.createElement("button");
button.textContent = "Calculate incoming crops sum";
 
// Set button styles
button.style.position = "fixed";
button.style.top = "100px";
button.style.left = "50px";
button.style.padding = "10px";
button.style.backgroundColor = "blue";
button.style.color = "white";
button.style.border = "none";
button.style.cursor = "pointer";
 
button.addEventListener("click", function() {
    calculateIncomingCropsSum(); // Don't forget to call the function
});
 
// Append the button to the body element
parentDiv.appendChild(button);
 
var button = document.createElement("button");
button.textContent = "Calculate troops consumption";
 
// Set button styles
button.style.position = "fixed";
button.style.top = "150px";
button.style.left = "50px";
button.style.padding = "10px";
button.style.backgroundColor = "blue";
button.style.color = "white";
button.style.border = "none";
button.style.cursor = "pointer";
 
button.addEventListener("click", function() {
    calculateTroopsConsumption(); // Don't forget to call the function
});
 
// Append the button to the body element
parentDiv.appendChild(button);
 
 
function calculateIncomingCrops() {
    var routeDivs = document.querySelectorAll('div.routes div.route');
    var hashMap = {};
    
    routeDivs.forEach(function(routeDiv) {
        var keyElement = routeDiv.querySelector('div.routeHeader div.otherVillage a[href^="/profile/"]');
        var key = keyElement ? keyElement.textContent.trim() : null;
        
        var sumValue = 0;
        var valueElements = routeDiv.querySelectorAll('div.delivery.current.transport');
        
        valueElements.forEach(function(valueElement) {
            var values = valueElement.querySelectorAll('span.value');
            if (values.length >= 4) { // Make sure there are enough values
                var valueString = values[3].textContent.trim().replace('.', '').replace(/\D/g,'');
                var value = parseInt(valueString);
                if (!isNaN(value)) { // Check if value is a valid number
                    sumValue += value;
                }
            }
            
            var hourElements = valueElement.querySelectorAll('div.arriveInAt span.time');
            if (hourElements.length > 0) { // Make sure there are enough elements
                var hour = hourElements[0].textContent.trim().split(":")[0];
                var newKey = key + " : " + hour;
                if (key !== null) {
                    if (!hashMap[newKey]) {
                        hashMap[newKey] = 0;
                    }
                    hashMap[newKey] += sumValue;
                }
            }
        });
    });
    
    // Sort the keys alphabetically
    var sortedKeys = Object.keys(hashMap).sort();
 
    var output = "";
    sortedKeys.forEach(function(key) {
        output += key + " : " + hashMap[key] + "\n";
    });
 
    // Open a popup window to display the output
    var popup = window.open("", "Popup", "width=400,height=300");
    popup.document.write("<pre>" + output + "</pre>");
}

 
function calculateIncomingCropsSum() {
    var routeDivs = document.querySelectorAll('div.routes div.route');
    var hashMap = {};
    
    routeDivs.forEach(function(routeDiv) {
        var keyElement = routeDiv.querySelector('div.routeHeader div.otherVillage a[href^="/profile/"]');
        var key = keyElement ? keyElement.textContent.trim() : null;
        
        var sumValue = 0;
        var valueElements = routeDiv.querySelectorAll('div.delivery.current.transport');
        
        valueElements.forEach(function(valueElement) {
            var values = valueElement.querySelectorAll('span.value');
            if (values.length >= 4) { // Make sure there are enough values
                var valueString = values[3].textContent.trim().replace('.', '').replace(/\D/g,'');
                var value = parseInt(valueString);
                if (!isNaN(value)) { // Check if value is a valid number
                    sumValue += value;
                }
            }
            
            var hourElements = valueElement.querySelectorAll('div.arriveInAt span.time');
            if (hourElements.length > 0) { // Make sure there are enough elements
                var hour = hourElements[0].textContent.trim().split(":")[0];
                if (key !== null) {
                    if (!hashMap[hour]) {
                        hashMap[hour] = 0;
                    }
                    hashMap[hour] += sumValue;
                }
            }
        });
    });
    
    // Sort the keys alphabetically
    var sortedKeys = Object.keys(hashMap).sort();
 
    var output = "";
    sortedKeys.forEach(function(key) {
        output += key + " : " + hashMap[key] + "\n";
    });
 
    // Open a popup window to display the output
    var popup = window.open("", "Popup", "width=400,height=300");
    popup.document.write("<pre>" + output + "</pre>");
}
 
function calculateTroopsConsumption(){
    var elements = document.querySelectorAll('table.troop_details');
 
     var hashMap = {};
 
    elements.forEach(function(container){
        var player_name = container.getAttribute('data-player-name');
        var valueElements = container.querySelectorAll('.value');
        if(hashMap[player_name] == null){
            var element = valueElements[0];
            if(element != null){
                var value = parseInt(element.textContent.trim().replace('.', '').replace(/\D/g,''));
            hashMap[player_name] = value
            }
        } else {
            var element = valueElements[0];
            if(element != null){
                var value = parseInt(element.textContent.trim().replace('.', '').replace(/\D/g,''));
            hashMap[player_name] += value
            }
        } 
    });
       // Sort the keys alphabetically
    var sortedKeys = Object.keys(hashMap).sort();
 
    var output = "";
    sortedKeys.forEach(function(key) {
        output += key + " : " + hashMap[key] + "\n";
    });
 
    // Open a popup window to display the output
    var popup = window.open("", "Popup", "width=400,height=300");
    popup.document.write("<pre>" + output + "</pre>");
}