How to match users to room perfectly

I am using following logic to make a user quickly join to a game.

1. Try "joinRandomRoom."
2. If it fails create a room and wait for other.


This will work perfectly, however fails in case both user started the game simultaneously. In that case both user will be waiting for other users. What is the perfect way to avoid this race condition.

I tried various solutions.
1.Create room name based on server time: Simultaneous room creation fails and failed user can retry joining.
2.Retry after joiningroom again after fixed amount of waiting.

None of the solution works perfectly in all the scenarios. Any suggestion?

-Shakthi

Comments

  • There is no way to avoid this racing condition if the players are joining a random game. This simply can happen but it should be really rare.
    If your game has few players, then this is a problem. You can access PhotonNetworking.countOfPlayersOnMaster in your client to give your user a hint that there are few players and that maybe they need to re-do random matchmaking to find an opponent.
    If there are many players, then the next players will join both games until full. Then it's not an issue at all.

    If you want 2 players to get into the same room, they both need to know the same room name. That name should be as unique as possible (like playerA+playerB+randomId). Then they can both use PhotonNetwork.JoinOrCreateRoom() which is new in PUN v1.25. That makes sure they get in the same room, no matter who is the first to create it.