Commands Overview: Outgoing Command Renamer

Discussion in 'Scripts & independent tools' started by Ramsay Bolton, Dec 29, 2017.

Share This Page

  1. Ramsay Bolton

    Ramsay Bolton New Member

    Joined:
    Nov 28, 2017
    Messages:
    15
    Likes Received:
    1
    The current approved ones are all broken. This script does the same and is legal on tribalwars.net.

    Could it get looked at for approval please? @dabookman

    What is does:
    Renames outgoing commands to fake, nuke, farm etc. This allows for quick filtering of your reports and commands.

    Code:
    javascript:$.getScript('c); void 0 
    Code:
    javascript: /*21-Dec-2014*/ var labels = {
        " ***NOBLE": {
            "min-snob": 1
        },
        " Fake-ram": {
            "min-ram": 1,
            "max-ram": 1
        },
        " Fake-cat": {
            "min-catapult": 1,
            "max-catapult": 1
        },
        " SCOUT": {
            "min-spy": 20
        },
        " Farm": {
            "min-pop": 15,
            "max-pop": 1999
        },
        " ***NUKE": {
            "min-pop": 2000
        }
    };
    $.each(labels, function(name, criteria) {
        $.each(criteria, function(type, amount) {
            if (!type.match("min") && !type.match("max")) {
                alert("label error at: (" + type + ": " + amount + ")");
                void(o);
            }
        });
    });
    var url = document.URL;
    var destination = "&screen=overview_villages&mode=commands";
    var iMode = url.indexOf(destination);
    if (iMode == -1) {
        var keyUrl = url.indexOf("village=") + 8;
        var valUrl = parseInt(url.substr(keyUrl)).toString().length;
        var baseUrl = url.substr(0, keyUrl + valUrl);
        document.location.href = baseUrl + destination;
    } else {
        var label = null;
        var name = "";
        var AssessTroop = {
            available: {
                "spear": false,
                "sword": false,
                "axe": false,
                "archer": false,
                "spy": false,
                "light": false,
                "marcher": false,
                "heavy": false,
                "ram": false,
                "catapult": false,
                "knight": false,
                "snob": false
            },
            availableAmount: {
                "spear": 0,
                "sword": 0,
                "axe": 0,
                "archer": 0,
                "spy": 0,
                "light": 0,
                "marcher": 0,
                "heavy": 0,
                "ram": 0,
                "catapult": 0,
                "knight": 0,
                "snob": 0
            },
            unitIndex: ["spear", "sword", "axe", "archer", "spy", "light", "marcher", "heavy", "ram", "catapult", "knight", "snob"],
            popAmount: {
                "spear": 1,
                "sword": 1,
                "axe": 1,
                "archer": 1,
                "spy": 2,
                "light": 4,
                "marcher": 5,
                "heavy": 6,
                "ram": 5,
                "catapult": 8,
                "knight": 10,
                "snob": 100
            },
            label: "",
            labels: labels,
            assessAvailibility: function(types) {
                $.each(types, function(i, type) {
                    AssessTroop.available[type.src.match(/unit_(.*).png/)[1]] = true;
                });
                $.each(AssessTroop.available, function(key, value) {
                    if (!value) {
                        AssessTroop.unitIndex.splice(AssessTroop.unitIndex.indexOf(key), 1);
                        delete AssessTroop.availableAmount[key];
                    }
                });
            },
            assessTroop: function(troops) {
                AssessTroop.storeTroopAmount(troops);
                AssessTroop.matchTroopAmountWithLabels();
                if (AssessTroop.label.match("NUKE")) {
                    AssessTroop.assessNukePower();
                }
                AssessTroop.insertTroopAmount();
                return AssessTroop.label;
            },
            assessNukePower: function() {
                AssessTroop.label = AssessTroop.label.concat(" " + Math.min((AssessTroop.totalPop() * 100 / 20500).toFixed(2), 100).toString() + "%");
            },
            insertTroopAmount: function() {
                AssessTroop.label = AssessTroop.label.concat(" (");
                $.each(AssessTroop.availableAmount, function(unit, amount) {
                    if (amount > 0) {
                        AssessTroop.label = AssessTroop.label.concat(" " + unit);
                        AssessTroop.label = AssessTroop.label.concat(":" + amount);
                    }
                });
                AssessTroop.label = AssessTroop.label.concat(" )");
            },
            storeTroopAmount: function(troops) {
                $.each(troops, function(i, troop) {
                    AssessTroop.availableAmount[AssessTroop.unitIndex[i]] = parseInt(troop.innerHTML);
                });
            },
            matchTroopAmountWithLabels: function() {
                $.each(labels, function(name, criteria) {
                    if (AssessTroop.troopMatch(criteria)) {
                        AssessTroop.label = AssessTroop.label.concat(name);
                    }
                });
            },
            troopMatch: function(criteria) {
                var flag = true;
                $.each(criteria, function(type, amount) {
                    if (type.match("min")) {
                        if (!AssessTroop.checkForMin(type, amount)) {
                            flag = false;
                        }
                    } else {
                        if (!AssessTroop.checkForMax(type, amount)) {
                            flag = false;
                        }
                    }
                });
                return flag;
            },
            checkForMin: function(type, amount) {
                type = type.match(/-(.*)/)[1];
                switch (type) {
                    case "unit":
                        return AssessTroop.totalUnit() >= amount;
                    case "pop":
                        return AssessTroop.totalPop() >= amount;
                    default:
                        return AssessTroop.availableAmount[type] >= amount;
                }
            },
            checkForMax: function(type, amount) {
                type = type.match(/-(.*)/)[1];
                switch (type) {
                    case "unit":
                        return AssessTroop.totalUnit() <= amount;
                    case "pop":
                        return AssessTroop.totalPop() <= amount;
                    default:
                        return AssessTroop.availableAmount[type] <= amount;
                }
            },
            totalPop: function() {
                var total = 0;
                $.each(AssessTroop.availableAmount, function(key, value) {
                    total += value * AssessTroop.popAmount[key];
                });
                return total;
            },
            totalUnit: function() {
                var total = 0;
                $.each(AssessTroop.availableAmount, function(key, value) {
                    total += value;
                });
                return total;
            },
            resetLabel: function() {
                AssessTroop.label = "";
            }
        };
        AssessTroop.assessAvailibility($("#commands_table img[alt][title]"));
        $.each($("#commands_table .nowrap"), function(i, command) {
            setTimeout(function(){        label = command.querySelector(".quickedit-label");
            if (label.innerHTML.match("Attack on")) {
                name = AssessTroop.assessTroop(command.querySelectorAll(".unit-item"));
                label.innerHTML = " " + name;
                command.querySelector(".rename-icon").click();
                command.querySelector(".btn").click();
                AssessTroop.resetLabel();
            }},130*i)
    
        });
    };
    void(o);