Newer
Older
RxSprout / app / src / main / java / com / example / sprout / rooms / RoomMainActivity.java
package com.example.sprout.rooms;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.sprout.R;
import com.example.sprout.R.id;
import com.example.sprout.R.layout;
import com.example.sprout.Sprout;
import com.example.sprout.battles.BattleActivity;

import net.arnx.jsonic.JSON;

import framework.gameMain.RealTimeActivity;
import framework.network.CallBack;

public class RoomMainActivity extends RealTimeActivity {
    boolean ready = false;
    private RoomsConnection connection;
    private Sprout sprout;
    private Member member[] = new Member[4];
    private String name[] = new String[4];
    private TextView memberName[] = new TextView[4];
    private int role[] = new int[4];
    private TextView roleType[] = new TextView[4];
    private Bitmap bitmapGunman;
    private Bitmap bitmapMagic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layout.activity_room_main);

        bitmapGunman = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        bitmapMagic = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

        sprout = (Sprout) getApplication();
        connection = sprout.getRoomsConnection();
        ((Sprout) RoomMainActivity.this.getApplication()).setMyRole(0);

        connection.setCallBack(new CallBack() {
            @Override
            public void onResponse(String response) {
                Room room = JSON.decode(response, Room.class);//自分の部屋の情報
                for (int i = 0; i < room.getMemberCount(); i++) {
                    System.out.println(room.getMemberByIndex(i).getUserName());
                    memberName[i].setText(room.getMemberByIndex(i).getUserName());
                    role[i] = room.getMemberByIndex(i).getRole();//i番目の人の職業
                    if (role[i] == 0) {
                        roleType[i].setText("ガンマン");
                    } else if (role[i] == 0) {
                        roleType[i].setText("マジシャン");
                    }
                }
            }
        });

        connection.addPathParam(String.valueOf(sprout.getRoomId()));
        connection.doGet();

		/*職業の選択(仮)*/
        Button SetButton1 = (Button) findViewById(id.RoomMain_button1);
        Button SetButton4 = (Button) findViewById(id.RoomMain_button4);

		/*ガンマン*/
        SetButton1.setOnClickListener(new View.OnClickListener() {


            public void onClick(View v) {
                ((Sprout) RoomMainActivity.this.getApplication()).setMyRole(0);
                connection = sprout.getRoomsConnection();

                connection.addPathParam(String.valueOf(sprout.getRoomId()));
                connection.addPathParam(String.valueOf(sprout.getMemberId()));
                connection.addFormParam("role", String.valueOf(0));
                connection.doPut();

            }
        });

		/*マジシャン*/
        SetButton4.setOnClickListener(new View.OnClickListener() {


            public void onClick(View v) {
                ((Sprout) RoomMainActivity.this.getApplication()).setMyRole(1);
                connection = sprout.getRoomsConnection();

                connection.addPathParam(String.valueOf(sprout.getRoomId()));
                connection.addPathParam(String.valueOf(sprout.getMemberId()));
                connection.addFormParam("role", String.valueOf(1));
                connection.doPut();
            }
        });


		/*準備完了⇔キャンセルのボタンの変更*/
        Button SetButton = (Button) findViewById(id.RoomMain_button2);
        SetButton.setOnClickListener(new View.OnClickListener() {


            public void onClick(View v) {

                if (ready == false) {
                    Button SetButton = (Button) findViewById(id.RoomMain_button2);
                    SetButton.setText("キャンセル");
                    ready = true;
                } else if (ready == true) {
                    Button SetButton = (Button) findViewById(id.RoomMain_button2);
                    SetButton.setText("準備完了");
                    ready = false;
                }
                connection = sprout.getRoomsConnection();

                connection.addPathParam(String.valueOf(sprout.getRoomId()));
                connection.addPathParam(String.valueOf(sprout.getMemberId()));
                connection.addFormParam("role", String.valueOf(2));
                connection.doPut();
            }
        });

		/*RoomListActivityへの移動(退出)*/
        Button ExitButton = (Button) findViewById(id.RoomMain_button3);
        ExitButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (ready == false) {
                    connection = sprout.getRoomsConnection();
                    connection.addPathParam(String.valueOf(sprout.getRoomId()));
                    connection.addPathParam(String.valueOf(sprout.getMemberId()));
                    connection.doDelete();

                    Intent Exit = new Intent(RoomMainActivity.this, RoomListActivity.class);
                    startActivity(Exit);
                }

            }
        });

        //1番目
        memberName[0] = (TextView) findViewById(id.textView2);
        roleType[0] = (TextView) findViewById(id.textView6);
        //ガンマン
        if (role[0] == 0) {
            ImageView img001 = (ImageView) findViewById(id.imageView1);
            img001.setImageBitmap(bitmapGunman);
        }
        //マジシャン
        else if (role[0] == 1) {
            ImageView img001 = (ImageView) findViewById(id.imageView1);
            img001.setImageBitmap(bitmapMagic);
        }

        //2番目
        memberName[1] = (TextView) findViewById(id.textView3);
        roleType[1] = (TextView) findViewById(id.textView7);
        //ガンマン
        if (role[1] == 0) {
            ImageView img002 = (ImageView) findViewById(id.imageView2);
            img002.setImageBitmap(bitmapGunman);
        }
        //マジシャン
        else if (role[1] == 1) {
            ImageView img002 = (ImageView) findViewById(id.imageView2);
            img002.setImageBitmap(bitmapMagic);
        }

        //3番目
        memberName[2] = (TextView) findViewById(id.textView4);
        roleType[2] = (TextView) findViewById(id.textView8);
        //ガンマン
        if (role[2] == 0) {
            ImageView img003 = (ImageView) findViewById(id.imageView3);
            img003.setImageBitmap(bitmapGunman);
        }
        //マジシャン
        else if (role[2] == 1) {
            ImageView img003 = (ImageView) findViewById(id.imageView3);
            img003.setImageBitmap(bitmapMagic);
        }

        //4番目
        memberName[3] = (TextView) findViewById(id.textView5);
        roleType[3] = (TextView) findViewById(id.textView9);
        //ガンマン
        if (role[3] == 0) {
            ImageView img004 = (ImageView) findViewById(id.imageView4);
            img004.setImageBitmap(bitmapGunman);
        }
        //マジシャン
        else if (role[3] == 1) {
            ImageView img004 = (ImageView) findViewById(id.imageView1);
            img004.setImageBitmap(bitmapMagic);
        }
    }

    @Override
    public void onResume() {
        // TODO 自動生成されたメソッド・スタブ
        super.onResume();
        this.start(500L);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.room_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void update(long interval) {
        // TODO Auto-generated method stub
        sprout = (Sprout) getApplication();
        connection = sprout.getRoomsConnection();

		/*memberのデータの更新*/
        connection.setCallBack(new CallBack() {
            @Override
            public void onResponse(String response) {
                Room room = JSON.decode(response, Room.class);
                for (int i = 0; i < 4; i++) {
                    memberName[i].setText("");
                }
                for (int i = 0; i < room.getMemberCount(); i++) {
                    System.out.println(room.getMemberByIndex(i).getUserName());
                    memberName[i].setText(room.getMemberByIndex(i).getUserName());
                    role[i] = room.getMemberByIndex(i).getRole();
                    if (role[i] == 0) {
                        roleType[i].setText("ガンマン");
                    } else if (role[i] == 1) {
                        roleType[i].setText("マジシャン");
                    }
                }

				/*戦闘準備が完了したらBattleActivityに移動(戦闘画面に)*/
                if (room.isReadyToFight() == true) {
                    System.out.println(room.isReadyToFight());
                    ((Sprout) getApplication()).setBattleId(room.getBattleId());
                    Intent Exit = new Intent(RoomMainActivity.this, BattleActivity.class);
                    startActivity(Exit);
                }

            }
        });

        connection.addPathParam(String.valueOf(sprout.getRoomId()));
        connection.doGet();

        //メンバーの情報(名前、職業)
        //		//1番目
        memberName[0] = (TextView) findViewById(id.textView2);
        roleType[0] = (TextView) findViewById(id.textView6);
        //		//ガンマン
        //		if(role[0] == 0){
        //			ImageView img001 = (ImageView)findViewById(R.id.imageView1);
        //			img001.setImageBitmap(bitmapGunman);
        //		}
        //		//マジシャン
        //		else if(role[0] == 1){
        //			ImageView img001 = (ImageView)findViewById(R.id.imageView1);
        //			img001.setImageBitmap(bitmapMagic);
        //		}

        //2番目
        memberName[1] = (TextView) findViewById(id.textView3);
        roleType[1] = (TextView) findViewById(id.textView7);
        //		//ガンマン
        //		if(role[1] == 0){
        //			ImageView img002 = (ImageView)findViewById(R.id.imageView2);
        //			img002.setImageResource(R.mipmap.gunman);
        //		}
        //		//マジシャン
        //		else if(role[1] == 1){
        //			ImageView img002 = (ImageView)findViewById(R.id.imageView2);
        //			img002.setImageResource(R.mipmap.magic);
        //		}

        //3番目
        memberName[2] = (TextView) findViewById(id.textView4);
        roleType[2] = (TextView) findViewById(id.textView8);
        //		//ガンマン
        //		if(role[2] == 0){
        //			ImageView img003 = (ImageView)findViewById(R.id.imageView3);
        //			img003.setImageResource(R.mipmap.gunman);
        //		}
        //		//マジシャン
        //		else if(role[2] == 1){
        //			ImageView img003 = (ImageView)findViewById(R.id.imageView3);
        //			img003.setImageResource(R.mipmap.magic);
        //		}

        //4番目
        memberName[3] = (TextView) findViewById(id.textView5);
        roleType[3] = (TextView) findViewById(id.textView9);
        //		//ガンマン
        //		if(role[3] == 0){
        //			ImageView img004 = (ImageView)findViewById(R.id.imageView4);
        //			img004.setImageResource(R.mipmap.gunman);
        //		}
        //		//マジシャン
        //		else if(role[3] == 1){
        //			ImageView img004 = (ImageView)findViewById(R.id.imageView4);
        //			img004.setImageResource(R.mipmap.magic);
        //		}
    }

    @Override
    public void onPause() {
        super.onPause();
        stop();
    }
}