server time
Published: 2020-06-30 01:36:02

hello everyone so I have a problem with the script and specifically this script should calculate the time twice and when it will send the attack (command) and the time when it will come back to me

problem is this server have lags (is write in server info mns)

they need to put the data into a script when calculating the time


skript 


const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
const isNumber = d => '0' <= d && d <= '9';
const getDate = date => {
    return {
        h: date.getHours().toString().padStart(2, 0),
        m: date.getMinutes().toString().padStart(2, 0),
        s: date.getSeconds().toString().padStart(2, 0),
        ms: date.getMilliseconds().toString().padStart(3, 0)
    }
}


let formData = {}
let time = {};


let year = (new Date()).getFullYear();
let month = (new Date()).getMonth();
let day =  (new Date()).getDate();

function setParam(name, value) {
    localStorage.setItem(name, value);
}

let attacks = document.getElementById('commands_incomings').getElementsByClassName('command-row no_ignored_command');
let x;
for(var i = 0; i < attacks.length; i++){
    x = attacks[i].insertCell(1);
    x.setAttribute('style','width:5px');
    x.innerHTML= '<span><button id="atk'+i+'" name="sendSnipe"><img class="quickbar_image"'
        +'data-src="https://dspl.innogamescdn.com/asset/28fc2c0/graphic//buildings/barracks.png" alt=""'+
        'src="https://dspl.innogamescdn.com/asset/28fc2c0/graphic//buildings/barracks.png"></button></span>';
    k = 'atk'+i;
    document.getElementById(k).addEventListener('click', command);
}

$('button[name=sendSnipe]').click(function(e){
    e.preventDefault();
});

function command(ind){
    var command_url, time_aux = [], id;
    $('#serverTime').click();
    console.log(ind);
    id = '#'+ind.path[1].id;
    if(id == '#') id = '#' + ind.path[0].id;
    if(id == '#') id = '#' + ind.path[2].id;
    command_url = $($(id).parent().parent().parent()).find('td:eq(0) .quickedit-content a:eq(0)').attr('href');
    console.log(id);
    console.log(command_url);

    console.log($(id));
    console.log($(id).parent().parent().parent());
    console.log($($(id).parent().parent().parent()).find('td:eq(2)'));
    time_aux = $($(id).parent().parent().parent()).find('td:eq(2)').text().match(/[+-]?\d+(?:\.\d+)?/g);
    console.log(time_aux);
    time.hour = parseInt(time_aux[0]);
    time.minute = parseInt(time_aux[1]);
    time.second = parseInt(time_aux[2]);
    time.millisecond = parseInt(time_aux[3]);
    console.log(time);
    console.log('jestem tu')

    $.ajax({
        type: 'GET',
        url: command_url + "&client_time=" + Math.round(Timing.getCurrentServerTime() / 1e3),
        headers: {
            "TribalWars-Ajax": 1
        },
        success: function (data) {
            console.log('entrou aqui.');
            var coord = [], villageToAttackId;
            coord = $(data).find('span.village_anchor:eq(0) a').text().match(/(\d\d\d)(\d\d\d)*(?!.*(\d\d\d)\|(\d\d\d))/g);
            villageToAttackId = parseInt($(data).find('span.village_anchor:eq(0)').data('id'));
            formData.x = coord[0];
            formData.y = coord[1];
            formData.source_village = game_data.village.id;
            console.log(formData);
            firstRequest(villageToAttackId);
        },
        error: function(data){
            console.log(data.error);
        }
    });
}

function firstRequest(villageToAttackId) {
    $.ajax({
        type: "GET",
        url: "/game.php?village=" + formData.source_village + "&screen=place&ajax=command&target=" + villageToAttackId + "&client_time=" + Math.round(Timing.getCurrentServerTime() / 1e3),
        data: {},
        dataType: "json",
        headers: {
            "TribalWars-Ajax": 1
        },
        success: function (data) {
            if (!data.error) {
                data = $(data.response.dialog);
                formData.spear = $('#unit_input_spear', data).data('all-count');
                formData.sword = $('#unit_input_sword', data).data('all-count');
                formData.axe = $('#unit_input_axe', data).data('all-count');
                formData.archer = $('#unit_input_archer', data).data('all-count');
                formData.spy = $('#unit_input_spy', data).data('all-count');
                formData.marcher = $('#unit_input_marcher', data).data('all-count');
                formData.light = $('#unit_input_light', data).data('all-count');
                formData.heavy = $('#unit_input_heavy', data).data('all-count');
                formData.ram = $('#unit_input_ram', data).data('all-count');
                formData.catapult = $('#unit_input_catapult', data).data('all-count');
                formData.snob = $('#unit_input_snob', data).data('all-count');
                formData.attack = 'l';
                console.dir(formData);
                secondRequest();
            }
        },
        error: function (data) {
            console.log(data.error);
        }
    });
}

function secondRequest() {
    $.ajax({
        type: "POST",
        url: "/game.php?village=" + formData.source_village + "&screen=place&ajax=confirm&h=" + csrf_token + "&client_time=" + Math.round(Timing.getCurrentServerTime() / 1e3),
        data: formData,
        dataType: "json",
        headers: {
            "TribalWars-Ajax": 1
        },
        success: function (data) {
            console.log(data);
            if (!data.error) {
                console.log('dupaasf');
                var date, s, m, ms, y, sn, duration, data_aux;
                data_aux = $(data.response.dialog);
                console.log(data_aux);
                duration = $('span.relative_time', data_aux).data('duration');
               // $('#attack_name', data_aux).val(time.hour.toString().padStart(2, 0) + ':' + time.minute.toString().padStart(2, 0) + ':' + time.second.toString().padStart(2, 0));
                formData.data = data_aux.serialize();
                let date_attack = (new Date(year, month, day, time.hour, time.minute, time.second));
                let date_attack_time = date_attack.getTime()

                setInterval(function () {
                    date = new Date(Timing.getCurrentServerTime()), s = date.getSeconds(), m = date.getMinutes(), y, sn;
                    x = (s + (m * 60));
                    sn = (time.minute * 60 + time.second) - x;
                    if (!(sn % 2)) {
                        sn = sn - 6;
                        setInterval(function () {
                            date = new Date(Timing.getCurrentServerTime()), ms = date.getMilliseconds();
                            y = (time.minute * 60 + time.second) - sn;
                            x = (s + (m * 60));
                            if (x == y && ms > (time.millisecond + 20) && ms <= (time.millisecond + 70)) {
                                let nametime = new Date(date.getTime() + Math.round((date_attack_time - (new Date).getTime()) / 2));
                                console.log(nametime);
                                $('#attack_name', data_aux).val(getDate(nametime).h + ':' + getDate(nametime).m  + ':' + getDate(nametime).s);
                                formData.data = data_aux.serialize();

                                thirdRequest(s, m, duration, sn, date);
                            }
                        }, 1);
                    }
                }, 1000);
            }
        },
        error: function (data) {
            console.log(data.error);
            console.log('dupa');
        }
    });
}

function thirdRequest(s, m, duration, sn, date) {
    setTimeout(function () {
        console.log('jestem')
        console.log(s, m, duration, sn, date)
        $.ajax({
            url: "/game.php?village=" + formData.source_village + "&screen=place&ajaxaction=popup_command&h=" + csrf_token + "&client_time=" + Math.round(Timing.getCurrentServerTime() / 1e3),
            data: formData.data,
            type: "POST",
            dataType: "json",
            headers: {
                "TribalWars-Ajax": 1
            },
            success: function () {
                setParam('id', time.hour + ':' + time.minute + ':' + time.second);
                setParam('envio', s + (m * 60));
                setParam('data', Date.parse(date));
                setParam('try', sn);
                setParam('duration', duration);
                location.reload();
            },
            error: function (data) {
                console.log(data.error);
            }
        });
    }, 10);
}