Feature ASH fold() command

Fluxxdog

Active member
This is one of those CLI commands that doesn't have an ASH counterpart.

boolean fold(item desired)

Returns true if you get desired.
Returns false if you don't for any reason.
Thoughts:
Can abide by hpAutoRecovery settings and will execute restore_hp() as appropriate.
If it fails because you don't have the appropriate item, prints an appropriate message to the CLI, such as "You don't have anything that folds into a flaming pink shirt"
 

Bale

Minion
I love to post functions from my scripts whenever I have an excuse, so...

PHP:
boolean fold(item doodad) {
	if(available_amount(doodad) > 0) return true;
	foreach other in get_related(doodad, "fold")
		if(available_amount(other) > 0)
			return cli_execute("fold "+doodad);
	return false;
}
 

Fluxxdog

Active member
I get what you're saying Bale since I have a similar thing. As been established before, the return value of cli_execute() does not return information about the actual command, only whether the CLI was engaged successfully. So if the fold fails, you still get true returned.
 

Theraze

Active member
At the very least, whether or not it ended in an error/red/abort state. :) That would be a failed execution.
 

Theraze

Active member
Which is different from what Fluxxdog said, correct? If I understand what he says, he believed that cli_execute() only returned whether or not the command was begun, not whether or not it was 'successful' or not. If you cli_execute("fold something") and the fold fails, the return should tell you that the fold failed...

Or am I wretchedly confused about this whole thing? It's always a major possibility. :)
 

Veracity

Developer
Staff member
Code:
	public static Value cli_execute( Interpreter interpreter, final Value string )
	{
		KoLmafiaCLI.DEFAULT_SHELL.executeLine( string.toString(), interpreter );
		return RuntimeLibrary.continueValue();
	}
It sends the line to the CLI interpreter and then returns true or false on whether KoLmafia is in a continuable state. As far as I know, cli_execute has never changed.

I'll let Fluxxdog elaborate on how he thought it worked.
 

Fluxxdog

Active member
Code:
> ash cli_execute("fold ice baby");

[COLOR="#FF0000"]You don't have anything transformable into that item!
[/COLOR]Returned: false

> ash cli_execute("fold origami pasties");

Using 1 origami riding crop...
[COLOR="#FF0000"]IOException during data post (inv_use.php?whichitem=3197&ajax=1). <==Caused by unplugging ethernet
[/COLOR]Finished using 1 origami riding crop.
Returned: true
cli_execute() returns false if the last state is an error state, regardless of command. That's been my understanding. The CLI counters command gives you a list of current counters, but when run through cli_execute, you only get true or false. If you want data you can use, the ASH get_counters gives you want you actually want.

The ASH command I'm requesting detects just a successful fold, not the returned error state of the CLI.
 

Veracity

Developer
Staff member
cli_execute() returns false if the last state is an error state, regardless of command.
Bingo. CLI commands do not have any other "value" that they can return to you. If your ASH script wants a useful value, you need to use a native ASH function, rather than invoking the CLI command - and that ASH function needs to be built on internal requests, rather than on a CLI command.

I completely understand that that is why you want a native "fold" function. (For the record, the CLI has "commands" and ASH has "functions"). I was responding to Theraze's muddled comment about how cli_execute "passes on the actual return value of CLI commands" - as if CLI commands have an "actual return value".
 

Fluxxdog

Active member
Thanks Veracity. And sorry for the command/function flip flop. I'm studying Java programming, I should be on the ball more. I'm just glad it's clear what I'm asking.
 

fronobulax

Developer
Staff member
So, knowing what we now know about vocabulary, the OP could be restated as a request for an ash fold function. :)

That said, and I think Bale alluded to this, if you wrote a wrapper around cli_execute that checked for the desired item after the cli command had returned and returned true if the item was present and false otherwise, wouldn't you have the desired functionality?
 

Fluxxdog

Active member
To properly handle checking for it, the wrapper would need to check the inventory before checking for a return value, likely an available_amount(doodad)>0. However, to do with a higher rate of accuracy, we'd need to refresh the inventory so if an error occurred, we would have the latest information. However, refreshing the inventory is a CLI command. There is no equivalent ASH function. Which means another possible discrepancy if an error occurred while trying to refresh the inventory. Which means... Eh, I feel like I'm starting to get recursive here. We've established that it's possible for the cli_execute() to return data that isn't helpful when executing a command. An error may occur, but that doesn't get reported. Only the final state does.

The function I'm asking for is just a way to report if something actually went wrong with trying to fold an item.

The only other option to make the wrapper work would be to have cli_execute() return false if ANY error state is encountered, not just the last one. That not only would be a different issue but could possibly break a lot of scripts and I don't want to be the one to suggest that.
 

Veracity

Developer
Staff member
However, to do with a higher rate of accuracy, we'd need to refresh the inventory so if an error occurred, we would have the latest information.
I hate it when people say that.

If your inventory is inaccurate, submit a bug report explaining what you did that caused it to become inaccurate, so we can fix the KoLmafia bug. Do not code in workarounds in your scripts assuming that the inventory is inaccurate.
 

Fluxxdog

Active member
Sorry, I thought that workaround was what fronobulax was suggesting. Otherwise I wouldn't need to check for the item after the CLI fold. It's also likely I misunderstood?

Maybe it's another case of misunderstanding terms. I thought a wrapper was just designed to process information on a basic level to present to another function or command, like making sure you're not dividing by zero or a string is formatted correctly for processing. A workaround is a measure taken to get something to "just work" until a proper solution is implemented, like mafia not tracking something yet. The wrapper frono suggests sounds more like working around cli_execute() not returning data that confirms command success, let alone item creation.
 

Veracity

Developer
Staff member
Fronobulax said "check the inventory before and after the command." There is nothing wrong with that; KoLmafia uses that technique itself.

He did not say "refresh the inventory", which is what I said that I hated, since it is an unnecessary server hit to workaround KoLmafia bugs which may or may not exist.
 

Theraze

Active member
Unplugging the internet during the command execution could cause it to come out of sync, as per the example in post 10 for the CLI command not having the right information. Otherwise, the CLI command already gives the information as to whether or not the fold was possible.
 

Fluxxdog

Active member
I admit Theraze, it's an extreme case.

Out of curiosity, what is it in mafia that actually does the folding in the CLI? Maybe instead of asking for someone to do it, I can be a bit more proactive and at least look at doing it myself. Like I said, I'm am studying for it, I should at least attempt to put that knowledge to use.
 

Darzil

Developer
I admit Theraze, it's an extreme case.

Out of curiosity, what is it in mafia that actually does the folding in the CLI? Maybe instead of asking for someone to do it, I can be a bit more proactive and at least look at doing it myself. Like I said, I'm am studying for it, I should at least attempt to put that knowledge to use.

Start at \src\net\sourceforge\kolmafia\textui\command\FoldItemCommand.java and work from there.
 
Top