Valmone / Umgomo

// ==UserScript==
// @name        Umgomo
// @namespace   https://openuserjs.org/user/Valmone
// @description Récapitulatif de Projet en prennant en compte les ressources en vol
// @copyright   2020, Valmone (https://openuserjs.org/users/Valmone)
// @license     MIT
// @include     *.ogame*gameforge.com/game/*
// @version     0.1.2
// @grant       GM_addStyle
// @grant       GM_setValue
// @grant       GM_getValue
// @grant       GM_deleteValue
// ==/UserScript==

// ==OpenUserJS==
// @author       Valmone
//               Vesselin Bontchev
//
//      Une partie du script viens de OGame Redesign: Resources in Flight fait par Vesselin Bontchev
// ==/OpenUserJS==



const uni = document.getElementsByName("ogame-universe-name")[0].getAttribute("content");

$('#rechts').append(`<ul id="UMGOMO_fastmenu" class="UMGOMO-horizontal" style="margin-left: 50%; margin-top: 10%;">
                        <li>
                            <a href="javascript: void(0);">
                                <span class='umgomo' ref=''>
                                    <img src="http://files.softicons.com/download/toolbar-icons/pixelbox-icons-by-icojam/png/16x16/1_028.png" alt="icone">
                                </span>
                            </a>
                        </li>
                    </ul>`);

let aze = $(".umgomo");
for (var i = 0; i < aze.length; i++) {
    aze[i].addEventListener("click", affiche, false);
}

function affiche() {
    if (GM_getValue("umgomo_affiche"+uni) == 0) {
        GM_setValue("umgomo_affiche"+uni, 1);
        loadMain();
    }
    else {
        GM_setValue("umgomo_affiche"+uni, 0);
        $("#umgomo").remove();
    }
}


if (GM_getValue("umgomo_affiche"+uni) == 1)
    loadMain();


if (document.location.href.indexOf ('/game/index.php?page=ingame&component=movement') >= 0)
    flyingResources();

function loadMain() {
    var script = setInterval(function() {
        GM_setValue("umgomo_coot"+uni, $("#umgomo").css("top"));
        GM_setValue("umgomo_cool"+uni, $("#umgomo").css("left"));
    }, 200);

    function ressINFleet(m, c, d) {
        this.metal = m;
        this.cristal = c;
        this.deut = d;
    }

    var html = `<link rel="stylesheet" href="//code.$.com/ui/1.12.1/themes/smoothness/$-ui.css">
                <div id="umgomo" class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-draggable ui-draggable-handle" tabindex="-1" role="dialog" style="height: auto; width: auto; top: `+ GM_getValue("umgomo_coot"+uni) +`; left: `+ GM_getValue("umgomo_cool"+uni) +`; display: block;">
                        <div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix ui-draggable-handle">
                            <span id="ui-id-4" class="ui-dialog-title">
                                Umgomo
                            </span>
                            <button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only ui-dialog-titlebar-close" role="button" title="">
                                <span class="ui-button-icon-primary ui-icon ui-icon-closethick" onclick="this.parentNode.parentNode.parentNode.remove();"></span>
                                <span class="ui-button-text"></span>
                            </button>
                        </div>
                        <div class="overlayDiv ui-dialog-content ui-widget-content" style="width: auto; min-height: 109px; max-height: none; height: auto;">
                            <div id="content">
                                <table class="umgomo_table" style="width: 100%;">
                                    <thead>
                                        <tr>
                                            <th><div style="width: 120px;">&nbsp;</div></th>
                                            <th width="120" style="visibility: visible; padding: 4px;">
                                                <div class="resourceIcon metal" style="visibility: visible; float: right;"></div>
                                            </th>
                                            <th width="120" style="visibility: visible; padding: 4px;">
                                                <div class="resourceIcon crystal" style="visibility: visible; float: right;"></div>
                                            </th>
                                            <th width="120" style="visibility: visible; padding: 4px;">
                                                <div class="resourceIcon deuterium" style="visibility: visible; float: right;"></div>
                                            </th>
                                            <th><div style="width: 50px;">&nbsp;</div></th>
                                        </tr>
                                    </thead>
                                    <body>
                                        <tr>
                                            <td class="label" style="visibility: visible; text-align: left;">&nbsp;</td>
                                            <td>&nbsp;</td>
                                            <td>&nbsp;</td>
                                            <td>&nbsp;</td>
                                        </tr>`;

    if (GM_getValue("umgomo_data"+uni) !== undefined) {
        var data = GM_getValue("umgomo_data"+uni),
            ressFleet = data[0],
            projets = data[1];
    } else {
        var ressFleet = 0,
            projets = new Array();
    }

    var suite = true;

    var total = new Array(),
        result = new Array();

    if (ressFleet == 0) {
        ressFleet = new ressINFleet(0, 0, 0);
    }

    total[0] = 0;   total[1] = 0;   total[2] = 0;

    for (var i = 0; i < projets.length && suite; i++) {

        if (ressFleet.metal < 0)
            ressFleet.metal = 0;
        if (ressFleet.cristal < 0)
            ressFleet.cristal = 0;
        if (ressFleet.deut < 0)
            ressFleet.deut = 0;

        projets[i].metal = projets[i].metal *projets[i].nbr;
        projets[i].cristal = projets[i].cristal *projets[i].nbr;
        projets[i].deut = projets[i].deut *projets[i].nbr;
        result[i] = new Array();
        result[i][0] = projets[i].nom;
        result[i][1] = projets[i].metal - ressFleet.metal;
        result[i][2] = projets[i].cristal - ressFleet.cristal;
        result[i][3] = projets[i].deut - ressFleet.deut;

        if (result[i][1] <= 0) {
            result[i][1] = 0;
            ressFleet.metal -= projets[i].metal;
        } else {
            ressFleet.metal = 0;
        }

        if (result[i][2] <= 0) {
            result[i][2] = 0;
            ressFleet.cristal -= projets[i].cristal;
        } else {
            ressFleet.cristal = 0;
        }

        if (result[i][3] <= 0) {
            result[i][3] = 0;
            ressFleet.deut -= projets[i].deut;
        } else {
            ressFleet.deut = 0;
        }


        total[0] += result[i][1];
        total[1] += result[i][2];
        total[2] += result[i][3];
    }

    html = html + `<tr class="alt">
                                <td>
                                    <h1 style="visibility: visible; font: 14.4px; font-weight: bold; font-size: 20px; text-align: center;">Σ</h1>
                                </td>
                                <td>
                                    <h1 id="total_metal_umgomo" style="visibility: visible; font: 14.4px; text-align: center; font-weight: bold; font-size: 20px; color: #c6556c; margin-right: 10px;">`+ simpli(total[0]) +`</h1>
                                </td>
                                <td>
                                    <h1 id="total_cristal_umgomo" style="visibility: visible; font: 14.4px; text-align: center; font-weight: bold; font-size: 20px; color: #00bfff; margin-right: 5px; margin-left: 5px;">`+ simpli(total[1]) +`</h1>
                                </td>
                                <td>
                                    <h1 id="total_deut_umgomo" style="visibility: visible; font: 14.4px; text-align: center; font-weight: bold; font-size: 20px; color: #6495e2; margin-left: 10px;">`+ simpli(total[2]) +`</h1>
                                </td>
                                <td>&nbsp;</td>
                            </tr>
                            <tr>
                                <td class="label" style="visibility: visible; text-align: left;">&nbsp;</td>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                            </tr>`;

    for (var i = 0; i < result.length; i++) {
        html += `<tr class="alt" id="`+i+`">
                    <td>
                        <h1 style="visibility: visible; font: 14.4px; font-weight: bold; font-size: 15px; text-align: center;" value="`+result[i][0]+`">`+ result[i][0] +`</h1>
                    </td>
                    <td>
                        <h1 style="visibility: visible; font: 14.4px; font-weight: bold; font-size: 12px; text-align: center; color: #c6556c; margin-right: 10px;" value="`+result[i][1]+`">`+simpli(result[i][1])+`</h1>
                    </td>
                    <td>
                        <h1 style="visibility: visible; font: 14.4px; font-weight: bold; font-size: 12px; text-align: center; color: #00bfff; margin-right: 5px; margin-left: 5px;" value="`+result[i][2]+`">`+simpli(result[i][2])+`</h1>
                    </td>
                    <td>
                        <h1 style="visibility: visible; font: 14.4px; font-weight: bold; font-size: 12px; text-align: center; color: #6495e2; margin-left: 10px;" value="`+result[i][3]+`">`+simpli(result[i][3])+`</h1>
                    </td>
                    <td>
                        <a href="javascript: void(0);" style="margin-left:30%;">
                            <span class="icon icon_setting umgomo_setting" id="umgomo_setting" data="`+i+`"></span><span style="padding-left: 2px;" class="icon icon_trash umgomo_trash" id="umgomo_trash" data="`+i+`"></span>
                        </a>
                    </td>
                </tr>
                <tr>
                    <td class="label" style="visibility: visible; text-align: left;">&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>`;
    }

    html = html + `      <tr class="alt">
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td>
                                    <a href="javascript: void(0);">
                                        <button id="ajout_umgomo" class="bold" role="presentation" style="visibility: visible; width: 100%; font-size: 15px; background-color: rgb(221, 221, 221);">+</button>
                                    </a>
                                </td>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                            </tr>
                        </body>
                    </table>
                </div>
            </div>
    </div>
    <script src="//code.$.com/$-1.12.4.js"></script><script src="//code.$.com/ui/1.12.1/$-ui.js"></script><script type="text/javascript">$("#umgomo").draggable();</script>`;

    $("body").append(html);
    document.getElementById("ajout_umgomo").addEventListener("click", ajout, false);
    let classname = $(".umgomo_setting");
    let classname2 = $(".umgomo_trash");
    for (var i = 0; i < classname.length; i++) {
        classname[i].addEventListener("click", setting, false);
        classname2[i].addEventListener("click", trash, false);
    }
    //document.getElementById("#umgomo_trash").addEventListener("click", trash, false);
    //document.getElementById("#umgomo_setting").addEventListener("click", setting, false);


    if ($("#total_metal_umgomo").text() == "0" && ressFleet.metal > 0) {
        $("#total_metal_umgomo").text("-"+ simpli(ressFleet.metal));
        $("#total_metal_umgomo").css("color", "gray");
        $("#total_metal_umgomo").css("font-size", "16px");
    }
    if ($("#total_cristal_umgomo").text() == "0" && ressFleet.cristal > 0) {
        $("#total_cristal_umgomo").text("-"+ simpli(ressFleet.cristal));
        $("#total_cristal_umgomo").css("color", "gray");
        $("#total_cristal_umgomo").css("font-size", "16px");
    }
    if ($("#total_deut_umgomo").text() == "0" && ressFleet.deut > 0) {
        $("#total_deut_umgomo").text("-"+ simpli(ressFleet.deut));
        $("#total_deut_umgomo").css("color", "gray");
        $("#total_deut_umgomo").css("font-size", "16px");
    }

    for (var i = 0; $("#"+i).html() != undefined; i++) {
        if (parseInt($("#"+i).children().eq(1).children().html()) == 0 && parseInt($("#"+i).children().eq(2).children().html()) == 0 && parseInt($("#"+i).children().eq(3).children().html()) == 0)
            $("#"+i).children().eq(0).children().css("color", "green");
    }
}

function ajout() {
    $("body").append(`<link rel="stylesheet" href="//code.$.com/ui/1.12.1/themes/smoothness/$-ui.css">
                        <div id="umgomo_ajout" class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-draggable ui-draggable-handle" tabindex="-1" role="dialog" style="height: auto; width: 200px; top: 10px; left: 10px; display: block;">
                                <div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix ui-draggable-handle">
                                    <span id="ui-id-4" class="ui-dialog-title">
                                        Ajout donnée
                                    </span>
                                    <button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only ui-dialog-titlebar-close" role="button" title="">
                                        <span class="ui-button-icon-primary ui-icon ui-icon-closethick" onclick="this.parentNode.parentNode.parentNode.remove();"></span>
                                        <span class="ui-button-text"></span>
                                    </button>
                                </div>
                                <div class="overlayDiv ui-dialog-content ui-widget-content" style="width: auto; min-height: 109px; max-height: none; height: auto;">
                                    <div id="content">
                                        <b><u style="font-size: 15px;">Nom: </u></b>
                                        <input id="nomInput" value="Astro" size="15" style="width: 100px; margin-left: 10px; text-align: center;">
                                        <br><br>

                                        <b><u style="font-size: 15px;">Prix: </u></b>
                                        <input id="metalInput" value="Métal" size="15" style="width: 100px; margin-left: 10px; text-align: center;"><br>
                                        <input id="cristalInput" value="Cristal" size="15" style="width: 100px; margin-left: 55px; text-align: center;"><br>
                                        <input id="deutInput" value="Deutérium" size="15" style="width: 100px; margin-left: 55px; text-align: center;">
                                        <br><br>

                                        <b><u style="font-size: 15px;">Quantité: </u></b>
                                        <input id="quantiteInput" value="1" size="15" style="width: 26px; margin-left: 10px; text-align: center;"> fois
                                        <br><br><br><br>
                                        <button id="save_umgomo" class="btn_blue" style="margin-left: 25%;">
                                            Sauvegarder
                                        </button>
                                    </div>
                                </div>
                        </div>
                        <script src="//code.$.com/$-1.12.4.js"></script><script src="//code.$.com/ui/1.12.1/$-ui.js"></script><script type="text/javascript">$("#umgomo_ajout").draggable();</script>`);

    document.getElementById("save_umgomo").addEventListener("click", save, false);
    function save() {
        function verif(a) {
            if (isNaN(a)) {
                return 0;
            } else {
                return parseInt(a);
            }
        }

        function umgomo(n, m, c, d, q) {
            this.nom = n;
            this.metal = m;
            this.cristal = c;
            this.deut = d;
            this.nbr = q;
        }

        if (GM_getValue("umgomo_data"+uni) !== undefined) {
            var data = GM_getValue("umgomo_data"+uni),
                data2 = data[1];
            data2.push(new umgomo($("#nomInput").val(), verif($("#metalInput").val()), verif($("#cristalInput").val()), verif($("#deutInput").val()), verif($("#quantiteInput").val())));
            data[1] = data2;
            GM_setValue("umgomo_data"+uni, data);
        } else {
            var data = new Array(),
                data2 = new Array();
            data[0] = 0;
            data2[0] = new umgomo($("#nomInput").val(), verif($("#metalInput").val()), verif($("#cristalInput").val()), verif($("#deutInput").val()), verif($("#quantiteInput").val()));
            data[1] = data2;
            GM_setValue("umgomo_data"+uni, data);
        }
        this.parentNode.parentNode.parentNode.remove();
        $("#umgomo").remove();
        loadMain();
    }
}

function trash() {
    let id = parseInt(this.getAttribute('data'));
    let data = GM_getValue("umgomo_data"+uni);
    let projets = data[1];
    projets.splice(id, 1);

    data[1] = projets;
    GM_setValue("umgomo_data"+uni, data);

    $("#umgomo").remove();
    loadMain();
}

function setting() {
    $("body").append(`<link rel="stylesheet" href="//code.$.com/ui/1.12.1/themes/smoothness/$-ui.css">
                        <div id="umgomo_ajout" class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-draggable ui-draggable-handle" tabindex="-1" role="dialog" style="height: auto; width: 200px; top: 10px; left: 10px; display: block;">
                                <div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix ui-draggable-handle">
                                    <span id="ui-id-4" class="ui-dialog-title">
                                        Modification
                                    </span>
                                    <button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only ui-dialog-titlebar-close" role="button" title="">
                                        <span class="ui-button-icon-primary ui-icon ui-icon-closethick" onclick="this.parentNode.parentNode.parentNode.remove();"></span>
                                        <span class="ui-button-text"></span>
                                    </button>
                                </div>
                                <div class="overlayDiv ui-dialog-content ui-widget-content" style="width: auto; min-height: 109px; max-height: none; height: auto;">
                                    <div id="content">
                                        <b><u style="font-size: 15px;">Nom: </u></b>
                                        <input id="nomInput" value="`+ GM_getValue('umgomo_data'+uni)[1][(this.getAttribute('data'))].nom +`" size="15" style="width: 100px; margin-left: 10px; text-align: center;">
                                        <br><br>

                                        <b><u style="font-size: 15px;">Prix: </u></b>
                                        <input id="metalInput" value="`+ GM_getValue('umgomo_data'+uni)[1][(this.getAttribute('data'))].metal +`" size="15" style="width: 100px; margin-left: 10px; text-align: center;"><br>
                                        <input id="cristalInput" value="`+ GM_getValue('umgomo_data'+uni)[1][(this.getAttribute('data'))].cristal +`" size="15" style="width: 100px; margin-left: 55px; text-align: center;"><br>
                                        <input id="deutInput" value="`+ GM_getValue('umgomo_data'+uni)[1][(this.getAttribute('data'))].deut +`" size="15" style="width: 100px; margin-left: 55px; text-align: center;">
                                        <br><br>

                                        <b><u style="font-size: 15px;">Quantité: </u></b>
                                        <input id="quantiteInput" value="`+ GM_getValue('umgomo_data'+uni)[1][(this.getAttribute('data'))].nbr +`" size="15" style="width: 26px; margin-left: 10px; text-align: center;"> fois
                                        <br><br><br><br>
                                        <button id="modif_umgomo" class="btn_blue" style="margin-left: 25%;" data="`+ this.getAttribute('data') +`">
                                            Modifier
                                        </button>
                                    </div>
                                </div>
                        </div>
                        <script src="//code.$.com/$-1.12.4.js"></script><script src="//code.$.com/ui/1.12.1/$-ui.js"></script><script type="text/javascript">$("#umgomo_ajout").draggable();</script>`);

    document.getElementById("modif_umgomo").addEventListener("click", modif, false);
    function modif() {
        function verif(a) {
            if (isNaN(a)) {
                return 0;
            } else {
                return parseInt(a);
            }
        }

        function umgomo(n, m, c, d, q) {
            this.nom = n;
            this.metal = m;
            this.cristal = c;
            this.deut = d;
            this.nbr = q;
        }

        if (GM_getValue("umgomo_data"+uni) !== undefined) {
            var data = GM_getValue("umgomo_data"+uni),
                data2 = data[1];
            data2[this.getAttribute('data')] = new umgomo($("#nomInput").val(), verif($("#metalInput").val()), verif($("#cristalInput").val()), verif($("#deutInput").val()), verif($("#quantiteInput").val()));
            data[1] = data2;
            GM_setValue("umgomo_data"+uni, data);
        } else {
            var data = new Array(),
                data2 = new Array();
            data[0] = 0;
            data2[0] = new umgomo($("#nomInput").val(), verif($("#metalInput").val()), verif($("#cristalInput").val()), verif($("#deutInput").val()), verif($("#quantiteInput").val()));
            data[1] = data2;
            GM_setValue("umgomo_data"+uni, data);
        }
        this.parentNode.parentNode.parentNode.remove();
        $("#umgomo").remove();
        loadMain();
    }
}

function simpli(x){
    var result = 0,
        round = 0;
    for (var i = 0; x >= 1000 ; i++) {
        var z = (x/1000);               //  1928.302
        z = Math.floor(z);              //  1928
        z = z *1000;                    //  1928000
        z = x-z;                        //  302
        if (z < 1) {
            z = "000";
        } else if (z < 10) {
            z = "00"+ z;
        } else if (z < 100) {
            z = "0"+ z;
        } else {}
        if (round > 0) {
            result = z +"."+ result;
        } else {
            result = z;
            round++;
        }
        x = (x-z) /1000;                //  1928
    }
    z = (x/1000);               //  1928.302
    z = Math.floor(z);              //  1928
    z = z *1000;                    //  1928000
    z = x-z;
    if (round > 0) {
        result = z +"."+ result;
    } else {
        result = z;
        round++;
    }
    //console.log("resultat: "+ result);
    return result;
}

function flyingResources() {
    function ressINFleet(m, c, d) {
        this.metal = m;
        this.cristal = c;
        this.deut = d;
    }

    var myRes = new Array(0, 0, 0);
    var myRes2 = new Array(0, 0, 0);
    var flightCargo = new Object;
    allFlights = document.querySelectorAll (".fleetinfo");
    if (allFlights.length > 0) {
        for (var i = 0; i < allFlights.length; i++) {
            var trs = allFlights [i].getElementsByTagName ("tr");
            var any = false;
            var shipInfos = trs.length;
            if (trs [shipInfos - 1].id == "freeSpace")
                shipInfos--;
            for (var j = 0; j < 3; j++) {
                myRes [j] = parseInt (trs [shipInfos - 3 + j].getElementsByTagName ("td") [1].textContent.replace (/\D+/gi, ""));
                if (myRes [j] > 0)
                    any = true;
            }
            myRes2[0] += myRes[0];
            myRes2[1] += myRes[1];
            myRes2[2] += myRes[2];
        }
    }
    else {
        allFlights = document.querySelectorAll (".anti_fleetDetails");
        for (var i = 0; i < allFlights.length; i++) {
            var trs = allFlights [i].textContent.split (/\n/);
            var any = false;
            var shipInfos = trs.length;
            if (trs [shipInfos - 1].id == "freeSpace")
                shipInfos--;
            for (var j = 0; j < 3; j++) {
                myRes [j] = parseInt (trs [shipInfos - 3 + j].replace (/\D+/gi, ""));
                if (myRes [j] > 0)
                    any = true;
            }
            myRes2[0] += myRes[0];
            myRes2[1] += myRes[1];
            myRes2[2] += myRes[2];
        }
    }

    let data = GM_getValue("umgomo_data"+uni);
    data[0] = new ressINFleet(myRes2[0], myRes2[1], myRes2[2]);
    GM_setValue("umgomo_data"+uni, data);
    loadMain();
    fadeBox("Récupération des ressources réussi !!", false);
}