I’m building a multiplayer game in PHP. Each game is split into stages that last a set amount of time – currently 2 minutes. In each stage, players work together for a set amount of time until the game either advances to the next stage, or the stage (and hence the game) is lost, and a new game starts again.
I have a vision of playing the game on an HTML page, where users can see how much time is left in the current stage through a constantly updating countdown clock. When the countdown clock reaches zero, the game either advances or ends, and the result is returned through Ajax. What I’m having problems with is the (theoretical) thought of 100 players playing the game simultaneously.
How do I call the update script? If I call it by running PHP, which player’s browser calls it? If a player’s browser refreshes the content one second before the stage actually finishes, what happens?
Is PHP the right language for a game like this?
“Is PHP the right language for a game like this?”
Yes, if you do it right. Here’s how you do it…
Using a method called Comet, you can have multiple clients pulling data from the server, but only when that data is updated. It works something like this:
In other words, it means that all clients receive up-to-date information immediately it is pushed to the server by any client. This means you don’t have to implement a “refresh every x seconds” system, which is good for 2 reasons:
1) You don;t waste time and bandwidth make requests to the server when nothing has happened;
2) All the clients get data pushed to them at the same time, that’s absolutely up-to-date and not x seconds old.
In practice, Comet is implemented via AJAX. Google for some examples of Comet- it’s pretty simple, and extremely useful.