Advertisement
Guest User

HaremHeroes Automatic Salery Fix

a guest
Jun 9th, 2018
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         HaremHeroes Automatic
  3. // @namespace    JDscripts
  4. // @version      1.8
  5. // @description  Open the menu in HaremHeroes(topright) to toggle AutoControlls. Supports AutoSalary, AutoQuest, AutoPachinko(Free) and AutoBattle. Messages are printed in local console.
  6. // @author       JD
  7. // @match        http*://nutaku.haremheroes.com/*
  8. // @require      https://cdn.jsdelivr.net/js-cookie/2.2.0/js.cookie.js
  9. // @grant        GM_addStyle
  10. // @license      MIT
  11. // ==/UserScript==
  12.  
  13. GM_addStyle('/* The switch - the box around the slider */ .switch { position: relative; display: inline-block; width: 60px; height: 34px; } /* Hide default HTML checkbox */ .switch input {display:none;} /* The slider */ .slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; -webkit-transition: .4s; transition: .4s; } .slider:before { position: absolute; content: ""; height: 26px; width: 26px; left: 4px; bottom: 4px; background-color: white; -webkit-transition: .4s; transition: .4s; } input:checked + .slider { background-color: #2196F3; } input:focus + .slider { box-shadow: 0 0 1px #2196F3; } input:checked + .slider:before { -webkit-transform: translateX(26px); -ms-transform: translateX(26px); transform: translateX(26px); } /* Rounded sliders */ .slider.round { border-radius: 34px; } .slider.round:before { border-radius: 50%; }');
  14.  
  15. var proceedQuest = function () {
  16.     //console.log("Starting auto quest.");
  17.     var currentQuestMunuOption = $("nav div[rel='content'] a:has(.continue_quest)");
  18.     if (currentQuestMunuOption === undefined || currentQuestMunuOption.attr("href") === undefined) {
  19.         console.log("Could not find current quest menu button for verification. Probably it hasn't yet loaded.");
  20.         return;
  21.     }
  22.     // Check if at correct page.
  23.     if (currentQuestMunuOption.attr("href") !== window.location.pathname) {
  24.         // Click on current quest to naviagte to it.
  25.         console.log("Navigating to current quest.");
  26.         sessionStorage.autoLoop = "false";
  27.         window.location = window.location.origin + currentQuestMunuOption.attr("href");
  28.         return;
  29.     }
  30.  
  31.     // Get the proceed button type
  32.     var proceedButtonMatch = $("#controls button:not([style='display: none;'])");
  33.     var proceedCostEnergy = Number($("#controls .cost span[cur='*']").text());
  34.     var proceedCostMoney = Number($("#controls .cost span[cur='$']").text().trim().replace(',', ''));
  35.     var proceedType = proceedButtonMatch.attr("act");
  36.  
  37.     if (proceedButtonMatch.length === 0) console.log("Could not find resume button.");
  38.     else if (proceedType === "free") {
  39.         console.log("Proceeding for free.");
  40.         proceedButtonMatch.click();
  41.     }
  42.     else if (proceedType === "pay") {
  43.         var energyCurrent = Number($("span[hero='energy_quest']").text());
  44.         var moneyCurrent = Number($("div[hero='soft_currency'] span").text().trim().replace(',', ''));
  45.         if(proceedCostEnergy <= energyCurrent)
  46.         {
  47.             // We have energy.
  48.             console.log("Spending "+proceedCostEnergy+" Energy to proceed.");
  49.         }
  50.         else
  51.         {
  52.             console.log("Quest requires "+proceedCostEnergy+" Energy to proceed.");
  53.             sessionStorage.questRequirement = "*"+proceedCostEnergy;
  54.             return;
  55.         }
  56.         if(proceedCostMoney <= moneyCurrent)
  57.         {
  58.             // We have money.
  59.             console.log("Spending "+proceedCostMoney+" Money to proceed.");
  60.         }
  61.         else
  62.         {
  63.             console.log("Spending "+proceedCostEnergy+" Money to proceed.");
  64.             sessionStorage.questRequirement = "$"+proceedCostMoney;
  65.             return;
  66.         }
  67.         proceedButtonMatch.click();
  68.         sessionStorage.autoLoop = "false";
  69.         location.reload();
  70.     }
  71.     else if (proceedType === "use_item") {
  72.         console.log("Proceeding by using X" + Number($("#controls .item span").text()) + " of the required item.");
  73.         proceedButtonMatch.click();
  74.     }
  75.     else if (proceedType === "battle") {
  76.         console.log("Proceeding to battle troll...");
  77.         sessionStorage.questRequirement = "battle";
  78.         // Proceed to battle troll.
  79.         proceedButtonMatch.click();
  80.         sessionStorage.autoLoop = "false";
  81.         location.reload();
  82.     }
  83.     else if (proceedType === "end_archive") {
  84.         console.log("Reached end of current archive. Proceeding to next archive.");
  85.         sessionStorage.autoLoop = "false";
  86.         proceedButtonMatch.click();
  87.     }
  88.     else if (proceedType === "end_play") {
  89.         console.log("Reached end of current play. Proceeding to next play.");
  90.         sessionStorage.autoLoop = "false";
  91.         proceedButtonMatch.click();
  92.     }
  93.     else {
  94.         console.log("Could not identify given resume button.");
  95.         sessionStorage.questRequirement = "unknownQuestButton";
  96.     }
  97. };
  98.  
  99. var getSalary = function () {
  100.     try {
  101.         if ($("#breadcrumbs span").last().text() === "Harem") {
  102.             console.log("Detected Harem Screen. Fetching Salary");
  103.             $("#harem_whole #harem_left .salary:not('.loads') button").each(function (index) {
  104.                 $(this).click();
  105.             });
  106.             document.cookie = "nextSalaryTime=;";
  107.         }
  108.         else {
  109.             // Not at Harem screen then goto the Harem screen.
  110.             console.log("Navigating to Harem window.");
  111.             sessionStorage.autoLoop = "false";
  112.             window.location = window.location.origin + $("nav div[rel='content'] a:has(.harem)").attr("href");
  113.             return;
  114.         }
  115.     }
  116.     catch (ex) {
  117.         console.log("Could not collect salary... " + ex);
  118.     }
  119. };
  120.  
  121. var doBossBattle = function()
  122. {
  123.     var currentPower = Number($("span[hero='energy_fight']").text());
  124.     if(currentPower < 1)
  125.     {
  126.         //console.log("No power for battle.");
  127.         return;
  128.     }
  129.     // Battles the latest boss.
  130.     // Navigate to latest boss.
  131.     if(window.location.pathname.startsWith("/battle.html"))
  132.     {
  133.         // On the battle screen.
  134.         doBattle();
  135.     }
  136.     else if(window.location.pathname.startsWith("/quest"))
  137.     {
  138.         // On some quest screen.
  139.         // Goto this area's screen.
  140.         console.log("Navigating to latest Troll.");
  141.         sessionStorage.autoLoop = "false";
  142.         window.location = window.location.origin + $("#breadcrumbs a[class='back']").last().attr("href");
  143.         return;
  144.     }
  145.     else if(window.location.pathname.startsWith("/world"))
  146.     {
  147.         // On some world screen.
  148.         // Click on the local Boss's battle button.
  149.         console.log("Entering battle with this troll.");
  150.         sessionStorage.autoLoop = "false";
  151.         window.location = window.location.origin + $("#worldmap a[class='troll_world']").attr("href");
  152.         return;
  153.     }
  154.     else{
  155.         console.log("Navigating to latest Troll.");
  156.         sessionStorage.autoLoop = "false";
  157.         /*window.location = "https://nutaku.haremheroes.com/battle.html?id_troll=3";*/
  158.         window.location = window.location.origin + $("nav div[rel='content'] a:has(.continue_quest)").attr("href");
  159.         return;
  160.     }
  161. };
  162.  
  163. var doBattle = function () {
  164.     //console.log("Performing auto battle.");
  165.     var currentPower = Number($("span[hero='energy_fight']").text());
  166.     if(currentPower < 1)
  167.     {
  168.         //console.log("No power for battle.");
  169.         return;
  170.     }
  171.     // Confirm if on correct screen.
  172.     if ($("#breadcrumbs span").last().text() === "Battle") {
  173.         // On battle page.
  174.         //console.log("On Battle Page.");
  175.         if ($("#arena[class='canvas']").length === 1) {
  176.             // Oponent choose screen
  177.             console.log("On opponent choose screen.");
  178.             // Fight the first opponent in list.
  179.             $(".opponents_arena .sub_block .grey_text_button:contains('Select')")[0].click();
  180.             sessionStorage.autoLoop = "false";
  181.         }
  182.         else if ($("#battle[class='canvas']").length === 1) {
  183.             // Battle screen
  184.             //console.log("On battle screen.");
  185.             var battleButton = $("#battle_middle button[rel='launch']");
  186.             if(battleButton === undefined)return;
  187.             if(battleButton.attr("price") === undefined){console.log("Could not detect battle button price. Maybe its not loaded yet.");return;}
  188.             if(currentPower >= battleButton.attr("price"))
  189.             {
  190.                 // We have the power.
  191.                 battleButton.click();
  192.                 // Skip
  193.                 setTimeout(function(){$("#battle_middle button[rel='skip']").click();},1000);
  194.                 setTimeout(function(){$("#battle_end div[style*='display: block;'] .blue_text_button").click();},2500);
  195.  
  196.                 if (sessionStorage.questRequirement === "battle") {
  197.                     // Battle Done.
  198.                     sessionStorage.questRequirement = "none";
  199.                 }
  200.             }
  201.             else
  202.             {
  203.                 // We need more power.
  204.                 console.log("Battle requires "+battleButton.attr("price")+" power.");
  205.                 sessionStorage.battlePowerRequired = battleButton.attr("price");
  206.                 if(sessionStorage.questRequirement === "battle")sessionStorage.questRequirement = "P"+battleButton.attr("price");
  207.             }
  208.         }
  209.         else {
  210.             console.log("Could not identify battle screen.");
  211.             if (sessionStorage.questRequirement === "battle") sessionStorage.questRequirement = "errorInAutoBattle";
  212.             return;
  213.         }
  214.     }
  215.     else
  216.     {
  217.         // Switch to the correct screen
  218.         console.log("Switching to battle screen.");
  219.         window.location = window.location.origin + $("nav div[rel='content'] a:has(.battle)").attr("href");
  220.         sessionStorage.autoLoop = "false";
  221.         return;
  222.     }
  223. };
  224.  
  225. var updateData = function () {
  226.     //console.log("updating UI");
  227.     sessionStorage.autoSalary = document.getElementById("autoSalaryCheckbox").checked;
  228.     sessionStorage.autoQuest = document.getElementById("autoQuestCheckbox").checked;
  229.     sessionStorage.autoBattle = document.getElementById("autoBattleCheckbox").checked;
  230.     sessionStorage.autoFreePachinko = document.getElementById("autoFreePachinko").checked;
  231. };
  232.  
  233. var getPachinko = function(){
  234.     try {
  235.         if ($("#breadcrumbs span").last().text() === "Pachinko") {
  236.             console.log("Detected Pachinko Screen. Fetching Pachinko");
  237.             $("#pachinko button[free=1]")[0].click();
  238.             document.cookie = "nextPachinkoTime=24hrs;max-age="+24*60*60;
  239.         }
  240.         else {
  241.             // Not at Pachinko screen then goto the Pachinko screen.
  242.             console.log("Navigating to Pachinko window.");
  243.             sessionStorage.autoLoop = "false";
  244.             window.location = window.location.origin + $("nav div[rel='content'] a:has(.pachinko)").attr("href");
  245.             return;
  246.         }
  247.     }
  248.     catch (ex) {
  249.         console.log("Could not collect pachinko... " + ex);
  250.     }
  251. };
  252.  
  253. var autoLoop = function () {
  254.     updateData();
  255.     var busy = false;
  256.     var page = window.location.href;
  257.     var currentPower = Number($("span[hero='energy_fight']").text());
  258.     //console.log("sal="+sessionStorage.autoSalary);
  259.     if(sessionStorage.autoFreePachinko === "true" && busy === false)
  260.     {
  261.         // Navigate to pachinko
  262.         if (Cookies.get("nextPachinkoTime") === undefined) {
  263.             console.log("Time to fetch Pachinko.");
  264.             getPachinko();
  265.             busy = true;
  266.         }
  267.     }
  268.     if (sessionStorage.autoSalary === "true" && busy === false) {
  269.         if (Cookies.get("nextSalaryTime") === undefined) {
  270.             console.log("Time to fetch salary.");
  271.             getSalary();
  272.             busy = true;
  273.         }
  274.         else if (Cookies.get("nextSalaryTime") === "") {
  275.             console.log("Salary fetched. Getting next fetch time");
  276.             if ($("nav div[rel='content'] a:has(.home)").attr("href") !== window.location.pathname) {
  277.                 console.log("Moving to home.");
  278.                 sessionStorage.autoLoop = "false";
  279.                 // Goto Home page.
  280.                 window.location = window.location.origin + $("nav div[rel='content'] a:has(.home)").attr("href");
  281.                 busy=true;
  282.                 return;
  283.             }
  284.             var closestTime = 0;
  285.             var Salerytime = document.getElementsByClassName('in').item(0).getElementsByTagName("span").item(0).innerText;
  286.             console.log("Salerytime: " + Salerytime);
  287.             if (Salerytime.charAt(Salerytime.length - 1) == "m") {
  288.                 closestTime = (parseInt(Salerytime.replace(/\D/g,''))) * 60;
  289.             } else if(Salerytime.charAt(Salerytime.length - 1) == "s"){
  290.                 closestTime = parseInt(Salerytime.replace(/\D/g,''));
  291.             }
  292.             console.log("closestTime: " + closestTime);
  293.             document.cookie = "nextSalaryTime=present;max-age=" + (closestTime < 0 ? 0 : closestTime);
  294.             console.log("New fetch time stored in nextSalaryTime cookie.(+" + closestTime + " sec.)");
  295.             busy = false;
  296.         }
  297.     }
  298.     if (sessionStorage.autoQuest === "true" && busy === false) {
  299.         if (sessionStorage.questRequirement === "battle") {
  300.             //console.log("Quest requires battle.");
  301.             doBossBattle();
  302.             busy = true;
  303.         }
  304.         else if (sessionStorage.questRequirement[0] === '$') {
  305.             if (Number(sessionStorage.questRequirement.substr(1)) < Number($("div[hero='soft_currency'] span").text().trim().replace(',', ''))) {
  306.                 // We have enough money... requirement fulfilled.
  307.                 console.log("Continuing quest, required money obtained.");
  308.                 sessionStorage.questRequirement = "none";
  309.                 proceedQuest();
  310.                 busy = true;
  311.             }
  312.             else {
  313.                 if(isNaN(sessionStorage.questRequirement.substr(1)))
  314.                 {
  315.                     sessionStorage.questRequirement = "none";
  316.                     console.log("Invalid money in session storage quest requirement !");
  317.                 }
  318.                 else{
  319.                     // Else we need more money.
  320.                     //console.log("Need money for quest, cannot continue. Turning ON AutoSalary.");
  321.                     sessionStorage.autoQuest = "true";
  322.                 }
  323.                 busy = false;
  324.             }
  325.         }
  326.         else if (sessionStorage.questRequirement[0] === '*') {
  327.             var energyNeeded = Number(sessionStorage.questRequirement.substr(1));
  328.             var energyCurrent = Number($("span[hero='energy_quest']").text());
  329.             if (energyNeeded <= energyCurrent) {
  330.                 // We have enough energy... requirement fulfilled.
  331.                 console.log("Continuing quest, required energy obtained.");
  332.                 sessionStorage.questRequirement = "none";
  333.                 proceedQuest();
  334.                 busy = true;
  335.             }
  336.             // Else we need energy, just wait.
  337.             else {
  338.                 busy = false;
  339.                 //console.log("Replenishing energy for quest.(" + energyNeeded + " needed)");
  340.             }
  341.         }
  342.         else if (sessionStorage.questRequirement[0] === 'P')
  343.         {
  344.             // Battle power required.
  345.             var neededPower = Number(sessionStorage.questRequirement.substr(1));
  346.             if(currentPower < neededPower)
  347.             {
  348.                 console.log("Quest requires "+neededPower+" Battle Power for advancement. Waiting...");
  349.                 busy = false;
  350.             }
  351.             else
  352.             {
  353.                 console.log("Battle Power obtained, resuming quest...");
  354.                 sessionStorage.questRequirement = "none";
  355.                 proceedQuest();
  356.                 busy = true;
  357.             }
  358.         }
  359.         else if (sessionStorage.questRequirement === "unknownQuestButton") {
  360.             console.log("AutoQuest disabled.AutoQuest cannot be performed due to unknown quest button. Please manually proceed the current quest screen.");
  361.             document.getElementById("autoQuestCheckbox").checked = false;
  362.             sessionStorage.autoQuest = "false";
  363.             sessionStorage.questRequirement = "none";
  364.             busy = false;
  365.         }
  366.         else if (sessionStorage.questRequirement === "errorInAutoBattle") {
  367.             console.log("AutoQuest disabled.AutoQuest cannot be performed due errors in AutoBattle. Please manually proceed the current quest screen.");
  368.             document.getElementById("autoQuestCheckbox").checked = false;
  369.             sessionStorage.autoQuest = "false";
  370.             sessionStorage.questRequirement = "none";
  371.             busy = false;
  372.         }
  373.         else if(sessionStorage.questRequirement === "none")
  374.         {
  375.             //console.log("NONE req.");
  376.             busy = true;
  377.             proceedQuest();
  378.         }
  379.         else
  380.         {
  381.             console.log("Invalid quest requirement : "+sessionStorage.questRequirement);
  382.             busy=false;
  383.         }
  384.     }else if(sessionStorage.autoQuest === "false"){sessionStorage.questRequirement = "none";}
  385.  
  386.     if(sessionStorage.autoBattle === "true")
  387.     {
  388.         if(busy === false && currentPower >= Number(sessionStorage.battlePowerRequired) && currentPower > 0)
  389.         {
  390.             sessionStorage.battlePowerRequired = "0";
  391.             busy = true;
  392.             if(sessionStorage.autoQuest === "true")
  393.             {
  394.                 if(sessionStorage.questRequirement[0] === 'P')
  395.                 {
  396.                     console.log("AutoBattle disabled for power collection for AutoQuest.");
  397.                     document.getElementById("autoBattleCheckbox").checked = false;
  398.                     sessionStorage.autoBattle = "false";
  399.                     busy = false;
  400.                 }
  401.                 else
  402.                     doBossBattle();
  403.             }
  404.             else
  405.                 doBossBattle();
  406.         }
  407.     }
  408.     else{sessionStorage.battlePowerRequired = "0";}
  409.  
  410.     if(busy === true && sessionStorage.userLink==="none")
  411.     {
  412.         sessionStorage.userLink = page;
  413.     }
  414.     else if(sessionStorage.userLink !=="none" && busy === false)
  415.     {
  416.         console.log("Restoring page "+sessionStorage.userLink);
  417.         window.location = sessionStorage.userLink;
  418.         sessionStorage.userLink = "none";
  419.     }
  420.  
  421.     if(isNaN(sessionStorage.autoLoopTimeMili)){
  422.         console.log("AutoLoopTimeMili is not a number.");
  423.         setDefaults();
  424.     }
  425.     else{
  426.         if (sessionStorage.autoLoop === "true") setTimeout(autoLoop, Number(sessionStorage.autoLoopTimeMili));
  427.         else console.log("autoLoop Disabled");
  428.     }
  429. };
  430.  
  431. var setDefaults = function () {
  432.     console.log("Setting Defaults.");
  433.     sessionStorage.autoSalary = "false";
  434.     sessionStorage.autoFreePachinko = "false";
  435.     sessionStorage.autoLoop = "true";
  436.     sessionStorage.userLink = "none";
  437.     sessionStorage.autoLoopTimeMili = "200";
  438.     sessionStorage.autoQuest = "false";
  439.     sessionStorage.autoBattle = "false";
  440.     sessionStorage.battlePowerRequired = "0";
  441.     sessionStorage.questRequirement = "none";
  442.     sessionStorage.freshStart = "no";
  443. };
  444.  
  445. var start = function () {
  446.     //console.log("script started");
  447.     // Add UI buttons.
  448.     var UIcontainer = $("#contains_all nav div[rel='content']");
  449.     UIcontainer.html('<div style="position: absolute;right: -16.475%; padding: 10px;width: inherit;text-align: center;display:flex;flex-direction:column;">'
  450.                      + '<span>AutoSal.</span><div><label class=\"switch\"><input id=\"autoSalaryCheckbox\" type=\"checkbox\"><span class=\"slider round\"></span></label></div>'
  451.                      +'<span>AutoQuest</span><div><label class=\"switch\"><input id=\"autoQuestCheckbox\" type=\"checkbox\"><span class=\"slider round\"></span></label></div>'
  452.                      +'<span>AutoBattle</span><div><label class=\"switch\"><input id=\"autoBattleCheckbox\" type=\"checkbox\"><span class=\"slider round\"></span></label></div>'
  453.                      +'<span>AutoPachinko(Free)</span><div><label class=\"switch\"><input id=\"autoFreePachinko\" type=\"checkbox\"><span class=\"slider round\"></span></label></div>'
  454.                      +'</div>'+UIcontainer.html());
  455.     document.getElementById("autoSalaryCheckbox").checked = sessionStorage.autoSalary === "true";
  456.     document.getElementById("autoQuestCheckbox").checked = sessionStorage.autoQuest === "true";
  457.     document.getElementById("autoBattleCheckbox").checked = sessionStorage.autoBattle === "true";
  458.     document.getElementById("autoFreePachinko").checked = sessionStorage.autoFreePachinko === "true";
  459.     sessionStorage.autoLoop = "true";
  460.     if (typeof sessionStorage.freshStart == "undefined" || isNaN(Number(sessionStorage.autoLoopTimeMili))) {
  461.         setDefaults();
  462.     }
  463.     autoLoop();
  464. };
  465. $("document").ready(start);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement