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 9794cca..b2bf511 100644 --- a/app/src/main/java/org/ntlab/irisclient/viewmodels/GameViewModel.java +++ b/app/src/main/java/org/ntlab/irisclient/viewmodels/GameViewModel.java @@ -1,9 +1,13 @@ package org.ntlab.irisclient.viewmodels; import org.ntlab.irisclient.entities.TurnJson; +import org.ntlab.irisclient.models.Game; import org.ntlab.irisclient.resources.GameRest; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; + +import java.util.List; + import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -18,23 +22,29 @@ private int tno; private String team; private String hint; - private int max; + private Integer max; private Integer openlist; final private MutableLiveData turnsMutableLiveData; final private MutableLiveData turnMutableLiveData; - //final private MutableLiveData GameState game; + final private MutableLiveData> colorMutableLiveData; + final private MutableLiveData> mapMutableLiveData; + final private Game game = new Game(); final private Retrofit retrofit; //更新比較用フィールド private String turnsPreData = null; private TurnJson turnPreData = null; + private List colorPreData = null; + private List mapPreData = null; //------------------------------------------------------------------ //コンストラクタ public GameViewModel() { this.turnsMutableLiveData = new MutableLiveData<>(); this.turnMutableLiveData = new MutableLiveData<>(); + this.colorMutableLiveData = new MutableLiveData<>(); + this.mapMutableLiveData = new MutableLiveData<>(); this.retrofit = new Retrofit.Builder() .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/iris/") .addConverterFactory(JacksonConverterFactory.create()) @@ -61,15 +71,22 @@ public LiveData getTurnLiveData() { return this.turnMutableLiveData; } - public Integer getTurnNumber(){ - return tno; + public LiveData> getcolorLiveData() { + return this.colorMutableLiveData; } -// public Integer getTurnNumber(String rid) { + public LiveData> getmapLiveData() { + return this.mapMutableLiveData; + } +// public Integer getTurnNumber(){ // return tno; // } //----------------------------------------------------------------------------- // updates + public void init() { + updatecolor(rid); + updatemap(rid); + } @Override public void update() { updateTurns(rid); @@ -138,7 +155,66 @@ }); } + //カードのカラー(r,b,g,d)の取得 + public void updatecolor(String rid) { + final GameRest gameRest = retrofit.create(GameRest.class); + Call> call = gameRest.getColor(rid); + call.enqueue(new Callback>() { + @Override + public void onResponse(Call> call, Response> response) { + if (response.isSuccessful()){ + if(colorPreData == null){ + //初回代入 + colorMutableLiveData.setValue(response.body()); + colorPreData = response.body(); + }if(response.body().equals(colorPreData)){ + //値が一緒なら書き換えない + }else{ + //値が異なるときのみライブデータを上書き + colorMutableLiveData.setValue(response.body()); + colorPreData = response.body(); + } + } + } + @Override + public void onFailure(Call> call, Throwable t) { + System.out.println("通信失敗:changeBelongsAndMaster"); + System.out.println(t); + } + }); + } + + //カードの並び順の取得 + public void updatemap(String rid) { + final GameRest gameRest = retrofit.create(GameRest.class); + Call> call = gameRest.getMap(rid); + + call.enqueue(new Callback>() { + @Override + public void onResponse(Call> call, Response> response) { + if (response.isSuccessful()){ + if(mapPreData == null){ + //初回代入 + mapMutableLiveData.setValue(response.body()); + mapPreData = response.body(); + }if(response.body().equals(mapPreData)){ + //値が一緒なら書き換えない + }else{ + //値が異なるときのみライブデータを上書き + mapMutableLiveData.setValue(response.body()); + mapPreData = response.body(); + } + } + } + + @Override + public void onFailure(Call> call, Throwable t) { + System.out.println("通信失敗:changeBelongsAndMaster"); + System.out.println(t); + } + }); + } }