package org.ntlab.irisclient;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import org.ntlab.irisclient.entities.TurnJson;
import org.ntlab.irisclient.viewmodels.GameViewModel;
public class GameMasterActivity extends AppCompatActivity{
private GameViewModel gameViewModel;
//操作可能かどうかを記録。これがfalseの時は何のボタンを押すこともできない。
//時間があればオフラインの動作はできるようにしたい。
private boolean isActive = false;
private String myTeam;
//赤チームの「ヒント入力」からスタート
private String currentTeam = "r";
private int turnState = 0;//0:ヒント入力,1:カード選択
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_master);
// Fragmentを作成します
DrawingCardFragment fragment = new DrawingCardFragment();
GamePlayerListFragment RedPlayerList = new GamePlayerListFragment("r");
GamePlayerListFragment BluePlayerList = new GamePlayerListFragment("b");
// Fragmentの追加や削除といった変更を行う際は、Transactionを利用します
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// 新しく追加を行うのでaddを使用します
// 他にも、よく使う操作で、replace removeといったメソッドがあります
// メソッドの1つ目の引数は対象のViewGroupのID、2つ目の引数は追加するfragment
transaction.add(R.id.container, fragment);
transaction.add(R.id.RedPlayerListContainer, RedPlayerList);
transaction.add(R.id.BluePlayerListContainer,BluePlayerList);
// 最後にcommitを使用することで変更を反映します
transaction.commit();
//Irisから必要な情報を取得
Iris iris = (Iris) this.getApplication();
String rid = iris.getRid();
String nickName = iris.getNickname();
myTeam = iris.getTeam();
//viewModelに必用な情報をセット
gameViewModel= new ViewModelProvider(this).get(GameViewModel.class);
gameViewModel.setRid(rid);
//ボタンの情報を入力
buttonProcesses();
//自分が赤チームマスターの場合はヒントが入力可能
if(myTeam == null) {
System.out.println(("GameMasterActivity: Gakuto: myteamがnullです"));
}else{
isActive = CheckActivity();
}
//タイマースタート呼び出し
gameViewModel.start(500,iris);
//ヒントの変更を監視
gameViewModel.getHintLiveData().observe(this, new Observer<String>() {
@Override
public void onChanged(String new_Hint) {
System.out.println("Gakuto:MasterActivity ヒントが更新されました");
EditText Hint = findViewById(R.id.GameHint);
if(new_Hint != null){
Hint.setText(new_Hint);
}else{
Hint.getEditableText().clear();
}
}
});
gameViewModel.getMaxLiveData().observe(this, new Observer<Integer>() {
@Override
public void onChanged(Integer new_HintMax) {
System.out.println("Gakuto:MasterActivity ヒントマックスが更新されました");
EditText HintMax = findViewById(R.id.GameHintMax);
if(new_HintMax != 0){
HintMax.setText(new_HintMax.toString());
}else{
HintMax.getEditableText().clear();
}
}
});
//どちらのターンかを監視
gameViewModel.getTurnsLiveData().observe(this, new Observer<String>() {
@Override
public void onChanged(String new_currentTeam) {
//チームの情報を更新
currentTeam = new_currentTeam;
//操作できるかを判断する。
isActive = CheckActivity();
}
});
//「ヒント入力」か「カード選択」かを監視
gameViewModel.getTurnStateLiveData().observe(this, new Observer<Integer>() {
@Override
public void onChanged(Integer new_turnState) {
//チームの情報を更新
turnState = new_turnState;
//操作できるかを判断する。
isActive = CheckActivity();
}
});
//マスターなので推測終了ボタンを入力不可に設定
// ImageButton を無効にする
Button FinishGuessButton = (Button) findViewById(R.id.finishGuessButton);
FinishGuessButton.setEnabled(false);
//FinishGuessButton.setColorFilter(0xaaCCCCCC);
}
private void buttonProcesses(){
Button SendHintButton = (Button) findViewById(R.id.SendHint);
SendHintButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!isActive){
return;
}//アクティブのときだけボタンを押せる
EditText Hint = findViewById(R.id.GameHint);
EditText HintMax = findViewById(R.id.GameHintMax);
String hint = null;
Integer hintMax = null;
//テキストを適した変数に変更
if(Hint.getText().toString().length() != 0){
System.out.println("Gakuto:MasterActivity ヒント" + Hint.getText().toString());
hint = Hint.getText().toString();
}
if(HintMax.getText().toString().length() != 0){
System.out.println("Gakuto:MasterActivity ヒントマックス" + HintMax.getText().toString());
hintMax = Integer.parseInt(HintMax.getText().toString());
//マックス0は受け付けない
if(hintMax < 0){
hintMax = 1;
}
}
//ニックネームまたは部屋番号が入力されていない場合エラーメッセージを表示する
if (hint == null) {
System.out.println("Gakuto:MasterActivity ヒントが入力されていません");
} else if (hintMax == null) {
System.out.println("Gakuto:MasterActivity 数字が入力されていません");
} else {
System.out.println("Gakuto:MasterActivity ヒントを送信" + hint + " " + hintMax);
gameViewModel.sendHint(hint,hintMax);
}
}
});
}
//操作可能かどうかを調べる関数。masterとかturnをオブザーブしておいて、変更があれば反映する。
private boolean CheckActivity(){
boolean isActive = false;
//非アクティブであればヒントの入力を禁止する
EditText Hint = findViewById(R.id.GameHint);
EditText HintMax = findViewById(R.id.GameHintMax);
Button SendHintButton = (Button) findViewById(R.id.SendHint);
Button FinishGuessButton = (Button) findViewById(R.id.finishGuessButton);
//自分のチームのターンで、かつ「ヒント入力」時間の場合は行動可能
if(currentTeam.equals(myTeam) && turnState == 0){
isActive = true;
Hint.setEnabled(true);
HintMax.setEnabled(true);
SendHintButton.setEnabled(true);
} else {
Hint.setEnabled(false);
Hint.setTextColor(0xff000000);
HintMax.setEnabled(false);
HintMax.setTextColor(0xff000000);
SendHintButton.setEnabled(false);
FinishGuessButton.setEnabled(false);
}
//デバッグ用
Iris iris = (Iris) this.getApplication();
if(isActive == true){
System.out.println("Gakuto:MasterActivity" + iris.getNickname() + "は操作可能");
}else{
System.out.println("Gakuto:MasterActivity" + iris.getNickname() + "は操作不可");
}
return isActive;
}
}