package room; import java.util.ArrayList; import java.util.Date; import account.Account; import account.AccountManager; import game.Game; import game.GameManager; import game.Team; public class Room { private int id; //部屋を一意に認識するid private String name; private String pass; private String mode; // 0 = divide前 / 1 = divide中 / 2 = divide後 private int divideFlag = 0; private Game game = null; private Team team = null; public static final int TEAM_RED = 0; public static final int TEAM_BLUE = 1; public static final int MAX_MEMBERS = 2; public static final int REMOVE_TIME = 30000; //30000ms = 30s private ArrayList<Account> members = new ArrayList<Account>(); // AccountManagerを使うために宣言しておく ↓シングルトンパターンを利用しているため GameManager gamemgr = GameManager.getInstance(); // 一部屋の人数を増やす public void addMember(Account ac) { members.add(ac); ac.setRoom(this); } // 一部屋の中にいる人が退出 acのroomとwatの初期化も public void removeMember(Account ac) { members.remove(ac); ac.setRoom(null); ac.setWait("no"); } // 一部屋内の人数をカウント public int getMemberCount() { return members.size(); } // 部屋の中のn番目のアカウント情報を取得 public Account getMember(int n) { return members.get(n); } //MAX_MEMBERS(6)人集まったらチーム分けを行う public void divideTeam() { game = new Game(); game.setRoom(this); gamemgr.addGame(game); //すでに入ってるgameが存在する場合にそのゲームから該当アカウントを削除する // gamemgr.searchGameSid(this); //ramdomは[0〜5までの連番][0〜99までの乱数]が入る int ramdom[][] = new int[MAX_MEMBERS][2]; //0〜5までの番号を割り当てる for(int i=0; i<MAX_MEMBERS; i++) { ramdom[i][0] = i; } //乱数を生成する for(int i=0; i<MAX_MEMBERS; i++) { ramdom[i][1] = (int)(Math.random() * 100); } //昇順に並び替える for(int i=0; i<MAX_MEMBERS; i++) { for(int j=0; j<MAX_MEMBERS-1; j++) { if(ramdom[j][1] > ramdom[j+1][1]) { //変数入れ替え int temp1 = ramdom[j][1]; ramdom[j][1] = ramdom[j+1][1]; ramdom[j+1][1] = temp1; //何番目かの入れ替え int temp2 = ramdom[j][0]; ramdom[j][0] = ramdom[j+1][0]; ramdom[j+1][0] = temp2; } } } //小さい方から順に3人を赤チーム getMember(0〜2)の人が赤チーム for(int i=0; i<MAX_MEMBERS/2; i++) { Account ac = getMember(ramdom[i][0]); game.setTeamTo(ac,TEAM_RED); ac.newPlayer(); } //その他3人を青チーム getMember(3〜5)の人が青チーム for(int i = MAX_MEMBERS/2; i<MAX_MEMBERS; i++) { Account ac = getMember(ramdom[i][0]); game.setTeamTo(ac,TEAM_BLUE); ac.newPlayer(); } } //しばらく通信のない人を削除 public void removeNotConnectMember() { long nowDate = new Date().getTime(); for(int i = 0; i<getMemberCount(); i++) { //最終接続時間からREMOVE_TIME以上経ってたら、その人を部屋から退室させる long time = (nowDate - getMember(i).getFinalConnectTime().getTime()); System.out.println("Roomの通信時間チェック : " + time); if(REMOVE_TIME <= time) { removeMember(getMember(i)); } } } //game public Game getGame() { return game; } public void removeGame() { game = null; } //name public String getName() { return name; } public void setName(String name) { this.name = name; } //id public int getId() { return id; } public void setId(int id) { this.id = id; } //pass public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } //mode public String getMode() { return mode; } public void setMode(String mode) { this.mode = mode; } //DivideFlag public int getDivideFlag() { return divideFlag; } public void setDivideFlag(int divideFlag) { this.divideFlag = divideFlag; } }