Search code examples
androidmultiplayer

Room Configuration did not work properly in google real time multiplayer services for android


i am trying to build a real time multi-player game and now i am exploring sample game provided by google for multi-player. Link is ...

https://github.com/playgameservices/android-samples/tree/master/ButtonClicker

issue is that when i change auto-match criteria configuration as per my requirement

void startQuickGame() {
final int MIN_OPPONENTS = 1, MAX_OPPONENTS = 3;
   Bundle autoMatchCriteria = RoomConfig.createAutoMatchCriteria(MIN_OPPONENTS,
           MAX_OPPONENTS, 0);
   RoomConfig.Builder rtmConfigBuilder = RoomConfig.builder(this);
   rtmConfigBuilder.setMessageReceivedListener(this);
   rtmConfigBuilder.setAutoMatchCriteria(autoMatchCriteria);
   rtmConfigBuilder.setRoomStatusUpdateListener(this);

   getGamesClient().createRoom(rtmConfigBuilder.build());
}

then this code not wait for 3rd or 4rh player in room (as i set in MAX_OPPONENTS ) and game starts immediately with 2 player(1 opponent). i want to add timer here and game starts after that specified time.

and surprisingly after room creation MIN_PLAYER value dose'nt work at all in this code that is for default room UI.

   final int MIN_PLAYERS = 2;
   Intent i = getGamesClient().getRealTimeWaitingRoomIntent(room, MIN_PLAYERS);

   // show waiting room UI
   startActivityForResult(i, RC_WAITING_ROOM);

my requirement is that after room creation i want to wait for a specific time and then game starts with joined player. no matter they are 2 , 3 or 4.


Solution

  • The automatching algorithm doesn't try very hard to maximize the number of players in the match -- if you set min to 1 and max to 3, getting a 2 player game is possible.

    What you should do in this case is set MIN_OPPONENTS and MAX_OPPONENTS to 3, and handle the game start logic from your code.

    If you want to implement this timer logic, you can't use our built-in waiting room UI, because it doesn't support starting a game early based on time. So, instead, implement your own waiting room with this logic:

    1. Start your timer.

    2. Show the progress on screen as you see peers being connected or disconnected via onPeerConnected() and onPeerDisconnected(), and other callbacks. See RoomStatusListener and RoomStatusUpdateListener.

    3. When the timer expires, decide what you're going to do. For example, if there are 2+ players connected, start the game. If not, wait longer, give up, etc.

    4. Handle the case when someone joins the game late. This will happen if 2 players joined, the timer expired, the game started and then a 3rd or 4th player shows up. Make sure the experience is not terrible for them (don't just kick them out) :-) If you can't integrate them into the ongoing game, you could have them stay in "spectator mode" and join in the next round/match/etc.

    Important: remember that the timers for different players won't be in sync in the logic above -- so you should send a real time reliable message to all peers when you decide to start the game, to let them know that the game is starting and that they should move to the game screen.

    If the logic gets more complicated, it might make sense to elect a "server". You can do that, for example, by saying that the client with the lowest Participant ID (lexicographically) is the server. Saying "whoever created the game is the server" is no good, because when automatching, everyone thinks they created the game.

    I hope this helped!