views:

102

answers:

3

So, I want to create car racing for my RPG game's players. Player can create race and choose how many participants can participate in race. After race is being created, other people can join it. When the maximum participants are collected, race begins.

My idea, when the last participant joins, then instantly choose the winner (who's car is the best, that person wins), but how can I do it? If I choose to pick the winner after the last participant joins, then I have to put many queries in one page (select data from table, then delete the race, then select players' cars' statistics and pick the winner and then again, using mysql, send message to everyone).

But this idea is really not optimal and it will lag cruelly for that last person. Maybe you have any ideas how I can avoid lag and make it more optimal. Thank you very much.

A: 

If you want the race to be processed when the last person joins it, you probably want to use asynchronous processing so that last person doesn't need to wait.

There's a question on asynchronous processing with PHP here and another one here

The basic idea is that the page that the last contestant executes returns quickly (probably just with a message to let them know that the race is under way) but the processing continues on the server.

DanK
+2  A: 

But this idea is really not optimal and it will lag cruelly for that last person.

What makes you think that? "Get the race, get the participants' character stats, get the participants' car stats, examine stats to pick a winner" is not a heavy processing load. Unless you're running your server on truly ancient hardware and sharing it with 100 other applications, it should take the user's browser longer to establish a network connection to your server than it does for you to determine the race results.

If you've actually tested this design and seen a significant delay for the final player over and above the delay for other page loads in your game, then you do have a problem and optimization is needed - but the problem is almost certainly with the algorithm you're using to calculate the winner, not with the use of multiple queries to generate a single page.

Always remember The Rules of Optimization Club.

Dave Sherohman
I haven't tried it yet, but I just suppose. Maybe I should try it.
hey
A: 

Complicated response - how i imagine this game working.

  1. user clicks 'create race' button and they are taken to the game lobby.
  2. another user sees a list of races currently available, clicks 'join race'
  3. each user, other then the creator, should click a 'ready' checkbox. This could be done using ajax. You could also use this background request to calculate that user's stats and update the database.
  4. each user will also have an ajax request every second, or less, to see who else is ready, if the game has started, etc.
  5. game creator sees that everyone is ready and clicks 'Race!' (all ajax, you could have a little race car give that slowly moves across the screen to show it's all processing) the process request that fires off all the complex calculations should be done here while the users are mesmerized by that little race car ( or simply text that says 'Racing...'). update the database, perhaps a table with the results.
  6. at this point the race is over, the creator gets the ajax reply back from the server. Each of the other users, who' browsers have been checking with the server every second or so now get a reply back from the server with the results.

I hope this all makes sense. If your not already using a JavaScript library i recommend you do as they make cross-browser ajax so simple.

Samuel