How do Real Time Strategy games work in PHP?

conradkleinespel picture conradkleinespel · Feb 27, 2012 · Viewed 9.4k times · Source

Some MMO Real Time Strategy games such as Travian or oGame are coded in PHP.

Could you briefly explain how such a game works behind the scenes ? How does the game make real time DB updates without player requests ?

Also, what kind of server load / bandwidth would one have to expect when running a RTS game such as Travian with 1000 active players ?

Answer

njs picture njs · Jan 8, 2013

Even though this topic is rather old, I do think I still have a 'better' (if I may say so myself) answer to your question then the vague "the updates are done by cronjobs" answer.

Travian i.e. gives you the illusion of it being real-time through the use of javascript. What actually happens in the back is the following:

Player A sends an attack to player B. In the MySQL database this is recorded with a timestamp of arrival. Every time player A changes or refreshes a page a script gets launched (by using includes) that checks for any activity in regards to this player (reinforcements arriving, attacks arriving at targets etc.). The script obviously checks the current time and looks at all activities with a timestamp that is less than the current one. This means the action should have taken place. Right at that moment the action actually gets processed.

This also means that if neither player A nor player B ever log in again that the attack will never be calculated, unless someone else also attacks player B - then all activities for player B and the attacking player will be processed.