Trouble with a Clan Stash Multi-Item Add Function

Potatoman

New member
I was chewing away at my list of Clan Bot parsing/submission functions, and decided that it'd be good for me to make a function for adding multiple items to the clan stash simultaneously.

So, I ran the debug log, and put together a function that (besides mercilessly ripping off zargon's kmail() function) multi-adds and removes a list of items from the clan stash:

HTML:
#Now in most recent, fixed state. Still evolving though :P

boolean clan_stash_modifier( boolean add_items , int[item] additions )
{
   int num = 0;
   string stash_submit = "clan_stash.php?action=addgoodies";
   string[int] submit_strings;
   boolean operation_success = true;
   
   string stash_html = "";
   string pwd_value = password_grab();

   if( add_items == true )
   {
   	if( count(additions) > 0 ) {
	    foreach key, int in additions {
	       	num = num+1;
	      	stash_submit = stash_submit + "&qty"+num+"="+int+"&item"+num+"="+to_int(key)+"";
	   	
              	if( num > 10 ) {
	            submit_strings[count(submit_strings)] = stash_submit;
	            stash_submit = "";
	  	    num = 0;
	        }
	    }
	}
	
      	if( stash_submit != "" ) {
	    submit_strings[count(submit_strings)] = stash_submit;
   	    for steve from 0 upto count(submit_strings)-1 {
           	stash_html = visit_url( submit_strings[steve]+"&pwd="+pwd_value+"" );
           	if( contains_text(stash_html,"You did not select any items to add to the Hoard.") ) { 
		   operation_success = false; 
		   print( "Select an item!" , "Red" ); 
		}
	    }
      	}
   }
   else
   {
       	if( count(additions) > 0 ) {
	    operation_success = true;
	    foreach key, int in additions {
		boolean working = take_stash(int,key);
	    	if( working == false ) operation_success = false;
	    }
   	}
	else
	{
	    print( "You have to provide a list of items to remove from the Stash." , "Red" );
	}
   }
   return operation_success;
}

The problem is that it seems to not be seeing the "if( add_items == true )", the existence of the boolean add_items, and tries to evaluate "else" as a variable (when I put that in instead of a "if( add_items == false )").

It looks like it should be working to me as-is, and the problem doesn't seem to be stemming from its location in a function library - even when isolated in its own, the problem persists. Not sure what's up here...
 
Last edited:

Veracity

Developer
Staff member
You are getting a compile error because you really do have a syntax error.
Code:
      	if( stash_submit != "" ) submit_strings[count(submit_strings)] = stash_submit;
   	    for steve from 0 upto count(submit_strings) {
           	stash_html = visit_url( "clan_stash.php?action=addgoodies"+submit_strings[steve]+"&pwd="+pwd_value+"" );
           	if(contains_text(stash_html,"You did not select any items to add to the Hoard.")) { operation_success = false; print( "Select an item!" ); }
	    }
      	}

That is indented as if you think there is a block following the if, but, as you can see, there is a single statement on the same line.
 

jasonharper

Developer
You're missing a '{' after the line that starts with if( stash_submit != "" ). As it is, the function ends with the '}' just before the 'else'.

You might want to consider finding a better editor - any text editor with any pretensions of being usable for programming work is going to have some way of checking that your delimiters balance properly. For example, in some editors double-clicking on an opening bracket or other delimiter will highlight everything between it and the matching closing delimiter, or vice versa.

Also, you're really doing things the hard way here. You can get exactly the same benefit in server hit reduction by simply calling put_stash() in a loop, with a call to batch_begin() before the loop, and batch_close() afterwards. The batch_close() is the call you want to check the return value of, to determine if the operation was successful.
 

Potatoman

New member
Thanks a ton, Veracity! Can't believe I missed that...

I feel like an idiot, but I guess it's not that odd a mistake - fresh eyes and all that are wonderful!

You might want to consider finding a better editor - any text editor with any pretensions of being usable for programming work is going to have some way of checking that your delimiters balance properly. For example, in some editors double-clicking on an opening bracket or other delimiter will highlight everything between it and the matching closing delimiter, or vice versa.

I've been thinking about that myself. For linux it's easy to grab one (just connect to the repository), but on Windows you have to know what you're looking for. For ASH or PERL, is there any free/open-source you'd suggest using on Windows?

As for the function, yeah - I'll look right into those. Soo many features in ASH I don't even know about, even when I'm working off of the wiki.

EDIT: I looked for the batch_begin() and batch_close() commands like you suggested, and couldn't find any info about them in ashref, or here on (+/- wiki.)KoLMafia.us - are they new in the latest version/updates? Also, do you run a loop inside of them, or are they the loop itself?
 
Last edited:

Camber

Member
Regarding ash editors on windows' boxen, check out this discussion: http://kolmafia.us/showthread.php?t=2120

Bale, myself and others use the open source app Notepad++. Bale has written a custom lang for ash.

Very nice!


Edit: And to me it looks like you are 1 '}' too heavy. Remove it from just before the else
 

Heffed

Member
The batch functions were added in r7551, almost a month ago - see the commit message for more details.

I'm glad you pointed this out! It's bothered me for quite some time that KoLmafia was able to transfer things in groups, but by script it had to be one at a time. I asked Holatuwol about it a long time ago and I honestly can't remember the reasoning for it being how it was.

Anyway, I've just altered my scripts to use the batch method. Thanks!
 
Top