krazyjakee / The Idle Class Autoclicker

// ==UserScript==
// @name         The Idle Class Autoclicker
// @namespace    https://www.smallgraygames.com/the-idle-class
// @version      0.3
// @description  Adds autoclick toggle button and options at the top. Clicks Earn dollars and aquisition fire button. Answers emails and much more...
// @author       krazyjakee
// @match        https://www.smallgraygames.com/the-idle-class
// @license      MIT
// @updateURL    https://openuserjs.org/meta/krazyjakee/The_Idle_Class_Autoclicker.meta.js
// @grant        none
// ==/UserScript==

const savedOptions = Object.assign({
    hireCount: 10,
}, JSON.parse(localStorage.getItem('autoclick')));

const optionsMenuHTML = `<div class="modal fade mailModal" id="autoClickModal" tabindex="-1" role="dialog" aria-labelledby="autoClickModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <i class="material-icons">mouse</i>
        <h3 class="modal-title">AutoClick Options</h3>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">×</span>
        </button>
      </div>
      <div class="modal-body">
        <table class="table">
          <tbody>
            <tr>
              <td><label for="autoclick-hireCount">Aquisition Hire Count</label></td>
              <td><input id="autoclick-hireCount" class="form-control" type="number" value="${savedOptions.hireCount}" /></td>
            </tr>
          </tbody>
        </table>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal">OK</button>
      </div>
    </div>
  </div>
</div>`;

function init () {
    const $ = window.$;
    console.log("Autoclicker loaded...");
    let $autoclick;
    const autoClick = () => {
        if (window.autoClickerTimer) {
            clearInterval(window.autoClickerTimer);
            window.autoClickerTimer = false;
            $autoclick.html("Enable AutoClick");
        }
        else {
            window.autoClickerTimer = setInterval(() => {
                game.addManualClicks();
                game.activeAcquisitions().forEach(acquisition => acquisition.fire());
            }, 0);
            $autoclick.html("Disable AutoClick");
        }
        if (window.autoClickerTimer2) {
            clearInterval(window.autoClickerTimer2);
            window.autoClickerTimer2 = false;
        } else {
            window.autoClickerTimer2 = setInterval(() => {
                // Mark all emails as read
                markAllAsRead();
                // Sell all patents
                game.research().sellPatents();

                game.activeAcquisitions().forEach(acquisition => {
                    // Acquisition Emails
                    acquisition.mail().forEach(mail => mail.respond());
                    // Answer all chats
                    acquisition.chats().forEach((chat) => { chat.respond([null, "<input value='FIRE THEM ALL' />"]) });
                    // Autosell completed aquisition
                    if (!acquisition.currentEmployees.val()) {
                        acquisition.sell();
                    }
                    // Autohire workers
                    game.activeAcquisitions()[0].workers().forEach((worker) => {
                        if (worker.num() < parseInt(savedOptions.hireCount)) worker.hire();
                    });
                });

                // Keep investments maxed out
                const slots = game.totalSimultaneousInvestmentsAllowed.val() - game.activeInvestments().length
                for (let i = 0; i < slots; i++) {
                    game.makeInvestment(10, 1440)
                }

                // Autofill composed mail
                if (!game.composedMail().to()) game.composedMail().to("business business business bus");
                if (!game.composedMail().subject()) game.composedMail().subject("business business business business business business busine");
                if (!game.composedMail().message()) game.composedMail().message("business business business business business business business business business business business business business business business business business business business business business business bu");
            }, 1000);
        }
    };

    // Add autoclick buttons
    const buttonStylings = {
        "font-size": "16px",
        "padding": "4px 8px",
        "margin-top": "8px",
        "margin-left": "8px"
    }
    $("#top-clickable-box")
        .append("<button id='autoclick' class='btn btn-primary btn-lg main-click'>Enable AutoClick</button>")
        .append("<button id='autoclickOptions' class='btn btn-primary btn-lg main-click'>AutoClick Options</button>");
    $autoclick = $("#autoclick");
    const $autoclickOptions = $("#autoclickOptions");

    // Suppress errors
    $(document.body)
        .append("<div class='messages-box' />")
        .append(optionsMenuHTML);

    // Add AutoClick options modal
    const $autoClickModal = $("#autoClickModal");
    $autoClickModal.modal({ show: false });
    $autoclick.css(buttonStylings).click(() => autoClick());
    $autoclickOptions.css(buttonStylings).click(() => $autoClickModal.modal('toggle'));

    ["hireCount"].forEach((id) => {
        const elem = $(`#autoclick-${id}`);
        elem.change((e) => {
           savedOptions[id] = elem.val();
           localStorage.setItem("autoclick", JSON.stringify(savedOptions));
        });
    });

    // Save more often
    setInterval(() => {
        game.saveGame();
    }, 5000);
}

init();