Main.java :  » Client » clientserverapi » my » client » game » Android Open Source

Android Open Source » Client » clientserverapi 
clientserverapi » my » client » game » Main.java
package my.client.game;

import my.client.api.ClientAPI;
import my.client.game.R;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity implements OnClickListener{
  private final String slotName = "slotGame1";
  private final String intentFil = "my.client.slotGame1.DATA";
  private final String server = "http://gserver-szlif.appspot.com/gserver";
  private ClientAPI api;
  int val=0;
  private Button butLogin;
  private Button butCreate;
  private Button butSc;
  private Button butSt;
  private Button butPa;
  private Button butEx;
  private TextView textResult;
  private EditText editLogin;
  
//  private boolean clicked=false;
  private boolean loginScreen=true;
  private boolean waitToStartScreen=false;
  private boolean buttonCreate = false;
  private String dataFromOtherPlayer;
  private String myData;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api=new ClientAPI(this.getApplicationContext(), new Rec(), "Game1", "Rek", server, intentFil, 2500);
        setContentView(R.layout.login);
        prepareDataLoginScreen();
    }
    @Override
    protected void onDestroy(){
      api.handleStopReceiving();
      super.onDestroy();
    }
    
    public void onReceiveServerData(String data){
      //Log.v("AAA","data="+data);
      if(loginScreen && !buttonCreate && data.equalsIgnoreCase("ACC")){
        loginScreen = false;
        waitToStartScreen=true;
        api.handleConnectionToSlot(slotName);
      }
      else if(loginScreen && !buttonCreate && data.contains("REJ")){
        Toast.makeText(this, "Can't log in", Toast.LENGTH_SHORT).show();
      }
      else if(buttonCreate && data.equalsIgnoreCase("ACC")){
        Toast.makeText(this, "Slot created, you may start game.", Toast.LENGTH_SHORT).show();
      }
      else if(loginScreen && buttonCreate && data.contains("REJ")){
        Toast.makeText(this, "Can't create slot, error: "+data, Toast.LENGTH_LONG).show();
      }
      else if(waitToStartScreen && data.equalsIgnoreCase("ACC")){
        api.handleStartReceiving();
      setContentView(R.layout.main);
          prepareDataMainScreen();
      }
      else if(waitToStartScreen && data.contains("REJ") ){
        Toast.makeText(this, "Can't connect to slot: "+slotName+ " - try create slot.", Toast.LENGTH_SHORT).show();
      }
      else if(data.contains("PAPER")){
        dataFromOtherPlayer="PAPER";
      }
      else if(data.contains("SCISSORS")){
        dataFromOtherPlayer="SCISSORS";
    }
      else if(data.contains("STONE")){
        dataFromOtherPlayer="STONE";
    }
      checkResult();
    }
    
    private void prepareDataLoginScreen(){
      butLogin = (Button) findViewById(R.id.buttonLogin);
      butCreate = (Button) findViewById(R.id.buttonCreate);
      editLogin = (EditText) findViewById(R.id.inputLoginName);
      butLogin.setOnClickListener(this);
      butCreate.setOnClickListener(this);
    }
    
    private void prepareDataMainScreen(){
      butSt = (Button) findViewById(R.id.buttonStone);
      butSc = (Button) findViewById(R.id.buttonScissors);
      butPa = (Button) findViewById(R.id.buttonPaper);
      butEx = (Button) findViewById(R.id.buttonExit);
      butSt.setOnClickListener(this);
      butPa.setOnClickListener(this);
      butSc.setOnClickListener(this);
      butEx.setOnClickListener(this);
      textResult = (TextView) findViewById(R.id.textResult);
    }
    
    private void checkResult(){
      if(myData!=null && dataFromOtherPlayer!=null){
        if(isVictory()){
          textResult.setText("VICTORY! you:"+myData+"  VS  "+dataFromOtherPlayer);
        }
        else if(isDraw()){
          textResult.setText("DRAW!  you:"+myData+"  VS  "+dataFromOtherPlayer);
        }
        else{
          textResult.setText("DEFEAT! you:"+myData+"  VS  "+dataFromOtherPlayer);
        }
        dataFromOtherPlayer=null;
        myData=null;
      }
    }
    
    private boolean isVictory(){
      if( dataFromOtherPlayer.equalsIgnoreCase("STONE") && myData.equalsIgnoreCase("PAPER") || 
          dataFromOtherPlayer.equalsIgnoreCase("PAPER") && myData.equalsIgnoreCase("SCISSORS") ||
          dataFromOtherPlayer.equalsIgnoreCase("SCISSORS") && myData.equalsIgnoreCase("STONE") ){
        return true;
      }
      return false;  
    }
    private boolean isDraw(){
      if( dataFromOtherPlayer.equalsIgnoreCase(myData)){
        return true;
      }
      return false;  
    }
    
    private class Rec extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
      Bundle bun = intent.getExtras();
      if(bun==null){
        Log.d("AAA", "bundle is null");
        return;
      }
      String received = bun.getString("serverData");
      if(received!=null && received.length()>0){
        onReceiveServerData(received);
      }
      else{
        Log.d("AAA", "nothing to receive");
      }
    }
  }

  @Override
  public void onClick(View v) {
    if (v.getId() == R.id.buttonLogin) {
      buttonCreate=false;
      if(loginScreen){
        api.handleLogin(editLogin.getText().toString());
      }
      else{
        api.handleConnectionToSlot(slotName);
      }
      
    }
    if (v.getId() == R.id.buttonCreate) {
      buttonCreate=true;
      api.handleSlotCreation("", slotName);
      Toast.makeText(this, "Creating slot: "+slotName+"...", Toast.LENGTH_SHORT).show();
    }
    else{
      if(myData!=null){
        return; //don't allow sending data before round end
      }
      if (v.getId() == R.id.buttonStone) {
        myData = "STONE";
        api.handleSendMessage("STONE");
        checkResult();
      }
      else if (v.getId() == R.id.buttonScissors) {
        myData = "SCISSORS";
        api.handleSendMessage("SCISSORS");
        checkResult();
      }
      else if (v.getId() == R.id.buttonPaper) {
        myData = "PAPER";
        api.handleSendMessage("PAPER");
        checkResult();
      }
      else if (v.getId() == R.id.buttonExit) {
        finish();
      }
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.