Jump to content

A waiting game.. Is this the best, or is there another way to do this?


l0gic

Recommended Posts

Haven't done much in PHP for a while, and as a refresher and a bit of a challenge to myself I have decided to write up a simple game of sorts.

 

Basically in the game I have townships, every play has their own town. And I want these towns to have events every 3-10 hours, and have these events have results such as births/deaths/fires/flooding/etc.

 

At the moment I'm looking at this kind of setup:

A database table with the city information.. city_id, city_owner_id, city_name, etc. I figure I can make another field such as city_nextevent and fill it with a timestamp plus a random number of seconds. then when the user views their city if city_nextevent is older than current time then run the event function and set the next event time.

 

In my head this idea seems to work well, until I realised that if the user doesn't log on for 20+ hours the events will not stack, they will only get the one event when they next view their town when several events should have passed. I realise I could use a cronjob to check all events across all towns, but is there a better way of doing it.

 

I had also though about adding several fields to the database containing say the next five event times, then updating each field after time had passed. Which gives more events if the user has been offline for 20+ hours. So if the play was offline for say a week, their town would pretty much dry up until they returned.

 

Basically, I want their town to run while they're offline. But was trying to avoid using a cronjob.

 

Anyone have any thoughts on how this could be done?

Link to comment
Share on other sites

But was trying to avoid using a cronjob.

 

The only way to do it without cron is to either

a) Code your own server app that works like cron and will run the code periodically.  -or-

b) run the code on any page load, regardless of who did it.  That way even if that player is gone, so long as someone is hitting your site it wil run.

 

Really though, the correct way to do this would be a cron job.  Why are you trying to avoid it?

 

Link to comment
Share on other sites

The only way to do it without cron is to either

a) Code your own server app that works like cron and will run the code periodically.  -or-

b) run the code on any page load, regardless of who did it.  That way even if that player is gone, so long as someone is hitting your site it wil run.

 

Really though, the correct way to do this would be a cron job.  Why are you trying to avoid it?

 

Developing on a Windows machine and thought there may be another way. But have talked to a couple of people and have decided to resurrect my LAMP box as it seems I should really be doing it the right way and not cutting corners.

 

Option b could work, until the point that 100 users are online and refreshing pages 500 times a minute.. Would be better off running it once a minute as a cron job.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.