How to decide who is the first player when a user plays fast games?

I use the following code in onRoomConnected(int statusCode, Room room) to determine who the first player is. But several times I get the first / second for both players. How to fix this error.

  if (quickGame) { myTurn = room.getParticipants().get(0).getParticipantId().equals(myId); } else { myTurn = room.getCreatorId().equals(myId); } if (myTurn) { Log.e(TAG, "First Player"); status.setText("Click a button to start"); } else { Log.e(TAG, "Second Player"); status.setText("Wait for opponent to start"); } 
+7
google-play-games
source share
4 answers

The set of participant identifiers is guaranteed to be the same for everyone who is in the room (but not in different matches). However, their order in the list is not guaranteed. Therefore, if you want to make easy choices (for example, to establish who goes first, etc.), you must rely on a set of participant identifiers, but not in order. Some of the ways you can do this are:

  • The identifier of the first player to enter in alphabetical order is the first player to play
  • The identifier of the participant, who comes in alphabetical order first, is responsible for randomly selecting a player to begin with. The rest of the clients will wait until the participants send a reliable message in real time containing the identifier of the selected participant.

Method (2) is preferred since it does not contain a possible bias.

Why? Although we do not indicate what the structure of the participant identifier is (it is just a string), the truth is that it encodes the information, so if you use the participant identifier, as a rule, you may encounter a strange distribution of who comes first. For example, you may find that a particular player always comes first, but this is because, coincidentally, their member ID is generated in such a way that this always happens. Therefore, it is definitely better to use the participant ID to choose who is the authority for the random decision, who comes first, and not who actually comes first.

+9
source share

One way to approach this is to track participants ids, as they are generated based on each room. This is how I do it in my Android code.

 @Override public ArrayList<String> getActiveRoomPlayerIDs() { if(mRoomCurrent != null) { ArrayList<String> newList = new ArrayList<String>(); for (Participant p : mRoomCurrent.getParticipants()) { dLog(listIgnoreTheseIDs.toString() + " is the list to ignore"); if(mRoomCurrent.getParticipantStatus(p.getParticipantId()) == Participant.STATUS_LEFT) { dLog(p.getParticipantId() + " left the room"); } else { newList.add(p.getParticipantId()); } } return newList; } return null; } 

The reason that I am so fit is because if the room members change during the game, I can use the same approach to deal with leaving the room.

onRoomConnected is called for ALL opponents with the NAME of participants, there is no number how many in this call

Added here for editing .. in my side libGDX then I do it

 private ArrayList<String> SortThisList(ArrayList<String> currentRoomIds) { Collections.sort(currentRoomIds); return currentRoomIds; } 

Then I use a sorted list to determine the order of the players ...

+1
source share

If there are no other criteria that matter, you can use my technique. Simply select the alphabetically smallest member ID as the server after completing the room formation.

 /**checks is this user is the alphabetically smallest participant id. * if so then the user is server. * @return if this user should be the server. */ private boolean isServer() { for(Participant p : mParticipants ) { if(p.getParticipantId().compareTo(mMyId)<0) return false; } return true; } 
+1
source share

I would suggest the following method:

When device A receives a response from Google that the room is connected, check to see if any other participants are present. If none of them are present, designate device A as player 1.

When device B receives a response from Google, it discovers that there is a different participant than it. In this case, wait.

In device A, you will receive a notification that the participant has connected, start the game and send a corresponding message to device B.

-one
source share

All Articles