mandalorien / Sethi

// ==UserScript==
// @namespace    https://openuserjs.org/users/mandalorien
// @name         Sethi
// @author mandalorien
// @version      0.1.1
// @description  Analyse la galaxy
// @copyright 2020, mandalorien (https://openuserjs.org/users/mandalorien)
// @license GPL-2.0-only
// @require      https://code.jquery.com/jquery-3.4.1.min.js
// @require      https://openuserjs.org/install/mandalorien/Sethi_-_Functions.user.js
// @match        https://*.ogame.gameforge.com/game/index.php?page=ingame&component=galaxy*
// @match        https://*.ogame.gameforge.com/game/index.php?page=messages*
// @match        https://*.ogame.gameforge.com/game/index.php?page=ingame&component=preferences*
// @updateURL https://openuserjs.org/meta/mandalorien/Sethi.meta.js
// @downloadURL https://openuserjs.org/install/mandalorien/Sethi.user.js
// @grant        GM_xmlhttpRequest
// @grant        GM_addStyle
// @run-at       document-end
// ==/UserScript==

// ==OpenUser JS==
// @author mandalorien
// ==OpenUser JS==

var db;
var KEYS = [37,38,39,40]; //left,high,right,low
var META_UNIVERSE = $('meta[name="ogame-universe"').attr("content");
var UNIVERSE = 0;
const regex = /s(.*)\-|U/;
let m;
var _LimitG = 9;
var _LimitS = 499;

$(document).ready(function(){


    if ((m = regex.exec(META_UNIVERSE)) !== null) {
        // The result can be accessed through the `m`-variable.

        if(window.openDatabase){
            db = openDatabase('sethi', '1.0', 'database', 1000000000);
            db.transaction(function (tx) {
                tx.executeSql('CREATE TABLE IF NOT EXISTS GALAXIES (Action,Universe,PlayerName,PlayerAlliance,PlanetName,HaveMoon,SizeMoon,Galaxy,System,Planet,IsNewPlanet,TimerPlanet,TimerMoon)');
            });
        }

        UNIVERSE = parseFloat(m[1]);
    }


    var GALAXY = parseFloat($("#galaxy_input").val());
    var SYSTEM = parseFloat($("#system_input").val());

    if($_GET('page') == 'ingame'){

        if($_GET('component') == 'preferences'){
            var _G = 1;
            var _S = 1;
            var refresh = setInterval(function(){
                if(_G == _LimitG && _S == _LimitS){
                    clearInterval(refresh);
                }

                if(_S > 499){
                    _G += 1;
                    _S = 1;
                }
                console.log("GALAXY - SYSTEM",_G,_S);
                letsGo(UNIVERSE,_G,_S);
                _S ++;
            },500);

        }else{
            letsGo(UNIVERSE,GALAXY,SYSTEM);
        }
         /*
        letsGo(UNIVERSE,GALAXY,SYSTEM);
        */
    }

    // When a player uses directional keys in the galaxy.
    $(document).on('keyup',function(e) {
        if(jQuery.inArray(e.keyCode, KEYS) !== -1){
            if($_GET('page') == 'ingame'){
                GALAXY = parseFloat($("#galaxy_input").val());
                SYSTEM = parseFloat($("#system_input").val());
                letsGo(UNIVERSE,GALAXY,SYSTEM);
            }
        }
    });

    // when a player click in button "Let's go"
    $(document).on('click','.btn_blue',function() {
        if($_GET('page') == 'ingame'){
            GALAXY = parseFloat($("#galaxy_input").val());
            SYSTEM = parseFloat($("#system_input").val());
            letsGo(UNIVERSE,GALAXY,SYSTEM);
        }
    });


    $(document).on('click',function(e) {
        console.log($_GET('page'));
        if($_GET('page') == 'ingame'){
            let _CLICK_EVENT = $(e.target).parent().attr('data-coords');
            if(_CLICK_EVENT != undefined){
                GALAXY = parseFloat($("#galaxy_input").val());
                SYSTEM = parseFloat($("#system_input").val());
                letsGo(UNIVERSE,GALAXY,SYSTEM);
            }
        }
    });
});

/****
 * PARAM : UNIVERSE is number of universe
 * PARAM : GALAXY is number current galaxy focus
 * PARAM : SYSTEM is number current galaxy focus
 *
****/
// https://xxx-fr.ogame.gameforge.com/game/index.php?page=ingame&component=galaxyContent&ajax=1

function letsGo(UNIVERSE,GALAXY,SYSTEM){
    console.log("GALAXY - SYSTEM",GALAXY,SYSTEM);

    let _R;
    let _Data =  new FormData();

    _R = 'https://s'+ UNIVERSE +'-fr.ogame.gameforge.com/game/index.php?page=ingame&component=galaxyContent&ajax=1';

    _Data.append('galaxy',GALAXY);
    _Data.append('system',SYSTEM);

    $.ajax({
        method: "POST",
        processData: false,
        contentType: false,
        url: _R,
        data: _Data,
        dataType:'json'
    })
        .done(function(_RESULT){

        let _HTML = $.parseHTML(_RESULT.galaxy);

        let mobileDIV = _HTML[4];
        let _TR = $(mobileDIV).find("#galaxytable tbody tr:not(.empty_filter)");

        $(_TR).each(function(Index,tr){

            var POSITION = parseFloat($(tr).find('.position').text());
            var PLANETNAME = ($(tr).find('.planetname').text()).trim();
            var PLAYERNAME = ($(tr).find('.playername a span').text()).trim();
            var ALLIANCE = ($(tr).find('span.allytagwrapper').text()).trim();
            var MOON = ($(tr).find('.moon').attr('rel') == undefined ? 0 : 1);
            var TIMERPLANET = $(tr).find('.microplanet').find('.activity');
            var TIMERMOON = $(tr).find('.moon').find('.activity'); //showMinutes .text
            var TP = -1;
            if(TIMERPLANET.length > 0){
                if($(TIMERPLANET).attr('class').indexOf('showMinutes') >= 0){ // that means it has exceeded 15!
                    TP = parseFloat($(TIMERPLANET).text().trim());
                }else if($(TIMERPLANET).attr('class').indexOf('minute15') >= 0){
                    TP = 0;
                }else{
                    TP = -1;
                }
            }

            var TM = -1;
            if(TIMERMOON.length > 0){
                if($(TIMERMOON).attr('class').indexOf('showMinutes') >= 0){ // that means it has exceeded 15!
                    TM = parseFloat($(TIMERMOON).text().trim());
                }else if($(TIMERMOON).attr('class').indexOf('minute15') >= 0){
                    TM = 0;
                }else{
                    TM = -1;
                }
            }

            let _DataObj = {
                Action:'PUT',
                Universe:UNIVERSE,
                PlayerName:PLAYERNAME,
                PlayerAlliance:ALLIANCE,
                PlanetName:PLANETNAME,
                HaveMoon:MOON,
                SizeMoon:0,
                Galaxy:GALAXY,
                System:SYSTEM,
                Planet:POSITION,
                IsNewPlanet:true,
                TimerPlanet:TP,
                TimerMoon:TM
            };

            let _Data = [
                'PUT',
                UNIVERSE,
                PLAYERNAME,
                ALLIANCE,
                PLANETNAME,
                MOON,
                0,
                GALAXY,
                SYSTEM,
                POSITION,
                true,
                TP,
                TM
                ];

            if(window.openDatabase){
                db.transaction(function (tx) {
                    /* GALAXIES Action,Universe,PlayerName,PlayerAlliance,PlanetName,HaveMoon,SizeMoon,Galaxy,System,Planet,IsNewPlanet,TimerPlanet,TimerMoon */

                    var _REQ = 'INSERT INTO GALAXIES ';
                    _REQ += '(Action,Universe,PlayerName,PlayerAlliance,PlanetName,HaveMoon,SizeMoon,Galaxy,System,Planet,IsNewPlanet,TimerPlanet,TimerMoon) ';
                    _REQ += 'VALUES ';
                    _REQ += '(?,?,?,?,?,?,?,?,?,?,?,?,?) ';
                    console.log(_REQ);
                    tx.executeSql(_REQ,_Data, function (tx, results) {
                        console.log("save : "+ _Data[2]);
                    });


                });
            }

        });
    });
}