DragonPrime - LoGD Resource Community
Welcome Guest
  • Good morning, Guest.
    Please log in, or register.
  • November 19, 2018, 03:04:54 AM
Home Forums News Downloads Login Register Advanced Search
* * *
DragonPrime Menu
Login
 
 
Resource Pages
Search

Pages: [1]   Go Down
  Print  
Author Topic: Gamewide Newday variables  (Read 3055 times)
0 Members and 1 Guest are viewing this topic.
Talisman
Administrator
Mod God
*****
Offline Offline

Posts: 5479



View Profile WWW
« on: May 11, 2004, 06:50:01 AM »

Other Problem:
I would like to add a function, that the bank can only be robbed once a day. But I have no idea how to manage it. I tried a setting "bankrobtoday", but I don't know how to get the game to set this value back to 0 every time a new day starts.

I was looking for a similar function for a couple of other things I'd like to try...didn't have much luck though.

Unlike some games which use a cron job to perform global functions at a particular time each day, LotGD doesn't seem to have a collective awareness of a newday.  The newday function acts solely on the individual players.

With a bit (or a lot) of work, you could probably set a gamesettings variable which would be activated everytime any player runs newday and which would only change once per game day...but I suspect there would be some major issues with avoiding multiple attempts to write to that variable.

Anybody else have any ideas?
Logged

Play the latest beta version here on DragonPrime
Kendaer
Guest
« Reply #1 on: May 11, 2004, 07:32:59 AM »

While I know this doesn't necessarily help you *yet*, this facility is actually in there in the 0.9.8 code with a newday-runonce module hook.
Which means you can write a module which will only execute once every server new day.

However, the way it's done is in the newday code, we do this.

Code:
       //Let's do a new day operation that will only fire off for
        //one user on the whole server.
                                                                               
        //check last time we did this vs now to see if it was a different game day.
        $lastnewdaysemaphore = convertgametime(strtotime(getsetting("newdaySemaphore","0000-00-00 00:00:00")));
        $gametoday = gametime();
        if (date("Ymd",$gametoday)!=date("Ymd",$lastnewdaysemaphore)){
            //it appears to be a different game day, acquire semaphore and check again.
            $sql = "LOCK TABLES settings WRITE";
            db_query($sql);
            clearsettings();
            $lastnewdaysemaphore = convertgametime(strtotime(getsetting("newdaySemaphore","0000-00-00 00:00:00")));
                                                                               
            $gametoday = gametime();
            if (date("Ymd",$gametoday)!=date("Ymd",$lastnewdaysemaphore)){
                //we need to run the hook, update the setting, and unlock.
                savesetting("newdaySemaphore",date("Y-m-d H:i:s"));
                $sql = "UNLOCK TABLES";
                db_query($sql);
                                                                               
                //run the hook.
                modulehook("newday-runonce",array());
            }else{
                //someone else beat us to it, unlock.
                $sql = "UNLOCK TABLES";
                db_query($sql);
            }
        }

Basically, if you replace the line 'modulehook('newday-runonce', array())' with the code you want to run exactly once, this should work.

The reason for the double check in there is to prevent two page hits from causing a race condition.  It works. Smiley

So, if you really want to do this, you can wedge this code into the middle of newday and make use of it, or you can wait for 0.9.8 and just write a module that uses the newday-runonce hook Smiley

--JT
« Last Edit: May 11, 2004, 07:35:12 AM by Kendaer » Logged
Kendaer
Guest
« Reply #2 on: May 11, 2004, 07:36:47 AM »

Please note, that it's possible that some of the functions like clearsettings() *might* not exist in 0.9.7.  If not you'll have to intuit what they do and write an equivalent.  (in fact clearsettings doesn't exist in 0.9.7)
Logged
MightyE
Global Moderator
Captain of the Guard
*****
Offline Offline

Posts: 104


Game Creator MIGHTYE


View Profile
« Reply #3 on: May 11, 2004, 07:54:43 AM »

Quote
The reason for the double check in there is to prevent two page hits from causing a race condition.

More specifically, we check the value outside of a semaphore (roughly, a semaphore means that  only one process can execute a semaphoric block of code at one time) to see if it looks like maybe this needs to be done.  We could check only one time and just acquire the semaphore every time before checking, but then we've got a blocking situation a lot of times when we don't need it.

So we check outside of the semaphore, if it looks like maybe we should be doing this operation, we acquire the lock and check again to ensure we don't have a race condition (common especially under high loads, such as what happens at the time when the new game day first starts).  Only once a game day will there be a blocking condition, the rest of the time we never have to block.

I rely on MySQL to provide the semaphore, and I block on the settings table (necessary since we'll be writing to it, and while you have any MySQL write lock, you can only write to tables that you have locked).  PHP provides semaphoric functions, but they are not compiled in by default, and not everyone will have them (most will not), and even then in some threaded environments such as ISAPI (windows), you're not actually guaranteed a semaphore is safe to use.  

Notably the MySQL approach will prevent other scripts from being able to read from the settings table while the lock is in place.  You'll be blocking all users of the site, not just those going through new day.  We release the lock before we do the work though, sure that only one process will make it to the work section.  Unless the server is being positively crushed, this shouldn't ever block for more than a few hundredths of a second.  If the server's being totally crushed, the amount of time it takes to make the 3 SQL calls that are necessary to pull this off (you lock, set a game setting, and unlock), and execute the half dozen lines shouldn't even really be that noticeable compared to the time it takes to run the rest of the page.  You will, however, see a small spike in load just due to the blocking, if your server's being crushed.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  


*
DragonPrime Notices
Play LoGD on Dragonprime

Support Us
No funds raised yet this year
Your help is greatly appreciated!
Recent Topics
DragonPrime LoGD
Who's Online
46 Guests, 0 Users
Home Forums News Downloads Login Register Advanced Search