Roukys / HHauto

GNU General Public License v3.0
45 stars 44 forks source link

Midday Thresholds reset #153

Closed YotoTheOne closed 3 years ago

YotoTheOne commented 3 years ago

It would be cool if the Thresholds values could reset to a fixed value during the midday break (between the end of a contest and the beginning of the next one).

It could be achieved with an on/off button "daily thresholds reset" and a button to "set default values" that would memorize current thresholds values. Or even the on/off button "daily thresholds reset" alone, that would memorize default values on activation.

I usually set high thresholds values so I can play as much as I can by myself, the script working only to ensure I don't loose any points if I'm late. So my AutoTrollBattle threshold is set to 18 and there is a troll fighting contest, I set the threshold to 0 so I can earn every point I can. The contest ends and the threshold is automatically reset to 18.

What do you think ?

YotoTheOne commented 3 years ago

Since that feature doesn't seem to interest anyone else, I think a separate script could do the job. Would someone be willing to write it ? I know some of you have separate scripts to refresh browser at fixed intervals or reset some options, maybe my request could be made quickly from one of those ? I suspect it's not quite complicated but I really lack the time to improve my javascript these days ^^

OldRon1977 commented 3 years ago

What exactly would be the use case for this? Maybe if you explain it a bit more like what you had in mind, it will make things easier to follow you :)

YotoTheOne commented 3 years ago

Well I'll try to be more clear : I usually cannot play during the day, only morning and night. I live in western Europe so the reset occurs at 13h00. At this time the daily contest stops and another one starts 30mn later. Say there is a troll fighting contest ending, I likely set my threshold to 0 that morning so I can earn every free point I can for that contest ; I'd like that threshold to go back to the "no troll fighting contest" value, which is 18 in my case. Another example : if there is a champion contest, I choose the weakest teams possible so the champions last the longest possible time ; some champions are likely not defeated at 13h, I'd like to stop loosing tickets until I can improve the teams to finish them fast.

So I need a way to reset a bunch of options to a default value at 13h. Since there is already a feature to "save config" and "load config" I just need a way to automate a "load config" at 13h.

Was that more understandable ?

OldRon1977 commented 3 years ago

The last sentence did it for me :) You need more than one profile which can be loaded automatically. Like "normal" and "troll-fight" the challenge would be to identify which profile to load at which trigger point. I am not sure how this could be managed.

The more simple solution would be "profile 1" and "profile 2" and a timer to switch. But that might be a problem for users with more than one tab running the script.

Hmmm... Maybe @Roukys has a solution :)

YotoTheOne commented 3 years ago

I don't even need something that complicated, just go back to default settings is enough ; then I can set the values manually on the evening according to the daily contest.

Roukys commented 3 years ago

@YotoTheOne yes that is clearer, thanks. Main issue would be to be able to save all vars in somewhere users can modify them when wanted, then to have the reset at 13h or next active burst before doing anything ... technically could be doable, @OldRon1977 my understanding is that @YotoTheOne want to have only one default profile and everything back to this profile at 13h However practically to graphically set it so people understand how it will work and won't be suprised at the reset that is more difficult :) next difficulty is to manage timezone for everyone though we could try and fetch the ingame timer

Roukys commented 3 years ago

let's see if others are interested can you try that in the mean time:

// ==UserScript==
// @name         HaremHeroes midday reset
// @version      1
// @description  HaremHeroes midday reset
// @author       roukys
// @match        http*://nutaku.haremheroes.com/*
// @match        http*://*.hentaiheroes.com/*
// @match        http*://*.gayharem.com/*
// @license      MIT
// ==/UserScript==

let secondsTo13 = (new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate(), 13, 0, 0, 0).getTime()-new Date().getTime());
setTimeout(middayReset(),secondsTo13*1000);

function middayReset()
{
        Storage().HHAuto_Setting_autoQuestThreshold="99";
        Storage().HHAuto_Setting_autoTrollThreshold="19";
        Storage().HHAuto_Setting_autoSeasonThreshold="9";
        Storage().HHAuto_Setting_autoLeaguesThreshold="14";
}
YotoTheOne commented 3 years ago

Thank you, that should do the job ! I need to execute that script once every day right ?

Roukys commented 3 years ago

no it should load everytime HH page load, like the script ;)

Roukys commented 3 years ago

I didn't test it thoroughly though, particularly concerning timezone ...

if needed you can change the line let secondsTo13 to match the good hour

YotoTheOne commented 3 years ago

No problem, I'll do the testing and fine tuning, you were kind enough to write it for me ! I'll keep posting here.

YotoTheOne commented 3 years ago

It didn't work today. The console says "ERROR: Execution of script 'HaremHeroes midday reset' failed! Illegal constructor". I'm looking into some documentation of the date constructor to see if I can find spot a syntax error.

Edit : since Date().GetTime() returns milliseconds, I don't understand why it is needed to multiply by 1000 within the setTimeout ? But removing that *1000 does not suppress the error. I think the error comes from within the function MiddayReset().

Edit 2 : function Storage() is not defined in that script, maybe that's the error ?

Edit 3 : copied

function Storage()
{
    return localStorage.HHAuto_Setting_settPerTab==="true"?sessionStorage:localStorage;
}

from HH auto++and pasted in HH midday reset, the error is gone ! Now testing if it works :)

YotoTheOne commented 3 years ago

Working perfectly now, thank you again ! I made some corrections, here is the full working code if anyone is interested :

// ==UserScript==
// @name         HaremHeroes midday reset
// @version      1
// @description  HaremHeroes midday reset
// @author       roukys
// @match        http*://nutaku.haremheroes.com/*
// @match        http*://*.hentaiheroes.com/*
// @match        http*://*.gayharem.com/*
// @license      MIT
// ==/UserScript==

const resetHour = 13;
const resetMinute = 0;
var currentTime = new Date();
let msToReset = (new Date(currentTime.getFullYear(), currentTime.getMonth(), currentTime.getDate(), resetHour, resetMinute ).getTime()-currentTime.getTime());

if(msToReset >= 0) {
    console.log("HaremHeroes midday reset in "+msToReset+" ms.");
    setTimeout(middayReset, msToReset);
} else {
    console.log("HaremHeroes midday reset already occured.");
}

function Storage()
{
    return localStorage.HHAuto_Setting_settPerTab==="true"?sessionStorage:localStorage;
}

function middayReset()
{
        console.log("HaremHeroes midday reset now !");
        Storage().HHAuto_Setting_autoQuestThreshold="90";
        Storage().HHAuto_Setting_autoTrollThreshold="18";
        Storage().HHAuto_Setting_autoSeasonThreshold="8";
        Storage().HHAuto_Setting_autoLeaguesThreshold="14";
}

You just need to set the correct value for resetHour to match your time zone, and adjust the thresholds values.

Edit : updated code with minutes

NinJaraya commented 3 years ago

Is it possible to do this with minutes too? Like 13:30?

YotoTheOne commented 3 years ago

Yes it is, just replace let msToReset = (new Date(currentTime.getFullYear(), currentTime.getMonth(), currentTime.getDate(), resetHour).getTime()-currentTime.getTime()); by let msToReset = (new Date(currentTime.getFullYear(), currentTime.getMonth(), currentTime.getDate(), resetHour, resetMinute ).getTime()-currentTime.getTime()); and add const resetMinute = 30; after const resetHour = 13;

Edit : updated full code in previous post with minutes

NinJaraya commented 3 years ago

:+1: :1st_place_medal:

YotoTheOne commented 3 years ago

For some reason it didn't reset the autoleague threshold today. Looking into it.

YotoTheOne commented 3 years ago

It does not work anymore with any values. The correct values are retrieved and logged but they are not updated within the main script. Still investigating ^^

Roukys commented 3 years ago

maybe it would be better to add a loop and set them multiple time, just in case ...

Roukys commented 3 years ago
function middayReset()
{
        for (let i =1, i<5, i++)
        {
            setTimeout(resetThreshold, i*500);
        }
}

function resetThreshold()
{
console.log("HaremHeroes midday reset now !");
        Storage().HHAuto_Setting_autoQuestThreshold="90";
        Storage().HHAuto_Setting_autoTrollThreshold="18";
        Storage().HHAuto_Setting_autoSeasonThreshold="8";
        Storage().HHAuto_Setting_autoLeaguesThreshold="14";
}
YotoTheOne commented 3 years ago

with the following code the correct values are retrieved both before and after the update.

function middayReset()
{
    logThresholds();

    console.log("HaremHeroes midday reset now !");
    Storage().HHAuto_Setting_autoQuestThreshold="90";
    Storage().HHAuto_Setting_autoTrollThreshold="18";
    Storage().HHAuto_Setting_autoSeasonThreshold="8";
    Storage().HHAuto_Setting_autoLeaguesThreshold="14";

    logThresholds();
}

function logThresholds()
{
    console.log(Storage().HHAuto_Setting_autoQuestThreshold+" ; "+Storage().HHAuto_Setting_autoTrollThreshold+" ; "+Storage().HHAuto_Setting_autoSeasonThreshold+" ; "+Storage().HHAuto_Setting_autoLeaguesThreshold);
}

but the main script hh auto++ does not take the new values into account. Could there be a conflict like values access rights that would prevent other scripts to modify values while the main script is running ? Or temp storage for variables ? Is sessionStorage script specific or is it the browser session ?

Edit : I'll try to integrate the code directly into the main script to be sure.

Edit 2 : I suspect that at each beginning of autoloop, the updateData() function from the main script retrieves the values set in the menu's textfields to update thresholds in place of the values set by the middayReset script.

YotoTheOne commented 3 years ago

OK that was the issue, now it's working. Here is the code :

// ==UserScript==
// @name         HaremHeroes midday reset
// @version      1
// @description  HaremHeroes midday reset
// @author       roukys and YotoTheOne
// @match        http*://nutaku.haremheroes.com/*
// @match        http*://*.hentaiheroes.com/*
// @match        http*://*.gayharem.com/*
// @license      MIT
// ==/UserScript==

const resetHour = 13;
const resetMinute = 0;
var currentTime = new Date();
var msToReset = (new Date(currentTime.getFullYear(), currentTime.getMonth(), currentTime.getDate(), resetHour, resetMinute).getTime()-currentTime.getTime());

if (msToReset >= 0) {
    console.log("HaremHeroes midday reset in "+msToReset+" ms.");
    setTimeout(middayReset, msToReset);
} else {
    console.log("HaremHeroes midday reset already occured.");
}

function Storage()
{
    return localStorage.HHAuto_Setting_settPerTab==="true"?sessionStorage:localStorage;
}

function logThresholds()
{
    console.log(Storage().HHAuto_Setting_autoQuestThreshold+" ; "+Storage().HHAuto_Setting_autoTrollThreshold+" ; "+Storage().HHAuto_Setting_autoSeasonThreshold+" ; "+Storage().HHAuto_Setting_autoLeaguesThreshold);
}

function middayReset()
{
    logThresholds();
    console.log("HaremHeroes midday reset now !");
    Storage().HHAuto_Setting_autoQuestThreshold = "90";
    Storage().HHAuto_Setting_autoTrollThreshold = "18";
    Storage().HHAuto_Setting_autoSeasonThreshold = "8";
    Storage().HHAuto_Setting_autoLeaguesThreshold = "14";
    document.getElementById("autoQuestThreshold").value = "90";
    document.getElementById("autoTrollThreshold").value = "18";
    document.getElementById("autoSeasonThreshold").value = "8";
    document.getElementById("autoLeaguesThreshold").value = "14";
    logThresholds();
}
NinJaraya commented 3 years ago

Is it possible to also work with Switch? I would like to turn on Collect in Mission after starting a new Contest that takes place at 13:30 in Europe, 9:30 in Brazil.

YotoTheOne commented 3 years ago

Then we need to set it off first inside the middayReset function, and then set another timer to reactivate it 30 mn later. A little more complicated but should work. I'll look into it if I find the time.

Edit : btw you have to set your local time in the script, so if you live in Brasil you need to replace const resetHour = 13; with const resetHour = 9;

Edit 2 : I have an error "Uncaught TypeError: Cannot set property 'value' of null" for the autoQuestThreshold textfield but the code works as intended anyway. I don't know why that specific element triggers the error and the others doesn't.

YotoTheOne commented 3 years ago

@NinJaraya here is the code :

// ==UserScript==
// @name         HaremHeroes contest reset
// @version      1
// @description  HaremHeroes contest reset
// @author       roukys and YotoTheOne
// @match        http*://nutaku.haremheroes.com/*
// @match        http*://*.hentaiheroes.com/*
// @match        http*://*.gayharem.com/*
// @license      MIT
// ==/UserScript==

// Set here your default values : 
const autoQuestDefaultThreshold = "90";
const autoTrollDefaultThreshold = "18";
const autoSeasonDefaultThreshold = "8";
const autoLeaguesDefaultThreshold = "14";
const autoMissionCollectDefault = true;

// set here the time the contest ends, local time from your PC :
const contestEndHour = 9;
const contestEndMinute = 0;

// stop here, do not modify anything else !
var currentTime = new Date();
var msToContestEnd = (new Date(currentTime.getFullYear(), currentTime.getMonth(), currentTime.getDate(), contestEndHour, contestEndMinute).getTime()-currentTime.getTime());

if (msToContestEnd >= 0) {
    console.log("Contest ends in "+msToContestEnd+" ms.");
    setTimeout(contestEnd, msToContestEnd);
} else {
    console.log("Contest already ended today.");
}

function Storage()
{
    return localStorage.HHAuto_Setting_settPerTab==="true"?sessionStorage:localStorage;
}

// function logThresholds()
// {
//     console.log(Storage().HHAuto_Setting_autoQuestThreshold+" ; "+Storage().HHAuto_Setting_autoTrollThreshold+" ; "+Storage().HHAuto_Setting_autoSeasonThreshold+" ; "+Storage().HHAuto_Setting_autoLeaguesThreshold);
// }

function contestEnd()
{
    //logThresholds();
    console.log("Contest ends now ! Setting default values.");
    Storage().HHAuto_Setting_autoQuestThreshold = autoQuestDefaultThreshold;
    Storage().HHAuto_Setting_autoTrollThreshold = autoTrollDefaultThreshold;
    Storage().HHAuto_Setting_autoSeasonThreshold = autoSeasonDefaultThreshold;
    Storage().HHAuto_Setting_autoLeaguesThreshold = autoLeaguesDefaultThreshold;
    Storage().HHAuto_Setting_autoMissionC = "false";
    document.getElementById("autoQuestThreshold").value = autoQuestDefaultThreshold;
    document.getElementById("autoTrollThreshold").value = autoTrollDefaultThreshold;
    document.getElementById("autoSeasonThreshold").value = autoSeasonDefaultThreshold;
    document.getElementById("autoLeaguesThreshold").value = autoLeaguesDefaultThreshold;
    document.getElementById("autoMissionCollect").checked = false;
    //logThresholds();
    setTimeout(contestStart, 1800000);
}

function contestStart()
{
    console.log("New contest starting now ! Mission collect set to "+autoMissionCollectDefault);
    Storage().HHAuto_Setting_autoMissionC = autoMissionCollectDefault;
    document.getElementById("autoMissionCollect").checked = autoMissionCollectDefault;
}

I set the time for you, I let you set your preferred default values. Tell me if it works for you.

@Roukys and the other coders around :

Edit : Now I would like :

NinJaraya commented 3 years ago

I may be talking nonsense. Contests end when Missions start. Just use the same timer.

by Google Translate

NinJaraya commented 3 years ago

Daylight Saving Time in France starts day 28. https://www.timeanddate.com/time/change/france

YotoTheOne commented 3 years ago

Contests end when Missions start.

Yes but you cannot start collecting rewards before the new contest starts in case you need the XP for that new contest.

NinJaraya commented 3 years ago

I did some tests. with the master switch off, it works perfectly. but with the master switch on, the timer bugs when the HH script reloads the game page.

by Google Translate

YotoTheOne commented 3 years ago

what do you mean "the timer bugs" ? What is written in the console ?

NinJaraya commented 3 years ago

Console starts with: "Contest ends in "x" ms." then: "Contest already ended today." next: "Contest ends now ! Setting default values." instead of: "New contest starting now ! Mission collect set to True." stay repeating: "Contest already ended today." in infinite loop.

NinJaraya commented 3 years ago

the line: setTimeout(contestStart, 1800000); resets every time that the game change of the page.

YotoTheOne commented 3 years ago

"Contest already ended today."

Sometimes one of those might slip in after the timer has expired but before the function contestEnd() is executed, that's not an issue.

"Contest ends now ! Setting default values."

It is supposed to happen there ; the contest ends 30 mn before a new contest starts. Was the time wrong for you ?

instead of: "New contest starting now ! Mission collect set to True."

So that last line never happens ?

the line: setTimeout(contestStart, 1800000); resets every time that the game change of the page.

How do you know ? Could you post the console log with timestamps ?

NinJaraya commented 3 years ago

I'll try...

NinJaraya commented 3 years ago

On the first log, I changed the setTimeout to 2 minutes to speed up the test. The Master Switch OFF, everything works normal. On the second log, I went back to the default. The Master Switch ON only with Missions. www.hentaiheroes.com-1616240068759.log www.hentaiheroes.com-1616245130259.log

by Google Translate

YotoTheOne commented 3 years ago

OK thank you for your tests. It seems you're right, for some reason the contestStart() function is not executed if the script resets before the end of the timeout. Let's try a different approach :

// ==UserScript==
// @name         HaremHeroes contest reset
// @version      1
// @description  HaremHeroes contest reset
// @author       roukys and YotoTheOne
// @match        http*://nutaku.haremheroes.com/*
// @match        http*://*.hentaiheroes.com/*
// @match        http*://*.gayharem.com/*
// @license      MIT
// ==/UserScript==

const autoQuestDefaultThreshold = "90";
const autoTrollDefaultThreshold = "18";
const autoSeasonDefaultThreshold = "8";
const autoLeaguesDefaultThreshold = "14";
const autoMissionCollectDefault = true;
const contestEndHour = 13;
const contestEndMinute = 0;

var currentTime = new Date();
var msToContestEnd = (new Date(currentTime.getFullYear(), currentTime.getMonth(), currentTime.getDate(), contestEndHour, contestEndMinute).getTime()-currentTime.getTime());
var msToContestStart = msToContestEnd + 1800000;

if (msToContestEnd >= 0) {
    console.log("Contest ends in "+msToContestEnd+" ms.");
    setTimeout(contestEnd, msToContestEnd);
} else {
    console.log("Contest already ended today.");
}

if (msToContestStart >= 0) {
    console.log("Contest starts in "+msToContestStart+" ms.");
    setTimeout(contestStart, msToContestStart);
} else {
    console.log("Contest already started today.");
}

function Storage()
{
    return localStorage.HHAuto_Setting_settPerTab==="true"?sessionStorage:localStorage;
}

// function logThresholds()
// {
//     console.log(Storage().HHAuto_Setting_autoQuestThreshold+" ; "+Storage().HHAuto_Setting_autoTrollThreshold+" ; "+Storage().HHAuto_Setting_autoSeasonThreshold+" ; "+Storage().HHAuto_Setting_autoLeaguesThreshold);
// }

function contestEnd()
{
    //logThresholds();
    console.log("Contest ends now ! Setting default values.");

    Storage().HHAuto_Setting_autoQuestThreshold = autoQuestDefaultThreshold;
    Storage().HHAuto_Setting_autoTrollThreshold = autoTrollDefaultThreshold;
    Storage().HHAuto_Setting_autoSeasonThreshold = autoSeasonDefaultThreshold;
    Storage().HHAuto_Setting_autoLeaguesThreshold = autoLeaguesDefaultThreshold;
    Storage().HHAuto_Setting_autoChamps = "false";
    Storage().HHAuto_Setting_autoMissionC = "false";

    document.getElementById("autoQuestThreshold").value = autoQuestDefaultThreshold;
    document.getElementById("autoTrollThreshold").value = autoTrollDefaultThreshold;
    document.getElementById("autoSeasonThreshold").value = autoSeasonDefaultThreshold;
    document.getElementById("autoLeaguesThreshold").value = autoLeaguesDefaultThreshold;
    document.getElementById("autoChamps").checked = false;
    document.getElementById("autoMissionCollect").checked = false;

    //logThresholds();
    //setTimeout(contestStart, 1800000);
}

function contestStart()
{
    console.log("New contest starting now ! Mission collect set to "+autoMissionCollectDefault);
    Storage().HHAuto_Setting_autoMissionC = autoMissionCollectDefault;
    document.getElementById("autoMissionCollect").checked = autoMissionCollectDefault;
}

Don't forget to set your preferred default values and keep me posted !

NinJaraya commented 3 years ago

:+1:

NinJaraya commented 3 years ago

so far, everything is going well!

YotoTheOne commented 3 years ago

May need time adjustment today for summer time (or daylight saving time).

NinJaraya commented 3 years ago

:+1:

YotoTheOne commented 3 years ago

I'm closing the issue, if interested the work continues here https://github.com/YotoTheOne/HH-contest-reset