Author Topic: Run Script Once Per Day / Month  (Read 331 times)

Legacy_Supreme_Pizza

  • Jr. Member
  • **
  • Posts: 90
  • Karma: +0/-0
Run Script Once Per Day / Month
« on: January 21, 2013, 08:16:25 am »


               I got my investment script working when I flip a switch. So I guess it's more like a slot machine at this point. ':blink:'

Anyway...
I need it to run once a day. There is one section in it that needs to run once a month so all I'd need to do for that is check the date.

I don't know how to get a script to fire without an event or placable trigger.
               
               

               
            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
Run Script Once Per Day / Month
« Reply #1 on: January 21, 2013, 03:54:09 pm »


               As far as I know you can't get a script to run without some sort of event.

For this why not just put it in the module HB event. Most of the time it won't do anything except a quick check if the saved day (or saved month) has changed. If  it has do the work and save
the current day (month).
               
               

               
            

Legacy_Squatting Monk

  • Hero Member
  • *****
  • Posts: 776
  • Karma: +0/-0
Run Script Once Per Day / Month
« Reply #2 on: January 22, 2013, 05:46:31 am »


               Unless you're planning on advancing the clock instead of letting it run as normal, don't use your HB script for this. Instead, start a pseudo-hb on the OnModuleLoad event. For example...

// my_daily_script.nss
void main()
{
    // Do stuff here...

    // Call ourselves again in one day
    float fDelay = HoursToSeconds(24);
    DelayCommand(fDelay, ExecuteScript("my_daily_script", OBJECT_SELF));
}

...and then...

// in your OnModuleLoad...
ExecuteScript("my_daily_script", OBJECT_SELF);

               
               

               


                     Modifié par Squatting Monk, 22 janvier 2013 - 05:47 .
                     
                  


            

Legacy_Supreme_Pizza

  • Jr. Member
  • **
  • Posts: 90
  • Karma: +0/-0
Run Script Once Per Day / Month
« Reply #3 on: January 23, 2013, 12:17:12 am »


               Wouldn't that restart the timer each reboot?

Could I just add the script to a bell ringer?

I have one of those.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    if (nHour==0 && GetLocalInt(OBJECT_SELF,"Called_Time")!=nHour)
    {
        SetLocalInt(OBJECT_SELF,"Called_Time",0);
// ** Execute script here?? **//
        DelayCommand(0.0,AssignCommand(OBJECT_SELF, PlaySound("as_cv_bell2")));
        DelayCommand(2.0,AssignCommand(OBJECT_SELF, PlaySound("as_cv_bell2")));
        DelayCommand(4.0,AssignCommand(OBJECT_SELF, PlaySound("as_cv_bell2")));
        DelayCommand(6.0,AssignCommand(OBJECT_SELF, PlaySound("as_cv_bell2")));
        DelayCommand(8.0,AssignCommand(OBJECT_SELF, PlaySound("as_cv_bell2")));
        DelayCommand(10.0,AssignCommand(OBJECT_SELF, PlaySound("as_cv_bell2")));
        DelayCommand(12.0,AssignCommand(OBJECT_SELF, PlaySound("as_cv_bell2")));
        DelayCommand(14.0,AssignCommand(OBJECT_SELF, PlaySound("as_cv_bell2")));
        DelayCommand(16.0,AssignCommand(OBJECT_SELF, PlaySound("as_cv_bell2")));
        DelayCommand(18.0,AssignCommand(OBJECT_SELF, PlaySound("as_cv_bell2")));
        DelayCommand(20.0,AssignCommand(OBJECT_SELF, PlaySound("as_cv_bell2")));
        DelayCommand(22.0,AssignCommand(OBJECT_SELF, PlaySound("as_cv_bell2")));
    }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Thanks for opening my eyes to that.
It seems so simple now.

How would I format an if statement to check for the date?

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                //If item is a bond
                if (sBondCheckString == "bond")
                {
//check for date here
                    if (iDSixPosTagSum >= 50)
                    {
                        //Generate new item tag from list at stocks_inc_const
                        string sItem = "sStock50";

                        //get "player" and "tag" from "temp_vault"
                        string sSQLx = "SELECT player, tag  FROM temp_vault Where columnid = " + sRowCount;
                        SQLExecDirect(sSQLx);
                        SQLFetch();
                        string sPlayerLoginID = SQLGetData(1);
                        string sPlayerCharacterName = (SQLGetData(2));

                        //Put the new item in "vault_items"
                        string sSQLw = "insert into vault_items (player, tag, container, item, count, identified)VALUES('"+ sPlayerLoginID +"','"+ sPlayerCharacterName +"','" + sVaultTag +"','" + sItem +"', 1, 1)";
                        SQLExecDirect(sSQLw);
                    }
                    else
                    {
                        //Generate new item tag from list at stocks_inc_const
                        string sItem = "sStock" + sDSixPosTagSum;

                        //get "player" and "tag" from "temp_vault"
                        string sSQLx = "SELECT player, tag  FROM temp_vault Where columnid = " + sRowCount;
                        SQLExecDirect(sSQLx);
                        SQLFetch();
                        string sPlayerLoginID = SQLGetData(1);
                        string sPlayerCharacterName = (SQLGetData(2));

                        //Put the new item in "vault_items"
                        string sSQLw = "insert into vault_items (player, tag, container, item, count, identified)VALUES('"+ sPlayerLoginID +"','"+ sPlayerCharacterName +"','" + sVaultTag +"','" + sItem +"', 1, 1)";
                        SQLExecDirect(sSQLw);
                    }
                }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Thanks again!
               
               

               
            

Legacy_Squatting Monk

  • Hero Member
  • *****
  • Posts: 776
  • Karma: +0/-0
Run Script Once Per Day / Month
« Reply #4 on: January 23, 2013, 03:36:21 am »


               Yeah, it will reset when the server resets, but that's okay. You just need to DelayCommand() the call in your OnModuleLoad script the appropriate number of seconds. So for a script that should fire at midnight every new day...

// In your OnModuleLoad, after you've restored the persistent time

// Figure out how many hours until midnight and delay the execution
int nDelay = (24 - GetTimeHour()) % 24;
DelayCommand(HoursToSeconds(nDelay), ExecuteScript("my_daily_script", OBJECT_SELF));

You might have to tweak the math if your persistent clock also accounts for minutes and seconds (mine does), but this'll get you started.
               
               

               


                     Modifié par Squatting Monk, 23 janvier 2013 - 03:56 .
                     
                  


            

Legacy_Supreme_Pizza

  • Jr. Member
  • **
  • Posts: 90
  • Karma: +0/-0
Run Script Once Per Day / Month
« Reply #5 on: January 26, 2013, 09:06:42 am »


               That is so sweet.
I don't even have to find out if it is successful or not.
All that needs to be done is add:

on duplicate key update count=count+1;

To the end of the existing query.

That is very handy.
I've been using MySQL for 5 years and didn't know that command existed.

Where did you run across that nugget?
               
               

               
            

Legacy_Squatting Monk

  • Hero Member
  • *****
  • Posts: 776
  • Karma: +0/-0
Run Script Once Per Day / Month
« Reply #6 on: January 26, 2013, 11:10:27 am »


               Yeah, it's pretty awesome. I've been using it in my queries for years. Not really sure where I first found it. Too bad it doesn't work in SQLite.
               
               

               
            

Legacy_FunkySwerve

  • Hero Member
  • *****
  • Posts: 2325
  • Karma: +0/-0
Run Script Once Per Day / Month
« Reply #7 on: January 26, 2013, 03:48:15 pm »


               

Supreme_Pizza wrote...

That is very handy.
I've been using MySQL for 5 years and didn't know that command existed.


Their online manual is handy: Click Me
I bookmarked the date/time functions in particular because I wind up using them more than the others, but that links to all the commands/manuals for a number of different versions.

This site was also handy when I was starting out, though I haven't used it in a long time:
Click Me

This site has a number of great examples for things that I found myself doing:
http://www.pantz.org...qlcommands.html

Funky