necromunger / Tribal Wars AI

// ==UserScript==
// @name         Tribal Wars AI
// @namespace    http://your.homepage/
// @version      0.1
// @description  AI that will play tribal wars
// @author       You
// @match        http://en78.tribalwars.net/*
// @grant        none
// ==/UserScript==

function Building (name, level, time, popUse, materials, element) {
    this.name = name;
    this.level = level;
    this.time = time;
    this.popUse = popUse;
    this.materials = {wood: materials.wood, stone: materials.stone, iron: materials.iron};
    this.element = element;
    
    if (this.name == "Timber camp" || this.name == "Clay pit" || this.name == "Iron mine") {
        this.reasource = true;
    }
    else {
        this.reasource = false;
    }
}

var display = $("<div style='position: fixed;top: 40%;width: 200px;height: 200px;background-color: #F3E6C1;border: solid 5px #8C4524;padding: 10px;'></div>").appendTo("body");

window.setInterval(function () {
    //remove the shit
	$("a[id*='cheap']").remove();
    
    if ($('#buildings').html() !== undefined) {
        if ($('#build_queue').html() === undefined) {
            
            var aryBuildings = [];
            
            $('#buildings').children().children("tr[id*='main']").each(function (index, value) {
                var name = $(this).children().first().children("a").first().children().first().prop('title')
                
                var level = $(this).children().first().children("span").html();
                if (level.indexOf("Level") > -1) {
                    level = parseInt(level.replace('Level ',''));
                }
                
                var time = $(this).children().eq(4).text();
                var popUse = $(this).children().eq(5).text().replace(' ','');
                
                var woodCost = $(this).children(".cost_wood").data('cost');
                var stoneCost = $(this).children(".cost_stone").data('cost');
                var ironCost = $(this).children(".cost_iron").data('cost');
                
                var materials = {wood: woodCost, stone: stoneCost, iron: ironCost};
                
                var element = $(this).children().last().children().first();
                
                if ($(this).children().eq(1).text() != "Building fully constructed") {
                    aryBuildings.push(new Building(name, level, time, popUse, materials, element));
                }
            });
            
            //make reasources more valueble
            aryBuildings.forEach(function (value, index){
                if (value.reasource === true) {
                    value.level -= 6;
                }
                else if (value.name == "Warehouse") {
                    value.level -= 3;
                }
            });
            
            //build reasource nodes
            var lowestReasource = 100;
            var selectedReasource = null;
            var breakOut = false;
            aryBuildings.forEach(function (value, index){
                if (breakOut === false) {
                    if (value.level < lowestReasource ){//&& value.reasource == true) {
                        lowestReasource = value.level;
                        selectedReasource = value;
                    }
                }
            });
            
            //build anything not built
            aryBuildings.forEach(function (value, index){
                if (value.level == "(not constructed)") {
                    selectedReasource = value;
                }
            });
            
            $(selectedReasource.element).trigger('click');
            $(display).html("Target Building: " + selectedReasource.name);
        }
    }
    
    
    if ($('#units_form').html() !== undefined) {
        var  attackCoords = [];
        //attackCoords.push({x: 358,y: 537});
        
        var main = $('#content_value');
        var templates = $(main).children().eq(6).children("table").children().children().find("a");
        
        if (($(main).data('setForce') === undefined || $(main).data('setForce') === false) && attackCoords.length > 0) {
            var templatesActual = [];
            templates.each(function (index, value) {
                if ($(value).hasClass("troop_template_selector")) {
                    templatesActual.push(value);
                }
            });
            
            //select attack force
            $(templatesActual[0]).click();
            
            //set attack location
            $(".target-input-field, .target-input-autocomplete, .ui-autocomplete-input").val(attackCoords[0].x + "|" + attackCoords[0].y);
            
            $(main).data('setForce', true);
            $(display).html("I'm trying to attack location: " + attackCoords[0].x + "|" + attackCoords[0].y + " With Army: " + $(templatesActual[0]).text());
        }
        
        if ($(main).data('setForce') === true && $("#content_value").children().length == 8) {
            $("#target_attack").trigger("click");
        }
    }
    
    if ($('#troop_confirm_go') !== undefined) {
        $('#troop_confirm_go').trigger("click");
    }
}, 1000);