iT邦幫忙

2024 iThome 鐵人賽

DAY 12
0

今天這篇文章來實作JoinRoom的部分,player2需要輸入player1創建的房間房號,以此加入房間
image


activity_join_room.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ab7557"
    tools:context=".joinroom.JoinRoom">

    <TextView
        android:id="@+id/textView10"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/black"
        android:gravity="center"
        android:text="加入房間"
        android:textColor="@color/white"
        android:textSize="30dp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/guideline20"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/btn_back"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_back"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/redbackground_blackline"
        android:gravity="center"
        android:text="返回"
        android:textColor="@color/white"
        android:textStyle="bold"
        app:backgroundTint="@null"
        app:layout_constraintBottom_toTopOf="@+id/guideline20"
        app:layout_constraintEnd_toStartOf="@+id/guideline18"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/et_input_room_number"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/brownline"
        android:ems="10"
        android:gravity="center"
        android:hint="請輸入房號"
        android:inputType="number"
        android:maxLength="5"
        android:textColor="@color/white"
        app:layout_constraintBottom_toTopOf="@+id/guideline22"
        app:layout_constraintEnd_toStartOf="@+id/guideline19"
        app:layout_constraintStart_toStartOf="@+id/guideline18"
        app:layout_constraintTop_toTopOf="@+id/guideline21" />

    <Button
        android:id="@+id/btn_submit"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/redbackground_blackline"
        android:gravity="center"
        android:text="加入"
        android:textColor="@color/white"
        android:textStyle="bold"
        app:backgroundTint="@null"
        app:layout_constraintBottom_toTopOf="@+id/guideline23"
        app:layout_constraintEnd_toStartOf="@+id/guideline19"
        app:layout_constraintStart_toStartOf="@+id/guideline18"
        app:layout_constraintTop_toTopOf="@+id/guideline22" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline18"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.25" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline19"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.75" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline20"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.1" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline21"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.45" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline22"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.55" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline23"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.65" />

</androidx.constraintlayout.widget.ConstraintLayout>

JoinRoom

public class JoinRoom extends AppCompatActivity implements DatabaseContract.view{
    private EditText et_input_room_number;
    private String input_number="",roomKey="";
    private Button btn_submit,btn_back;
    private DatabasePresenter dbp = new DatabasePresenter(this);
    private ArrayList musicList = new ArrayList();
    private ArrayList colorList = new ArrayList();
    private Boolean player1OnlineState,player2OnlineState;
    private DialogData dialogData = new DialogData(this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_join_room);

        et_input_room_number = findViewById(R.id.et_input_room_number);
        btn_submit = findViewById(R.id.btn_submit);
        btn_back = findViewById(R.id.btn_back);

        getLastPageData();

        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                input_number = et_input_room_number.getText().toString();
                dialogData.showBackDialog("房間搜索中...");
                InputRandomNum();
            }
        });

        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void InputRandomNum(){
        FirebaseFirestore database = FirebaseFirestore.getInstance();
        database.collection("RoomData")
                .whereEqualTo("RoomNumber", input_number)
                .get()
                .addOnCompleteListener(task -> {
                    Log.d("TAG", "InputRandomNum: "+input_number);
                    Log.d("TAG", "InputRandomNum: "+task);
                    Log.d("TAG", "InputRandomNum: "+task.isSuccessful());
                    Log.d("TAG", "InputRandomNum: "+task.getResult());
                    Log.d("TAG", "InputRandomNum: "+task.getResult().getDocuments());
                    Log.d("TAG", "InputRandomNum: "+task.getResult().getDocuments().size());
                    if(task.isSuccessful() && task.getResult() != null && task.getResult().getDocuments().size() > 0){
                        dbp.getData(input_number);
                    }
                    else{
                        dialogData.dismissBackDialog();
                        Toast.makeText(this,"未找到房間,請輸入有效的房間號碼", Toast.LENGTH_SHORT).show();
                    }
                });
    }
    public void addSuccess(){
        Log.d("TAG", "資料上傳成功");
    }
    public void addError(){
        Log.d("TAG", "資料上傳失敗");
    }
    public void getSuccess(String getId,
                           String getRoomNumber,
                           Boolean getPlayer1OnlineState,
                           Boolean getPlayer2OnlineState,
                           String getMoveState,
                           HashMap<String,Object> getChessboardData,
                           HashMap<String,Object> getChessmanMoveData,
                           String getTurn,
                           HashMap<String,Object> getPawnMove1,
                           HashMap<String,Object> getPawnMove2,
                           HashMap<String,Object> getCastlingMove1,
                           HashMap<String,Object> getCastlingMove2,
                           String getDate,
                           String getPlayer1message,
                           String getPlayer2message,
                           String getWinPlayer){
        Log.d("TAG", "資料獲取成功");
        roomKey = getId;
        player1OnlineState = getPlayer1OnlineState;
        player2OnlineState = getPlayer2OnlineState;
        if(player1OnlineState == true && player2OnlineState == false){
            dialogData.dismissBackDialog();
            Intent intent = new Intent(getApplicationContext(), Player2ChessGame.class);
            intent.putExtra("room_number",input_number);
            intent.putIntegerArrayListExtra("musicData",musicList);
            intent.putIntegerArrayListExtra("colorData",colorList);
            intent.putExtra("gameMode","link");
            intent.putExtra("roomKey",roomKey);
            startActivity(intent);
        }else if(player2OnlineState == true && player1OnlineState == false){
            dialogData.dismissBackDialog();
            Intent intent = new Intent(getApplicationContext(), Player1ChessGame.class);
            intent.putExtra("room_number",input_number);
            intent.putIntegerArrayListExtra("musicData",musicList);
            intent.putIntegerArrayListExtra("colorData",colorList);
            intent.putExtra("gameMode","link");
            intent.putExtra("roomKey",roomKey);
            startActivity(intent);
        }
    }
    public void getError(){
        Log.d("TAG", "資料獲取失敗");
    }
    public void uploadSuccess(){
        Log.d("TAG", "資料修改成功");
    }
    public void uploadError(){
        Log.d("TAG", "資料修改失敗");
    }
    public void deleteSuccess(){
        Log.d("TAG", "資料刪除成功");
    }
    public void deleteError(){
        Log.d("TAG", "資料刪除失敗");
    }
    public void getLastPageData(){
        musicList = getIntent().getIntegerArrayListExtra("musicData");
        colorList = getIntent().getIntegerArrayListExtra("colorData");
    }
}

需要注意,跳轉頁面的部份分為player1上線中,及player2上線中:

  • player1上線中: 加入視為player2
  • player2上線中: 加入視為player1

這樣我們就可以透過房間號碼搜尋資料,並加入指定的房間


上一篇
【DAY 11】activity - CreateJoinRoom & layout
下一篇
【DAY 13】data - CreateBoardData
系列文
基於Firebase整合生成式AI研究開發雙人國際象棋系統(Based on Firebase and AI to research chess system)30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言