Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deciding who is player one and two in a round based game with Google Play Game Services

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?

like image 664
Ulrich Scheller Avatar asked Jun 16 '13 16:06

Ulrich Scheller


1 Answers

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 );
like image 189
PEK Avatar answered Sep 20 '22 14:09

PEK