Walkyrie / EnergyROI

// ==UserScript==
// @name         EnergyROI
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Ogame tool to determinate how is the best way to get energy!
// @author       Walkyrie
// @license   MIT
// @copyright 2019, Walkyrie (https://openuserjs.org//users/Walkyrie)
// @include     *.ogame*gameforge.com/game/index.php*
// @grant          GM_getValue
// @grant          GM_setValue
// ==/UserScript==

function et_cost(lvl){
    var r ={};
    r.m=0;
    r.c= 800*Math.pow(2,lvl)
    r.d= r.c /2
    return (r.d + r.m/2.5 + r.c/1.5).toFixed(0);
}

function sp_cost(lvl){
    var r ={};
    r.m=50*Math.pow(1.5,lvl)
    r.c= 20*Math.pow(1.5,lvl)
    r.d= 0;
    return (r.d + r.m/2.5 + r.c/1.5).toFixed(0);
}

function et_prod(lvl){
    var planets = GM_getValue("planets");
    var ids = Object.keys(planets);
    var sum = 0 ;
    for(var i=0;i<ids.length;i++){
        var p = planets[ids[i]];
        sum-= Number(f_prod(p.f_l,lvl));
        sum+= Number(f_prod(p.f_l,lvl+1));
    }
    return sum;
}

function f_prod(lvl,e_lvl){
    return (30*lvl*Math.pow(1.05+(e_lvl)*0.01,lvl)).toFixed(0);
}

function sp_prod(lvl){
    return (20*(lvl)*Math.pow(1.1,lvl)).toFixed(0);
}

function sat_sol_prod(maxTemp){
    return Math.floor((maxTemp+140)/6)
}

function num(n){
     return parseFloat(n).toLocaleString(
         undefined, // leave undefined to use the browser's locale,
             // or use a string like 'en-US' to override it.
        // { minimumFractionDigits: 2 }
     );
}

function getPlanet(){
    var planets = GM_getValue("planets");
    if(!planets){
        planets = {};
        GM_setValue("planets",planets);
    }
    var planet_id = document.head.querySelector("[name~=ogame-planet-id][content]").content;
    var planet;
    if(!planets[planet_id]){
        planet = {};
        planet.id = planet_id;
        planet.name = document.head.querySelector("[name~=ogame-planet-name][content]").content;;
        planets[planet.id] = planet;
    }
    return planets[planet_id];
}

function savePlanet(planet){
    var planets = GM_getValue("planets");
    planets[planet.id] = planet;
    GM_setValue("planets",planets);
}

function num_colons(){
    return document.getElementById("planetList").children.length
    }

(function() {
    'use strict';

    // Your code here...
    var toolsList =document.getElementById("menuTableTools");
    var li = document.createElement("li");
    li.id= "EnergyROI";
    var a = document.createElement("a");
    a.classList.add("menubutton");
    a.id="energyRoi";
    a.href="#"
    a.accessKey="";
    a.target="_self";
    var span = document.createElement("span");
    span.classList.add("textlabel");
    span.innerHTML="Energy ROI";
    toolsList.appendChild(li);
    li.appendChild(a);
    a.appendChild(span);
    a.addEventListener("click", function (event)
        {
            royTableOverView ()
        }, true)

    if(/page=research/.test(location.href)){
       pageResearch();
    }else if(/page=resources/.test(location.href)){
          setTimeout(function(){
              pageResources();
          }
          ,500)
    }else if(/page=overview/.test(location.href)){
          setTimeout(function(){
              pageOverview();
          }
          ,500)
    }

    console.log(GM_getValue("planets"));
})();

function royTableOverView(){
    var content = document.getElementById("contentWrapper");
    content.innerHTML ="";
}

function pageResearch(){
     var et = document.getElementById("base1")
        console.log(et);
        if(et!= null){
            et.onclick = function(){
                setTimeout(function(){
                    var et_description = document.getElementsByClassName("production_info")[0];
                    var level = parseInt(document.getElementById("content").getElementsByClassName("level")[0].textContent.replace( /^\D+/g, ''));
                    var li = document.createElement("li");
                    li.innerHTML = "Costo in deuterio Ammortizzato:";

                    var span = document.createElement("span");
                    li.appendChild(span);
                    et_description.appendChild(li);
                    span.classList.add("time");
                    span.innerHTML ="" + num(et_cost(level));

                    li = document.createElement("li");
                    li.innerHTML = "Energia Prodotta:";

                    span = document.createElement("span");
                    li.appendChild(span);
                    et_description.appendChild(li);
                    span.classList.add("time");
                    span.innerHTML ="" + num(et_prod(level,level+1));

                    li = document.createElement("li");
                    li.innerHTML = "Costo Unità di Energia:";

                    span = document.createElement("span");
                    li.appendChild(span);
                    et_description.appendChild(li);
                    span.classList.add("time");
                    span.innerHTML ="" + num(et_cost(level)/et_prod(level));



                },500);
            }
        }
}

function pageResources(){
    var f_b= document.getElementById("button5");
    var f_l=parseInt(f_b.getElementsByClassName("level")[0].textContent.replace( /^\D+/g, ''));

    var s_b= document.getElementById("button4");
    var s_l=parseInt(s_b.getElementsByClassName("level")[0].textContent.replace( /^\D+/g, ''));

    var planet = {};
    planet.id= document.head.querySelector("[name~=ogame-planet-id][content]").content;;
    planet.name = document.head.querySelector("[name~=ogame-planet-name][content]").content;;
    planet.f_l =f_l;
    planet.s_l = s_l;
    var planets = GM_getValue("planets");
    if(!planets){
        planets = {};
    }
    planets[planet.id] = planet;
    GM_setValue("planets",planets);
    var sp = document.getElementById("button4")
        if(sp!= null){
            sp.addEventListener("click", function (event){
                setTimeout(function(){
                    var description = document.getElementsByClassName("production_info")[0];
                    var li = document.createElement("li");
                    li.innerHTML = "Costo in deuterio:";

                    var span = document.createElement("span");
                    li.appendChild(span);
                    description.appendChild(li);
                    span.classList.add("time");
                    var cost = (sp_cost( planet.s_l+1));
                    span.innerHTML ="" + num(cost);

                    li = document.createElement("li");
                    li.innerHTML = "Energia Prodotta:";

                    span = document.createElement("span");
                    li.appendChild(span);
                    description.appendChild(li);
                    span.classList.add("time");
                    var prod = (sp_prod(planet.s_l+1)- sp_prod(planet.s_l));
                    span.innerHTML ="" +num(prod);

                    li = document.createElement("li");
                    li.innerHTML = "Costo Unità di Energia:";

                    span = document.createElement("span");
                    li.appendChild(span);
                    description.appendChild(li);
                    span.classList.add("time");
                    span.innerHTML ="" + (cost/prod).toFixed(0);

                },500);
            });
        }
}

function pageOverview(){
    var maxTemp = parseInt(document.getElementById("temperatureContentField").textContent.replace( /^\w+/g, '')) + 40;
    var planet = getPlanet();
    planet.maxTemp = maxTemp;
    planet.solar_prod = sat_sol_prod(maxTemp);
    savePlanet(planet);
    console.log(planet);
}