views:

39

answers:

1

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 turn

  • Request will be sent to the server to perform necessary operations.
  • 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, not the one that initiated the request.

I know that this could be implemented using some kind of periodic AJAX call that checks 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 somehow? Or is there a better way to do this?

Any help would be appreciated!

Thanks in advance!

+1  A: 

An in-memory database of currently running games/turns, and one check from user B every 1.5 secs or so, won't really generate a huge number of requests, or server load.

You can even have a polling scheme like 7s, 5s, 3s, 2s, 1s, 1s and so on, according to what fits your game.

You can even leave PHP out entirely, if you just touch a session file whenever a turn is done, and check the last-modified client side.

gnud