Help please with donating to statues

jwylot

Member
I'm trying to add a module to my aftercore script for automatically donating to the statues as I regularly forget. I'd be really glad if someone could take a look at what I've done and let me know where I've gone wrong. Thank you.

Code:
void statuing()
	{
	int left_donate;
	int max_donate = my_level() * 10000;
	int sofar = to_int (get_property( "heroDonationBoris" ));
	if (sofar < 1000000)
		{
		left_donate = 1000000 - sofar;
		if ( left_donate > max_donate)
		{
		string give_it = "donate boris " + max_donate;
		}
	else
		{
		string give_it = "donate boris " + left_donate;
		cli_execute (give_it);
		}
		}
	else
		{
		int sofar = to_int (get_property( "heroDonationJarlsberg" ));
		}
		if (sofar < 1000000)
		{
		left_donate = 1000000 - sofar;
		if ( left_donate > max_donate)
		{
		string give_it = "donate jarl " + max_donate;
		}
	else
		{
		string give_it = "donate jarl " + left_donate;
		cli_execute (give_it);
		}
		}
	else
		{
		int sofar = to_int (get_property( "heroDonationSneakyPete" ));
		}
		if (sofar < 1000000)
		{
		left_donate = 1000000 - sofar;
		if ( left_donate > max_donate)
		{
		string give_it = "donate pete " + max_donate;
		}
		else
		{
		string give_it = "donate pete " + left_donate;
		cli_execute (give_it);
		}
		}
		}
 
Here is the same code with sane indenting, so you can see what is happening a little better.
PHP:
void statuing()
{
	int left_donate;
	int max_donate = my_level() * 10000;

	int sofar = to_int (get_property( "heroDonationBoris" ));
	if (sofar < 1000000)
	{
		left_donate = 1000000 - sofar;
		if ( left_donate > max_donate)
		{
			string give_it = "donate boris " + max_donate;
		}
		else
		{
			string give_it = "donate boris " + left_donate;
			cli_execute (give_it);
		}
	}
	else
	{
		int sofar = to_int (get_property( "heroDonationJarlsberg" ));
	}

	if (sofar < 1000000)
	{
		left_donate = 1000000 - sofar;
		if ( left_donate > max_donate)
		{
			string give_it = "donate jarl " + max_donate;
		}
		else
		{
			string give_it = "donate jarl " + left_donate;
			cli_execute (give_it);
		}
	}
	else
	{
		int sofar = to_int (get_property( "heroDonationSneakyPete" ));
	}

	if (sofar < 1000000)
	{
		left_donate = 1000000 - sofar;
		if ( left_donate > max_donate)
		{
			string give_it = "donate pete " + max_donate;
		}
		else
		{
			string give_it = "donate pete " + left_donate;
			cli_execute (give_it);
		}
	}
}
What is your problem exactly? One of them might be that you are changing the value of the sofar variable at the wrong places. You might have other problems with this code though, such as using cli_execute() in certain cases but not others.
 
Last edited:
Thanks SO much for the proper indenting, slz. I'd tried to muddle through, and just... yeah. Now I sound more sane. Maybe. :D

1) You redefine sofar every time you call it. You don't put "int left_donate" every time you update it... why do it to sofar? Only define it the first time... no potential issues with it possibly not being defined because you're doing it inside an if, so just do it right.
2) You set give_it if the left amount is greater than the max donation, but never call it. You only call it if the left amount is less... that's probably your hanging point. Easiest fix is to put the cli_execute after that if/else line, so it triggers after you've set the give_it string.
3) You currently keep running through the script until it ends... which means that if you need to donate to Boris, you'll also donate to Jarlsberg and Sneaky Pete. You may want to put a "return;" after your cli_execute lines so they stop the donations after a single series. Either that, or completely refactor your decision code.
 
I recently started being interested in trophies, and your post got me motivated to add donating to my login script too. Here is what I came up with:
PHP:
boolean safe_cli_execute( string cmd )   
{   
    boolean success; 
    try   
    {   
        cli_execute( cmd );   
        success = true;   
    }   
    finally   
    {   
        return success;   
    }   
    return success;   
} 

int total_donation( string hero ) 
{ 
    return get_property( "heroDonation" + hero ).to_int(); 
} 

string to_command( string hero ) 
{ 
    switch( hero ) 
    { 
    case "Boris": return "boris"; 
    case "Jarlsberg": return "jarl"; 
    case "SneakyPete": return "pete"; 
    } 
    return ""; 
} 

item to_key( string hero ) 
{ 
    switch( hero ) 
    { 
    case "Boris": return $item[ Boris's key ]; 
    case "Jarlsberg": return $item[ Jarlsberg's key ]; 
    case "SneakyPete": return $item[ Sneaky Pete's key ]; 
    } 
    return $item[ Xlyinia's notebook ]; 
} 

void statuing() 
{ 
    int donation; 
    int max_donation = my_level() * 10000; 
    foreach hero in $strings[ Boris, Jarlsberg, SneakyPete ] 
    { 
        if ( total_donation( hero ) >= 1000000 ) continue; 
        if ( max_donation <= 0 ) return;
        if ( my_meat() == 0 )
        {
            print( "You need meat to make donations!" );
            return;
        }
        if ( item_amount( hero.to_key() ) == 0 ) 
        { 
            print( "You need a " + hero.to_key() + " to donate to " + hero, "red" ); 
            continue; 
        } 
        donation = min( my_meat(), min( max_donation , 1000000 - total_donation( hero ) ) ); 
        if ( safe_cli_execute( "donate " + hero.to_command() + " " + donation ) ) 
        { 
            max_donation -= donation; 
        }
        // the donation failed, you have probably donated the limit for the day
        else return;
    } 
} 

void main() 
{ 
    statuing(); 
}

EDIT: Apparently I misunderstood how donation works. You can donate a total of max_donate for all three statues, not to each one individually.

EDIT2: code updated to reflect this.

EDIT3: return from statuing() if you don't have enough meat, or if a donation fails.

EDIT3: changed safe_cli_execute() to make sure the return value was correct.
 
Last edited:
Thanks guys. As you can tell, I'm really new to scripting and I learned a lot from both of you today. I guess the routine just got out of hand and I got lost in the illogic somewhere.
 
EDIT: Apparently I misunderstood how donation works. You can donate a total of max_donate for all three statues, not to each one individually.

EDIT2: code updated to reflect this.
Slyz, your version is so much more elegant but it still seems to want to donate max_donation to each statue.
 
Hmmm.....I got that but cli still showed it trying to donate 130K to each statue when I ran it. On another note (he says deftly changing the subject) my script also includes shore trips for the 1000 trip trophy. If I want to keep track of the total trips, am I right in thinking I need to import zlib and create a variable for tracking these?

This is the first time I've tried writing something like this from scratch so forgive me if I'm asking the obvious.

I'm more than happy to drop the entire script here, if it's of interest. So far it does the following:

PVP fights for flowers
Eats black puddings and fights them
Drinks white canadians and spleens
Gets a clodhopper faxed in and uses 4-d camera followed by 5 putty copies
Donates to statues
Does shoretrips
Overdrinks, dresses for max pvp fights and goes to PVP adventure sister clan
 
Or you can set a personal preference with set_property such as
Code:
set_property("shoretrips", to_int(get_property("shoretrips")) + 1)
That will increase the amount taken by 1 for each time it is called.

Edit: You might want to just set it to whatever you're currently at first though...
 
Hmmm.....I got that but cli still showed it trying to donate 130K to each statue when I ran it.
max_donation is decremented only when the donation is successful. If, like me, you have already donated for the day, it will fail three times.

Maybe we should simply return from statuing() if the donation is unsuccessful, because the only reason left for it to fail is having donated the maximum for the day.

You can simply add
PHP:
else return;
at the end of the foreach.

EDIT: you would need to add a check for my_meat() > 0 I edited the post with the code.
 
Last edited:
It doesn't seem to be hitting the "max_donation -= donation;" line (I inserted a print statement to check). I'll have a look later to see if I can understand how try/finally works as it doesn't seem to appear in the wiki.
 
I see. The try...finally structure doesn't work exactly like I thought.

EDIT: the "finally" part is executed even if there is a return in the "try" part, of course. safe_cli_execute() should be:
PHP:
boolean safe_cli_execute( string cmd )  
{  
    boolean success;
    try  
    {  
        cli_execute( cmd );  
        success = true;  
    }  
    finally  
    {  
        return success;  
    }  
    return success;  
}
I edited the post with the code to reflect this.
 
Last edited:
You could try adding a print-statement to each of the try-finally parts (before the returns) so see if they get executed.

Edit: Ninjad while I was eating breakfast :)
 
Yes I inserted print statements for testing tomorrow but that master of .ash, Slyz, has cracked it. Thanks a bunch. Now I can do more housework while collecting trophies :)
 
Back
Top