diff --git a/app/src/main/java/org/ntlab/irisclient/DrawingCardFragment.java b/app/src/main/java/org/ntlab/irisclient/DrawingCardFragment.java index debd96f..a5d716e 100644 --- a/app/src/main/java/org/ntlab/irisclient/DrawingCardFragment.java +++ b/app/src/main/java/org/ntlab/irisclient/DrawingCardFragment.java @@ -69,10 +69,10 @@ Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); - setupWithViewModel(); + gameViewModel = new ViewModelProvider(this).get(GameViewModel.class); + resources = getResources(); View view = inflater.inflate(R.layout.fragment_card_drawing, container, false); - Context context = getContext(); // ImageButton16個の型を使いまわしやすいように配列で使用 imageButtons = new ImageButton[] { @@ -113,34 +113,6 @@ (ImageView) view.findViewById(R.id.backColour15) }; - for (int i = 0; i < drawingList.size(); i++) { - // タップとロングタップのセット - imageButtons[i].setOnClickListener(this::onClick); - imageButtons[i].setOnLongClickListener(this::onLongTap); - backColors[i].setOnLongClickListener(this::onLongTap); - - // 画像のセット - String urlSt = drawingList.get(map.get(i)).getDrawing(); - setImage(i, urlSt, context); - - // 初回だけセットする外枠の、メンバーとマスターで表示分け - // メンバー:全て肌色、マスター:赤・青・黒・グレーの表示 - if(isMaster == true) { - if (colorList.get(i) == "r") { - backColors[i].setBackground(resources.getDrawable(R.drawable.red_image)); - } else if (colorList.get(i) == "b") { - backColors[i].setBackground(resources.getDrawable(R.drawable.blue_image)); - } else if (colorList.get(i) == "g") { - backColors[i].setBackground(resources.getDrawable(R.drawable.gray_image)); - } else if (colorList.get(i) == "d") { - backColors[i].setBackground(resources.getDrawable(R.drawable.black_image)); - } - } else { - backColors[i].setBackground(resources.getDrawable(R.drawable.skin_image)); - } - - } - // Openされた画像ををObserveして、Viewに反映 gameViewModel.getOpenLiveData().observe ( getViewLifecycleOwner(), @@ -150,6 +122,15 @@ } ); + gameViewModel.getImageLiveData().observe( + getViewLifecycleOwner(), + gameJsonObserver -> { + System.out.println("Hello"); + System.out.println(gameJsonObserver.getDrawingList()); + setupWithViewModel(); + } + ); + return view; } @@ -189,70 +170,46 @@ } private void setupWithViewModel() { - gameViewModel = new ViewModelProvider(this).get(GameViewModel.class); - //drawingList = gameViewModel.getGame().getDrawingList(); - //map = gameViewModel.getGame().getMap(); - //colorList = gameViewModel.getGame().getColorList(); + Iris iris = (Iris) this.getActivity().getApplication(); + myTeam = iris.getTeam(); + drawingList = gameViewModel.getGame().getDrawingList(); + map = gameViewModel.getGame().getMap(); + colorList = gameViewModel.getGame().getColorList(); - // 本来は上を開放するが、値が空なので仮値を指定(テストフローでも値が空) - Drawing drawing0 = new Drawing(); - Drawing drawing1 = new Drawing(); - Drawing drawing2 = new Drawing(); - Drawing drawing3 = new Drawing(); - Drawing drawing4 = new Drawing(); - Drawing drawing5 = new Drawing(); - Drawing drawing6 = new Drawing(); - Drawing drawing7 = new Drawing(); - Drawing drawing8 = new Drawing(); - Drawing drawing9 = new Drawing(); - Drawing drawing10 = new Drawing(); - Drawing drawing11 = new Drawing(); - Drawing drawing12 = new Drawing(); - Drawing drawing13 = new Drawing(); - Drawing drawing14 = new Drawing(); - Drawing drawing15 = new Drawing(); - drawing0.setDrawing("http://nitta-lab-www.is.konan-u.ac.jp/irisdata/image/test16.jpg"); - drawing1.setDrawing("http://nitta-lab-www.is.konan-u.ac.jp/irisdata/image/test01.jpg"); - drawing2.setDrawing("http://nitta-lab-www.is.konan-u.ac.jp/irisdata/image/test02.jpg"); - drawing3.setDrawing("http://nitta-lab-www.is.konan-u.ac.jp/irisdata/image/test03.jpg"); - drawing4.setDrawing("http://nitta-lab-www.is.konan-u.ac.jp/irisdata/image/test04.jpg"); - drawing5.setDrawing("http://nitta-lab-www.is.konan-u.ac.jp/irisdata/image/test05.jpg"); - drawing6.setDrawing("http://nitta-lab-www.is.konan-u.ac.jp/irisdata/image/test06.jpg"); - drawing7.setDrawing("http://nitta-lab-www.is.konan-u.ac.jp/irisdata/image/test07.jpg"); - drawing8.setDrawing("http://nitta-lab-www.is.konan-u.ac.jp/irisdata/image/test08.jpg"); - drawing9.setDrawing("http://nitta-lab-www.is.konan-u.ac.jp/irisdata/image/test09.jpg"); - drawing10.setDrawing("http://nitta-lab-www.is.konan-u.ac.jp/irisdata/image/test10.jpg"); - drawing11.setDrawing("http://nitta-lab-www.is.konan-u.ac.jp/irisdata/image/test11.jpg"); - drawing12.setDrawing("http://nitta-lab-www.is.konan-u.ac.jp/irisdata/image/test12.jpg"); - drawing13.setDrawing("http://nitta-lab-www.is.konan-u.ac.jp/irisdata/image/test13.jpg"); - drawing14.setDrawing("http://nitta-lab-www.is.konan-u.ac.jp/irisdata/image/test14.jpg"); - drawing15.setDrawing("http://nitta-lab-www.is.konan-u.ac.jp/irisdata/image/test15.jpg"); + Context context = getContext(); - drawingList = new HashMap() { - { - put(11, drawing11); - put(12, drawing12); - put(1, drawing1); - put(8, drawing8); - put(6, drawing6); - put(14, drawing14); - put(4, drawing4); - put(15, drawing15); - put(10, drawing10); - put(2, drawing2); - put(0, drawing0); - put(7, drawing7); - put(13, drawing13); - put(3, drawing3); - put(5, drawing5); - put(9, drawing9); + for (int i = 0; i < drawingList.size(); i++) { + // タップとロングタップのセット + imageButtons[i].setOnClickListener(this::onClick); + imageButtons[i].setOnLongClickListener(this::onLongTap); + backColors[i].setOnLongClickListener(this::onLongTap); + + // 画像のセット + String urlSt = drawingList.get(map.get(i)).getDrawing(); + setImage(i, urlSt, context); + + System.out.println(colorList.get(i)); + String color = colorList.get(i); + // 初回だけセットする外枠の、メンバーとマスターで表示分け + // メンバー:全て肌色、マスター:赤・青・黒・グレーの表示 + if(isMaster == true) { + if (colorList.get(i).contains("r")) { + backColors[i].setBackground(resources.getDrawable(R.drawable.red_image)); + } else if (colorList.get(i).contains("b")) { + backColors[i].setBackground(resources.getDrawable(R.drawable.blue_image)); + } else if (colorList.get(i).contains("g")) { + backColors[i].setBackground(resources.getDrawable(R.drawable.gray_image)); + } else if (colorList.get(i).contains("d")) { + backColors[i].setBackground(resources.getDrawable(R.drawable.black_image)); + } else { + System.out.println("本当は...:「" + colorList.get(i) + "」"); + } + } else { + backColors[i].setBackground(resources.getDrawable(R.drawable.skin_image)); } - }; - map = new ArrayList<>(Arrays.asList(2, 13, 6, 0, 7, 8, 4, 9, 14, 1, 3, 15, 11, 5, 12, 10)); - colorList = new ArrayList<>(Arrays.asList("r", "b", "b", "r", "b", "r", "d", "b", "g", "b", "r", "g", "b", "r", "g", "r")); - System.out.print(drawingList.size()); - System.out.print("よばれたよ"); + } + } /** @@ -336,16 +293,16 @@ for(int i=0; i< openList.size(); i++) { if (openList.get(i) == true) { // オープンだったときの描画 - if (colorList.get(i) == "r") { + if (colorList.get(i).contains("r")) { backColors[i].setBackground(resources.getDrawable(R.drawable.red_image)); imageButtons[i].setVisibility(View.INVISIBLE); - } else if (colorList.get(i) == "b") { + } else if (colorList.get(i).contains("b")) { backColors[i].setBackground(resources.getDrawable(R.drawable.blue_image)); imageButtons[i].setVisibility(View.INVISIBLE); - } else if (colorList.get(i) == "g") { + } else if (colorList.get(i).contains("g")) { backColors[i].setBackground(resources.getDrawable(R.drawable.gray_image)); imageButtons[i].setVisibility(View.INVISIBLE); - } else if (colorList.get(i) == "d") { + } else if (colorList.get(i).contains("d")) { backColors[i].setBackground(resources.getDrawable(R.drawable.black_image)); imageButtons[i].setVisibility(View.INVISIBLE); finishGameAlertMake(i, myTeam, nowTurn); @@ -380,16 +337,16 @@ private void setOpenRealTime(int cno, boolean isOK) { // 裏返す処理 if(isOK) { - if (colorList.get(cno) == "r") { + if (colorList.get(cno).contains("r")) { backColors[cno].setBackground(resources.getDrawable(R.drawable.red_image)); imageButtons[cno].setVisibility(View.INVISIBLE); - } else if (colorList.get(cno) == "b") { + } else if (colorList.get(cno).contains("b")) { backColors[cno].setBackground(resources.getDrawable(R.drawable.blue_image)); imageButtons[cno].setVisibility(View.INVISIBLE); - } else if (colorList.get(cno) == "g") { + } else if (colorList.get(cno).contains("g")) { backColors[cno].setBackground(resources.getDrawable(R.drawable.gray_image)); imageButtons[cno].setVisibility(View.INVISIBLE); - } else if (colorList.get(cno) == "d") { + } else if (colorList.get(cno).contains("d")) { backColors[cno].setBackground(resources.getDrawable(R.drawable.black_image)); imageButtons[cno].setVisibility(View.INVISIBLE); finishGameAlertMake(cno, myTeam, nowTurn); @@ -397,6 +354,7 @@ setAnime(cno); } //TODO: gameViewModel.set〇〇(cno)が完成したら + //gameViewModel.se } diff --git a/app/src/main/java/org/ntlab/irisclient/viewmodels/GameViewModel.java b/app/src/main/java/org/ntlab/irisclient/viewmodels/GameViewModel.java index 7073c10..5f27c8f 100644 --- a/app/src/main/java/org/ntlab/irisclient/viewmodels/GameViewModel.java +++ b/app/src/main/java/org/ntlab/irisclient/viewmodels/GameViewModel.java @@ -1,11 +1,8 @@ package org.ntlab.irisclient.viewmodels; -import org.ntlab.irisclient.Iris; import org.ntlab.irisclient.entities.GameJson; import org.ntlab.irisclient.entities.TurnJson; import org.ntlab.irisclient.resources.GameRest; -import org.ntlab.irisclient.resources.RoomsRest; - import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; @@ -25,8 +22,8 @@ private Integer tno; private Integer cno; private String team; - - + private String hint; + private Integer max; private List openList; final private MutableLiveData turnsMutableLiveData; @@ -35,8 +32,7 @@ final private MutableLiveData> opensMutableLiveData; final private MutableLiveData> mapMutableLiveData; final private MutableLiveData imageMutableLiveData; - final private GameJson game = new GameJson(); - final private GameRest gameRest; + private GameJson game = new GameJson(); final private Retrofit retrofit; //更新比較用フィールド @@ -60,7 +56,7 @@ .addConverterFactory(JacksonConverterFactory.create()) .build(); - this.gameRest = retrofit.create(GameRest.class); + testgame(); // 通信をして game に値をセットさせる関数 } //---------------------------------------------------- @@ -69,6 +65,12 @@ this.rid = rid; } + public void putHint(String hint, int max) { + this.hint = hint; + this.max = max; + updateTurn(); + } + public void setOpenList(Integer cno) { this.cno = cno; } @@ -76,70 +78,29 @@ //-------------------------------------------------------------- // getter -// public LiveData getTurnsLiveData() { -// return this.turnsMutableLiveData; -// } + public LiveData getTurnsLiveData() { + return this.turnsMutableLiveData; + } public LiveData getTurnLiveData() { return this.turnMutableLiveData; } -// public LiveData> getColorLiveData() { + // public LiveData> getColorLiveData() { // return this.colorMutableLiveData; // } public LiveData> getOpenLiveData() { return this.opensMutableLiveData; } -// public LiveData> getMapLiveData() { + // public LiveData> getMapLiveData() { // return this.mapMutableLiveData; // } -// public LiveData getImageLiveData() { -// return this.imageMutableLiveData; -// } + public LiveData getImageLiveData() { + return this.imageMutableLiveData; + } public GameJson getGame() { return game; } - //--------------------------------------------- - // マスターのヒントを送信するメソッド + 最大回答数も送信 - public void putHint(String hint, Integer max) { - - Call call = gameRest.putHint(rid, tno, hint, max); - - call.enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) { - if (response.isSuccessful()){ - System.out.println("通信成功:changeBelongsAndMaster"); - } - } - @Override - public void onFailure(Call call, Throwable t) { - System.out.println("通信失敗:changeBelongsAndMaster"); - System.out.println(t); - } - }); - } - -// public void putTeam(String team){ -// final Iris iris = retrofit.create(Iris.class); -// Call call = iris.setTeam(team); -// -// call.enqueue(new Callback() { -// @Override -// public void onResponse(Call call, Response response) { -// if (response.isSuccessful()){ -// System.out.println("通信成功:changeBelongsAndMaster"); -// } -// } -// @Override -// public void onFailure(Call call, Throwable t) { -// System.out.println("通信失敗:changeBelongsAndMaster"); -// System.out.println(t); -// } -// }); -// } - - //----------------------------------------------------------------------------- // updates public void init() { @@ -150,45 +111,46 @@ @Override public void update() { - //updateTurns(rid); - updateTurn(rid); + updateTurns(rid); + //updateTurn(rid); updateOpens(rid); } -// //r,bの取得(今どちらのチームかの判別) -// public void updateTurns(String rid) { -// final GameRest gameRest = retrofit.create(GameRest.class); -// Call call = gameRest.getTeam(rid); -// -// call.enqueue(new Callback() { -// @Override -// public void onResponse(Call call, Response response) { -// if (response.isSuccessful()){ -// if(turnsPreData == null){ -// //初回代入 -// turnsMutableLiveData.setValue(response.body()); -// turnsPreData = response.body(); -// }else if(response.body().equals(turnsPreData)){ -// //値が一緒なら書き換えない -// }else{ -// //値が異なるときのみライブデータを上書き -// turnsMutableLiveData.setValue(response.body()); -// turnsPreData = response.body(); -// } -// } -// } -// -// @Override -// public void onFailure(Call call, Throwable t) { -// System.out.println("通信失敗:changeBelongsAndMaster"); -// System.out.println(t); -// } -// }); -// } + //r,bの取得(今どちらのチームかの判別) + public void updateTurns(String rid) { + final GameRest gameRest = retrofit.create(GameRest.class); + Call call = gameRest.getTeam(rid); - //hint,openList,maxの取得 - public void updateTurn(String rid) { - Call call = gameRest.getTurns(rid, tno); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful()){ + if(turnsPreData == null){ + //初回代入 + turnsMutableLiveData.setValue(response.body()); + turnsPreData = response.body(); + }else if(response.body().equals(turnsPreData)){ + //値が一緒なら書き換えない + }else{ + //値が異なるときのみライブデータを上書き + turnsMutableLiveData.setValue(response.body()); + turnsPreData = response.body(); + } + } + } + + @Override + public void onFailure(Call call, Throwable t) { + System.out.println("通信失敗:changeBelongsAndMaster"); + System.out.println(t); + } + }); + } + + //hint,openList,maxの更新 + public void updateTurn() { + final GameRest gameRest = retrofit.create(GameRest.class); + Call call = gameRest.putHint(rid, tno, hint, max); call.enqueue(new Callback() { @Override @@ -308,7 +270,25 @@ System.out.println(t); } }); + } + public void testgame() { + final GameRest gameRest = retrofit.create(GameRest.class); + Call call = gameRest.getGame("gametest"); -} + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if(response.isSuccessful()) { + game = response.body(); + imageMutableLiveData.setValue(response.body()); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + } + }); + } +} \ No newline at end of file