ySomic / Stream Legends Auto Fighter

// ==UserScript==
// @namespace     https://openuserjs.org/users/ySomic
// @name          Stream Legends Auto Fighter
// @description   An actually working autofighter/grinder for stream legends extension
// @copyright     2018, ySomic (https://openuserjs.org/users/ySomic)
// @license       MIT
// @version       0.0.2
// @match        https://ro13roxp88918kulntih9uzm7vs9jr.ext-twitch.tv/ro13roxp88918kulntih9uzm7vs9jr/1.14.3440/*
// @updateURL https://openuserjs.org/meta/ySomic/Stream_Legends_Auto_Fighter.meta.js
// @grant none
// @author ySomic
// ==/UserScript==

// ==OpenUserJS==
// @author ySomic
// ==/OpenUserJS==

(function () {
  'use strict';

  var intervalFighter;
  var fightsBefore = 0;
  var continuePastFights = false;
  var continueBossFigth = false;
  var fights = -1;
  var fighterOn = false;

  function eventFire(el, etype) {
    if (el.fireEvent) {
      el.fireEvent('on' + etype);
    }
    else {
      var evObj = document.createEvent('Events');
      evObj.initEvent(etype, true, false);
      el.dispatchEvent(evObj);
    }
  }

  function ifExistThen(el, callback) {
    if (el !== undefined) {
      callback();
    }
  }

  function buttonActive(el, callbackExists, callbackNonExists) {
    if (el !== undefined && !(el.classList.contains("srpg-button-disabled") || el.classList.contains("srpg-awaiting-request-spinner"))) {
      ifExistThen(callbackExists, callbackExists);
    }
    else {
      ifExistThen(callbackNonExists, callbackNonExists);
    }
  }

  function continueFromFight() {
    var div_PostFightDiv = document.getElementsByClassName("fight-results-button")[1];
    buttonActive(div_PostFightDiv, function () {
      var btn_PostFight = document.getElementsByClassName("post-fight-button")[0];
      buttonActive(btn_PostFight, function () {
        eventFire(btn_PostFight, 'click');
      });
    });
  }

  function continueFromReward() {
    var div_RewardsCTAButtons = document.getElementsByClassName("rewards-cta-buttons")[0];
    buttonActive(div_RewardsCTAButtons, function () {
      var btn_Continue = document.getElementsByClassName("srpg-button-continue")[0];
      buttonActive(btn_Continue, function () {
        eventFire(btn_Continue, 'click');
      });
    })
  }

  function selectPreviousFight() {
    var btn_Areas = document.getElementsByClassName("node-completed");
    if (fightsBefore > 5) {
      fightsBefore = 5;
    }
    var select = btn_Areas.length - fightsBefore - 1;
    if (select >= 0) {
      var btn_AreaBefore = btn_Areas[select];
      buttonActive(btn_AreaBefore, function () {
        eventFire(btn_AreaBefore, 'click');
      })
    }
  }

  function startFight() {
    var div_FightButtnDiv = document.getElementsByClassName("srpg-map-actions-fight-buttons")[0];
    buttonActive(div_FightButtnDiv, function () {
      var btn_FightButton = div_FightButtnDiv.getElementsByClassName("srpg-button")[0];
      buttonActive(btn_FightButton, function () {
        startFightUI();
        if (fights % 10 == 0) {
          console.log("Starting fight nr: " + fights + ".");
        }
        eventFire(btn_FightButton, 'click');
      })
    });
  }

  function drawMenu() {
    var html = "<div id=\"bgDiv\" style=\"position: absolute;width: 200px;height: 163px;z-index:500;bottom:55px;left:10px;background-color:#268074;opacity: 0.3\">\n" +
      "\n" +
      "</div>\n" +
      "\n" +
      "<div id=\"btnsDiv\" style=\"position: absolute;width: 200px;min-height: 163px;z-index:600;bottom:55px;left:10px;color:#000\">\n" +
      "    <button id=\"toggle\" style=\"box-sizing:border-box;width: 180px;margin:10px 10px;\">Toggle off</button>\n" +
      "    <br>\n" +
      "    <button id=\"autoFight\" style=\"box-sizing:border-box;width: 85px;margin:5px 3px 10px 10px;display: inline-block\">Auto</button>\n" +
      "    <button id=\"grind\" style=\"box-sizing:border-box;width: 85px;margin:5px 10px 10px 3px;display: inline-block\">Grind</button>\n" +
      "    <br>\n" +
      "    <span style=\"width: 85px;margin:5px 0 10px 12px;float:left\">Lvl's before: </span> <input style=\"width: 85px;margin:5px 10px 10px 0;float:right\" name=\"areasBefore\" type=\"text\" id=\"areasBefore\" value=\"0\">\n" +
      "    <br>\n" +
      "    <span style=\"display: inline-block;box-sizing:border-box;max-width:150px;margin:5px 0 10px 10px\">Fights Nr: </span><span id=\"fights\" style=\"display: inline-block;font-weight: bold;margin-left:10px;width:5px\">999</span>\n" +
      "</div>";

    document.body.insertAdjacentHTML('beforeend', html);
  }

  function selectRaid() {
    var div_raidMap = document.getElementsByClassName("map-raid")[0];
    buttonActive(div_raidMap, function () {
      // RAID ACTIVE!
      eventFire(div_raidMap, 'click');
    })
  }

  function continueRaid() {
    var div_leaderboard = document.getElementsByClassName("raid-leaderboard-rewards")[0];
    buttonActive(div_leaderboard, function () {
      var btn_Continue = document.getElementsByClassName("srpg-button-maps")[0];
      buttonActive(btn_Continue, function () {
        eventFire(btn_Continue, 'click');
      });
    })
  }

  function btnToggle() {
    fighterOn = !fighterOn;
    fighterInterval(fighterOn);
  }

  function btnAutoFight() {
    if (!fighterOn) {
      fighterOn = true;
      fighterInterval(fighterOn);
    }

    continueBossFigth = true;
    continuePastFights = false;
    setDisables();
  }

  function btnGrind() {
    if (!fighterOn) {
      fighterOn = true;
      fighterInterval(fighterOn);
    }

    continueBossFigth = false;
    continuePastFights = true;
    setDisables();
  }

  function setDisables() {
    setDisableAutoFight();
    setDisableGrind();
  }

  function setDisableAutoFight() {
    disable(document.getElementById("autoFight"), continueBossFigth);

  }

  function setDisableGrind() {
    disable(document.getElementById("grind"), continuePastFights);

  }

  function disable(element, disabled) {
    if (disabled) {
      element.setAttribute("disabled", "disabled");
    }
    else {
      element.removeAttribute("disabled");
    }
  }

  function bindMenu() {
    document.getElementById("toggle").addEventListener("click", btnToggle);

    document.getElementById("autoFight").addEventListener("click", btnAutoFight);
    document.getElementById("grind").addEventListener("click", btnGrind);

    document.getElementById("areasBefore").addEventListener("change", function () {
      var value = this.value;
      console.log("new areas: " + value);
      fightsBefore = value;
    });
  }

  function startFightUI() {
    fights++;
    document.getElementById("fights").innerHTML = fights;
  }

  function setCorrectToggleText(value) {
    if (value) {
      document.getElementById("toggle").innerHTML = "Turn off";
    }
    else {
      document.getElementById("toggle").innerHTML = "Turn on";
    }
  }

  function fighterInterval(on) {
    if (on) {
      continueBossFigth = false;
      continuePastFights = true;
      console.log("Starting Timer");
      intervalFighter = setInterval(function () {
        selectRaid();
        continueFromFight();
        continueFromReward();
        continueRaid();
        if (continuePastFights) {
          selectPreviousFight();
          setTimeout(startFight, 100);
        }
        else if (continueBossFigth) {
          startFight();
        }
      }, 1000)
    }
    else {
      clearInterval(intervalFighter);
      continueBossFigth = false;
      continuePastFights = false;
    }

    setCorrectToggleText(on);
    setDisables();
  }

  (function () {
    console.log("Make sure you're in the iframe!");

    // Reseting console
    var i = document.createElement('iframe');
    i.style.display = 'none';
    document.body.appendChild(i);
    window.console = i.contentWindow.console;

    //make menu
    drawMenu();
    bindMenu();
    setCorrectToggleText();

    //Start overall interval
    fighterInterval(fighterOn);
  })();
})();