diff --git a/app/src/main/java/org/ntlab/irisclient/viewmodels/RoomViewModel.java b/app/src/main/java/org/ntlab/irisclient/viewmodels/RoomViewModel.java index 3cf79f5..5ca4db5 100644 --- a/app/src/main/java/org/ntlab/irisclient/viewmodels/RoomViewModel.java +++ b/app/src/main/java/org/ntlab/irisclient/viewmodels/RoomViewModel.java @@ -1,5 +1,6 @@ package org.ntlab.irisclient.viewmodels; +import org.ntlab.irisclient.Iris; import org.ntlab.irisclient.entities.RoomJson; import org.ntlab.irisclient.models.Member; import org.ntlab.irisclient.models.Settings; @@ -7,9 +8,8 @@ import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; -import androidx.lifecycle.ViewModel; -import java.io.Console; +import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -23,12 +23,14 @@ //フィールド private String rid; + private Iris iris; final private MutableLiveData> membersLiveData; final private MutableLiveData settingsMutableLiveData; final private MutableLiveData stateLiveData; final private Retrofit retrofit; //更新比較用フィールド + private List sortedMembersList; private List membersPreData; private Settings settingsPreData; @@ -83,6 +85,67 @@ return true; } + private List sortMembersList(List oldMembers) { + List sortedMembers = new ArrayList<>(); + List> priorityInteger = new ArrayList<>(5); + List list0 = new ArrayList(); + List list1 = new ArrayList(); + List list2 = new ArrayList(); + List list3 = new ArrayList(); + List list4 = new ArrayList(); + + /* + 並び順優先順位 + 0.赤チームスパイマスター + 1.赤チーム諜報員 + 2.青チームスパイマスター + 3.青チーム諜報員 + 4.無所属の人 + */ + + for(int i = 0 ; i < oldMembers.size();i++){ + + //赤チームなら + if (oldMembers.get(i).getBelongs().equals("r")){ + if(oldMembers.get(i).isMaster()){ + //赤マスター優先度0 + list0.add(i); + } else { + //赤諜報員優先度1 + list1.add(i); + } + + //青チームなら + } else if(oldMembers.get(i).getBelongs().equals("b")) { + if(oldMembers.get(i).isMaster()) { + //青マスター優先度2 + list2.add(i); + } else { + //青諜報員優先度3 + list3.add(i); + } + + //その他のチーム(n)なら + } else { + list4.add(i); + } + + } + priorityInteger.add(list0); + priorityInteger.add(list1); + priorityInteger.add(list2); + priorityInteger.add(list3); + priorityInteger.add(list4); + + for(int i = 0; i < 5; i++) { + priorityInteger.get(i).forEach(num -> sortedMembers.add(oldMembers.get(num))); + } + + //System.out.println(sortedMembers); + //sortedMembers.forEach(k -> System.out.println(k.getNickname() + " " + k.getBelongs() + " " + k.isMaster())); + return sortedMembers; + } + //----------------------------------------------------------------------------- //各Activityでのボタン処理 //チーム&マスター変更 @@ -90,7 +153,7 @@ final RoomsRest roomsRest = retrofit.create(RoomsRest.class); Call call = roomsRest.putMember(rid, nickname, belongs, isMaster); - call.enqueue(new Callback() { + /*call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (response.isSuccessful()){ @@ -102,35 +165,21 @@ System.out.println("通信失敗:changeBelongsAndMaster"); System.out.println(t); } - }); + });*/ } //メンバー削除 - /*public void deleteMember(String rid, String nickname) { + public void deleteMember(String rid, String nickname) { final RoomsRest roomsRest = retrofit.create(RoomsRest.class); - Call<> - - 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); - } - }); - }*/ + Call call = roomsRest.deleteMember(rid, nickname); + } //スタートボタンの処理(OwnerActivityのみ) public void startDrawing(String rid, String nickname) { final RoomsRest roomsRest = retrofit.create(RoomsRest.class); Call call = roomsRest.putState(rid,nickname,2); - call.enqueue(new Callback() { + /*call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (response.isSuccessful()){ @@ -142,7 +191,7 @@ System.out.println("通信失敗:startDrawing"); System.out.println(t); } - }); + });*/ } //設定情報の変更処理(OwnerActivityのみ) @@ -150,7 +199,7 @@ final RoomsRest roomsRest = retrofit.create(RoomsRest.class); Call call = roomsRest.putSettings(rid, dTimer, dTimerTimes, gTimer, gTimerTimes, gTimerFTTimes); - call.enqueue(new Callback() { + /*call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (response.isSuccessful()){ @@ -162,7 +211,7 @@ System.out.println("通信失敗:changeSettings"); System.out.println(t); } - }); + });*/ } //----------------------------------------------------------------------------- // updates @@ -189,23 +238,30 @@ //もしもレスポンスが空なら何もしない } else { if (response.body().size() != membersPreData.size()) { - //配列のサイズが異なるならその時点でライブデータ書き換え - membersLiveData.setValue(response.body()); - membersPreData = response.body(); + //配列のサイズが異なるならその時点で並び替えてライブデータ書き換え + sortedMembersList = sortMembersList(response.body()); + membersLiveData.setValue(sortedMembersList); + //iris.setMemberList(sortedMembersList); + membersPreData = sortedMembersList; } else { //配列のサイズが同じ時は比較開始 if (compareMembers(response.body(), membersPreData)) { //比較してtrueなら何もしない } else { - //比較してfalseならライブデータ書き換え - membersLiveData.setValue(response.body()); - membersPreData = response.body(); + //比較してfalseなら並び替えてライブデータ書き換え + sortedMembersList = sortMembersList(response.body()); + membersLiveData.setValue(sortedMembersList); + //iris.setMemberList(sortedMembersList); + membersPreData = sortedMembersList; } } } } else { - membersLiveData.setValue(response.body()); - membersPreData = response.body(); + //最初のアップデートは無条件でライブデータ書き換え + sortedMembersList = sortMembersList(response.body()); + membersLiveData.setValue(sortedMembersList); + //iris.setMemberList(sortedMembersList); + membersPreData = sortedMembersList; } }