1
\$\begingroup\$

I'm trying to experiment in creating an MMO browser-based game using PHP, MySQL, Javascript, HTML5, and CSS.

There is this certain functionality that I want in my game that I'm having trouble with.

In detail, here's what happens:

  1. The user clicks an action

  2. Then a time limit will commence

  3. After the time limit, then a script will run and the changes will take place.

I've heard of cron jobs but these changes are not in a set time. It depends on when a player makes a move or something.

Here are the possible ways I can think of carrying this out:

  1. You save the time when the user makes a move, and when the user checks back again, there will be a script that will check if the time the user made a move and compare it with the current time. The problem with this is that, I wan't the changes to take place even when the player is offline. With this method, the changes will only happen after the player checks back again.

  2. A cron job will do the above action of checking the time the user made a move and compare it with the current time. Probably every 30 minutes or every hour. The problem with this is, won't this be too much for the server? And still, it will be every 30 minutes or so. Will this be possible too if done in seconds?

  3. A combination of the two. A cron job will check every hour and it will also check every time a user logged back in.

Of course these are the solutions I can think of using only cron jobs. I don't know if there is any better technology out there. I do hope that there is a better way like actually running a script after a certain amount of time elapse after an action even when offline. If there is, tell me. I'm talking server-side here ok.

Yeah, anyone can help me out with this?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

The cron-job way is definitely the easiest one and it's good enough for normal use cases.

If the changes the users makes affects the other users (for example, make a fire, which, if let unattended it would burn other player's houses in the meantime), then you can't use the first idea. Otherwise having some sort of "TODO" lists that get completed (finish the tasks) when he relogs is fine.

You ask about 'too much for the server'. You don't have to worry about. If you keep the cron jobs constant and frequent enough, you'll have a better performance. That is, doing small things over a long period is better than cramming many into fewer time. The overhead of invoking the cron job, compared to the actual work done might be negligible.

However, here are two more ways that come to my mind right now:

  1. Windows Azure. There is a PHP SDK (https://github.com/windowsazure/azure-sdk-for-php). To be more specific you'd need to create an Azure Worker Role which is deployed there. Then, from PHP you'd post some tasks to be done in a Queue container. The Worker role polls for remaining tasks and executes them.
  2. Node.js (http://nodejs.org/). You can create a server that, just like the Windows Azure Worker role, awaits for tasks from the PHP scripts. Then, you'd just have to move the logic of time-based work in those tasks. You can look for more info in here: http://dev.iron.io/worker/languages/nodejs/
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .