TheMaximus / AutoZbieractwo 3 lvl TYLKO LK Cactus

// ==UserScript==
// @name         AutoZbieractwo 3 lvl TYLKO LK Cactus
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  free surki v2
// @author       Maxi
// @match        https://pl138.plemiona.pl/game.php?village=6218&screen=place&mode=scavenge
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    function selectLight() {
        document.querySelector("#scavenge_screen > div > div.candidate-squad-container > table > tbody > tr:nth-child(2) > td:nth-child(5) > a").click()
    };

    function clickZawodowiZbieracze() {
        document.querySelector("#scavenge_screen > div > div.options-container > div:nth-child(3) > div.status-specific > div > div.action-container > a.btn.btn-default.free_send_button").click()
    }

    function setRestart() {
        var nextScavengerTime = getSleepTime();
        console.log("Wyslanie kolejnego zbieractwa za "+nextScavengerTime/1000+" sekund")
        setTimeout(function() { main() }, nextScavengerTime)
    }

    function getSleepTime() {
        var time = getRemainingTime()
        return time*1000 + getRandomInt(10, 300)*1000
    }

    function getRemainingTime() {
        var time = 0
        var timeRemainingEl = document.querySelector("#scavenge_screen > div > div.options-container > div:nth-child(3) > div.status-specific > div > ul > li:nth-child(4) > span.return-countdown")
        if (timeRemainingEl == undefined) { return 0 }

        var timeRemaining = timeRemainingEl.textContent.split(":")
        time += parseInt(timeRemaining[0])*3600
        time += parseInt(timeRemaining[1])*60
        time += parseInt(timeRemaining[2])
        console.log("Kolejne zbieractwo za " + time + " sekund")
        return time
    }

    function getRandomInt(min, max) {
        min = Math.ceil(min);
        max = Math.floor(max);
        return Math.floor(Math.random() * (max - min + 1)) + min
    }

    function waitToEnd() {
        var timeRemaining = getRemainingTime()*1000 + 10*1000
        console.log("Wysłanie za "+timeRemaining/1000)
        setTimeout(function() { main() }, timeRemaining)
    }

    function main() {
        selectLight()
        setTimeout(function() { clickZawodowiZbieracze() }, 1000)
        setTimeout(function() { setRestart() }, 5000)
    }

    waitToEnd()
})();