Feature - Implemented charpane.php hit after item use

Catch-22

Active member
I noticed that after using a tasty fun good rice candy (as an example), KoLmafia hits the charpane page.

Is there any real need for this? The result of using the candy is determined by the response of inv_use.php, the charpane hit afterwards (at least from the GUI/CLI) just seems frivolous, at least in this case.

I've opened this as "Waiting for Info", so I can hear back from the community why this extra hit should be required.

Edit: This behaviour is not just limited to item use, that's just the easiest example I can provide. When you uneffect something, I believe the response text contains the results too, but KoLmafia hits charpane after an uneffect too.
 
Last edited:

slyz

Developer
Mafia grabs any new effect you get from the charpane. Maybe that's not the only reason, I know we rely on the charpane for many things.
 

Catch-22

Active member
Mafia grabs any new effect you get from the charpane. Maybe that's not the only reason, I know we rely on the charpane for many things.

Well, that may be true, but the result of the item use is determined by the response text. Skill use is another case where the charpane seems to be extraneously hit. Here's a run down:


cast skill
kolmafia posts to skills.php
kolmafia parses response text of skills.php
kolmafia updates your active effects based on the response from skills.php
kolmafia gets charpane


So if KoLmafia already knows the results, why fetch the charpane?

There are some effects that may change your stats in a way which the response text doesn't reflect, which could warrant a hit to charpane, but not every skill or item does that and KoLmafia is usually smart enough to predict the outcome of those effects regardless of what charpane says.
 
Last edited:

Winterbay

Active member
I guess hitting the charpane is a nice way to make sure we are not out of synch with KoL for whatever reason (some new thing that eats effects of whatever).
 

lostcalpolydude

Developer
Staff member
I think it's KoL reloading the charpane (based on checking just now outside of mafia when casting a skill), and mafia just passes along the request. Your food example gives an effect, so I wouldn't be surprised if every case of gaining or losing an effect leads KoL to refresh the charpane.
 

Catch-22

Active member
I think it's KoL reloading the charpane (based on checking just now outside of mafia when casting a skill), and mafia just passes along the request. Your food example gives an effect, so I wouldn't be surprised if every case of gaining or losing an effect leads KoL to refresh the charpane.

The browser only reloads the charpane because of javascript (top.charpane.location.href="charpane.php";). I know that's the behaviour of the browser and it happens in a lot of places (not just when you gain an effect), but why does KoLmafia need that? Why not rely on KoLmafia's knowledge of the players existing state and adjust things accordingly? This, for example, is why we do not refresh the inventory every time we use an item, we know how many items the player had before item use, we know the item was used successfully, so KoLmafia saves on server hits by just updating the inventory value internally. For the most part this works, and saves on server hits.
 

Winterbay

Active member
Because it is also used in the relay browser and then we want to do what KoL does? (I'm just guessing)
 

Theraze

Active member
Also, if you happen to have Anapests, Haiku, Cheezburger, or anything added after that messes with your effect texts, checking the charpane may be the only way to validate what really happened.
 

lostcalpolydude

Developer
Staff member
The browser only reloads the charpane because of javascript (top.charpane.location.href="charpane.php";). I know that's the behaviour of the browser and it happens in a lot of places (not just when you gain an effect), but why does KoLmafia need that? Why not rely on KoLmafia's knowledge of the players existing state and adjust things accordingly? This, for example, is why we do not refresh the inventory every time we use an item, we know how many items the player had before item use, we know the item was used successfully, so KoLmafia saves on server hits by just updating the inventory value internally. For the most part this works, and saves on server hits.

KoL provides a way to use items that doesn't lead to a request to reload inventory, which is very different than ignoring a request from KoL to load a page. While there is an effort to avoid unnecessary page loads, I don't think mafia ever ignores a request to load a page (short of specific cases like mallbot prevention).

At best this thread seems like a feature request (where the benefits would barely be noticeable) rather than a bug. Without looking at the code, I have a feeling it will somehow result in weird bugs in niche cases.
 

Catch-22

Active member
Because it is also used in the relay browser and then we want to do what KoL does? (I'm just guessing)

Yeah, I figured this might be the only reason (which is why I specified GUI/CLI). If that's the case, we could differentiate between a relay browser request and one from the regular KoLmafia interface. If it were though, I don't see how hitting charpane internally would even force a refresh in the browser anyway.

While there is an effort to avoid unnecessary page loads, I don't think mafia ever ignores a request to load a page (short of specific cases like mallbot prevention).

I don't know what you mean by "ignoring a request from KoL to load a page". KoLmafia doesn't run the javascript, it's a separate request to charpane.php. If you were to close the charpane frame in your browser, or disable javascript, your browser would not try to reload it.

At best this thread seems like a feature request (where the benefits would barely be noticeable) rather than a bug. Without looking at the code, I have a feeling it will somehow result in weird bugs in niche cases.

"Feature Request - Waiting for Info" wasn't an option :p

I just think anything that is going to reduce unnecessary load on the servers is inherently a good thing and the benefits might actually be pretty noticeable seeings as page load times is a pretty significant factor when it comes to the responsiveness of KoLmafia.

Anyway, I'm not saying it should be completely removed, but for a large number of cases I think hitting the charpane afterwards isn't really needed. If I cast Disco Aerobics as part of a mana burn (ie. I already have turns of "Disco State of Mind"), we know internally how much MP that's going to cost and how many adventures of "Disco State of Mind" it's going to give, so why should we hit the charpane after casting it? Am I missing some other reason why a charpane visit might be justified in this instance?
 
Last edited:

Theraze

Active member
If I cast Disco Aerobics as part of a mana burn (ie. I already have turns of "Disco State of Mind"), we know internally how much MP that's going to cost and how many adventures of "Disco State of Mind" it's going to give, so why should we hit the charpane after casting it? Am I missing some other reason why a charpane visit might be justified in this instance?

Unexpected holidays or holiday effects, such as Boris not getting festival bonuses during Jarlsberg or the like causing MP costs for skills to be wrong?
 

Catch-22

Active member
Unexpected holidays or holiday effects, such as Boris not getting festival bonuses during Jarlsberg or the like causing MP costs for skills to be wrong?

So get the MP cost right :p Also as far as I am aware, you can't cast Disco Aerobics in Boriscore anyway, or are you referring to something else?

How about angry farmer candy extending sugar rush?
 
Last edited:

Theraze

Active member
I was referring to the "we know how much skills cost so we don't need to validate it" part of things. :)

And who knows... as soon as mafia drops parsing for sugar rush or other skills, that means that it's time for Jick and Co to run a "Sugarmas" holiday or the like, where... who knows. :) But the one thing that appears constant is that the KoL devs love keeping the mafia devs (and, for that matter, all KoL players) on their toes. :)
 

Catch-22

Active member
Hmmm to me that sounds kind of like saying "Maybe they'll make a holiday one day where items randomly don't get used when you actually use them, so we should hard refresh the inventory every time we use an item".

It may be worth noting that MP usage in KoLmafia is actually updated immediately after the response is received, and then charpane is hit after that.

Also may I note that the body size of charpane is on average around 5 times the size of api.php?what=status&for=KoLmafia and as far as I know the API contains all of the information that the charpane has these days anyway, right?

Just food for thought.
 

Theraze

Active member
I believe that there are still several things which Veracity has requested be added to status or other apis, for which we still need the charpane refresh. I'm not certain of the latest list of everything found in the charpane v. everything available through that single api call though... :(
 

Catch-22

Active member
I believe that there are still several things which Veracity has requested be added to status or other apis, for which we still need the charpane refresh. I'm not certain of the latest list of everything found in the charpane v. everything available through that single api call though... :(

"refresh all" doesn't even go near the charpane, so I'm guessing there is nothing missing from the API that isn't also missing from the charpane.
 

Veracity

Developer
Staff member
"refresh all" doesn't even go near the charpane, so I'm guessing there is nothing missing from the API that isn't also missing from the charpane.
That was true until they added the number of PVP fites remaining to the charpane. I have requested that be added to the API. But, since we don't parse it from there, anyway, since displaying it is opt-in, that's not a big concern.

I've thought about - and experimented with - doing this for years. The following code in GenericRequest determines if we generate a request for charpane.php:

Code:
		if ( this instanceof RelayRequest )
		{
			this.containsUpdate = false;
		}
		else if ( effectCount != KoLConstants.activeEffects.size() || this.getAdventuresUsed() > 0 )
		{
			this.containsUpdate = true;
		}
		else
		{
			this.containsUpdate |= this.responseText.indexOf( "charpane.php" ) != -1;
		}

		if ( this.containsUpdate )
		{
			new CharPaneRequest().run();
			RelayServer.updateStatus();
		}

Note that we never look at the char pane after a RelayRequest; we assume that the browser will generate one, if KoL asked for it, and we will pull what we need from it.

Note that we will ask for the char pane if you've gained or lost an effect or we believe that the request might have consumed an adventure.

And finally, note that if KoL tells us that the char pane has changed, we will load it, just like the Browser would in such a case. (Which is why saying "oh, it's just Javascript and the Browser won't load it if you turn off Javascript" is a red herring; if you try to run vanilla KoL with Javascript disabled, you will not be happy, for that and many other reasons.)

The middle check is probably subsumed by the last one, because if you execute a request in the browser and KoL does not tell you the char pane has changed, it's pointless to fetch it again. I.e., any change in effects or adventures used will spur KoL to say "get the char pane".

We could probably skip refreshing the char pane just for gaining or losing effects, but tracking adventures is The Big Reason we look at the char pane. KoLmafia does not "count adventures", contrary to the impression of many many posts that I've seen over the years. KoLmafia looks at the char pane and detects that the adventure count has changed and then updates things that depend on that. THAT is something that is not going to change; if we start actually "counting adventures", every time KoL adds something new that uses an adventure - or doesn't use an adventure (a choice option that lets you skip the choice, for example), then, suddenly, counters break and screams of outrage arise from users until we add it to the code.

This request wants to skip requests after items are used. Fine. First, complete the refactoring of UseItemRequest to make sure that all updating of KoLmafia's internal state occurs when we are parsing the response text, rather than doing some of it when the request is submitted and, perhaps, rolling it back if it failed. I think UseItemRequest could stand a major refactoring. It is complicated by the fact that sometimes item usage comes back as a response to inv_use.php and sometimes as a redirect to inventory.php?action=message. And there are so many special cases for items, with special messages determining exactly what happened when you use them. Yes, we can detect "You gain an effect", and such. Perhaps we can detect "You gain (or lose) xxx HP (or MP)".

I think the most promising idea is to change the line in GenericRequest.processResponse() that calls "new CharPaneRequest().run()" to do an ApiRequest?what=status instead and just parse the JSON (using existing code) rather than the HTML text.
 

slyz

Developer

kolmafia updates your active effects based on the response from skills.php
I didn't see anything in UseSkillRequest.java that does that. As Veracity (and my Eclipse-less source searching fu) confirmed, we really do rely on the charpane refresh to scrap any change to your current effects.

I'll try to play a bit using api.php instead of charpane.php to see how it goes.
 

fronobulax

Developer
Staff member
It may not be relavent and it may not be a correct memory, but I seem to recall visiting the charpane after using something was part of the logic that detected new content and made mafia behave reasonably between the time the content was introduced and the time that mafia had code and data changed and released to support the content.
 

slyz

Developer
I followed Veracity's advice and replaced
PHP:
new CharPaneRequest().run();
with
PHP:
new ApiRequest().run();
in GenericRequest. I then cast a few buffs, automated a day's worth of adventures, and overdrank. I didn't run into any problem, apart from the constant "Loading character status..." message in the gCLI which we should remove.

I didn't notice any speedup, though, and in a sense we are still using the same number of server hits. I guess using less bandwidth is a good thing.

I guess someone could submit the change and we will deal with the issues that arise, à la Holatuwol. Or I can list everything we update when we visit charpane.php, and then list everything we update when we visit api.php?what=status. That would give us an idea of what to expect.
 
Last edited:
Top