Tornados / Chaos

// ==UserScript==
// @name         Chaos
// @author       Tornados
// @version      1.1.1
// @license MIT  
// @downloadURL https://openuserjs.org/install/Tornados/Chaos.user.js
// @updateURL https://openuserjs.org/meta/Tornados/Chaos.meta.js
// @include      https://anticombats.com/bk
// @require http://code.jquery.com/jquery-3.4.1.min.js
// ==/UserScript==

(function() {
    'use strict';

    var listTime = 630;
    var enterTime = 640;
    var maxCommonTime = 300;
    var minCommonTime = 690;
    var commonEnterTime = 10;
    var checkAdminTime = 20;
    var refreshTime = 4;
    var gearListTime = 45;
    var gearCheckTime = 55;
    var isActive = true;
    var isStopped = false;

    function run () {
        if (isActive) {
            var serverTime = c["time"];
            var time = serverTime % 3600;
            if (time == listTime) {
                $("#main")[0].src = "main.php?zayvka=1&r=5&rnd=1";
            }

            if (time % 60 == refreshTime) {
                $("#main")[0].src = "main.php?zayvka=1&r=5&rnd=1";
            }

            if (time % 60 == checkAdminTime) {
                var minutesToBattle = $("#main").contents().find("table:eq(2) td:first").text().split("через")[1].split(" ")[1].split(".")[0];
                if (minutesToBattle > 5) {
                    $("#main").contents().find("img[src$='i/clear.gif']").click();
                }
            }

            if (!isStopped) {
                if (time == enterTime) {
                    var iframe = $("#main").contents();
                    var image = iframe.find("img[src$='//img.anticombats.com/king.gif']")[0];
                    var radio = getPreviousSibling(image, "input");
                    radio.checked = true;
                    iframe.find("input[value$='Принять участие в мясорубке']").click();
                }

                if (time % 60 == commonEnterTime && (time < maxCommonTime || time > minCommonTime)) {
                    $("#main").contents().find("#haot [name='levellogin1']").val(0);
                    // $("#main").contents().find("[name='otmorozok']")[0].checked = true;
                    $("#main").contents().find("input:eq(4)").val("Просьба не заходить!");
                    $("#main").contents().find("#haot .btnnew").click();
                }
            }

            if (time % 60 == gearListTime) {
                $("#main")[0].src = "main.php?rnd=1";
            }

            if (time % 60 == gearCheckTime) {
                var emptyGear = 0;
                var leftGear = $("#main").contents().find("table:eq(2) img");
                var rightGear = $("#main").contents().find("table:eq(5) img");
                leftGear.each(function(i) {
                    if ($(this).attr("title") != undefined) {
                        emptyGear++;
                    }
                });
                rightGear.each(function(i) {
                    if ($(this).attr("title") != undefined) {
                        emptyGear++;
                    }
                });

                if (emptyGear > 0) {
                    isStopped = true;

                    //var xhr = new XMLHttpRequest();
                    //xhr.open("GET", "https://slack.com/api/chat.postMessage?token=xoxp-80286206673-80285609159-849118301559-43a0c896f8e117b2f40246d3ac6d3fab&channel=alerts&username=Chaos&text=" + encodeURI("Gear problem!"), true);
                    //xhr.send();
                } else {
                    isStopped = false;
                }
            }
        }
    }

    var getPreviousSibling = function (elem, selector) {
        var sibling = elem.previousElementSibling;
        if (!selector) return sibling;
        while (sibling) {
            if (sibling.matches(selector)) return sibling;
            sibling = sibling.previousElementSibling;
        }

    };

    setInterval(run, 1000);

    top.addEventListener("keypress", function(e) {
        if (e.code == "KeyO" && e.shiftKey) {
            isActive = !isActive;
        }
    });
})();