I have a round-based Multiplayer game for Android that has been working over XMPP before, and I want to switch to Google Play Game Services. In the old version, there was an XMPP bot, deciding which player will be player 1 or 2. This is important to know which player should make the first move.
With Google Play Game Services, I found a solution that nearly works:
@Override
public void onRoomConnected(int statusCode, Room room) {
ArrayList<Participant> participants = room.getParticipants();
Participant first = participants.get(0);
if (first.getPlayer() == null || !first.getPlayer().getPlayerId().equals(myPlayerId)) {
LaskaField.HUMAN_PLAYER = 2;
LaskaField.OTHER_PLAYER = 1;
opponent = first.getDisplayName();
} else {
LaskaField.HUMAN_PLAYER = 1;
LaskaField.OTHER_PLAYER = 2;
opponent = participants.get(1).getDisplayName();
}
setPlayerNames();
}
This way works fine when inviting another player. However, it often fails when both players select auto-matching. In this case, both players are on the same position in the participants ArrayList. Therefore, they will both appear as the same player on their currently used device.
What is the right way to select player 1 and 2, with no central instance for deciding this. Is there any useful data in the participants list that I didn't find with the debugger?
I have solved the problem by comparing the player ids (which are random for each game):
String myid = mActiveRoom.getParticipantId(client.getCurrentPlayerId());
String remoteId = null;
ArrayList<String> ids = mActiveRoom.getParticipantIds();
for(int i=0; i<ids.size(); i++)
{
String test = ids.get(i);
if( !test.equals(myid) )
{
remoteId = test;
break;
}
}
boolean iMakeTheFirstMove = ( myid.compareTo(remoteId) > 0 );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With