luluu9 / Autozbieractwo

// ==UserScript==
// @name         Autozbieractwo
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  ZMIEŃ @MATCH W USTAWIENIACH NA TWÓJ LINK DO ZBIERACTWA NP. https://pl138.plemiona.pl/game.php?village=1118&screen=place&mode=scavenge
// @author       lulu xD
// @match        https://pl138.plemiona.pl/game.php?village=3131&screen=place&mode=scavenge
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function selectAllNoKnight() {
        document.querySelector("#scavenge_screen > div > div.candidate-squad-container > table > tbody > tr:nth-child(2) > td:nth-child(9) > a").click()
        document.querySelector("#scavenge_screen > div > div.candidate-squad-container > table > tbody > tr:nth-child(2) > td:nth-child(8) > 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() {
        selectAllNoKnight()
        setTimeout(function() { clickZawodowiZbieracze() }, 1000)
        setTimeout(function() { setRestart() }, 5000)
    }

    waitToEnd()
})();