l0gic Posted February 25, 2012 Share Posted February 25, 2012 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? Quote Link to comment Share on other sites More sharing options...
kicken Posted February 25, 2012 Share Posted February 25, 2012 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? Quote Link to comment Share on other sites More sharing options...
l0gic Posted February 26, 2012 Author Share Posted February 26, 2012 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. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 26, 2012 Share Posted February 26, 2012 You can use a scheduled task on Windows to accomplish the same thing (execute a script at scheduled times/intervals.) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.