Sachith Senanayaka Sachith Senanayaka - 1 year ago 94
MySQL Question

Algorithm to select an user from a database (For a online dual player game)

If we suppose a online dual player game.
There is a table in a database (Users table) with columns id, name, level, ...

When a user click a button it has to search for another user(who also clicked the button to search) with similar level and connect those users and start the game.

What I want is the algorithm to connect two players who are searching for a one when clicked a button.

It is easy if any can give a php code.

Answer Source

Let me give you an idea. Share your thought if you find anything not fitting your request.

Player's table:
**id   name level status**
1      1    1     idle
2      2    3     playing
3      3    1     idle
4      4    3     playing
5      5    1     idle

step 1: only idle player can search for player. say player:1 pressed search button. then player:1's status will be searching. but it won't find anything as no body else is in search state and of the same state (searching).

send asynchronous request from the client that will periodically look for player via a php get/post request.

repeat this for any player who pressed search.

step 2: the asynchronous(eg. javascript ajax) request will request to php to look into database to match the criteria. when found, server(php end) will change state of both player 1 and 5 to playing and write a row in game table. and return game id as response to the javascript request.

game table:
**id playerOneId playerTwoId pLayerOneStatus  pLayerTwoStatus status**    
1    1            5          ready            ready          started  

step-3: the moment asynchronous request get game id as response it will redirect the player to game page. and set the player status to ready.

step-4: the moment both player are in ready state. the game will be started.

step-5: once the game end change player's table status to idle. change game status to end

Let me know if it serves your question.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download