3jameo3 / HaremHeroes Automatic Redone

Not exactly sure what is going on I am currently trying to debug it but it is a slow process.

It is definitely the hardest to debug considering the amount of time between missions and how long it takes to recreate the conditions to test. We'll be able to solve it over time. I can't debug right now but I'll get to it when I can. My first thought is just that it might not be detecting the "groovy" button for the rewards properly. We'll see. Probably will have to put some debug or console.log() in there to see what is happening later.

Re: @twelve27:

Its not only the button mashing its constantly trying to loop through the claim mission section and when you edit that out it loops through the accept mission section. Debugging missions is a pain if they hadn't changed the timers then it would still be working but I digress.

if (sessionStorage.autoMissions === "true" && busy === false) {
        if (Cookies.get("nextActivityTime") === undefined && ($("#home_missions_bar1 .text").length) === 0 && ($("#home_missions_bar2 .text").length) <= 2 && ($("div[class='collect_notif']:not([style=''])"))) {
            console.log("Time to get a mission.");
            getMission();
            busy = true;
        }
        else if (Cookies.get("nextActivityTime") === "" && ($("#home_missions_bar1 .text").length) >= 0 && ($("#home_missions_bar2 .text").length) <= 2) {
            console.log("Accepting a mission. Getting countdown to next mission");
            if ($("nav div[rel='content'] a:has(.home)").attr("href") !== window.location.pathname) {
                console.log("Moving to home.");
                sessionStorage.autoLoop = "false";
                window.location = window.location.origin + $("nav div[rel='content'] a:has(.home)").attr("href");
                busy=true;
                return;
            }
            var MB1 = $("#home_missions_bar1 .text").text().replace('Complete in ','')
            //console.log(MB1)
            if (MB1.includes('h') && MB1.includes('m')) {
                var MB1 = $("#home_missions_bar1 .text").text().replace('Complete in ','').replace('h','').replace('m','').replace(' ','');
                //console.log('"'+MB1+'"')
                if (MB1.length == 1) {
                    var closestTime1 = Number((MB1[0]) * 3600);
                }
                if (MB1.length == 2) {
                    var closestTime1 = Number((MB1[0]) * 3600) + (Number(MB1[1]) * 60);
                }
                if (MB1.length == 3) {
                    var closestTime1 = Number((MB1[0]) * 3600) + (Number(MB1[1] + MB1[2]) * 60);
                }
                if (MB1.length == 4) {
                    var closestTime1 = Number((MB1[0] + MB1[1]) * 3600) + (Number(MB1[2] + MB1[3]) * 60);
                }
                //console.log('"'+closestTime1+'"')
                }
            if (MB1.includes('m')) {
                MB1.replace('m','')
                //console.log('"'+MB1+'"')
                var closestTime1 = Number(MB1.replace('m','')) * 60
                //console.log('"'+closestTime1+'"')
            }
            if (MB1.includes('s')) {
                MB1.replace('s','')
                //console.log('"'+MB1+'"')
                var closestTime1 = Number(MB1.replace('s',''))
                //console.log('"'+closestTime1+'"')
                }
            document.cookie = "nextActivityTime=present;max-age=" + (closestTime1 < 0 ? 0 : closestTime1);
            if (Cookies.get("nextActivityTime") === undefined && MB1.length >= 0) {
                getMission();
            }
            console.log("Time until next mission stored in nextActivityTime cookie.(+" + closestTime1 + " sec.)");
            busy = false;
        }
        if (Cookies.get("nextActivityTime") === "" && ($("#home_missions_bar1 .text").length) >= 0 && ($("#home_missions_bar2 .text").length) >= 0 && $("div[class='collect_notif']:not([style='display: none;'])")) {
            console.log("Time to collect the completed mission(s).");
            collectMission();
            busy = true;
        }
        if ((Cookies.get("nextActivityTime") === "" || Cookies.get("nextActivityTime") === undefined || Cookies.get("nextActivityTime") === 0) && ($("#home_missions_bar1 .text").length) <= 2 && ($("#home_missions_bar2 .text").length) >= 2) {
            console.log("All Missions Completed. Getting time until missions can be accepted again.");
            if ($("nav div[rel='content'] a:has(.home)").attr("href") !== window.location.pathname) {
                console.log("Moving to home.");
                sessionStorage.autoLoop = "false";
                window.location = window.location.origin + $("nav div[rel='content'] a:has(.home)").attr("href");
                busy=true;
                return;
            }
            var MB2 = $("#home_missions_bar2 .text").text().replace('Complete in ','');
            //console.log(MB2)
            if (MB2.includes('h') && MB2.includes('m')) {
                var MB2 = $("#home_missions_bar2 .text").text().replace('Complete in ','').replace('h','').replace('m','').replace(' ','');
                //console.log('"'+MB2+'"')
                if (MB2.length == 1) {
                    var closestTime1 = Number((MB2[0]) * 3600);
                }
                if (MB2.length == 2) {
                    var closestTime1 = Number((MB2[0]) * 3600) + (Number(MB2[1]) * 60);
                }
                if (MB2.length == 3) {
                    var closestTime1 = Number((MB2[0]) * 3600) + (Number(MB2[1] + MB2[2]) * 60);
                }
                if (MB2.length == 4) {
                    var closestTime1 = Number((MB2[0] + MB2[1]) * 3600) + (Number(MB2[2] + MB2[3]) * 60);
                }
                //console.log('"'+closestTime1+'"')
                }
            if (MB2.includes('m')) {
                MB2.replace('m','')
                //console.log('"'+MB2+'"')
                var closestTime1 = Number(MB2.replace('m','')) * 60
                //console.log('"'+closestTime1+'"')
            }
            if (MB2.includes('s')) {
                MB2.replace('s','')
                //console.log('"'+MB2+'"')
                var closestTime1 = Number(MB2.replace('s',''))
                //console.log('"'+closestTime1+'"')
                }
            document.cookie = "nextActivityTime=present;max-age=" + (closestTime1 < 0 ? 0 : closestTime1);
            console.log("Time until new missions stored in nextActivityTime cookie.(+" + closestTime1 + " sec.)");
            busy = false;
        }
    }```

Code I am currently testing atm, it is the previous version to what is in the current script with a couple edits.

Re: @3jameo3:
Guess you don't want syntax highlighting to assist the understanding for @twelve27 (or anyone else following) ... code fences are the ones that allow that not multi-line back ticks for inline code.

Re: @Marti:

Actually the code button provided that. It didn't give 3 of the ' only 1 when it was used on the first line of the reply. If you could be so kind as to fix the highlighting problem that would appreciated as well as inform whoever is in charge of bugs about it I would appreciate it very much. I completely forgot to preview before posting a reply. Thank you in advance for the help.

Re: @3jameo3:

If you could be so kind as to fix the highlighting problem that would appreciated...

Done.

... as well as inform whoever is in charge of bugs about it I would appreciate it very much.

That would be you in this case. A code fence is only applied if the code is pasted by the user into the text area first; then you highlight what is code; and then tap the </> button... otherwise it's inline code single surrounded back ticks. I usually find it easier just to type the triple back tick and the language I'm using after. We mention this at this FAQ. While the inline code multiline isn't detrimental to policy it does make it DOM single node heavy and difficult to read and parse when it's validated (try a DOM inspection sometime on a multi-line code block vs a fence sometime ;)

Apologies for going off topic here but trying to be a little helpful without seeming too obtrusive. :)

Re: @Marti:

Thank you for the FAQ page it was an interesting read. Now I know how to change the highlighting if needed. Thank you for all of your help.

Re: @3jameo3:

So far the code that I posted above does work. However, the game still needs to change screens before it can grab a new mission or grab the time until the mission is completed.

Re: @twelve27:

that was meant as a reply to you now myself guess that is what I get for waking up and immediately trying to do things.

Re: @3jameo3:
Hm, would a redirect of the user back to the last known page work then? Seems having the reward pop up and being stuck on the screen is causing the error. After claim reward is clicked, just go the previous page the user was at or back to the main screen and it should resolve the issue, right?

Re: @twelve27:

With the current code mine stops working before the collecting part of the script activates as well as sometimes halts before the accepting mission which is the redirection problem, and it throws undefined when the all missions completed part activates. The later should be an easy fix with just an undefined handler.

I could implement a redirection when the groovy button is detected in the claiming missions phase and that should solve the issue of that snag.

For instance right now my missions are halted because of the script stopping before claiming a mission. I am assuming that the conditions I set are too strict for it but at the same time it doesn't actually make sense to me how they are too strict. Even a redirection caused by auto salary didn't engage the collection feature. Then sometimes it works flawlessly.

So add this to the MB2 area under the

document.cookie = "nextActivityTime=present;max-age=" + (closestTime1 < 0 ? 0 : closestTime1);
if (Cookies.get("nextActivityTime") === undefined && MB2.length >= 0) {
                getMission();
            }

And for the groovy button add this either before or after the clicking function

window.location = window.location.origin + $("nav div[rel='content'] a:has(.activities)").attr("href");

You could comment out anything you don't want in it if you wish considering this will redirect the screen back to the missions screen.

Not sure if this will completely fix the issues or not though.

Maybe a redirect to home would actually be better for the undefined check. I'm not sure at the moment So in the next 2 days I will test both.

Re: @3jameo3:

A redirect to home would probably be the best option for undefined check because there is already multiple detection scenarios that happen on the home page. I'll test things as I can. I'm curious about what is not working about the script from a few days ago after you brought everything together. That is the version that I am still looking at at the moment.

So here is what I have in collectMission that has some slight changes:

if (buttonMatch.length === 0){ console.log("Could not find mission button.");}
      else if (Claim && (Cookies.get("nextActivityTime") <= 1)) {
        console.log("Claiming mission");
        Claim.click();
        document.cookie = "nextActivityTime=;";
        sessionStorage.autoLoop = "false";
        window.location = window.location.origin;
        return;
      }
      else if (click) {}
      else if (($("#popups #mission_rewards")) && Groovy) {
        console.log("Claiming mission reward(s).");
        Groovy.click();
        document.cookie = "nextActivityTime=;";
      }

adding in some brackets for the first if and removing the sleep statements. When the claim mission is clicked I go to window origin. Unfortunately I can't test more because I don't have missions available. But when I used this for the final mission I had left, it successfully went to the main page and got the time until the next mission. Come to think of it, I'm not sure if we need any sleep statements in the script.

Also, we'll need to make a change that clicks the groovy button when all missions are completed.

When the new missions rolled out tonight, it didn't grab it right away but rather after the next salary was grabbed. But it did return to the home screen after it got the mission so it seems like the snipped I have above is working properly. I'm wondering what to do about salary occasionally and missions frequently not activating and doing there actions right away.

Re: @twelve27:

Also, we'll need to make a change that clicks the groovy button when all missions are completed.

I am assuming you are referring to the 25 gold present box? I have the code, at least for the button, in the script just have never actually tested to see what all I need to do to make it work.

Referring to your code I may change it to that depending on if it seems to be working completely. At the current time I had to fix salary so I went ahead and added in the same button format as missions while I was at it. Also seems like collecting missions is my only problem well unless the mission screen is open then everything seems to work wonders, unsure about the next time to new missions part though (MB2).

I'm wondering what to do about salary occasionally and missions frequently not activating and doing there actions right away.

I have been trying to figure out that problem for a long while. I so far haven't got a single clue on how to fix the issue minus leaving one of the pages, market harem etc, up at all times which forces it to change screens which works.

Re: @3jameo3:
Oh, you already fixed salary and updated? Nice. I actually just got up an hour ago and came to check on the script cause I forgot there was an update until a few minutes ago. Great, I'll test out the code as well. I figured their UI change would break the script. Later on I'll take a look at how we can move things faster or try to figure out why they take a while sometimes.

Okay, its hard to test the present box since it is once per day so it will probably take some time to debug anything that might arrive from that.

Do your missions work with this current script? I was testing it out now and it seems missions aren't functioning. Might be a matter of my browser and cookies though

Went to do a boss battle and it decided to suddenly grab the mission then. Not sure why though. Any clue?

Re: @twelve27:

Do your missions work with this current script?

I am still fine tuning the script though not much has been done due to other errors like the salary incident when they decided to change the entire layout which I actually like. Currently with the code I have posted above missions seem to work as long as market/harem/missions/etc is open or the page changes screens sometimes. Other than that I have yet to be able to get it fully functional without any problems. Going from python to java after a good long while is messing with my head.

Re: @3jameo3:

Earlier today after my latest comment I implanted your new salary code into the script that I had going last night with the mission redirect. So at the very least I think I have it running for the most part from the home page. Currently I'm investigating the slow to start actions instead since the main components of salary and mission is working.

Also going to take a look to see if there is a way to get the koban collection at the end of daily missions to be fully functional. Of course I also want to look into the missions proceeding without needing salary to activate first.

Auto Missions is really giving me a major headache even with your changes it just does not want to work.

Re: @3jameo3:

I've done some editing around but not sure how much of my copy that I have changed or how much yours have changed so I'll post the mission snippets that have been running for me so far.

var getMission = function () {
  // console.log("Auto Missions Running");
  try {
    if ($('#breadcrumbs span').last().text() === 'Activities') {
      console.log('Detected mission screen. Accepting a mission');
      var buttonMatch = $('#missions button:not([style=\'display: none;\'])');
      var Accept = $('#missions button[rel^=\'mission_start\']:not([style=\'display: none;\'])');
      /*
      var Finish = $("#missions button[rel^='finish']:not([style='display: none;'])");
      var Present = $("#base_block #end_gift button:not([style='display: none;'])");*/
      //console.log(Present);
      if (buttonMatch.length === 0) {
        console.log('Could not find mission button.');
      }
      else if (Accept) {
        console.log('Starting mission.');
        Accept.click();
        sleep(100);
        document.cookie = 'nextActivityTime=;';
        window.location = window.location.origin;
      }
      /*
      else if (Finish) {
          console.log("Lets not click that.");
          //Finish.click();
      }
      else if (($("#base_block #end_gift")) && Present) {
          console.log("Claiming mission completion reward(s).");
          Present.click();
          sleep(100);
          window.location = window.location.origin;
      }
      */
      else {
        console.log('Could not identify given mission button.');
        sessionStorage.missionRequirement = 'unknownMissionButton';
      }
      sessionStorage.autoLoop = 'false';
      window.location = window.location.origin + $('nav div[rel=\'content\'] a:has(.activities)').attr('href');
    }
    else {
      // Not at Mission screen then goto the Mission screen.
      if (Cookies.get('nextActivityTime') === '0' || Cookies.get('nextActivityTime') === undefined || Cookies.get('nextActivityTime') === '' || (($('#home_missions_bar1 .text').length) === 0 && ($('#home_missions_bar2 .text').length) <= 2)) {
        console.log('Navigating to mission window.');
        sessionStorage.autoLoop = 'false';
        window.location = window.location.origin + $('nav div[rel=\'content\'] a:has(.activities)').attr('href');
        return;
      }
    }
  }
  catch (ex) {
    console.log('Could not do any missions... ' + ex);
  }
};
var collectMission = function () {
  // console.log("Auto Missions Running");
  try {
    if ($('#breadcrumbs span').last().text() === 'Activities') {
      console.log('Detected mission screen. Collecting mission(s)');
      var buttonMatch = $('#missions button:not([style=\'display: none;\'])');
      var Claim = $('#missions button[rel^=\'claim\']:not([style=\'display: none;\'])');
      var Groovy = $('#popups #mission_rewards button[rel^=\'groovy\']:not([style=\'display: none;\'])');

      if (buttonMatch.length === 0) { console.log('Could not find mission button.');}
      else if (Claim && (Cookies.get('nextActivityTime') <= 1)) {
        console.log('Claiming mission');
        Claim.click();
        document.cookie = 'nextActivityTime=;';
        sessionStorage.autoLoop = 'false';
        window.location = window.location.origin;
        return;
      }
      else if (click) {}
      else if (($('#popups #mission_rewards')) && Groovy) {
        console.log('Claiming mission reward(s).');
        Groovy.click();
        document.cookie = 'nextActivityTime=;';
      }
      else {
        console.log('Could not identify given mission button.');
        sessionStorage.missionRequirement = 'unknownMissionButton';
      }
    }
    else {
      // Not at Mission screen then goto the Mission screen.
      if (Cookies.get('nextActivityTime') === '0' || Cookies.get('nextActivityTime') === undefined || Cookies.get('nextActivityTime') === '' || (($('#home_missions_bar1 .text').length) === 0 && ($('#home_missions_bar2 .text').length) <= 2)) {
        console.log('Navigating to Mission window.');
        sessionStorage.autoLoop = 'false';
        window.location = window.location.origin + $('nav div[rel=\'content\'] a:has(.activities)').attr('href');
        return;
      }
    }
  }
  catch (ex) {
    console.log('Could not collect mission(s)... ' + ex);
  }
};

The only time it messed up was when I set a busted console.log() in there. Otherwise, its been running fine. It ran all my missions while I was away. Of course it has the same issue as you and I have been researching, the pausing and waiting for a different screen to activate.

I didn't see that eileen is active again. Going to see if I can transplant the mission stuff into eileen's code since it also includes arena