I think this approach will be fine. The database is really good at getting 
you a single result quickly if that's all you ask for (e.g. 
PlayerQueue.where.not(player_id: 
current_player_id).order(:created_at).first). Some psuedocode:

if the first player in the queue is still looking for a match
  match current player with first player
else if the first player in the queue is disconnected
  delete first record in queue
  start over
else
  insert the current player into the queue
end

Obviously, this will need to be restructured when translated to code, but I 
think you get the idea. There is a race condition here that only matters 
are very small scale. You could also implement a worker that cleans up the 
table when a client is no longer connected. This table should really be 
quite small, but with high turnover, and the database will do fine job of 
giving you a record quickly from it when using ORDER and LIMIT.

By the way, I would probably opt for "find me the *oldest* unmatched player 
that is still looking for a match" rather than the most recent one. 
Searching for the most recent one treats the players that have been looking 
the longest as the least likely to receive a match.

On Sunday, September 14, 2014 1:35:53 AM UTC-4, Ruby-Forum.com User wrote:
>
> I'm using Ruby on Rails to build a real-time game web app. When a player 
> taps "Play Now", I want to match them up with another player that is 
> looking for a game (match-making). With my basic understanding of RoR, 
> one immediate way I think I can do this is create a DB table that 
> contains the players that are looking for a game. When a new player 
> wants to play, I can just query the DB for the two most recent players 
> and connect them together, and remove them from the DB. But this doesn't 
> sound optimal since the list of players can potentially be large and the 
> query could be slow. 
>
> Are there more efficient ways to implement this kind of match-making in 
> RoR? Or a more efficient way to implement a persistent queue? 
>
> Other ideas? 
>
> Thanks in advance for your wisdom! 
>
> -- 
> Posted via http://www.ruby-forum.com/. 
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/7e6c4c48-e040-4552-8835-1ed51c722497%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to