Running a script once per day?

Is there some automated way to do this? Perhaps as part of breakfast? Or would that not be reliable? Should I just do it manually?

I'm thinking about the mushroom fields script here
 

zarqon

Well-known member
Putting it in your login script should work. Be careful though, because the login script runs again if you lose your connection and mafia has to log back in. For that reason it's best to only include things that can be safely run multiple times, or include a check in the script to see if it's already been done. For example, for eating it's easy to check my_fullness(), so you can safely include eating here.

As long as your script has the appropriate safety checks, putting it in your login script is reliable.
 

Alhifar

Member
I know this is a slight necro, but I happened to need this anyways, so I wrote up a little scriptlet for running a script exactly once a day. (Or, theoretically, any given number of times per day.)

In your login script:
Code:
void main()
{
	if( get_property( "_sessionNumber" ) == "" ) set_property( "_sessionNumber" , "1" );
	if( get_property( "_lastRun" ) == "" ) set_property( "_lastRun" , today_to_string() );
	if( get_property( "_lastRun" ) != today_to_string() )
	{
		set_property( "_lastRun" , today_to_string() );
		set_property( "_sessionNumber" , "1" );
	}
	if( get_property( "_sessionNumber" ).to_int() == 1 )
	{
		## Anything you want done only the first time mafia is run goes here
	}
}

And in a logout script:
Code:
void main()
{
	if( get_property( "_lastRun" ) == today_to_string() ) set_property( "_sessionNumber" , ( get_property( "_sessionNumber" ).to_int() + 1 ).to_string() );
}

EDIT: You could actually put the code from the logout script after the end of the stuff you want done once only for a better failsafe if mafia times out. Probably would work better unless you want to use the session number in your logout script as well.
 

zarqon

Well-known member
Nice! One little fix: changing the variable in the logout permits the player to run something multiple times in one session. Changing the variable in the logout is unreliable anyway, since sometimes the logout script is not called (mafia hangs, the session expires, or a script is run multiple times during debugging. It would be better to take care of it at the same time as everything else:

...
if( get_property( "_lastRun" ) != today_to_string() ) {
set_property( "_lastRun" , today_to_string() );
set_property( "_sessionNumber" , "1" );
} [b]else set_property( "_sessionNumber" , ( get_property( "_sessionNumber" ).to_int() + 1 ).to_string() );[/b]
...
 

Alhifar

Member
Actually, should probably be:
Code:
void main()
{
	if( get_property( "_sessionNumber" ) == "" ) set_property( "_sessionNumber" , "1" );
	if( get_property( "_lastRun" ) == "" ) set_property( "_lastRun" , today_to_string() );
	if( get_property( "_lastRun" ) != today_to_string() )
	{
		set_property( "_lastRun" , today_to_string() );
		set_property( "_sessionNumber" , "1" );
	}
	if( get_property( "_sessionNumber" ).to_int() == 1 )
	{
		## Stuff to run once a day goes here.
	}
	if( get_property( "_lastRun" ) == today_to_string() ) set_property( "_sessionNumber" , ( get_property( "_sessionNumber" ).to_int() + 1 ).to_string() );
}

If you placed the session number incrementing line before the part to be run once, it would end up never being run, unless I'm mistaken.
 

zarqon

Well-known member
That's kind of the idea. It won't run, other than the first time (when _lastRun == today). Note the "else".

That said, either of the above will work.
 

Alhifar

Member
Yeah, I completely missed that "else". Sorry about that.

In any case, I've started getting weird results. Today, it decided to set my session number to 4 on the increment, instead of 2. Possibly has to do with the other property setting bugs I've seen a few threads about here. EDIT: Found the problem, had to do with a completely unrelated script in my login script that I managed to typo.

EDIT2: I was thinking today (yes, I know that's probably dangerous) and realized that these two lines:
Code:
if( get_property( "_sessionNumber" ) == "" ) set_property( "_sessionNumber" , "1" );
if( get_property( "_lastRun" ) == "" ) set_property( "_lastRun" , today_to_string() );

are rather redundant, and can be removed. The next line should set them if they aren't set anyways.
 
Top