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 d5309fd..a022885 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,9 @@ package org.ntlab.irisclient.viewmodels; -import org.ntlab.irisclient.Iris; import org.ntlab.irisclient.entities.GameJson; -import org.ntlab.irisclient.entities.RoomJson; import org.ntlab.irisclient.entities.TurnJson; -import org.ntlab.irisclient.models.Room; import org.ntlab.irisclient.resources.GameRest; + import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; @@ -23,11 +21,6 @@ //フィールド private String rid; private Integer tno; - private Integer cno; - private String team; - private String hint; - private Integer max; - private List openList; final private MutableLiveData turnsMutableLiveData; final private MutableLiveData turnMutableLiveData; @@ -35,6 +28,7 @@ final private MutableLiveData> opensMutableLiveData; final private MutableLiveData> mapMutableLiveData; final private MutableLiveData imageMutableLiveData; + final private MutableLiveData QMutableLiveData; private GameJson game = new GameJson(); final private GameRest gameRest; final private Retrofit retrofit; @@ -43,6 +37,7 @@ private String turnsPreData = null; private TurnJson turnPreData = null; private List opensPreData = null; + private Boolean[] QPreData = null; //------------------------------------------------------------------ //コンストラクタ @@ -50,157 +45,253 @@ this.turnsMutableLiveData = new MutableLiveData<>(); this.turnMutableLiveData = new MutableLiveData<>(); this.mapMutableLiveData = new MutableLiveData<>(); - this.colorMutableLiveData = new MutableLiveData<>(); this.opensMutableLiveData = new MutableLiveData<>(); this.imageMutableLiveData = new MutableLiveData<>(); + this.QMutableLiveData = new MutableLiveData<>(); this.retrofit = new Retrofit.Builder() .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/iris/") .addConverterFactory(JacksonConverterFactory.create()) .build(); - this.gameRest = retrofit.create(GameRest.class); - //RoomJson roomJson = new RoomJson(); - //System.out.println("adfasdfdsafdsa" + roomJson.getRid()); - /** - * 追記ここから(追記2個分の1個目) - * 変更1)initが呼ばれていなかった()→ initを呼び出し - * 変更2)gameが宣言のみで値がずっとnullだった → setGameの関数で通信を行う - */ - init(); // 変更1 - setGame(rid); // 変更2 - /** - * 追記ここまで - */ +// startColor(rid); +// startMap(rid); +// startImage(rid); + testGame(); // 通信をして game に値をセットさせる関数 } //---------------------------------------------------- //setter public void setRid(String rid) { this.rid = rid; + //初期値呼び出し + startColor(rid); + startMap(rid); + startImage(rid); } - public void setOpenList(Integer cno) { - this.cno = cno; - } - - //-------------------------------------------------------------- // getter - public LiveData getTurnsLiveData() { - return this.turnsMutableLiveData; - } public LiveData getTurnLiveData() { return this.turnMutableLiveData; } - // public LiveData> getColorLiveData() { -// return this.colorMutableLiveData; -// } public LiveData> getOpenLiveData() { return this.opensMutableLiveData; } - // public LiveData> getMapLiveData() { -// return this.mapMutableLiveData; -// } public LiveData getImageLiveData() { return this.imageMutableLiveData; } + public LiveData getQLiveData() { + return this.QMutableLiveData; + } public GameJson getGame() { return game; } - - //--------------------------------------------- - // マスターのヒントを送信するメソッド + 最大回答数も送信 - public void putHint(String hint, Integer max) { - + //マスターのヒント、最大回答数を送信 + public void sendHint(int tno, 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"); + System.out.println("通信成功:sendHint"); } } @Override public void onFailure(Call call, Throwable t) { - System.out.println("通信失敗:changeBelongsAndMaster"); + System.out.println("通信失敗:sendHint"); 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); -// } -// }); -// } + //どのマスを開いたかを送信 + public void sendOpenList(Integer cno) { + Call call = gameRest.setOpenList(rid, tno, cno); + + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful()){ + System.out.println("通信成功:sendOpenList"); + } + } + @Override + public void onFailure(Call call, Throwable t) { + System.out.println("通信失敗:sendOpenList"); + System.out.println(t); + } + }); + } + + //現在のTurnstateを送信 + public void sendTurnstate(String rid) { + Call call = gameRest.setTurnstate(rid, tno); + + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful()){ + System.out.println("通信成功:sendTurnstate"); + } + } + @Override + public void onFailure(Call call, Throwable t) { + System.out.println("通信失敗:sendTurnstate"); + System.out.println(t); + } + }); + } + + //現在のGamestateを送信 + public void sendEndstate(String rid) { + Call call = gameRest.setEndstate(rid, tno); + + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful()){ + System.out.println("通信成功:sendEndstate"); + } + } + @Override + public void onFailure(Call call, Throwable t) { + System.out.println("通信失敗:sendEndstate"); + System.out.println(t); + } + }); + } + + //疑っているマスの番号を送信 + public void sendQ(Integer cno) { + Call call = gameRest.changeQ(rid, tno, cno); + + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful()){ + System.out.println("通信成功:sendQ"); + } + } + @Override + public void onFailure(Call call, Throwable t) { + System.out.println("通信失敗:sendQ"); + System.out.println(t); + } + }); + } + + //カードのカラー(r,b,g,d)の取得 + private void startColor(String rid) { + Call> call = gameRest.getColorList(rid); + + call.enqueue(new Callback>() { + @Override + public void onResponse(Call> call, Response> response) { + if (response.isSuccessful()){ + colorMutableLiveData.setValue(response.body()); + } + } + + @Override + public void onFailure(Call> call, Throwable t) { + System.out.println("通信失敗:updateColor"); + System.out.println(t); + } + }); + } + + //カードの並び順の取得 + private void startMap(String rid) { + Call> call = gameRest.getMap(rid); + + call.enqueue(new Callback>() { + @Override + public void onResponse(Call> call, Response> response) { + if (response.isSuccessful()) { + mapMutableLiveData.setValue(response.body()); + } + } + + @Override + public void onFailure(Call> call, Throwable t) { + System.out.println("通信失敗:updateMap"); + System.out.println(t); + } + }); + } + + //カード(画像)の取得 + private void startImage(String rid) { + Call call = gameRest.getGame(rid); + + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful()) { + imageMutableLiveData.setValue(response.body()); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + System.out.println("通信失敗:updateImage"); + System.out.println(t); + } + }); + + } //----------------------------------------------------------------------------- // updates - public void init() { - updateColor(rid); - updateMap(rid); - updateImage(rid); - } - @Override public void update() { //updateTurns(rid); updateTurn(rid); updateOpens(rid); + updateQ(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の取得 + 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("通信失敗:updateTurns"); + System.out.println(t); + } + }); + } + */ + + //hint,openList,maxの更新 public void updateTurn(String rid) { Call call = gameRest.getTurns(rid, tno); @@ -224,36 +315,15 @@ @Override public void onFailure(Call call, Throwable t) { - System.out.println("通信失敗:changeBelongsAndMaster"); + System.out.println("通信失敗:updateTurn.hint no yatsu"); System.out.println(t); } }); } - //カードのカラー(r,b,g,d)の取得 - private void updateColor(String rid) { - final GameRest gameRest = retrofit.create(GameRest.class); - Call> call = gameRest.getColorList(rid); - - call.enqueue(new Callback>() { - @Override - public void onResponse(Call> call, Response> response) { - if (response.isSuccessful()){ - colorMutableLiveData.setValue(response.body()); - } - } - - @Override - public void onFailure(Call> call, Throwable t) { - System.out.println("通信失敗:changeBelongsAndMaster"); - System.out.println(t); - } - }); - } //現在開いているすべてのカードを取得([tffftffffftttff]みたいなリストを逐一更新する、マスが開くたびに更新) public void updateOpens(String rid) { - final GameRest gameRest = retrofit.create(GameRest.class); Call> call = gameRest.getOpens(rid); call.enqueue(new Callback>() { @@ -276,61 +346,46 @@ @Override public void onFailure(Call> call, Throwable t) { - System.out.println("通信失敗:changeBelongsAndMaster"); + System.out.println("通信失敗:updateOpens"); System.out.println(t); } }); } - //カードの並び順の取得 - private void updateMap(String rid) { - final GameRest gameRest = retrofit.create(GameRest.class); - Call> call = gameRest.getMap(rid); - call.enqueue(new Callback>() { + + //疑っているマス一覧の更新 + public void updateQ(String rid) { + Call call = gameRest.getQ(rid, tno); + + call.enqueue(new Callback() { @Override - public void onResponse(Call> call, Response> response) { - if (response.isSuccessful()) { - mapMutableLiveData.setValue(response.body()); + public void onResponse(Call call, Response response) { + if (response.isSuccessful()){ + if(turnsPreData == null){ + //初回代入 + QMutableLiveData.setValue(response.body()); + QPreData = response.body(); + }else if(response.body().equals(QPreData)){ + //値が一緒なら書き換えない + }else{ + //値が異なるときのみライブデータを上書き + QMutableLiveData.setValue(response.body()); + QPreData = response.body(); + } } } @Override - public void onFailure(Call> call, Throwable t) { - System.out.println("通信失敗:changeBelongsAndMaster"); + public void onFailure(Call call, Throwable t) { + System.out.println("通信失敗:updateTurn.hint no yatsu"); System.out.println(t); } }); } - //カード(画像)の取得 - private void updateImage(String rid) { - final GameRest gameRest = retrofit.create(GameRest.class); - Call call = gameRest.getGame(rid); - - call.enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) { - if (response.isSuccessful()) { - imageMutableLiveData.setValue(response.body()); - } - } - - @Override - public void onFailure(Call call, Throwable t) { - System.out.println("通信失敗:changeBelongsAndMaster"); - System.out.println(t); - } - }); - } - - /** - * 追記ここから(追記2個分の2個目) - * 変更3) 通信をして、gameとimageMutableLiveDataに初期値を代入する - */ - public void setGame(String rid) { - final GameRest gameRest = retrofit.create(GameRest.class); - Call call = gameRest.getGame(rid); + public void testGame() { + Call call = gameRest.getGame("gametest"); call.enqueue(new Callback() { @Override @@ -343,11 +398,9 @@ @Override public void onFailure(Call call, Throwable t) { - System.out.println(""); + System.out.println("通信失敗:testGame"); + System.out.println(t); } }); } - /** - * 追記ここまで - */ } \ No newline at end of file