kaori00 / Display Total Production In Ressource Summary

// ==UserScript==
// @name         Display Total Production In Ressource Summary
// @namespace    http://tampermonkey.net/
// @version      0.5.0
// @description  Visit all planet production settings page, to display production of all planets
// @author       Kaori
// @match        https://*.ogame.gameforge.com/game/index.php?page=resourceSettings*
// @grant        none
// @run-at document-end
// @copyright 2020, kaori00 (https://openuserjs.org/users/kaori00)
// @license MIT
// @updateURL https://openuserjs.org/meta/kaori00/Display_Total_Production_In_Ressource_Summary.meta.js
// @downloadURL https://openuserjs.org/install/kaori00/Display_Total_Production_In_Ressource_Summary.user.js
// @require       https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js
// ==/UserScript==

(function() {
    //'use strict';


    class kaoProduction{
        constructor(){
            this.planetList = document.querySelectorAll('.smallplanet');
            this.storageName = 'kao-prod';

            this.current = {};
            this.current.planet = (document.querySelector('.smallplanet .active') || document.querySelector('.smallplanet .planetlink')).parentNode;
            this.current.planetId = this.current.planet.id.replace("planet-", "");
            this.current.coords = this.current.planet.querySelector('.planet-koords').textContent.slice(1, -1);
            this.current.hasMoon = this.current.planet.querySelector('.moonlink') ? true : false;
            this.current.isMoon = this.current.hasMoon && this.current.planet.querySelector('.moonlink.active') ? true : false;
            this.current.name = this.current.isMoon ? 'Moon' : this.current.planet.querySelector('.smallplanet .planet-name').textContent;

            // get data & init default values
            this.json = JSON.parse(localStorage.getItem(this.storageName)) || {};
            this.json.planetProd = this.json.planetProd || {};
            this.json.planetProd[this.current.planetId] = this.json.planetProd[this.current.planetId] || {};

            this.start();
        }
        saveData()
        {
            localStorage.setItem(this.storageName, JSON.stringify(this.json));
        }
        start(){

            //total ress
            let ressTable = document.querySelector('#inhalt > div.contentRS > div.mainRS > form > table > tbody');
            let ress = ressTable.querySelectorAll('tr.summary > td span');

            //base
            let baseTable = document.querySelector('#inhalt > div.contentRS > div.mainRS > form > table > tbody > tr:nth-child(3)');
            let base = baseTable.querySelectorAll('td span');

            //metal
            let metal = document.querySelector('#inhalt > div.contentRS > div.mainRS > form > table > tbody > tr.\\31  > td > span');
            //cristal
            let cristal = document.querySelector('#inhalt > div.contentRS > div.mainRS > form > table > tbody > tr.alt.\\32  > td > span');
            //deut
            let deut = document.querySelector('#inhalt > div.contentRS > div.mainRS > form > table > tbody > tr.\\33  > td > span');

            //plasma
            let plasmaTable = document.querySelector("#inhalt > div.contentRS > div.mainRS > form > table > tbody > tr.\\31 22");
            let plasma = plasmaTable.querySelectorAll('td span');

            //foreuse
            let foreuseTable = document.querySelector("#inhalt > div.contentRS > div.mainRS > form > table > tbody > tr.\\32 17");
            let foreuse = foreuseTable.querySelectorAll('td span');

            //objet
            let objetTable = document.querySelector("#inhalt > div.contentRS > div.mainRS > form > table > tbody > tr.\\31 000");
            let objet = objetTable.querySelectorAll('td span');

            //geologue
            let geologueTable = document.querySelector("#inhalt > div.contentRS > div.mainRS > form > table > tbody > tr.\\31 001");
            let geologue = geologueTable.querySelectorAll('td span');

            //commandement
            let commandementTable = document.querySelector("#inhalt > div.contentRS > div.mainRS > form > table > tbody > tr.\\31 003");
            let commandement = commandementTable.querySelectorAll('td span');


            //bonus alliance
            let allianceBonusTable = document.querySelector("#inhalt > div.contentRS > div.mainRS > form > table > tbody > tr.\\31 005");
            let allianceBonus = allianceBonusTable.querySelectorAll('td span');
            console.log(allianceBonusTable);
            console.log(allianceBonus);

            //bonus classe
            let classBonusTable = document.querySelector("#inhalt > div.contentRS > div.mainRS > form > table > tbody > tr.\\31 004");
            let classBonus = classBonusTable.querySelectorAll('td span');


            if(!this.current.isMoon){
                let pid = this.current.planetId;
                let metalProd = (ress && ress[0]) ? ress[0].getAttribute('title') : 0;
                let cristalProd = (ress && ress[1]) ? ress[1].getAttribute('title') : 0;
                let deutProd = (ress && ress[2]) ? ress[2].getAttribute('title') : 0;

                this.json.planetProd[pid].metal = parseInt(metalProd.replaceAll('.','')) || 0;
                this.json.planetProd[pid].cristal = parseInt(cristalProd.replaceAll('.','')) || 0;
                this.json.planetProd[pid].deut = parseInt(deutProd.replaceAll('.','')) || 0;

                //base
                let baseMetalProd = base[0].innerText;
                let baseCristalProd = base[1].innerText;

                this.json.planetProd[pid].baseMetal = parseInt(baseMetalProd.replaceAll('.','')) || 0;
                this.json.planetProd[pid].baseCristal = parseInt(baseCristalProd.replaceAll('.','')) || 0;
                this.json.planetProd[pid].baseDeut = 0;

                let mMetalProd = metal.innerText;
                let mCristalProd = cristal.innerText;
                let mDeutProd = deut.innerText;

                this.json.planetProd[pid].mm = parseInt(mMetalProd.replaceAll('.','')) || 0;
                this.json.planetProd[pid].mc = parseInt(mCristalProd.replaceAll('.','')) || 0;
                this.json.planetProd[pid].md = parseInt(mDeutProd.replaceAll('.','')) || 0;

                //plasma
                let plasmaMetalProd = plasma[0].innerText;
                let plasmaCristalProd = plasma[1].innerText;
                let plasmaDeutProd = plasma[2].innerText;

                this.json.planetProd[pid].plasmaMetal = parseInt(plasmaMetalProd.replaceAll('.','')) || 0;
                this.json.planetProd[pid].plasmaCristal = parseInt(plasmaCristalProd.replaceAll('.','')) || 0;
                this.json.planetProd[pid].plasmaDeut = parseInt(plasmaDeutProd.replaceAll('.','')) || 0;


                //foreuse
                let foreuseMetalProd = foreuse[0].innerText;
                let foreuseCristalProd = foreuse[1].innerText;
                let foreuseDeutProd = foreuse[2].innerText;

                this.json.planetProd[pid].foreuseMetal = parseInt(foreuseMetalProd.replaceAll('.','')) || 0;
                this.json.planetProd[pid].foreuseCristal = parseInt(foreuseCristalProd.replaceAll('.','')) || 0;
                this.json.planetProd[pid].foreuseDeut = parseInt(foreuseDeutProd.replaceAll('.','')) || 0;

                //objet
                let objetMetalProd = objet[0].innerText;
                let objetCristalProd = objet[1].innerText;
                let objetDeutProd = objet[2].innerText;

                this.json.planetProd[pid].objetMetal = parseInt(objetMetalProd.replaceAll('.','')) || 0;
                this.json.planetProd[pid].objetCristal = parseInt(objetCristalProd.replaceAll('.','')) || 0;
                this.json.planetProd[pid].objetDeut = parseInt(objetDeutProd.replaceAll('.','')) || 0;

                //geologue
                let geologueMetalProd = geologue[0].innerText;
                let geologueCristalProd = geologue[1].innerText;
                let geologueDeutProd = geologue[2].innerText;

                this.json.planetProd[pid].geologueMetal = parseInt(geologueMetalProd.replaceAll('.','')) || 0;
                this.json.planetProd[pid].geologueCristal = parseInt(geologueCristalProd.replaceAll('.','')) || 0;
                this.json.planetProd[pid].geologueDeut = parseInt(geologueDeutProd.replaceAll('.','')) || 0;

                //class
                let classBonusMetalProd = classBonus[0].innerText;
                let classBonusCristalProd = classBonus[1].innerText;
                let classBonusDeutProd = classBonus[2].innerText;
                console.log(classBonusMetalProd);
                console.log(classBonusCristalProd);
                console.log(classBonusDeutProd);
                this.json.planetProd[pid].classBonusMetal = parseInt(classBonusMetalProd.replaceAll('.','')) || 0;
                this.json.planetProd[pid].classBonusCristal = parseInt(classBonusCristalProd.replaceAll('.','')) || 0;
                this.json.planetProd[pid].classBonusDeut = parseInt(classBonusDeutProd.replaceAll('.','')) || 0;

                //ally
                let allianceBonusMetalProd = allianceBonus[0].innerText;
                                console.log('allianceBonusMetalProd',allianceBonusMetalProd);
                let allianceBonusCristalProd = allianceBonus[1].innerText;
                let allianceBonusDeutProd = allianceBonus[2].innerText;

                this.json.planetProd[pid].allianceBonusMetal = parseInt(allianceBonusMetalProd.replaceAll('.','')) || 0;
                console.log('allianceBonusMetalProd',allianceBonusMetalProd);
                this.json.planetProd[pid].allianceBonusCristal = parseInt(allianceBonusCristalProd.replaceAll('.','')) || 0;
                this.json.planetProd[pid].allianceBonusDeut = parseInt(allianceBonusDeutProd.replaceAll('.','')) || 0;


                //commandement
                let commandementMetalProd = commandement[0].innerText;
                let commandementCristalProd = commandement[1].innerText;
                let commandementDeutProd = commandement[2].innerText;
                this.json.planetProd[pid].commandementMetal = parseInt(commandementMetalProd.replaceAll('.','')) || 0;
                this.json.planetProd[pid].commandementCristal = parseInt(commandementCristalProd.replaceAll('.','')) || 0;
                this.json.planetProd[pid].commandementDeut = parseInt(commandementDeutProd.replaceAll('.','')) || 0;

                this.saveData();
            }
            function createNewEntry(title,metal,cristal,deut,destination){
                destination.appendChild(createNewLine('[all]'+title+' par heure :',metal,cristal,deut));
                destination.appendChild(createNewLine('[all]'+title+' par jour :',metal*24,cristal*24,deut*24));
                destination.appendChild(createNewLine('[all]'+title+' par semaine :',metal*24*7,cristal*24*7,deut*24*7));
                destination.appendChild(createNewLine('[all]'+title+' par mois :',metal*24*31,cristal*24*31,deut*24*31));
                destination.appendChild(createNewLine('[all]'+title+' par an :',metal*24*365,cristal*24*364,deut*24*365));
            }
            function createNewLine(title,metal,cristal,deut){
                metal = nFormatter(metal,2);
                cristal = nFormatter(cristal,2);
                deut = nFormatter(deut,2);
                let newTR = document.createElement('tr');
                let titleTD = createNewCol('<em>'+title+'</em>','label',2);
                let metalTD = createNewCol('<span>'+metal+'</span>','undermark',0);
                let cristalTD = createNewCol('<span>'+cristal+'</span>','undermark',0);
                let deutTD = createNewCol('<span>'+deut+'</span>','undermark',0);
                newTR.appendChild(titleTD);
                newTR.appendChild(metalTD);
                newTR.appendChild(cristalTD);
                newTR.appendChild(deutTD);
                newTR.appendChild(document.createElement('td'));
                newTR.appendChild(document.createElement('td'));
                if(!document.querySelector('#inhalt > div.contentRS > div.mainRS > form > table > tbody > tr:last-child').classList.contains('alt')){
                    newTR.classList.add('alt');
                }
                newTR.addEventListener('mouseover', function(e){this.classList.add('hover')});
                newTR.addEventListener('mouseout', function(e){this.classList.remove('hover')});
                return newTR;
            }
            function createNewCol(content,className,colspan){
                let td = document.createElement('td');
                td.innerHTML = content;
                td.classList.add(className);
                td.setAttribute('colspan',colspan)
                return td;
            }
            function nFormatter(num, digits) {
                var si = [
                    { value: 1, symbol: "" },
                    { value: 1E3, symbol: "k" },
                    { value: 1E6, symbol: "M" },
                    { value: 1E9, symbol: "G" },
                    { value: 1E12, symbol: "T" },
                    { value: 1E15, symbol: "P" },
                    { value: 1E18, symbol: "E" }
                ];
                var rx = /\.0+$|(\.[0-9]*[1-9])0+$/;
                var i;
                for (i = si.length - 1; i > 0; i--) {
                    if (num >= si[i].value) {
                        break;
                    }
                }
                return (num / si[i].value).toFixed(digits).replace(rx, "$1") + si[i].symbol;
            }
            var mMetalProd = 0,
                mCristalProd = 0,
                mDeutProd = 0,
                baseMetal = 0,
                baseCristal = 0,
                baseDeut = 0,
                totalHM = 0,
                totalHC = 0,
                totalHD = 0,
                totalJM = 0,
                totalJC = 0,
                totalJD = 0,
                totalSM = 0,
                totalSC = 0,
                totalSD = 0,
                totalMM = 0,
                totalMC = 0,
                totalMD = 0,
                plasmaMetalH = 0,
                plasmaCristalH = 0,
                plasmaDeutH = 0,
                foreuseMetalH = 0,
                foreuseCristalH = 0,
                foreuseDeutH = 0,
                geologueMetalH = 0,
                geologueCristalH = 0,
                geologueDeutH = 0,
                commandementMetalH = 0,
                commandementCristalH = 0,
                commandementDeutH = 0,
                allianceBonusMetalH = 0,
                allianceBonusCristalH = 0,
                allianceBonusDeutH = 0,
                classBonusMetalH = 0,
                classBonusCristalH = 0,
                classBonusDeutH = 0,
                objetMetalH = 0,
                objetCristalH = 0,
                objetDeutH = 0,
                totalSansPayantM = 0,
                totalSansPayantC = 0,
                totalSansPayantD = 0,
                totalSansClassM = 0,
                totalSansClassC = 0,
                totalSansClassD = 0;


            var planetProd = this.json.planetProd;

            Object.keys(planetProd).forEach(function (key) {
                baseMetal = baseMetal + planetProd[key].baseMetal;
                baseCristal = baseCristal + planetProd[key].baseCristal;
                baseDeut = baseDeut + planetProd[key].baseDeut;

                mMetalProd = mMetalProd + planetProd[key].mm;
                mCristalProd = mCristalProd + planetProd[key].mc;
                mDeutProd = mDeutProd + planetProd[key].md;

                totalHM = totalHM + planetProd[key].metal;
                totalHC = totalHC + planetProd[key].cristal;
                totalHD = totalHD + planetProd[key].deut;


                plasmaMetalH = plasmaMetalH + planetProd[key].plasmaMetal;
                plasmaCristalH = plasmaCristalH + planetProd[key].plasmaCristal;
                plasmaDeutH = plasmaDeutH + planetProd[key].plasmaDeut;

                foreuseMetalH = foreuseMetalH + planetProd[key].foreuseMetal;
                foreuseCristalH = foreuseCristalH + planetProd[key].foreuseCristal;
                foreuseDeutH = foreuseDeutH + planetProd[key].foreuseDeut;

                objetMetalH = objetMetalH + planetProd[key].objetMetal;
                objetCristalH = objetCristalH + planetProd[key].objetCristal;
                objetDeutH = objetDeutH + planetProd[key].objetDeut;

                geologueMetalH = geologueMetalH + planetProd[key].geologueMetal;
                geologueCristalH = geologueCristalH + planetProd[key].geologueCristal;
                geologueDeutH = geologueDeutH + planetProd[key].geologueDeut;

                commandementMetalH = commandementMetalH + planetProd[key].commandementMetal;
                commandementCristalH = commandementCristalH + planetProd[key].commandementCristal;
                commandementDeutH = commandementDeutH + planetProd[key].commandementDeut;

                commandementMetalH = commandementMetalH + planetProd[key].commandementMetal;
                commandementCristalH = commandementCristalH + planetProd[key].commandementCristal;
                commandementDeutH = commandementDeutH + planetProd[key].commandementDeut;

                allianceBonusMetalH = allianceBonusMetalH + planetProd[key].allianceBonusMetal;
                allianceBonusCristalH = allianceBonusCristalH + planetProd[key].allianceBonusCristal;
                allianceBonusDeutH = allianceBonusDeutH + planetProd[key].allianceBonusDeut;

                classBonusMetalH = classBonusMetalH + planetProd[key].classBonusMetal;
                classBonusCristalH = classBonusCristalH + planetProd[key].classBonusCristal;
                classBonusDeutH = classBonusDeutH + planetProd[key].classBonusDeut;

                totalSansPayantM = (totalHM - objetMetalH - geologueMetalH - commandementMetalH - (geologueMetalH == 0 ? 0 : foreuseMetalH *0.1));
                totalSansPayantC = (totalHC - objetCristalH - geologueCristalH - commandementCristalH - (geologueCristalH == 0 ? 0 : foreuseCristalH *0.1));
                totalSansPayantD = (totalHD - objetDeutH - geologueDeutH - commandementDeutH - (geologueDeutH == 0 ? 0 : foreuseDeutH * 0.1));

                totalSansClassM = (totalHM - foreuseMetalH -allianceBonusMetalH - classBonusMetalH - objetMetalH - geologueMetalH - commandementMetalH - (geologueMetalH == 0 ? 0 : foreuseMetalH *0.1));
                totalSansClassC = (totalHC - foreuseCristalH - allianceBonusCristalH - classBonusCristalH - objetCristalH - geologueCristalH - commandementCristalH - (geologueCristalH == 0 ? 0 : foreuseCristalH *0.1));
                totalSansClassD = (totalHD - foreuseDeutH - allianceBonusDeutH - classBonusDeutH - objetDeutH - geologueDeutH - commandementDeutH - (geologueDeutH == 0 ? 0 : foreuseDeutH * 0.1));

            });


            createNewEntry('Total',totalHM,totalHC,totalHD,ressTable);
            createNewEntry('Total No AM',totalSansPayantM,totalSansPayantC,totalSansPayantD,ressTable);
            createNewEntry('TotalNo AM No Classes',totalSansClassM,totalSansClassC,totalSansClassD,ressTable);
            createNewEntry('Plasma',plasmaMetalH,plasmaCristalH,plasmaDeutH,ressTable);
            createNewEntry('Foreuse',foreuseMetalH,foreuseCristalH,foreuseDeutH,ressTable);
            createNewEntry('Booster',objetMetalH,objetCristalH,objetDeutH,ressTable);
            createNewEntry('Geologue',geologueMetalH,geologueCristalH,geologueDeutH,ressTable);
            createNewEntry('Commandement',commandementMetalH,commandementCristalH,commandementDeutH,ressTable);
            createNewEntry('Classe bonus',classBonusMetalH,classBonusCristalH,classBonusDeutH,ressTable);
            createNewEntry('Alliance bonus',allianceBonusMetalH,allianceBonusCristalH,allianceBonusDeutH,ressTable);

            var destination = document.querySelector('.mainRS');
            var canvas = document.createElement('canvas');
            canvas.id ='graphRess';
            destination.appendChild(canvas);
            var myChart = new Chart(canvas,{
                type: 'pie',
                data: {
                    labels: ['Métal', 'Cristal', 'Deutérium'],
                    datasets: [{
                        label: 'Type de ressources',
                        data: [totalHM, totalHC, totalHD],
                        backgroundColor: [
                            '#c76593',
                            '#80d4dc',
                            '#74e6a0'
                        ],
                        borderColor: [
                            '#c76593',
                            '#80d4dc',
                            '#74e6a0'
                        ],
                    }]
                },
                options: {
                    scales: {
                        yAxes: [{
                            ticks: {
                                beginAtZero: true
                            }
                        }]
                    }
                }
            });



            var canvasProp = document.createElement('canvas');
            canvasProp.id ='canvasProp';
            destination.appendChild(canvasProp);
            var myChartProp = new Chart(canvasProp,{
                type: 'pie',
                data: {
                    labels: ['base', 'mine', 'plasma','foreuse','booster','geologue','commandement','alliance','classe'],
                    datasets: [{
                        label: 'Type de ressources',
                        data: [
                            baseMetal+baseCristal+baseDeut,
                            mMetalProd+mCristalProd+mDeutProd,
                            plasmaMetalH+plasmaCristalH+plasmaDeutH,
                            foreuseMetalH+foreuseCristalH+foreuseDeutH,
                            objetMetalH+objetCristalH+objetDeutH,
                            geologueMetalH+geologueCristalH+geologueDeutH,
                            commandementMetalH+commandementCristalH+commandementDeutH,
                            allianceBonusMetalH+allianceBonusCristalH+allianceBonusDeutH,
                            classBonusMetalH+classBonusCristalH+classBonusDeutH],
                        backgroundColor: [
                            '#EC7063',
                            '#AF7AC5',
                            '#5499C7',
                            '#48C9B0',
                            '#F4D03F',
                            '#EB984E',
                            '#F0F3F4',
                            '#f5a7af',
                            '#f54e9f',
                        ],
                        borderColor: [
                            '#EC7063',
                            '#AF7AC5',
                            '#5499C7',
                            '#48C9B0',
                            '#F4D03F',
                            '#EB984E',
                            '#F0F3F4',
                            '#f5a7af',
                            '#f54e9f',
                        ],
                    }]
                },
                options: {
                    scales: {
                        yAxes: [{
                            ticks: {
                                beginAtZero: true
                            }
                        }]
                    }
                }
            });
        }
    }


    let kaoProd = new kaoProduction();

})();