kikyalex / TWReports++

// ==UserScript==
// @name         TWReports++
// @namespace    http://tampermonkey.net/
// @version      0.5.1
// @description  Still in beta!
// @author       Kiky
// @match        https://*.triburile.ro/game.php?*screen=report*
// @match        https://*.triburile.ro/public_report/*
// @require      https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js
// @grant        none
// @updateURL    https://openuserjs.org/install/kikyalex/TWReports++.user.js
// @downloadURL  https://openuserjs.org/install/kikyalex/TWReports++.user.js
// @encoding utf-8
// ==/UserScript==


(function() {
    'use strict';
    console.log("TWReports++ is on");
    $(document).on('click', ".printReport", function() {
        event.preventDefault();
        var self = $(this);
        self.hide();
        var table = self.closest('table');
        $.each(table.find('img'), function(i,e) { var tmp = $('<div></div>').css(
            {
                'background': 'center no-repeat url(' + $(e).attr('src') + ')',
                'width' : $(e).width(),
                'height' : $(e).height(),
                'margin-left' : "6px",
                'display' : "inline-block"
            }).attr({
            'class' : $(e).attr('class')
        }); $(e).replaceWith(tmp); });

        var scaledElement = table.clone().css({
            'transform': 'scale(1)',
            '-ms-transform': 'scale(1)',
            '-webkit-transform': 'scale(1)',
        }).addClass("scaled-element");
        $("body").append(scaledElement); // add the element to body
        //scaledElement.css('float', 'right');
        html2canvas(scaledElement, {
            allowTaint: true,
            onrendered: function(canvas) {
                var newWin = window.open('','');
                newWin.document.body.appendChild(canvas);
                self.show();
                $(".scaled1-element").remove(); // removed when rendering is done
            }
        });
    });

    var oda = 0;
    var odd = 0;
    var od = 0;
    var odaRes = [0, 0, 0, 0];
    var oddRes = [0, 0, 0, 0];
    var trupeRes = [
        50,		30,		10,      1,//lance
        30,		30, 	70,      1, //sabie
        60,  	30,     40,      1, //topor
        100,  	30,  	60,      1, //arc
        50, 	50, 	20,      2,//spion
        125,   100,    250,      4, //cav usoara
        250,   100,    150,      5, //arcas calare
        200,   150,    600,      6, //cav grea
        300,   200,    200,      5, //berbec
        320,   400,    100,      8, //catapulta
        20,     20,     40,      1, //paladn
        40000,  50000,  50000,  100,// GN
        0,       0,      0,      0 // garda din ferma
    ];
    function fetch(x)
    {
        x = $("table:has(th:contains('" + x + "')):last table td:contains('Pierderi:')").parent().html();
        return (x == null) ? [] : x.match(/\d+/g);
    }



    var al = fetch('Agresor:').slice(1);
    var dl = fetch('Apărător:').slice(1);
    var l = al.length > 0 ? al.length : dl.length-1;
    switch (l)
    {
        case 9:
            oda = dl[0] * 4 + dl[1] * 5 + dl[2] * 1 + dl[3] * 1 + dl[4] * 5 + dl[5] * 23 + dl[6] * 4 + dl[7] * 200;
            odd = al[0] * 1 + al[1] * 2 + al[2] * 4 + al[3] * 2 + al[4] * 13 + al[5] * 15 + al[6] * 8 + al[7] * 200;
            break;
        case 10:
            oda = dl[0] * 4 + dl[1] * 5 + dl[2] * 1 + dl[3] * 1 + dl[4] * 5 + dl[5] * 23 + dl[6] * 4 + dl[7] * 12 + dl[8] * 200;
            odd = al[0] * 1 + al[1] * 2 + al[2] * 4 + al[3] * 2 + al[4] * 13 + al[5] * 15 + al[6] * 8 + al[7] * 10 + al[8] * 200;
            break;
        case 11:
            oda = dl[0] * 4 + dl[1] * 5 + dl[2] * 1 + dl[3] * 5 + dl[4] * 1 +  dl[5] * 5 + dl[6] * 6 + dl[7] * 23 + dl[8] * 4 + dl[9] * 12 + dl[10] *  200;
            odd = al[0] * 1 + al[1] * 2 + al[2] * 4 + al[3] * 2 + al[4] * 2 +  al[5] * 13 + al[6] * 12 + al[7] * 15 + al[8] * 8 + al[9] * 10 + al[10] *  200;
            break;
        case 12:
            oda = dl[0] * 4 + dl[1] * 5 + dl[2] * 1 + dl[3] * 5 + dl[4] * 1 +  dl[5] * 5 + dl[6] * 6 + dl[7] * 23 + dl[8] * 4 + dl[9] * 12 + dl[10] *  40 + dl[11] * 200;
            odd = al[0] * 1 + al[1] * 2 + al[2] * 4 + al[3] * 2 + al[4] * 2 +  al[5] * 13 + al[6] * 12 + al[7] * 15 + al[8] * 8 + al[9] * 10 + al[10] *  20 + al[11] * 200;

            $.each(al, function(i,e) {
                odaRes[0] += e * trupeRes[4*i];
                odaRes[1] += e * trupeRes[4*i+1];
                odaRes[2] += e * trupeRes[4*i+2];
                odaRes[3] += e * trupeRes[4*i+3];
            });

            $.each(dl, function(i,e) {
                oddRes[0] += e * trupeRes[4*i];
                oddRes[1] += e * trupeRes[4*i+1];
                oddRes[2] += e * trupeRes[4*i+2];
                oddRes[3] += e * trupeRes[4*i+3];
            });
            break;
    }
    od = oda + odd;

    oda = isNaN(oda) ? "?" : oda;
    odd = isNaN(odd) ? "?" : odd;
    od = isNaN(od) ? "?" : od;
    if($('.report_ReportAttack h3').length > 0)
    {
        $("<h4 class='customOd'><table><tr><td>Total OD</td><td> " + od + "</td></tr><tr><td>ODA</td><td> " + oda + "</td></tr><tr><td>ODD</td><td> " + odd+"</td></tr></table></h4>").insertAfter($('.report_ReportAttack h3'));
    }
    else
    {
        $("<h4 class='customOd'><table><tr><td>Total OD</td><td> " + od + "</td></tr><tr><td>ODA</td><td> " + oda + "</td></tr><tr><td>ODD</td><td> " + odd+"</td></tr></table></h4>").insertAfter($('#attack_luck'));
    }

    $("<details close><summary>Arata resurse <i>irosite</i></summary><table style='text-align:right;'><tr><td><b>Atacator: </b></td><td><span class='icon header wood'></span></td><td>"+odaRes[0]+"</td><td><span class='icon header stone'></span></td><td>"+odaRes[1]+"</td><td><span class='icon header iron'></span></td><td>"+odaRes[2]+"</td><td><span class='icon header population'></span></td><td>"+odaRes[3]+"</td></tr><tr><td><b>Aparator: </b></td><td><span class='icon header wood'></span></td><td>"+oddRes[0]+"</td><td><span class='icon header stone'></span></td><td>"+oddRes[1]+"</td><td><span class='icon header iron'></span></td><td>"+oddRes[2]+"</td><td><span class='icon header population'></span></td><td>"+oddRes[3]+"</td></tr></table></details><br/>").insertAfter($('.customOd'));

    $("<a href='#' class='printReport' style='float:right; margin-top:5px;'>Print</button>").insertBefore($('.customOd'));

})();