| | package org.ntlab.irisclient.viewmodels; |
---|
| | |
---|
| | import org.ntlab.irisclient.entities.GameJson; |
---|
| | 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; |
---|
| | |
---|
| |
---|
| | |
---|
| | |
---|
| | //フィールド |
---|
| | private String rid; |
---|
| | private int tno; |
---|
| | private Integer tno; |
---|
| | private Integer cno; |
---|
| | private String team; |
---|
| | private String hint; |
---|
| | private Integer max; |
---|
| | private Integer openlist; |
---|
| | |
---|
| | final private MutableLiveData<String> turnsMutableLiveData; |
---|
| | final private MutableLiveData<TurnJson> turnMutableLiveData; |
---|
| | final private MutableLiveData<List<String>> colorMutableLiveData; |
---|
| | final private MutableLiveData<List<Boolean>> opensMutableLiveData; |
---|
| | final private MutableLiveData<List<Integer>> mapMutableLiveData; |
---|
| | final private Game game = new Game(); |
---|
| | final private MutableLiveData<GameJson> imageMutableLiveData; |
---|
| | final private GameJson game = new GameJson(); |
---|
| | final private Retrofit retrofit; |
---|
| | |
---|
| | //更新比較用フィールド |
---|
| | private String turnsPreData = null; |
---|
| | private TurnJson turnPreData = null; |
---|
| | private List<Boolean> opensPreData = null; |
---|
| | |
---|
| | //------------------------------------------------------------------ |
---|
| | //コンストラクタ |
---|
| | public GameViewModel() { |
---|
| | this.turnsMutableLiveData = new MutableLiveData<>(); |
---|
| | this.turnMutableLiveData = new MutableLiveData<>(); |
---|
| | this.colorMutableLiveData = new MutableLiveData<>(); |
---|
| | this.opensMutableLiveData = new MutableLiveData<>(); |
---|
| | this.mapMutableLiveData = new MutableLiveData<>(); |
---|
| | this.imageMutableLiveData = new MutableLiveData<>(); |
---|
| | this.retrofit = new Retrofit.Builder() |
---|
| | .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/iris/") |
---|
| | .addConverterFactory(JacksonConverterFactory.create()) |
---|
| | .build(); |
---|
| |
---|
| | //setter |
---|
| | public void setRid(String rid) { |
---|
| | this.rid = rid; |
---|
| | } |
---|
| | // public void setCno(Integer cno) { |
---|
| | // this.cno = cno; |
---|
| | // } |
---|
| | |
---|
| | public void putHint(String hint, int max) { |
---|
| | this.hint = hint; |
---|
| | this.max = max; |
---|
| |
---|
| | } |
---|
| | public LiveData<List<String>> getColorLiveData() { |
---|
| | return this.colorMutableLiveData; |
---|
| | } |
---|
| | public LiveData<List<Boolean>> getOpenLiveData() { |
---|
| | return this.opensMutableLiveData; |
---|
| | } |
---|
| | public LiveData<List<Integer>> getMapLiveData() { |
---|
| | return this.mapMutableLiveData; |
---|
| | } |
---|
| | public void getGame() { |
---|
| | return; |
---|
| | } |
---|
| | // public Integer getTurnNumber(){ |
---|
| | // return tno; |
---|
| | // } |
---|
| | public LiveData<GameJson> getImageLiveData() { |
---|
| | return this.imageMutableLiveData; |
---|
| | } |
---|
| | public GameJson getGame() { |
---|
| | return game; |
---|
| | } |
---|
| | |
---|
| | |
---|
| | //----------------------------------------------------------------------------- |
---|
| | // updates |
---|
| | public void init() { |
---|
| | updateColor(rid); |
---|
| | updateMap(rid); |
---|
| | } |
---|
| | updateImage(rid); |
---|
| | } |
---|
| | |
---|
| | @Override |
---|
| | public void update() { |
---|
| | updateTurns(rid); |
---|
| | updateTurn(rid); |
---|
| | updateOpens(rid); |
---|
| | } |
---|
| | |
---|
| | //r,bの取得(今どちらのチームかの判別) |
---|
| | public void updateTurns(String rid) { |
---|
| |
---|
| | }); |
---|
| | } |
---|
| | |
---|
| | //カードのカラー(r,b,g,d)の取得 |
---|
| | public void updateColor(String rid) { |
---|
| | final GameRest gameRest = retrofit.create(GameRest.class); |
---|
| | Call<List<String>> call = gameRest.getColor(rid); |
---|
| | private void updateColor(String rid) { |
---|
| | final GameRest gameRest = retrofit.create(GameRest.class); |
---|
| | Call<List<String>> call = gameRest.getColorList(rid); |
---|
| | |
---|
| | call.enqueue(new Callback<List<String>>() { |
---|
| | @Override |
---|
| | public void onResponse(Call<List<String>> call, Response<List<String>> response) { |
---|
| |
---|
| | } |
---|
| | }); |
---|
| | } |
---|
| | |
---|
| | //現在開いているすべてのカードを取得 |
---|
| | public void updateOpens(String rid) { |
---|
| | final GameRest gameRest = retrofit.create(GameRest.class); |
---|
| | Call<List<Boolean>> call = gameRest.getOpens(rid); |
---|
| | |
---|
| | call.enqueue(new Callback<List<Boolean>>() { |
---|
| | @Override |
---|
| | public void onResponse(Call<List<Boolean>> call, Response<List<Boolean>> response) { |
---|
| | if (response.isSuccessful()){ |
---|
| | if(opensPreData == null){ |
---|
| | //初回代入 |
---|
| | opensMutableLiveData.setValue(response.body()); |
---|
| | opensPreData = response.body(); |
---|
| | }if(response.body().equals(turnPreData)){ |
---|
| | //値が一緒なら書き換えない |
---|
| | }else{ |
---|
| | //値が異なるときのみライブデータを上書き |
---|
| | opensMutableLiveData.setValue(response.body()); |
---|
| | opensPreData = response.body(); |
---|
| | } |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | @Override |
---|
| | public void onFailure(Call<List<Boolean>> call, Throwable t) { |
---|
| | System.out.println("通信失敗:changeBelongsAndMaster"); |
---|
| | System.out.println(t); |
---|
| | } |
---|
| | }); |
---|
| | } |
---|
| | |
---|
| | //カードの並び順の取得 |
---|
| | public void updateMap(String rid) { |
---|
| | private void updateMap(String rid) { |
---|
| | final GameRest gameRest = retrofit.create(GameRest.class); |
---|
| | Call<List<Integer>> call = gameRest.getMap(rid); |
---|
| | |
---|
| | call.enqueue(new Callback<List<Integer>>() { |
---|
| |
---|
| | } |
---|
| | }); |
---|
| | } |
---|
| | |
---|
| | //カード(画像)の取得 |
---|
| | private void updateImage(String rid) { |
---|
| | final GameRest gameRest = retrofit.create(GameRest.class); |
---|
| | Call<GameJson> call = gameRest.getGame(rid); |
---|
| | |
---|
| | call.enqueue(new Callback<GameJson>() { |
---|
| | @Override |
---|
| | public void onResponse(Call<GameJson> call, Response<GameJson> response) { |
---|
| | if (response.isSuccessful()) { |
---|
| | imageMutableLiveData.setValue(response.body()); |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | @Override |
---|
| | public void onFailure(Call<GameJson> call, Throwable t) { |
---|
| | System.out.println("通信失敗:changeBelongsAndMaster"); |
---|
| | System.out.println(t); |
---|
| | } |
---|
| | }); |
---|
| | } |
---|
| | |
---|
| | } |
---|
| | |
---|
| | |
確認お願いします
ok
e6b8285
intomaster
fromGVM1018
on 20 Oct 2022