package com.shephertzdemo.activity;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
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.ConnectEvent;
import com.shephertz.app42.gaming.multiplayer.client.listener.ConnectionRequestListener;
import com.shephertzdemo.R;
import com.shephertzdemo.utils.Constant;
import com.shephertzdemo.utils.Utils;
public class MainActivity extends Activity implements ConnectionRequestListener {
private WarpClient theClient;
EditText et_username;
private String authData = "";
public ProgressDialog progressDialog;
LinearLayout layoutLogin, loyoutRoom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layoutLogin = (LinearLayout) findViewById(R.id.linearLayout1);
loyoutRoom = (LinearLayout) findViewById(R.id.linearLayout2);
layoutLogin.setVisibility(View.VISIBLE);
et_username = (EditText) findViewById(R.id.nameEditText);
init();
}
private void init() {
Log.d("init", "init-start");
try {
Log.d("init", "init-try");
WarpClient.initialize(Constant.APP_KEY, Constant.HOST_NAME);
WarpClient.setRecoveryAllowance(Constant.MAX_RECOVERY_ATTEMPT);
theClient = WarpClient.getInstance();
} catch (Exception e) {
Log.d("init", "init-catch");
e.printStackTrace();
Utils.showToastAlert(this, Constant.ALERT_INIT_EXEC);
}
Log.d("init", "init-end");
}
public void onPlayGameClicked(View v) {
Log.d("onclick", "Play button click");
if (et_username.getText().length() == 0) {
Utils.showToastAlert(this,
getApplicationContext().getString(R.string.enterName));
return;
}
String userName = et_username.getText().toString().trim();
loginToAppWarp(userName, "");
}
private void loginToAppWarp(String name, String string) {
Log.d("loginToAppWarp", "loginToAppWarp");
Log.d("loginToAppWarp", "" + name);
Utils.userName = name;
theClient.connectWithUserName(name, string);
progressDialog = ProgressDialog.show(this, "", "Connecting...");
progressDialog.setCancelable(true);
}
@Override
public void onStart() {
super.onStart();
theClient.addConnectionRequestListener(this);
}
@Override
public void onStop() {
super.onStop();
theClient.removeConnectionRequestListener(this);
}
@Override
public void onConnectDone(final ConnectEvent event, String desc) {
Log.d("onConnectDone", "onConnectDone");
Log.d("onConnectDone", event.getResult() + " listener " + desc);
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("run", "run");
if (progressDialog != null) {
progressDialog.dismiss();
progressDialog = null;
}
Log.d("result ", String.valueOf(event.getResult()));
Log.d("sucess ", String.valueOf(WarpResponseResultCode.SUCCESS));
Log.d("bad request ", String.valueOf(WarpResponseResultCode.BAD_REQUEST));
Log.d("auth error ", String.valueOf(WarpResponseResultCode.AUTH_ERROR));
Log.d("connection error ", String.valueOf(WarpResponseResultCode.CONNECTION_ERROR));
Log.d("resurce not found ", String.valueOf(WarpResponseResultCode.RESOURCE_NOT_FOUND));
Log.d("conn error recoverable", String.valueOf(WarpResponseResultCode.CONNECTION_ERROR_RECOVERABLE));
Log.d("resource move ", String.valueOf(WarpResponseResultCode.RESOURCE_MOVED));
Log.d("result size error ", String.valueOf(WarpResponseResultCode.RESULT_SIZE_ERROR));
Log.d("sucess recoverd ", String.valueOf(WarpResponseResultCode.SUCCESS_RECOVERED));
Log.d("unknown error ", String.valueOf(WarpResponseResultCode.UNKNOWN_ERROR));
if (event.getResult() == WarpResponseResultCode.SUCCESS) {
Log.d("success", "success");
goToRoomList();
} else if (event.getResult() == WarpResponseResultCode.BAD_REQUEST) {
Log.d("bad requset", "bad request");
theClient.disconnect();
} else {
Log.d("else", "else");
Utils.showToastAlert(MainActivity.this,
Constant.ALERT_CONN_FAIL + event.getResult());
}
}
});
}
@Override
public void onDisconnectDone(ConnectEvent event) {
if (event.getResult() == WarpResponseResultCode.SUCCESS) {
} else {
Utils.showToastAlertOnUIThread(MainActivity.this,
Constant.ALERT_ERR_DISCONN);
}
}
@Override
public void onInitUDPDone(byte arg0) {
}
protected void goToRoomList() {
loyoutRoom.setVisibility(View.VISIBLE);
layoutLogin.setVisibility(View.GONE);
}
}
thanks in advance