3

Here is my problem, for those of you who looked at the title and thought "PHP does not wait on user input because it is a server side language and therefore your problem is a client-side one," just hear me out.

I'm making a game. It is mulitplayer game, therefore multiple users. At the start of every game round the users involved in the game are prompted with a chose of things they want to do that round. Course, the round isn't to commence until everyone has made a selection on what they want to do that round.

See, that is my problem. How do I make a script wait for 'all' users to send a request (send input) before continuing execution? The server-side language is PHP. The answer wouldn't be with the client as the client is only responsible for one user and wouldn't know what the other users are doing.

Thanks.

Mikodite Yvette
  • 171
  • 1
  • 1
  • 4
  • so store 2 vars: how much is online and how much already answered. while this numbers mismatch - dont run other actions. – StasGrin Sep 11 '12 at 23:00
  • Sounds like either a `CRON` scheduled script running periodically and marshaling the process, or a long-running background script using `sleep()` or something to periodically poll and push. Keep in mind, though, you probably still need the client to long poll (or, one day, create a socket stream). – Jared Farrish Sep 11 '12 at 23:01
  • You have to use sessions, a database or some flat file to store the current state. – bfavaretto Sep 11 '12 at 23:01
  • This is similar to the site chess.com where the game starts when users join for a tournament and the tournament begins at a particular time – FirmView Sep 11 '12 at 23:04
  • A persistence layer like http://redis.io/ could probably come in handy, if you need [concurrency](http://stackoverflow.com/questions/1938671/concurrency-in-hibernate). – Jared Farrish Sep 11 '12 at 23:05

2 Answers2

3

Fundamentally, you have two choices:

Choice one, you have each script check if it has all the data it needs, and then do all the work to calculate the next move (or whatever). That is actually much harder to get right than it sounds, because you run into problems of concurrency.

Basically, that approach leads to more than one "process" - page load - trying to do the same work on the same data, and that opens the door to races where you either don't do the work at all, or where you do it twice.

Choice two, which sounds harder, is where you write another PHP script that checks to see if it has all the moves, calculates the outcome, and updates the database (or whatever) in the background.

Then, run that off a cron job, or something like that, so you only have one instance running at a time. That makes life easier: your "is everything done" script is only running once, and so you don't have to worry about races - but there might be a lag between the last move being submitted and calculating the outcome.

That approach is actually easier in the long run, because while it involves more code and more moving parts, it actually avoids the hard problems (concurrency) in return for a few more easy problems (a bit more code, using cron).

You can improve on both of those, of course, but those are the fundamental models. Locking and other coordination techniques can make "calculate in the last page" work better, but they involve you addressing the races.

Using various "background job" tools can improve the latency of the second approach, by letting you trigger the check instantly rather than just on a timer. You still have some latency, but the user doesn't see as much of it.

Really, though, you get to pick one of those two strategies and go with it.

(Also, I strongly advise that if you can, grab a framework or something where someone else already solved these problems, then use that.)

Daniel Pittman
  • 16,733
  • 4
  • 41
  • 34
  • I'm really interested in the idea of Pagoda Box's [Background Workers](http://help.pagodabox.com/customer/portal/articles/430779). – Jared Farrish Sep 11 '12 at 23:08
0

Since you can't push data out, I'd tackle this as follows:

  1. collect the submitted information on the server in eg DB
  2. run a client-side script to check periodically if all required information is available
  3. on the client evaluate the response: if true, start game - if not "wait" = keep checking

so you need:

  • script on the server that collects the info
  • server script that checks if all info is available
  • client script that checks periodically and evaluates
Rid Iculous
  • 3,696
  • 3
  • 23
  • 28