Sign in to follow this  
moothecow2k

[web] PHP MMO Time-Passing

Recommended Posts

I plan on making a PHP MMO. I've got a fair bit of experience. One thing baffles me though: time refreshing. Take a game like Utopia. Every x minutes/hours (typically every 24 hours), time "moves forward". Things get built. Resources incremented. Research advances. Armies battle. How on earth do you code THAT part? I suspect it has something to do with timed actions ("chron jobs" or "scheduled tasks"), but I'm not sure. Any insight would be appreciated.

Share this post


Link to post
Share on other sites
You'll need some kind of database on your server to keep track of all the objects in the world. When the page refreshes you find out how long ago the building was first put in to construction, after a certain amount of time has passed you show a different stage of construction and eventually you show a finished building.

In a nutshell thats how that would all work out. Keep track of everything and have objects show with different images as time goes by.

Share this post


Link to post
Share on other sites
In other words, it's refresh-on-demand? As in, when you log in, it updates your stuff?

Assume: Utopia-style game.

Hmm. But what if you don't log in for, say, 48 hours, and in the meantime, you're attacked 3 times? The server has to update your info after every attack, so the players can gauge you properly in subsequent attacks.

That either implies instantenous attack results, or a global "time will move forward at midnight GMT" style, which I assumed from the start.

Share this post


Link to post
Share on other sites
When a friend of mine made his own Utopia-clone, he simply added a scheduled task that opened a web browser to his special "refresh" page every 24 hours. At least it's a simple solution.

Share this post


Link to post
Share on other sites
That's pretty cool--I was thinking batch files/scripting, but this would be FAR more elegant.

The interesting thing about the on-demand approach is that you don't waste resources updating accounts for people who never log in, and it spreads out the CPU usage over time. Fascinating. But it doesn't really allow for multiple interactions.

Share this post


Link to post
Share on other sites
You're looking for a cron job (*nix) or a scheduled task (Windows). Basically it executes your script every X hours, minutes, days, whatever. In this case, your script would subtract time from remaining build time, move armies across lands, etc. etc.

Share this post


Link to post
Share on other sites
Still go with a refresh on demand, this goes even for attacking. If someone is set to attack someone else that has not been on in a while, you refresh that person then, and continue on with the attack. No scheduled tasks needed for this.

Share this post


Link to post
Share on other sites
I personally use chron jobs. The problem I ran across by doing an auto update (ie, on demand) was that I ended up doing it more often than if I just updated everyones account on the hour. So... be sure to look at all the advantages/disadvantages before you put a whole lot of effort into coding.

Share this post


Link to post
Share on other sites
I guess it depends entirely on the dynamics of the game. I should probably compute the worst-case number of refreshes per day and see if it exceeds the auto one-hour refresh style...

On demand definitely makes more sense, efficiency-wise.

Share this post


Link to post
Share on other sites
Quote:
Original post by moothecow2k
On demand definitely makes more sense, efficiency-wise.


Not really, actually. Efficiency-minded would be a cron-job scheduled at low-points in traffic. The thing is with on-demand refreshing that you have to be able to refresh very fast, or else the user will experience a noticable longer pageload when the refresh occurs.

Then again, a refresh probabely won't take long at all. My advice: use a cron job if the game demands it (e.g. stuff needs to happen regardless if the user is visting or not) or if it greatly siplifies your code. Use on-demand elsewhere.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this