# How to simulate timed attacks in browser-based games?

In my MMORTS browser-based game, I want that all attacks be performed 7 minutes later after the player does it.

So, when the player clicks on attack button, a countdown becomes visible to both the attacker and the defender.

Showing countdown and handling its events isn't my question. In fact, my Q is how can I implement this countdown in server side and database.

I haven't any idea about managing this situation. Any helps is highly appreciated...

That's actually not that much game related, as general programming related.

It all depends on your language and environment, but the theory relies in http://en.wikipedia.org/wiki/Background_process and http://en.wikipedia.org/wiki/Asynchronous_I/O.

There are multiple ways, some of them:

• Easiest, and most friendly, easy to set up would be AJAX - you'd simply start a timeout, once it expires, call the AJAX, and on successful response act accordingly. (Though, it won't work if you need the attack to be performed even if the user is not online.)
• http://en.wikipedia.org/wiki/Cron (it's for *NIX), Scheduled Tasks probably is the alternative for Windows.
• Message queues (probably, the best way), used as task/job schedulers:

• You could also (not the best way at all) upon request, start a background process that'd be given the task execution time, and task itself. The process would run in a continuous loop, once it's current time matches or exceeds the given time, call the task and kill the process.

• There is also the option to simply queue the actions by storing them inside database. Now, if the player quits, and he or anyone related to the action joins again, upon loading the game, you'd look if the entity has some actions that have happened. If so, you simulate them all at once, affecting any related entities too, then giving the player the simulated state from where he'd continue.

If you could provide the environment / language you are working with, the answer could be narrowed down and tailored more for your specific problem.

• Than ks for your thorough response. In fact I'm hesitant between asp.net mvc and node.js to develop my game. However it seems that i'll be forced to move through node.js... – Amin Saqi Aug 20 '13 at 6:02
• @AminSaghi Why? Use SignalR with ASP.NET and store the 7 minute timer in the SignalR memory session, and after the 7 minutes store it in the DB. – JClaspill Aug 21 '13 at 17:13