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 5f27c8f..a385d96 100644 --- a/app/src/main/java/org/ntlab/irisclient/viewmodels/GameViewModel.java +++ b/app/src/main/java/org/ntlab/irisclient/viewmodels/GameViewModel.java @@ -1,7 +1,10 @@ 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; @@ -33,6 +36,7 @@ final private MutableLiveData> mapMutableLiveData; final private MutableLiveData imageMutableLiveData; private GameJson game = new GameJson(); + final private GameRest gameRest; final private Retrofit retrofit; //更新比較用フィールド @@ -56,7 +60,22 @@ .addConverterFactory(JacksonConverterFactory.create()) .build(); - testgame(); // 通信をして game に値をセットさせる関数 + 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 + /** + * 追記ここまで + */ } //---------------------------------------------------- @@ -65,12 +84,6 @@ 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; } @@ -101,6 +114,47 @@ } + //--------------------------------------------- + // マスターのヒントを送信するメソッド + 最大回答数も送信 + 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() { @@ -111,46 +165,45 @@ @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); +// //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); +// } +// }); +// } - 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); + //hint,openList,maxの取得 + public void updateTurn(String rid) { + Call call = gameRest.getTurns(rid, tno); call.enqueue(new Callback() { @Override @@ -270,12 +323,11 @@ System.out.println(t); } }); - } - public void testgame() { + public void setGame(String rid) { final GameRest gameRest = retrofit.create(GameRest.class); - Call call = gameRest.getGame("gametest"); + Call call = gameRest.getGame(rid); call.enqueue(new Callback() { @Override @@ -288,6 +340,7 @@ @Override public void onFailure(Call call, Throwable t) { + System.out.println(""); } }); }