I'm currently developing a game using Google Play Multiplayer Services. Everything works fine, e.g. login to Google+, leaderboards and achievements.
Now I try to add a multiplayer function (via the Google Play Games network). My code does not throw any exceptions, it just does not work.
The only logcat
output from my app is "created" (private void startQuickGame () {
).
public class GameExplorerQuickGame extends Activity implements RoomUpdateListener, RoomStatusUpdateListener {
private GoogleApiClient client = RXTXChooser.mGoogleApiClient;
private Room room;
final static int RC_WAITING_ROOM = 10002;
@Override
public void onCreate (Bundle b) {
super.onCreate(b);
if (!this.client.isConnected()) this.client.connect();
startQuickGame();
}
private void startQuickGame () {
Bundle am = RoomConfig.createAutoMatchCriteria(1, 1, 0);
RoomConfig.Builder roomConfigBuilder = makeBasicRoomConfigBuilder();
roomConfigBuilder.setAutoMatchCriteria(am);
RoomConfig roomConfig = roomConfigBuilder.build();
Games.RealTimeMultiplayer.create(this.client, roomConfig);
System.out.println("created"); // printed into logcat
}
// create a RoomConfigBuilder that's appropriate for your implementation
private RoomConfig.Builder makeBasicRoomConfigBuilder () {
return RoomConfig.builder(this).setMessageReceivedListener(this.googleRxTx).setRoomStatusUpdateListener(this);
}
@Override
public void onRoomConnected (int statusCode, Room room) {
this.room = room;
System.out.println("onRoomConnected");
if (statusCode != GamesStatusCodes.STATUS_OK) {
} else {
//starting Game Activity...
}
}
@Override
public void onJoinedRoom (int statusCode, Room room) {
this.room = room;
System.out.println("onJoinedRoom");
if (statusCode != GamesStatusCodes.STATUS_OK) {
return;
}
Intent i = Games.RealTimeMultiplayer.getWaitingRoomIntent(this.client, room, Integer.MAX_VALUE);
startActivityForResult(i, RC_WAITING_ROOM);
}
@Override
public void onRoomCreated (int statusCode, Room room) {
this.room = room;
System.out.println("onRoomCreated()");
if (statusCode != GamesStatusCodes.STATUS_OK) {
return;
}
// get waiting room intent
Intent i = Games.RealTimeMultiplayer.getWaitingRoomIntent(this.client, room, Integer.MAX_VALUE);
startActivityForResult(i, RC_WAITING_ROOM);
}
@Override
public void onActivityResult (int request, int response, Intent intent) {
if (request == RC_WAITING_ROOM) {
if (response == Activity.RESULT_OK) {
//starting Game Activity...
} else if (response == Activity.RESULT_CANCELED) {
Games.RealTimeMultiplayer.leave(this.client, null, this.room.getRoomId());
} else if (response == GamesActivityResultCodes.RESULT_LEFT_ROOM) {
Games.RealTimeMultiplayer.leave(this.client, null, this.room.getRoomId());
}
}
}
@Override
public void onConnectedToRoom (Room arg0) {
System.out.println("onConnectedToRoom");
this.room = arg0;
}
@Override
public void onDisconnectedFromRoom (Room room) {
this.room = room;
System.out.println("onDisconnectedFromRoom");
Games.RealTimeMultiplayer.leave(this.client, null, room.getRoomId());
}
// are we already playing?
boolean mPlaying = false;
final static int MIN_PLAYERS = 2;
// returns whether there are enough players to start the game
boolean shouldStartGame (Room room) {
int connectedPlayers = 0;
for (Participant p: room.getParticipants()) {
if (p.isConnectedToRoom()) ++connectedPlayers;
}
return connectedPlayers >= MIN_PLAYERS;
}
// Returns whether the room is in a state where the game should be canceled.
boolean shouldCancelGame (Room room) {
for (Participant p: room.getParticipants()) {
// if (!p.isConnectedToRoom()) return true;
}
return false;
}
@Override
public void onPeersConnected (Room room, List<String> peers) {
System.out.println("onPeersConnected. List.size= " + peers.size());
if (this.mPlaying) {
} else if (shouldStartGame(room)) {
}
}
@Override
public void onPeersDisconnected (Room room, List<String> peers) {
this.room = room;
System.out.println("onPeersDisconnected");
if (this.mPlaying) {
} else if (shouldCancelGame(room)) {
Games.RealTimeMultiplayer.leave(this.client, null, this.room.getRoomId());
}
}
@Override
public void onPeerLeft (Room room, List<String> peers) {
this.room = room;
System.out.println("onPeerLeft");
if (!this.mPlaying && shouldCancelGame(room)) {
Games.RealTimeMultiplayer.leave(this.client, null, this.room.getRoomId());
}
}
@Override
public void onPeerDeclined (Room room, List<String> peers) {
this.room = room;
if (!this.mPlaying && shouldCancelGame(room)) {
Games.RealTimeMultiplayer.leave(this.client, null, this.room.getRoomId());
}
}
@Override
public void onP2PConnected (String arg0) {
System.out.println("onP2PConnected");
}
@Override
public void onP2PDisconnected (String arg0) {
System.out.println("onP2PDisconnected");
}
@Override
public void onPeerInvitedToRoom (Room arg0, List<String> arg1) {
System.out.println("onPeerInvitedRoom");
this.room = arg0;
}
@Override
public void onPeerJoined (Room arg0, List<String> arg1) {
System.out.println("onPeerJoined");
this.room = arg0;
}
@Override
public void onRoomAutoMatching (Room arg0) {
System.out.println("onRoomAutoMatching");
this.room = arg0;
}
@Override
public void onRoomConnecting (Room arg0) {
System.out.println("onRoomCeonnecting");
this.room = arg0;
}
@Override
public void onLeftRoom (int arg0, String arg1) {
System.out.println("onLeftRoom");
}
}