iGuest

Turn based web game in PHP - session handling issue I Want To Create A PHP Text Based Web Game

Recommended Posts

Turn based web game in PHP - session handling issueI Want To Create A PHP Text Based Web Game

Hi all!I am working on a turn based web game in PHP5.This is pretty simple game, a kind of board game, two people join a "session" and they play until one of them wins.My problem in nutshell:User A and User B play a game.User A finished his turnA request will be sent to the server to perform necessary operations. So far so good..Now it is time for User B to move..But how could I notify User B about this?I mean, now the server has to communicate with the other user, the one that is inactive, and not the one who initiated the request

I know that this could be implemented using some kind of periodic AJAX call to check whether the opponent finished his turn etc, but such a thing generates a huge number of requests.Isn't there a better way to solve this? I'm thinking of something like this:User A's turn ends ----> Server saves his score ----> Server contacts User B ----> User B's turn gets started.Is this possible using PHP and comet-style requests? Or is there a better way?

Any help would be appreciated!Thanks in advance!-question by Tommy

Share this post


Link to post
Share on other sites

You could achieve this with periodic AJAX checks for completion of User A's move as you said.

You could keep the period of checks to around 4 to 5 seconds. This would not create very high resource usage. Also if you are trying to target large amount of users playing the game simultaneously then you should get premium paid hosting that could handle such amount of resource usage. (Like hostgator.com)

Unfortunately there is no way that the Server would contact User B. Always a user can query a server and server only returns the response. The Server cannot send a response without any query from the user's side.

One more way you could do is that you could keep a refresh button on User B's side and whenever he/she presses this button you can do the AJAX check for completion of User A's turn. But practically this would not be good enough and users would not like to keep on pressing the refresh button.

So basically the ebst way is to keep a check with periodic AJAX requests to the Server from a user to check the completion of his opponent's turn and keeping the time interval between such periodic requests to be a comfortable time of 4 to 5 seconds.
This would only slow down the update rate to a negligible amount and would only keep a fair amount of resource usage on the Server.

I hope it helps.

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