Raw Source
jobijoba / ogamex-expe

// ==UserScript==
// @name         ogamex-expe
// @namespace    http://tampermonkey.net/
// @version      0.7.5
// @description  Statistiques pour les expéditions de ogamex.net
// @author       Jobijoba
// @include       *.ogamex.net/*
// @grant        GM_addStyle
//@require http://code.jquery.com/jquery-3.4.1.min.js
// @updateURL https://openuserjs.org/meta/jobijoba/ogamex-expe.meta.js
// @downloadURL https://openuserjs.org/install/jobijoba/ogamex-expe.user.js
// @copyright 2021, Jobijoba
// @license MIT
// ==/UserScript==

(function() {
    'use strict';
    var href = window.location.href;
    var tab_vaisseaux = new Array();
    tab_vaisseaux['SpyProbe'] = [0,1000,0];
    tab_vaisseaux['LightFighter'] = [3000,1000,0];
    tab_vaisseaux['HeavyFighter'] = [6000,4000,0];
    tab_vaisseaux['Cruiser'] = [20000,7000,2000];
    tab_vaisseaux['Battleship'] = [45000,15000,0];
    tab_vaisseaux['BattleCruiser'] = [30000,40000,15000];
    tab_vaisseaux['PlanetBomber'] = [50000,25000,15000];
    tab_vaisseaux['Destroyer'] = [60000,50000,15000];
    tab_vaisseaux['Reaper'] = [85000,55000,20000];
    tab_vaisseaux['Galleon'] = [320000,240000,60000];
    tab_vaisseaux['Demolisher'] = [800000,800000,400000];
    tab_vaisseaux['DeathStar'] = [5000000,4000000,1000000];
    tab_vaisseaux['Falcon'] = [4800000,3200000,1800000];
    tab_vaisseaux['Avatar'] = [19600000,16400000,7200000];
    tab_vaisseaux['LightCargo'] = [2000,2000,0];
    tab_vaisseaux['HeavyCargo'] = [6000,6000,0];
    tab_vaisseaux['ColonyShip'] = [10000,20000,10000];
    tab_vaisseaux['Recycler'] = [10000,6000,2000];
    tab_vaisseaux['AsteroidMiner'] = [40000,20000,8000];

    /*
    Met mot en minuscule et première lettre en majuscule
    */
    function capitalizeFirstLetter(string) {
        string = string.toLowerCase();
        return string.charAt(0).toUpperCase() + string.slice(1);
    }
    /*
    Calcul le pourcentage des éxpéditions
    */
    function pcExpe(type, total){
        return (Math.round((parseInt(type) / parseInt(total)) * 10000) / 100).toLocaleString(["ban", "id"])+ '%';
    }
    /*
    Affuche stats expéditions
    */
    function showStats(type,total){
        return '<td>'+ type.toLocaleString(["ban", "id"]) +'</td><td>'+ pcExpe(type, total) +'</td>';
    }

    function intFormate(str){
        return parseInt(str.replace(/\./g,''));
    }
    function strFormate(number){
        return number.toLocaleString(["ban", "id"]);
    }

    var expe = "";
    var combat = "";
    var ast = "";
    var time = 300;
    /*
    Détection des cliques qui lances la fonction de scan des expéditions
    */
    function clickExpe(){
        $('#subnav-expeditions').click(function(){
                expe = setTimeout(function(){
                    clearTimeout(expe);
                    scanExpe();
                },time);
        });
    }
    function clickExpePage(currentPage){
        $('.fa-angle-right,.fa-angle-left,.fa-angle-double-right,.fa-angle-double-left').click(function(){
            var targetPage = parseInt($(this).attr('data-target-page'));
            if(currentPage != targetPage){
                expe = setTimeout(function(){
                    clearTimeout(expe);
                    scanExpe();
                },time);
            }
        });

    }
    /*
    Détection des cliques qui lances la fonction de scan les combats d'expéditions
    */
    function clickCombat(){
        $('#subnav-combat-reports').click(function(){
            combat = setTimeout(function(){
                clearTimeout(combat);
                scanCombat();
            },500);
        });
    }
     function clickCombatPage(currentPage){
        $('.fa-angle-right,.fa-angle-left,.fa-angle-double-right,.fa-angle-double-left').click(function(){
            var targetPage = parseInt($(this).attr('data-target-page'));
            if(currentPage != targetPage){
                combat = setTimeout(function(){
                    clearTimeout(combat);
                    scanCombat();
                },time);
            }
        });

    }
    /*
    Détection des cliques qui lances la fonction de scan des expéditions
    */
    function clickAst(){
        $('#subnav-fleet-other').click(function(){
                ast = setTimeout(function(){
                    clearTimeout(ast);
                    scanAst();
                },time);
        });
    }
    function clickAstPage(currentPage){
        $('.fa-angle-right,.fa-angle-left,.fa-angle-double-right,.fa-angle-double-left').click(function(){
            var targetPage = parseInt($(this).attr('data-target-page'));
            if(currentPage != targetPage){
                ast = setTimeout(function(){
                    clearTimeout(ast);
                    scanAst();
                },time);
            }
        });

    }
    /*
    Scan des expéditions
    */
    function scanExpe(){
        for(var i = 0; i < 100; i++){
            if($('.message-item:eq('+i+')').html() == undefined){
                break;
            }
            var message = $('.message-item:eq('+i+')');
            message.css('border-right','5px solid red')
            var msgId = $('.message-item:eq('+i+')').attr('data-msg-id');
            if(localStorage['ogx_msgId'].indexOf(msgId) != -1){
                //detectTypeExpe(message);
                $('.message-item:eq('+i+')').css('border-right','5px solid green')
            }else{
                localStorage['ogx_msgId'] = localStorage['ogx_msgId'] + msgId;
                detectTypeExpe(message);

            }
        }
        var currentPage = parseInt($('.page-index-text').html().split('/')[0]);
        clickExpePage(currentPage);
    }
    /*
    Scan des combats d'expéditions
    */
    function scanCombat(){
        for(var i = 0; i < 100; i++){
            var message = $('.message-item:eq('+i+')').html();
            if(message == undefined){ break;}

            var msgIdCombat = $('.message-item:eq('+i+')').attr('data-msg-id');
            if(message.indexOf('Aliens ') != -1 || message.indexOf('Pirates ')!= -1){
                if(localStorage['ogx_msgIdCombat'].indexOf(msgIdCombat) != -1){
                    $('.message-item:eq('+i+')').css('border-right','5px solid green')
                }else{
                    $('.message-item:eq('+i+')').css('border-right','5px solid red')
                }
            }
            
        }
        $('.btn-report-more-info').click(function(){
            var msgIdCombat = $(this).parent().parent().attr('data-msg-id');
            var message = $(this).parent().parent().html();
            $(this).parent().parent().addClass('c'+msgIdCombat);
            var jour = localStorage.ogx_jours;
            jour = JSON.parse(jour);
            var date = $(this).parent().parent().find('span:eq(1)').html().split(' ')[0];
            //console.log(date);
            if(!jour[date]){
                jour[date] = new Object();
                jour[date]['metal'] = 0;
                jour[date]['cristal'] = 0;
                jour[date]['deut'] = 0;
                jour[date]['nbexpe'] = 0;
                jour[date]['nbcombat'] = 0;
                console.log(jour)
            }
            //console.log(message);
            if(message.indexOf('Aliens ') != -1 || message.indexOf('Pirates ')!= -1){

                var gai = localStorage.ogx_gain;
                gai = JSON.parse(gai);
                combat = setTimeout(function(){
                    if(combat != ""){
                        clearTimeout(combat);
                    }
                    var testperte = new Array(0,0,0);

                    var result  = parseInt($('.pagination-item.active').prev().html()) + 1;
                    var cdr     = $('#combat-report-modal>div:eq(1)>div:eq(1)').html();
                    var metal   = parseInt(cdr.split('Metal</div>"> : ')[1].split('</div>')[0].replace(/\./g,''));
                    var cristal = parseInt(cdr.split('Crystal</div>"> : ')[1].split('</div>')[0].replace(/\./g,''));

                    gai.combat['metal'] = parseInt(gai.combat['metal']) + metal;
                    gai.combat['cristal'] = parseInt(gai.combat['cristal']) + cristal;
                    gai.ress_total['metal'] = parseInt(gai.ress_total['metal']) + metal;
                    gai.ress_total['cristal'] = parseInt(gai.ress_total['cristal']) + cristal;

                    jour[date]['metal'] = parseInt(jour[date]['metal']) + metal;
                    jour[date]['cristal'] = parseInt(jour[date]['cristal']) + cristal;


                    var rc = $('div.x-round-units:eq('+result+') .x-round-units-defender').html();
                    var lines = rc.split('<img');
                    for(var i = 1; i < lines.length; i++){
                        var nom = lines[i].split('CCC;">')[1].split('<')[0].replace(/\ /g,'');
                        var perte = parseInt(lines[i].split('35;">')[1].split('<')[0].replace(/\./g,'').replace(/-/,0));
                        testperte[0] -= tab_vaisseaux[nom][0] * perte;
                        testperte[1] -= tab_vaisseaux[nom][1] * perte;
                        testperte[2] -= tab_vaisseaux[nom][2] * perte;
                    }
                    var rentaMetal = parseInt(metal)+parseInt(testperte[0]);
                    var rentaCristal = parseInt(cristal)+parseInt(testperte[1]);
                    var rentaDeut = parseInt(testperte[2]);

                    var rentaMetalValue = strFormate(rentaMetal + (rentaCristal * 1.5) + (rentaDeut *3));
                    console.log(rentaMetalValue);
                    $('.c'+msgIdCombat).find('.btn-report-more-info').html('RENTA: '+rentaMetalValue);






                    gai.combat['metal'] = parseInt(gai.combat['metal']) + testperte[0];
                    gai.combat['cristal'] = parseInt(gai.combat['cristal']) + testperte[1];
                    gai.combat['deut'] = parseInt(gai.combat['deut']) + testperte[2];

                    gai.ress_total['metal'] = parseInt(gai.ress_total['metal']) + testperte[0];
                    gai.ress_total['cristal'] = parseInt(gai.ress_total['cristal']) + testperte[1];
                    gai.ress_total['deut'] = parseInt(gai.ress_total['deut']) + testperte[2];

                    jour[date]['metal'] = parseInt(jour[date]['metal']) + testperte[0];
                    jour[date]['cristal'] = parseInt(jour[date]['cristal']) + testperte[1];
                    jour[date]['deut'] = parseInt(jour[date]['deut']) + testperte[2];

                    //console.log(testperte[0]+testperte[1]+testperte[2]);

                    if(localStorage.ogx_msgIdCombat.indexOf(msgIdCombat) != -1){
                        //console.log('ok');
                    }else{
                        localStorage.ogx_msgIdCombat += msgIdCombat;
                        gai = JSON.stringify(gai);
                        localStorage.ogx_gain = gai;
                        jour = JSON.stringify(jour);
                        localStorage.ogx_jours = jour;
                        clickCombat();
                    }
                    $(this).html(rentaMetalValue)
                },500);
            }
            $(this).parent().parent().css('border-right','5px solid green')





            
        });
        var currentPage = parseInt($('.page-index-text').html().split('/')[0]);
        clickCombatPage(currentPage);
    }
    /*
    Détection du type de résuktat d'expédition
    */
    function detectTypeExpe(message){
        var css = message;
        message = message.html();
        var lines = '';
        var metal = '';
        var cristal = '';
        var deut = '';
        var exp = localStorage.ogx_expe;
        exp = JSON.parse(exp);
        var gai = localStorage.ogx_gain;
        gai = JSON.parse(gai);
        var jour = localStorage.ogx_jours;
        jour = JSON.parse(jour);
        var diver = localStorage.ogx_divers;
        diver = JSON.parse(diver);

        var type = "";
        var date = message.split('<span>')[2].split('</span>')[0].split(' ')[0];
        //console.log(date);
        if(!jour[date]){
            //console.log(date);
            jour[date] = new Object();

            jour[date]['metal'] = 0;
            jour[date]['cristal'] = 0;
            jour[date]['deut'] = 0;
            jour[date]['nbexpe'] = 0;
            jour[date]['nbcombat'] = 0;
            //console.log(jour)
        }
        if(message.indexOf('Resources obtained::') != -1){
            exp.ressources++;
            if(message.indexOf('Metal') != -1){
                lines = message.split('Metal</div>"> : ')[1];
                lines = lines.split('</div>')[0];
                metal = parseInt(lines.replace(/\./g,''));
                gai.ress['metal'] = parseInt(gai.ress['metal']) + metal;
                gai.ress_total['metal'] = parseInt(gai.ress_total['metal']) + metal;
                jour[date]['metal'] = parseInt(jour[date]['metal']) + metal;
            }
            if(message.indexOf('Crystal') != -1){
                lines = message.split('Crystal</div>"> : ')[1];
                lines = lines.split('</div>')[0];
                cristal = parseInt(lines.replace(/\./g,''));
                gai.ress['cristal'] = parseInt(gai.ress['cristal']) + cristal;
                gai.ress_total['cristal'] = parseInt(gai.ress_total['cristal']) + cristal;
                jour[date]['cristal'] = parseInt(jour[date]['cristal']) + cristal;
            }
            if(message.indexOf('Deuterium') != -1){
                lines = message.split('Deuterium</div>"> : ')[1];
                lines = lines.split('</div>')[0];
                deut = parseInt(lines.replace(/\./g,''));
                gai.ress['deut'] = parseInt(gai.ress['deut']) + deut;
                gai.ress_total['deut'] = parseInt(gai.ress_total['deut']) + deut;
                jour[date]['deut'] = parseInt(jour[date]['deut']) + deut;
            }
        }
        else if(message.indexOf('Dark matters found::') != -1){

            var antimat = parseInt(message.split('Dark matter</div>"> : ')[1].split('</div>')[0].replace(/\./g,''))
            console.log(antimat)
            if(!gai.am){
                gai.am = 0;
            }
            gai.am = parseInt(gai.am) + antimat;


            exp.am++;
        }
        else if(message.indexOf('Nothing remarkable') != -1){
            exp.rien++;
        }
        else if(message.indexOf('ambushed by the pirates') != -1){
            exp.pirates++;
            jour[date]['nbcombat'] = parseInt(jour[date]['nbcombat']) + 1;
        }
        else if(message.indexOf('alien creatures') != -1){
            exp.aliens++;
            jour[date]['nbcombat'] = parseInt(jour[date]['nbcombat']) + 1;
        }
        else if(message.indexOf('Ships found:') != -1){
            exp.vaisseaux++;
            lines = message.split('<tr');
            for(var v = 1; v < lines.length; v++ ){
                var lines2 = lines[v];
                lines2 = lines2.split('<td');
                var nom = lines2[1].split('>')[1];
                nom = nom.split(' </td')[0].replace(/ /g,'');
                var nb = lines2[2].split('>:')[1].split('</td')[0].replace(/\./g,'');
                gai.flotte[nom] = parseInt(gai.flotte[nom]) + parseInt(nb);
            }
            if(message.indexOf('Metal') != -1){
                var lines = message.split('Metal</div>"> : ')[1];
                lines = lines.split('</div>')[0];
                metal = parseInt(lines.replace(/\./g,''));
                gai.ress_flotte['metal'] = parseInt(gai.ress_flotte['metal']) + metal;
                gai.ress_total['metal'] = parseInt(gai.ress_total['metal']) + metal;
                jour[date]['metal'] = parseInt(jour[date]['metal']) + metal;
            }
            if(message.indexOf('Crystal') != -1){
                lines = message.split('Crystal</div>"> : ')[1];
                lines = lines.split('</div>')[0];
                cristal = parseInt(lines.replace(/\./g,''));
                gai.ress_flotte['cristal'] = parseInt(gai.ress_flotte['cristal']) + cristal;
                gai.ress_total['cristal'] = parseInt(gai.ress_total['cristal']) + cristal;
                jour[date]['cristal'] = parseInt(jour[date]['cristal']) + cristal;
            }
            if(message.indexOf('Deuterium') != -1){
                lines = message.split('Deuterium</div>"> : ')[1];
                lines = lines.split('</div>')[0];
                deut = parseInt(lines.replace(/\./g,''));
                gai.ress_flotte['deut'] = parseInt(gai.ress_flotte['deut']) + deut;
                gai.ress_total['deut'] = parseInt(gai.ress_total['deut']) + deut;
                jour[date]['deut'] = parseInt(jour[date]['deut']) + deut;
            }
        }
        else if(message.indexOf('slower than calculated') != -1){
            exp.retard++;
        }
        else if(message.indexOf('earlier than calculated') != -1){
            exp.avance++;
        }
        else if(message.indexOf('The expedition fleet has found a container.') != -1){
            exp.container++;
        }
        else if(message.indexOf('was caught in a black hole') != -1){
            diver.expenotn = -1;
            exp.tn++;
        }
        exp.total++;
        diver.expenotn++;
        jour[date]['nbexpe'] = parseInt(jour[date]['nbexpe']) + 1;
        //console.log(gai);
        exp = JSON.stringify(exp);
        localStorage.ogx_expe = exp;
        gai = JSON.stringify(gai);
        localStorage.ogx_gain = gai;
        jour = JSON.stringify(jour);
        localStorage.ogx_jours = jour;

        diver = JSON.stringify(diver);
        localStorage.ogx_divers = diver;
        css.css('border-right','5px solid #0f0');
    }
    function localStorageCreate(){
        if(!localStorage.ogx_msgId){
            localStorage.ogx_msgId = '';
        }
        if(!localStorage.ogx_msgIdCombat){
            localStorage.ogx_msgIdCombat = '';
        }
        if(!localStorage.ogx_version || localStorage.ogx_version != '0.7.1'){
            localStorage.ogx_version = '0.7.1';
        }
        if(!localStorage.ogx_expe){
            var e_expe = new Object();
            e_expe.total = 0;
            e_expe.ressources = 0;
            e_expe.vaisseaux = 0;
            e_expe.pirates = 0;
            e_expe.aliens = 0;
            e_expe.am = 0;
            e_expe.container = 0;
            e_expe.rien = 0;
            e_expe.retard = 0;
            e_expe.avance = 0;
            e_expe.tn = 0;
            localStorage.ogx_expe = JSON.stringify(e_expe);
        }
        if(!localStorage.ogx_gain){
            var e_gain = new Object();
            e_gain.ress = {
                'metal' : 0,
                'cristal' : 0,
                'deut' : 0
            };
            e_gain.ress_flotte = {
                'metal' : 0,
                'cristal' : 0,
                'deut' : 0
            };
            e_gain.combat = {
                'metal' : 0,
                'cristal' : 0,
                'deut' : 0
            };
            e_gain.ress_total = {
                'metal' : 0,
                'cristal' : 0,
                'deut' : 0
            };
            e_gain.flotte = {
                'LightCargo' : 0,
                'HeavyCargo' : 0,
                'Recycler' : 0,
                'LightFighter' : 0,
                'HeavyFighter' : 0,
                'Cruiser' : 0,
                'Battleship' : 0,
                'BattleCruiser' : 0,
                'PlanetBomber' : 0,
                'Destroyer' : 0,
                'Reaper' : 0
            };
            localStorage.ogx_gain = JSON.stringify(e_gain);
        }
        if(!localStorage.ogx_options){
            var e_options = new Object();
            e_options.start = Date.now();
            e_options.unite = "";
            localStorage.ogx_options = JSON.stringify(e_options);
        }
        if(!localStorage.ogx_jours){
            var e_jours = new Object();
            localStorage.ogx_jours = JSON.stringify(e_jours);
        }
        if(!localStorage.ogx_joursCombats){
            var e_joursCombats = new Object();
            localStorage.ogx_joursCombats = JSON.stringify(e_joursCombats);
        }
        if(!localStorage.ogx_divers){
            var e_divers = new Object();
            e_divers.expenotn = 0;
            e_divers.amtotal = 0;
            localStorage.ogx_divers = JSON.stringify(e_divers);
        }
    }
    function displayStats(time){




        var href2 = href.split('ogamex.net/')[1];
        var containerOgx = '.content-section';
        if(href2.indexOf('home') != -1 || href2 == ''){ containerOgx = '#overview-container'; }
        if(href2.indexOf('galaxy') != -1){ containerOgx = '#galaxy-container'; }
        if(href2.indexOf('jumpgate') != -1){
            $('<div id="buildings-container">').prependTo('#content-wrapper');
            containerOgx = '#buildings-container';
        }
        $(containerOgx).html('')

        var e = localStorage;

        var contents = '';
        contents += '<div class="expe">';
        contents += '<div class="titre"><div style="color:white; text-align: center; padding: 15px 0px 20px 0px; font-size: 20px;">Ogamex Expe</div><span class="pageActive" id="btn-stats">Mes stats</span><span id="btn-classement">Classement</span><span id="btn-options">Options</span><div class="titresmall">v'+localStorage.ogx_version+'</div></div>';
        contents += '<div id="contenu">';
        contents += '</div>';
        contents += '</div>';
        $('<div id="coontainer-expe">').appendTo(containerOgx).html(contents);
        stats(e,time);
        cssStyle();
        $('.titre span').click(function(){
            $('.titre span').removeClass('pageActive');
            $(this).addClass('pageActive');
            if($(this).attr('id') == 'btn-stats'){
                stats(e,time);
            }
            if($(this).attr('id') == 'btn-classement'){
                classement(e,time);
            }
            if($(this).attr('id') == 'btn-options'){
                options(e,time);
            }

        });

        var id = localStorage.ogx_msgId;
        if(id.length > 432000){
            localStorage.ogx_msgId = id.substr(id.length-216000,id.length);
        }
        var idc = localStorage.ogx_msgIdCombat;
        if(idc.length > 216000){
            localStorage.ogx_msgIdCombat = idc.substr(idc.length-108000,idc.length);
        }

    }

    /*
    Cr"ation du bouton pour voir les stats
    */
    $('<div class="menu-item">').appendTo('#left-menu-1');
    $('div.menu-item:last-of-type').html('<a id="stats-expe" href="#" class="text-item">Stats Expe</a>');
    $('#stats-expe').click(function(e){
        e.preventDefault();
        false;
        $('div.menu-item').removeClass('active');
        $(this).parent().addClass('active');
        displayStats(Date.now());

    });
    /*
    CSS
    */
    function cssStyle(){
        GM_addStyle(`
                .expe tr:nth-of-type(odd){
                    background-color: #242b36;
                }
                .expe tr:nth-of-type(even){
                    background-color: #0D0E10;
                }
                .expe tr:hover,
                .section div.titre_section span:hover{
                    background-color: #424851;
                }
                .titre{
                    position: relative;
                    text-align: left;
                    padding: 0px 0px 15px 0px;
                    font-size: 16px;
                    color: #888;
                }
                .titre span{
                    padding: 20px 10px;
                    cursor: pointer;
                }
                .titre span:hover{
                    color: white;
                }
                .titre .pageActive{
                    color: #ddd;;
                }
                .titresmall{
                    position: absolute;
                    right: 0px;
                    top:0px;
                    padding: 0px 0px 15px 0px;
                    font-size: 10px;
                    color: #aaa;
                }
                .expe .left{
                    width: 49%;
                    float:left;
                }
                .expe .right{
                    width: 49%;
                    float:right;
                }
                .expe{
                   background-color: #000;
                   color: #ddd;
                   font-size: 11px;
                   border: 4px solid #2B3F5A;
                   padding: 10px;
                   margin: 20px;
                }
                .expe table{
                    width: 100%;
                }
                .expe tr{
                    //border-top: 1px solid #424851;
                    //border-bottom: 1px solid #424851;
                }
                .expe td{
                    padding: 8px;
                    vertical-align: middle;
                    width: 110px;
                    text-align: right;
                }
                .classement td{
                    padding: 8px 4px;
                    vertical-align: middle;
                    text-align: center;
                }
                .classement td:nth-of-type(1){
                    width: 15px;
                }
                .classement td:nth-of-type(2){
                    width: 100px;
                }
                .classement td:nth-of-type(3),.classement td:nth-of-type(4){
                    width: 20px;
                }
                .stats td:first-of-type{
                    padding-right: 0px;
                    width: 0px;
                    text-align: left;
                    color: #2f2;
                }
                .section{
                    margin-top: 15px;
                    border: 3px solid #424851;
                }
                .section div.titre_section{
                    width: 100%;
                    color: orange;
                    font-size: 13px;
                    padding: 15px 0px;
                    position: relative;
                    text-align: center;
                    background-color: #131a25;
                }
                .section div.titre_section span{
                    position: absolute;
                    top: 0;
                    padding: 15px 7px;
                    height: 100%;
                    z-index: 1000;
                    cursor: pointer;
                }
                .section div.titre_section span:nth-of-type(1){
                    left: 0px;
                    border-right: 1px solid #424851;
                }
                .section div.titre_section span:nth-of-type(2){
                    right: 0px;
                    border-left: 1px solid #424851;
                }

            `);
    }
    function stats(e,time){
        var data_jours = JSON.parse(e.ogx_jours);
        var ogx_expe = JSON.parse(e.ogx_expe);
        var ogx_gain = JSON.parse(e.ogx_gain);
        var ogx_options = JSON.parse(e.ogx_options);
        var expeParJour = (Math.round(ogx_expe.total / ((parseInt(Date.now()) - parseInt(ogx_options.start)) / 86400000)* 100)/100).toLocaleString(["ban", "id"]);

        var stats = '';
        stats += '<div class="stats">';
        stats += '<div class="left">';
        stats += '<div class="section">';
        stats += '<div class="titre_section">Répartition des expéditions</div>';
        stats += '<table>';
        stats += '<tr><td>Ressources</td>'+ showStats(ogx_expe.ressources,ogx_expe.total) +'</tr>';
        stats += '<tr><td>Vaisseaux</td>'+ showStats(ogx_expe.vaisseaux,ogx_expe.total) +'</tr>';
        stats += '<tr><td>Pirates</td>'+ showStats(ogx_expe.pirates,ogx_expe.total) +'</tr>';
        stats += '<tr><td>Aliens</td>'+ showStats(ogx_expe.aliens,ogx_expe.total) +'</tr>';
        stats += '<tr><td>Antimatière</td>'+ showStats(ogx_expe.am,ogx_expe.total) +'</tr>';
        stats += '<tr><td>Container</td>'+ showStats(ogx_expe.container,ogx_expe.total) +'</tr>';
        stats += '<tr><td>Rien</td>'+ showStats(ogx_expe.rien,ogx_expe.total) +'</tr>';
        stats += '<tr><td>Retard</td>'+ showStats(ogx_expe.retard,ogx_expe.total) +'</tr>';
        stats += '<tr><td>Avance</td>'+ showStats(ogx_expe.avance,ogx_expe.total) +'</tr>';
        stats += '<tr><td>Trous Noir</td>'+ showStats(ogx_expe.tn,ogx_expe.total) +'</tr>';
        stats += '<tr><td>Total</td><td>' + ogx_expe.total + '</td><td>'+ expeParJour +' / J</td></tr>';
        stats += '</table>';
        stats += '</div>';
        stats += '<div class="section">';
        stats += '<div class="titre_section">Ressources  - <small>'+pcValeurEnMetal(ogx_gain,'r')+'</small></div>';
        stats += '<table>';
        stats += '<tr><td colspan="2">Métal</td><td>' + parseInt(ogx_gain.ress['metal']).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Cristal</td><td>' + parseInt(ogx_gain.ress['cristal']).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Deutérium</td><td>' + parseInt(ogx_gain.ress['deut']).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Valeur en métal</td><td>' + valeurEnMetal(ogx_gain.ress) + '</td></tr>';
        stats += '</table>';
        stats += '</div>';
        stats += '<div class="section">';
        stats += '<div class="titre_section">Ressources Combats  - <small>'+pcValeurEnMetal(ogx_gain,'c')+'</div>';
        stats += '<table>';
        stats += '<tr><td colspan="2">Métal</td><td>' + parseInt(ogx_gain.combat['metal']).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Cristal</td><td>' + parseInt(ogx_gain.combat['cristal']).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Deutérium</td><td>' + parseInt(ogx_gain.combat['deut']).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Valeur en métal</td><td>' + valeurEnMetal(ogx_gain.combat) + '</td></tr>';
        stats += '</table>';
        stats += '</div>';
        stats += '<div class="section" id="afficherJour">';

        stats += '</div>';
        var nbJour = (Date.now() - parseInt(ogx_options.start)) / 86400000;
        stats += '<div class="section">';
        stats += '<table>';
        stats += '<div class="titre_section">Moyenne par jour</div>';
        stats += '<table>';
        stats += '<tr><td colspan="2">Métal</td><td>' + gainMoyen(ogx_gain.ress_total.metal, nbJour) + '</td></tr>';
        stats += '<tr><td colspan="2">Cristal</td><td>' + gainMoyen(ogx_gain.ress_total.cristal, nbJour) + '</td></tr>';
        stats += '<tr><td colspan="2">Deutérium</td><td>' + gainMoyen(ogx_gain.ress_total.deut, nbJour) + '</td></tr>';
        stats += '<tr><td colspan="2">Valeur en métal</td><td>' + gainMoyen(parseInt(ogx_gain.ress_total.metal)+1.5*parseInt(ogx_gain.ress_total.cristal)+3*parseInt(ogx_gain.ress_total.deut), nbJour) + '</td></tr>';
        stats += '</table>';
        stats += '</div>';
        stats += '</div>';
        stats += '<div class="right">';
        stats += '<div class="section">';
        stats += '<div class="titre_section">Vaisseaux trouvés</div>';
        stats += '<table>';
        stats += '<tr><td colspan="2">Light Cargo</td><td>' + parseInt(ogx_gain.flotte['LightCargo']).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Heavy Cargo</td><td>' + parseInt(ogx_gain.flotte['HeavyCargo']).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Recycler</td><td>' + parseInt(ogx_gain.flotte['Recycler']).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Light Fighter</td><td>' + parseInt(ogx_gain.flotte['LightFighter']).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Heavy Fighter</td><td>' + parseInt(ogx_gain.flotte['HeavyFighter']).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Cruiser</td><td>' + parseInt(ogx_gain.flotte['Cruiser']).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Battleship</td><td>' + parseInt(ogx_gain.flotte['Battleship']).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Battle Cruiser</td><td>' + parseInt(ogx_gain.flotte['BattleCruiser']).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Planet Bomber</td><td>' + parseInt(ogx_gain.flotte['PlanetBomber']).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Destroyer</td><td>' + parseInt(ogx_gain.flotte['Destroyer']).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Reaper</td><td>' + parseInt(ogx_gain.flotte['Reaper']).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '</table>';
        stats += '</div>';
        stats += '<div class="section">';
        stats += '<div class="titre_section">Ressources Flotte  - <small>'+pcValeurEnMetal(ogx_gain,'f')+'</div>';
        stats += '<table>';
        stats += '<tr><td colspan="2">Métal</td><td>' + parseInt(ogx_gain.ress_flotte['metal']).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Cristal</td><td>' + parseInt(ogx_gain.ress_flotte['cristal']).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Deutérium</td><td>' + parseInt(ogx_gain.ress_flotte['deut']).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Valeur en métal</td><td>' + valeurEnMetal(ogx_gain.ress_flotte) + '</td></tr>';
        stats += '</table>';
        stats += '</div>';
        stats += '<div class="section">';
        stats += '<div class="titre_section">Ressources Total</div>';
        stats += '<table>';
        stats += '<tr><td colspan="2">Métal</td><td>' + parseInt(ogx_gain.ress_total['metal']).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Cristal</td><td>' + parseInt(ogx_gain.ress_total['cristal']).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Deutérium</td><td>' + parseInt(ogx_gain.ress_total['deut']).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Valeur en métal</td><td>' + valeurEnMetal(ogx_gain.ress_total) + '</td></tr>';
        stats += '</table>';
        stats += '</div>';
        stats += '<div class="section">';
        stats += '<div class="titre_section">Moyenne par expédition</div>';
        stats += '<table>';
        stats += '<tr><td colspan="2">Métal</td><td>' + gainMoyen(ogx_gain.ress_total.metal, ogx_expe.total) + '</td></tr>';
        stats += '<tr><td colspan="2">Cristal</td><td>' + gainMoyen(ogx_gain.ress_total.cristal, ogx_expe.total) + '</td></tr>';
        stats += '<tr><td colspan="2">Deutérium</td><td>' + gainMoyen(ogx_gain.ress_total.deut, ogx_expe.total) + '</td></tr>';
        stats += '<tr><td colspan="2">Valeur en métal</td><td>' + valeurEnMetal(ogx_gain.ress_total,ogx_expe.total) + '</td></tr>';
        stats += '</table>';
        stats += '</div>';
        stats += '<div class="section">';
        stats += '<div class="titre_section">Antimatière</div>';
        stats += '<table>';
        stats += '<tr><td colspan="2">Moyenne des gains d\'am</td><td>' + Math.round(parseInt(ogx_gain.am)/ogx_expe.am).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Moyenne par expédition</td><td>' + Math.round(parseInt(ogx_gain.am)/ogx_expe.total).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Moyenne par jour</td><td>' + Math.round(parseInt(ogx_gain.am)/nbJour).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '<tr><td colspan="2">Total</td><td>' + parseInt(ogx_gain.am).toLocaleString(["ban", "id"]) + '</td></tr>';
        stats += '</table>';
        stats += '</div>';
        stats += '</div>';
        stats += '<div style="clear:both"></div>';
        stats += '</div>';
        $('#contenu').html(stats);

        afficherJour(time);
    }
    function options(e,time){
        var ogx_options = JSON.parse(e.ogx_options);
        var ogx_dateInput = dateStart(ogx_options.start);
        var options = '';
        options += '<div class="stats">';
        options += '<div class="left">';
        options += '<div class="section">';
        options += '<div class="titre_section">Statistiques expéditions</div>';
        options += '<table>';
        options += '<tr>';
        options += '<td colspan="2">Date de début</td>';
        options += '<td><input type="date" id="date2" value="'+ogx_dateInput.date+'" ><br />';
        options += '<input type="time" id="time" value="'+ogx_dateInput.time+'">';
        options += '<input type="button" id="valider" value="valider"></td>';
        options += '<tr></tr>';
        options += '</table>';
        options += '</div>';
        options+= '</div>';
        options += '<div class="right">';
        options += '<div class="section">';
        options += '<div class="titre_section">Divers</div>';
        options += '<table>';
        options += '<td colspan="2">Liste des planètes: </td>';
        options += '<td><input size="1" id="colonnePlanet" value="'+ogx_options.colonnePlanet+'" /> colonnes</td>';
        options += '</tr>';
        options += '</table>';
        options += '</div>';
        options += '</div>';
        options += '<div style="clear:both"></div>';
        options += '</div>';
        $('#contenu').html(options);

        $('#valider').click(function(){
            console.log(Date.parse($('#date2').val() + ' ' + $('#time').val()));

            var start = Date.parse($('#date2').val() + ' ' + $('#time').val());
            ogx_options.start = start;
            localStorage.ogx_options = JSON.stringify(ogx_options);

        });
        /*
            Nombre de colonnes liste de planètes
            */
        if(!ogx_options.colonnePlanet){
            ogx_options.colonnePlanet = 1;
            localStorage.ogx_options = JSON.stringify(ogx_options);
        }
        $('#colonnePlanet').keyup(function(){

            ogx_options.colonnePlanet = $(this).val();
            localStorage.ogx_options = JSON.stringify(ogx_options);
            colonePlanet()

        });
    }
    function classement(e,time){
        var post = new Object();
        post.mypid = $('head').html().split("window.currentPlayerId = '")[1].split("';")[0];
        post.data_jours = localStorage.ogx_jours;
        post.data_jours = JSON.parse(post.data_jours);
        post.pseudo = currentPlayerName;
        post.univers = href.split('https://')[1].split('.ogamex.net')[0];
        post.user_id = currentPlayerId;
        $.post("https://www.fedbac.fr/ogx_ajax.php", post, function(json){
            var classement = '';
            classement += '<div class="classement">';
            classement += '<div class="section">';
            classement += '</div>';
            classement += '</div>';
            $('#contenu').html(classement);
            if(json == 'no'){

                var html = '<div style="line-height: 1.5em; font-size: 15px;">Accès restreint !<br />';
                html += "Communique ton ID suivant à qui tu sais<br />";
                html += post.mypid;
                html += "</div>";
                $('.classement .section').html(html);
            }else{
                afficherClassementJour(time,JSON.parse(json))
            }
        }, "text");

    }
    /*
    Edition de la date de début des expéditions
    */
    function dateStart(start){
        var ogx_d = new Object();
        ogx_d.Y = new Date(start).getFullYear();
        ogx_d.M = new Date(start).getMonth()+1;
        if(ogx_d.M < 10){ ogx_d.M = '0'+ogx_d.M; }
        ogx_d.D = new Date(start).getDate();
        if(ogx_d.D < 10){ ogx_d.D = '0'+ogx_d.D; }
        ogx_d.h = new Date(start).getHours();
        if(ogx_d.h < 10){ ogx_d.h = '0'+ogx_d.h; }
        ogx_d.m = new Date(start).getMinutes();
        if(ogx_d.m < 10){ ogx_d.m = '0'+ogx_d.m; }
        ogx_d.date = ogx_d.Y+'-'+ogx_d.M+'-'+ogx_d.D;
        ogx_d.time = ogx_d.h+':'+ogx_d.m;
        //console.log(ogx_d);
        return ogx_d;
    }
    /*
    Moyenne des expéditions pat jour ou par expéditions
    */
    function gainMoyen(gain, nb){
        var moyenne =  ((parseInt(gain) / nb));
        moyenne = Math.round(moyenne).toLocaleString(["ban", "id"]);
        return moyenne;
    }
    /*
    Equivalent des ressources en métal
    */
    function valeurEnMetal(gain,nb = 1){

        return (Math.round(((parseInt((gain.metal))) + ((1.5*parseInt(gain.cristal)))+((3*parseInt(gain.deut))))/nb)).toLocaleString(["ban", "id"]);
    }
    /*
    Pourcentage des gains Ressources, Flottes et Combats
    en fonction de la valeur en métal
    */
    function pcValeurEnMetal(gain,type){
        var t = parseInt(valeurEnMetal(gain.ress_total).replace(/\./g,''));
        var r = parseInt(valeurEnMetal(gain.ress).replace(/\./g,''));
        var f = parseInt(valeurEnMetal(gain.ress_flotte).replace(/\./g,''));
        var c = parseInt(valeurEnMetal(gain.combat).replace(/\./g,''));

        if(type == 'r' ){
            return ((Math.round((r / t) * 10000))/100).toLocaleString(["ban", "id"]) + '%';
        }
        if(type == 'f' ){
            return ((Math.round((f / t) * 10000))/100).toLocaleString(["ban", "id"]) + '%';
        }
        if(type == 'c' ){
            return ((Math.round((c / t) * 10000))/100).toLocaleString(["ban", "id"]) + '%';
        }
    }
    /*
    Timestamp(ms) vers Date (ex: 18.06.2021)
    Sert pour classer l'affichage des gains journalier
    */
    function timeToDate(time){
        var Y = new Date(time).getFullYear();
        var M = new Date(time).getMonth()+1;
        if(M < 10){ M = '0'+M; }
        var D = new Date(time).getDate();
        if(D < 10){ D = '0'+D; }
        var H = new Date(time).getHours();

        return D+'.'+M+'.'+Y;
    }
    /*
    Affichage des gains journalier
    */
    function afficherJour(time){
        var prev = time - 86400000;
        var next = time + 86400000;
        var date = timeToDate(time);
        var jours = localStorage.ogx_jours;
        jours = JSON.parse(jours);
        if(!jours[date]){
            jours[date] = new Array();
            jours[date]['metal'] = 0;
            jours[date]['cristal'] = 0;
            jours[date]['deut'] = 0;
            jours[date]['nbexpe'] = 0;
            jours[date]['nbcombat'] = 0;
        }
        var data = jours[date];
        var str = '';
        str += '<div id="jour" class="titre_section">';
        str += date+' (Expé: '+ data['nbexpe'] +' | Combat: '+ data['nbcombat'] +')';
        str += '<span data-time="'+prev+'"><i class="fas fa-angle-left"></i></span> ';
        str += '<span data-time="'+next+'"><i class="fas fa-angle-right"></i></span>';
        str += '</div>';
        str += '<table>';
        str += '<tr><td colspan="2">Métal</td><td>' + parseInt(data['metal']).toLocaleString(["ban", "id"]) + '</td></tr>';
        str += '<tr><td colspan="2">Cristal</td><td>' + parseInt(data['cristal']).toLocaleString(["ban", "id"]) + '</td></tr>';
        str += '<tr><td colspan="2">Deutérium</td><td>' + parseInt(data['deut']).toLocaleString(["ban", "id"]) + '</td></tr>';
        str += '<tr><td colspan="2">Valeur en métal</td><td>' + valeurEnMetal(data) + '</td></tr>';
        str += '</table>';

        $('#afficherJour').html(str);
        $('#jour span:eq(0)').click(function(){

            afficherJour(prev,jours)
        });
        $('#jour span:eq(1)').click(function(){

            afficherJour(next,jours)
        });
    }
    function afficherClassementJour(time,jours){
        var prev = time - 86400000;
        var next = time + 86400000;
        var date = timeToDate(time);

        if(!jours[date]){
            jours[date] = new Array();
            jours[date]['metal'] = 0;
            jours[date]['cristal'] = 0;
            jours[date]['deut'] = 0;
            jours[date]['nbexpe'] = 0;
            jours[date]['nbcombat'] = 0;
        }
        var tabData = jours[date];
        var data = '';
        var str = '';
        str += '<div id="classementJours" class="titre_section">';
        str += date;
        str += '<span data-time="'+prev+'"><</span> ';
        str += '<span data-time="'+next+'">></span>';
        str += '</div>';
        str += '<table>';
        str += '<tr><td>#</td><td>Joueurs</td><td>Expé</td><td>Cbts</td><td>Métal</td><td>Cristal</td><td>Deutérium</td>';
        for(var i = 0; i < tabData.length; i++){
            data = tabData[i];

            str += '<tr>';
            str += '<td>' + parseInt(i+1) + '</td>';
            str += '<td>' + data['pseudo'] + '</td>';
            str += '<td>' + data['nbexpe'] + '</td>';
            str += '<td>' + data['nbcombat'] + '</td>';
            str += '<td>' + parseInt(data['metal']).toLocaleString(["ban", "id"]) + '</td>';
            str += '<td>' + parseInt(data['cristal']).toLocaleString(["ban", "id"]) + '</td>';
            str += '<td>' + parseInt(data['deut']).toLocaleString(["ban", "id"]) + '</td>';
            //str += '<td>' + valeurEnMetal(data) + '</td>';
            str += '</tr>';
        }
        str += '</table>';

        $('.classement .section').html(str);
        $('#classementJours span:eq(0)').click(function(){

            afficherClassementJour(prev,jours)
        });
        $('#classementJours span:eq(1)').click(function(){

            afficherClassementJour(next,jours)
        });
    }
    /*
    Nombre de colonnes pour la liste des planètes
    */
    function colonePlanet(){
        var options = localStorage.ogx_options;
        options = JSON.parse(options);
        $('#other-planets').css('min-height','805px');
        $('#other-planets').css('column-count',options.colonnePlanet);
        $('#other-planets').css('width',options.colonnePlanet * 150+'px');
    }
   

    $('#other-planets .selected:eq(0)').css('background-color','#aaa')
    $('#other-planets .selected:eq(0) span').css('color','#000')
    $('#other-planets .selected:eq(0) span').css('font-weight','bold')
    
    if($('.fleet-info-section').hasClass('hostile')){
        console.log('cool');

        $('<div class="alert-attaque">').appendTo('#header');
        $('.alert-attaque').css('position','absolute');
        $('.alert-attaque').css('a-index','10000');
        $('.alert-attaque').css('left','400px');
        $('.alert-attaque').css('top','80px');
        $('.alert-attaque').css('height','80px');
        $('.alert-attaque').css('width','201px');
        $('.alert-attaque').css('border','5px solid red');
        $('.alert-attaque').css('background-color','#700');
        $('.alert-attaque').css('color','yeallow');
        $('.alert-attaque').css('color','yellow');
        $('.alert-attaque').css('padding','7px 20px');
        $('.alert-attaque').css('text-align','center');
        $('.alert-attaque').css('font-size','14px');
        $('.alert-attaque').css('line-height','1.3em');
        $('.alert-attaque').html("Attention tu te fais attaquer mon p'tit lapin");

    }

    localStorageCreate();
    clickExpe();
    clickCombat();
    clickAst();
    colonePlanet()

})();