Kalish / RaidingIncome

// ==UserScript==
// @name         RaidingIncome
// @description  Raiding Income
// @copyright    Kalish, 2020
// @namespace    http://tampermonkey.net/
// @version      1.0
// @license      MIT
// @author       Kalish
// @match        https://*.crownofthegods.com/
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    const TropaCarga = {Guard:0,Warship:3000,Druid:10,Priestess:10,Horseman:15,Scorpion:0,Sorcerer:5,Ballista:0,Scout:0,Praetor:20,Ranger:10,Arbalist:15,Ram:0,Vanquisher:10,Stinger:1500,Senator:0,Galley:1000,Triari:20},
          TropaType = ["Guard","Ballista","Ranger","Triari","Priestess","Vanquisher","Sorcerer", "Scout","Arbalist","Praetor","Horseman","Druid","Ram","Scorpion","Galley","Stinger","Warship"],
          RaidLoote = [0,400,1000,4500,15000,33000,60000,120000,201000,300000,446000],
          VelTropas = {Scout:8,Horseman:10,Druid:10,Praetor:10,Arbalist:10,Vanquisher:20,Sorcerer:20,Ranger:20,Triari:20,Priestess:20,Ram:30,Ballista:30,Scorpion:30,Senator:40,Stinger:5,Galley:5,Warship:5},
          ResearchValores = [0,1,3,6,10,15,20,25,30,35,40,45,50];
    $('#warCounc').append('<button id="CheckResHora" style="right:6%;margin-top:95px;width:150px;height:30px;font-size:12px;position:absolute;" class="regButton greenb">Raiding Income</button>');
    function StatusResPorHora() {
        const SIResPorHora = [" ", " k", " m", " b", " t", " p", " e"];
        let ResPorHoraCid,ResPorHora,TotalResPorHora=0,NumRaids=0,NumRaidsCid=0,ResVel,ContResPorHora=new Array(60).fill(0),ContStringPorHora="",tierSI,NContResPorHora=new Array(60).fill(0);
        fetch("overview/graid.php").then(response => response.json()).then(raids => {
            raids.a.forEach(city => {
                ResPorHora=0;
                city[12].forEach(raid => {
                    ResVel=100;
                    raid[5].forEach(TrRaidando => {
                        if([2,3,4,5,6].includes(TrRaidando.tt)) ResVel = Math.min(ResVel,ResearchValores[cotg.player.research()[8]]);
                        else if([8,9,10,11].includes(TrRaidando.tt)) ResVel = Math.min(ResVel,ResearchValores[cotg.player.research()[9]]);
                        else if([14,15,16].includes(TrRaidando.tt)) ResVel = Math.min(ResVel,ResearchValores[cotg.player.research()[13]]);
                        ResPorHora += (1 > TropaCarga[TropaType[TrRaidando.tt]]*TrRaidando.tv/RaidLoote[raid[2].match(/(?<=l\s).+?(?=\s\()/g)]/(2-raid[2].match(/(?<=\().+?(?=\%)/g)/100)) ? TropaCarga[TropaType[TrRaidando.tt]]*TrRaidando.tv/(Math.sqrt(Math.pow(((raid[8]-raid[8]%65536)/65536-(city[0]-city[0]%65536)/65536),2)+Math.pow((raid[8]%65536-city[0]%65536),2))*VelTropas[TropaType[TrRaidando.tt]]/(1+ResVel/100)*2+120)*60 : RaidLoote[raid[2].match(/(?<=vel\s).+?(?=\s\()/g)]/(2-raid[2].match(/(?<=\().+?(?=\%)/g)/100)/(Math.sqrt(Math.pow(((raid[8]-raid[8]%65536)/65536-(city[0]-city[0]%65536)/65536),2)+Math.pow((raid[8]%65536-city[0]%65536),2))*VelTropas[TropaType[TrRaidando.tt]]/(1+ResVel/100)*2+120)*60;
                    });
                    NumRaids++;
                    NContResPorHora[Number(city[2].split(" ")[1])]++;
                    if(Number($("#cityDropdownMenu option:selected").val()) == city[0]) NumRaidsCid++;
                });
                ResPorHoraCid = (Number($("#cityDropdownMenu option:selected").val()) == city[0]) ? ResPorHora.toFixed()+" R/h" : ResPorHoraCid;
                TotalResPorHora += (!isNaN(ResPorHora)) ? ResPorHora: 0;
                ContResPorHora[Number(city[2].split(" ")[1])] += (!isNaN(ResPorHora)) ? ResPorHora: 0;
            });
            tierSI = Math.log10(TotalResPorHora) / 3 | 0;
            ResPorHora = (tierSI != 0) ? (TotalResPorHora/Math.pow(10,tierSI*3)).toFixed(2)+SIResPorHora[tierSI]+"R/h" : TotalResPorHora.toFixed(2)+" R/h";
            ContResPorHora.forEach((cont,ncont) => {
                if (cont != 0) {
                    tierSI = Math.log10(cont) / 3 | 0;
                    ContStringPorHora += (tierSI != 0) ? "<p>C"+ncont+" = "+(cont/Math.pow(10,tierSI*3)).toFixed(2)+SIResPorHora[tierSI]+"R/h with "+NContResPorHora[ncont]+" raids ("+Math.round(cont/TotalResPorHora*100)+"%)</p>" : "<p>C"+ncont+" = "+cont.toFixed(2)+" R/h with "+NContResPorHora[ncont]+" raids ("+Math.round(cont/TotalResPorHora*100)+"%)</p>";
                }
            });
            $("#RaidPorHoraCheck").html("<p>Total = "+ResPorHora+" with "+NumRaids+" raids</p>"+ContStringPorHora+"<p>Current City ("+$('#cityDropdownMenu option:selected').val()%65536+":"+(Number($('#cityDropdownMenu option:selected').val())-$('#cityDropdownMenu option:selected').val()%65536)/65536+") = "+ResPorHoraCid+" with "+NumRaidsCid+" raids</p>");
            $("#CheckResHoraBox").height((ContResPorHora.filter(r => {return r != 0;}).length*20+200)+"px");
        });
    }
    $("#CheckResHora").click(() => {
        if(!$("#CheckResHoraBox").length) {
            $("body").append(`
<div id="CheckResHoraBox" style="width:320px;height:200px;background-color:#E2CBAC;-moz-border-radius:10px;-webkit-border-radius:10px;border-radius:10px;border:4px ridge #DAA520;position:absolute;right:100px;top:100px;z-index:1000000;" class="ui-draggable">
<div class="popUpBar ui-draggable-handle" style="margin-top:0px;">
<span class="smallredheading" style="margin-left:10px;margin-top:10px;font-size:x-large;">Resources per hour</span>
<button id="councillorXbutton" class="xbutton" onclick="$('#CheckResHoraBox').remove();" style="margin-top:0px;margin-right:10px;"><div id="xbuttondiv"><div><div id="centxbuttondiv"></div></div></div></button></div>
<div id="returnbody" class="popUpWindow"><span class="tooltipstered">
<button class="greenb" id="UpdateResPerH" style="border-radius:6px;width:298px;">Update</button>
<br/><br/><p id="RaidPorHoraCheck" class="smallredheading" style="color:black;">Analysing...</p></span></div></div>`);
            $("#CheckResHoraBox").draggable({handle:".popUpBar",containment:"window",scroll: false}).resizable();
            $("#UpdateResPerH").click(StatusResPorHora);
            StatusResPorHora();
        }});
})();