1

So I have a multiplayer game built in libGDX running perfectly fine on desktop (gets WarpResponseResultCode.SUCCESS) at onConnectDone. On android the result code is CONNECTION_ERROR and I have no idea if it is something to do with using the wrong App42MultiPlayerGaming.jar (I'm using the same on android and desktop) or if there is something else)

WarpControler.java handles callbacks

package appwarp;


import java.util.HashMap;

import com.greenpipestudios.paradox.gamestate.PlayState;
import com.shephertz.app42.gaming.multiplayer.client.WarpClient;
import com.shephertz.app42.gaming.multiplayer.client.command.WarpResponseResultCode;
import com.shephertz.app42.gaming.multiplayer.client.events.RoomEvent;

public class WarpController {

private static WarpController instance;





private boolean showLog = true;

private final String apiKey = "SHHHHHH!!!";
private final String secretKey = "SHHHHHH!!!";

private WarpClient warpClient;

public String localUser;
private String roomId;
public int numUsers;

private boolean isConnected = false;
boolean isUDPEnabled = false;

private WarpListener warpListener ;
public static final int minUsers = 1;
public static final int maxUsers = 3;

private int STATE;
// Game state constants
public static final int WAITING = 1;
public static final int STARTED = 2;
public static final int COMPLETED = 3;
public static final int FINISHED = 4;

// Game completed constants
public static final int GAME_WIN = 5;
public static final int GAME_LOOSE = 6;

//


public WarpController() {
    initAppwarp();
    warpClient.addConnectionRequestListener(new ConnectionListener(this));
    warpClient.addChatRequestListener(new ChatListener(this));
    warpClient.addZoneRequestListener(new ZoneListener(this));
    warpClient.addRoomRequestListener(new RoomListener(this));
    warpClient.addNotificationListener(new NotificationListener(this));
    numUsers = 0;
}


public static WarpController getInstance(){
    if(instance == null){
        instance = new WarpController();
    }
    return instance;
}

public void startApp(String localUser){
    this.localUser = localUser;
    warpClient.connectWithUserName(localUser);

}

public void setListener(WarpListener listener){
    this.warpListener = listener;
}

public void stopApp(){
    if(isConnected){
        warpClient.unsubscribeRoom(roomId);
        warpClient.leaveRoom(roomId);
    }
    warpClient.disconnect();
}

private void initAppwarp(){
    try {
        WarpClient.initialize(apiKey, secretKey);
        WarpClient.enableTrace(true);
        warpClient = WarpClient.getInstance();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void sendGameUpdate(String msg){
    if(isConnected){
        if(isUDPEnabled){
            warpClient.sendUDPUpdatePeers((localUser+"#@"+msg).getBytes());
        }else{
            warpClient.sendUpdatePeers((localUser+"#@"+msg).getBytes());
        }
    }
}

public void updateResult(int code, String msg){
    if(isConnected){
        STATE = COMPLETED;
        HashMap<String, Object> properties = new HashMap<String, Object>();
        properties.put("result", code);
        warpClient.lockProperties(properties);
    }
}

public void onConnectDone(boolean status){
    log("onConnectDone: "+status);
    if(status){
        warpClient.initUDP();
        warpClient.joinRoomInRange(1, maxUsers - 1, false);
    }else{
        isConnected = false;
        System.out.println("onConnectDone error");
        handleError();
    }
}

public void onDisconnectDone(boolean status){

}

public void onRoomCreated(String roomId){
    if(roomId!=null){
        warpClient.joinRoom(roomId);
    }else{
        System.out.println("onRoomCreated error");
        handleError();
    }
}

public void onJoinRoomDone(RoomEvent event){
    log("onJoinRoomDone: "+event.getResult());
    if(event.getResult()==WarpResponseResultCode.SUCCESS){// success case
        this.roomId = event.getData().getId();
        warpClient.subscribeRoom(roomId);
    }else if(event.getResult()==WarpResponseResultCode.RESOURCE_NOT_FOUND){// no such room found
        HashMap<String, Object> data = new HashMap<String, Object>();
        data.put("result", "");
        warpClient.createRoom("superjumper", "shephertz", maxUsers, data);
    }else{
        warpClient.disconnect();
        System.out.println("onJoinRoomDone error");
        handleError();
    }
}

public void onRoomSubscribed(String roomId){
    log("onSubscribeRoomDone: "+roomId);
    if(roomId!=null){
        isConnected = true;
        warpClient.getLiveRoomInfo(roomId);
    }else{
        warpClient.disconnect();
        System.out.println("onRoomSubscribed error");
        handleError();
    }
}

public void onGetLiveRoomInfo(String[] liveUsers){
    log("onGetLiveRoomInfo: "+liveUsers.length);
    numUsers = liveUsers.length;
    PlayState ps  = (PlayState) warpListener;
    ps.lastonein = true;
    if(numUsers >=minUsers ){
        startGame();    
    }else{
        waitForOtherUser();
    }
    warpListener.setPlayerColor(numUsers);
}

public void onUserJoinedRoom(String roomId, String userName){
    /*
     * if room id is same and username is different then start the game
     */

    numUsers++;
    if(!localUser.equals(userName) && numUsers >= minUsers){
        startGame();
    }
    warpListener.onUserJoined(roomId, userName);
}

public void onSendChatDone(boolean status){
    log("onSendChatDone: "+status);
}

public void onGameUpdateReceived(String message){


//      log("onMoveUpdateReceived: message"+ message );
    String userName = message.substring(0, message.indexOf("#@"));
    //String data = message.substring(message.indexOf("#@")+2, message.length());
    if(!localUser.equals(userName) && message != null){
        warpListener.onGameUpdateReceived(message);
    }
}

public void onResultUpdateReceived(String userName, int code){
    if(localUser.equals(userName)==false){
        STATE = FINISHED;
        warpListener.onGameFinished(code, true);
    }else{
        warpListener.onGameFinished(code, false);
    }
}

public void onUserLeftRoom(String roomId, String userName){
    log("onUserLeftRoom "+userName+" in room "+roomId);
    warpListener.onUserLeft(roomId, userName);
    numUsers--;
}

public int getState(){
    return this.STATE;
}

private void log(String message){
    if(showLog){
        System.out.println(message);
    }
}

private void startGame(){
    STATE = STARTED;
    warpListener.onGameStarted("Start the Game");
}

private void waitForOtherUser(){
    STATE = WAITING;
    warpListener.onWaitingStarted("Waiting for other user");
}

private void handleError(){
    if(roomId!=null && roomId.length()>0){
        warpClient.deleteRoom(roomId);
    }
    System.out.println("Kicked for error! Contact the developer or the U.S. President!");
    disconnect();
}

public void handleLeave(){
    if(isConnected){
        warpClient.unsubscribeRoom(roomId);
        warpClient.leaveRoom(roomId);
        if(STATE!=STARTED){
            warpClient.deleteRoom(roomId);
        }
        warpClient.disconnect();
    }
}

private void disconnect(){
    warpClient.removeConnectionRequestListener(new ConnectionListener(this));
    warpClient.removeChatRequestListener(new ChatListener(this));
    warpClient.removeZoneRequestListener(new ZoneListener(this));
    warpClient.removeRoomRequestListener(new RoomListener(this));
    warpClient.removeNotificationListener(new NotificationListener(this));
    warpClient.disconnect();
}
}

Connection Listener.java invokes the callbacks

package appwarp;

import com.shephertz.app42.gaming.multiplayer.client.command.WarpResponseResultCode;
import com.shephertz.app42.gaming.multiplayer.client.events.ConnectEvent;
import com.shephertz.app42.gaming.multiplayer.client.listener.ConnectionRequestListener;

public class ConnectionListener implements ConnectionRequestListener {

WarpController callBack;

public ConnectionListener(WarpController callBack){
    this.callBack = callBack;
}

public void onConnectDone(ConnectEvent e) {
    System.out.println("Connect Code: " + e.getResult());
    if(e.getResult()==WarpResponseResultCode.SUCCESS){

        callBack.onConnectDone(true);
    }else{
        callBack.onConnectDone(false);
    }
}

public void onDisconnectDone(ConnectEvent e) {

}

@Override
public void onInitUDPDone (byte result) {
    if(result==WarpResponseResultCode.SUCCESS){
        callBack.isUDPEnabled = true;
    }
}

}
Steven Landow
  • 153
  • 1
  • 21

2 Answers2

0

Sort of embarrassing but I will leave this up for someone else who needs to figure this out: All I had to do is enable internet permissions in the android manifest xml.

Steven Landow
  • 153
  • 1
  • 21
0

Yes, You have to provide internet permission in AndroidManifest.xml file.