Travian marketplace calculator

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

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

// ==UserScript==
// @name         Travian marketplace calculator
// @namespace    https://*.travian.com/*
// @version      1.003
// @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 button = document.createElement("button");
button.textContent = "Calculate incoming";

// 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.style.zIndex = "9999";

button.addEventListener("click", function() {
    calculateData(); // Don't forget to call the function
});

// Append the button to the body element
document.body.appendChild(button);


function calculateData() {
    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>");
}