package com.ntlab.irisserver.entities; import com.fasterxml.jackson.annotation.JsonProperty; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import java.util.*; public class Room implements IStateListener { @JsonProperty("rid") public String rid; //String rid = null; @JsonProperty("state") private int state; //int state; @JsonProperty("ownerName") public String ownerName; //String ownerName = null; @JsonProperty("members") Map<String,Member> members= new HashMap<>(); @JsonProperty("settings") Settings settings = new Settings(); @JsonProperty("game") Game game =null; private Member redMaster; private Member blueMaster; public Room(String rid, String owner) { this.rid=rid; ownerName=owner; } public String getOwnerName() { return ownerName; } public Settings getSettings() { return settings; } public List<Member> getMembers() { List<Member> memberslist = new ArrayList<Member>(members.values()) ; return memberslist; } public void setRedMaster(Member redMaster) { this.redMaster = redMaster; } public void setBlueMaster(Member blueMaster) { this.blueMaster = blueMaster; } //プレイヤーの情報を変更する public void changeTeamAndMaster(String nick, String belongs, boolean isMaster) { //変更対象の情報を取得 Member m = members.get(nick); //変更先のチームに既に所属している場合はチームに変更なし if(!(m.getBelongs().equals(belongs))) { //1チームの最大人数は8人 //変更先のチームの人数を数える int memberscount = 0; List<Member> membersList = new ArrayList<Member>(members.values()) ; for(Member mem :membersList) { if(mem.getBelongs().equals(belongs)) { memberscount++; } } //変更先のチームが満員であれば変更不可 if(memberscount >= 8) { return; }else{ m.setBelongs(belongs); } } //変更対象がマスターであればマスター不在に修正 if(m.isMaster()){ m.setMaster(false); if(redMaster == m){ redMaster = null; } if(blueMaster == m){ blueMaster = null; } } //マスターに変更する処理 if(isMaster){ //マスターを変更 if(belongs.equals("r")) { //赤チームのマスターをメンバーに変更 if(redMaster != null) { redMaster.setMaster(false); } //変更対象を赤チームのマスターに変更 m.setMaster(true); redMaster = m; } if(belongs.equals("b")) { //青チームのマスターをメンバーに変更 if(blueMaster != null) { blueMaster.setMaster(false); } //変更対象を赤チームのマスターに変更 m.setMaster(true); blueMaster = m; } } } public Game getGame() { return game; } public Member getMember(String nick) { Member m = members.get(nick); return m; } public void addMember(String nick) { Member m=new Member(nick); m.setNickname(nick); members.put(nick,m); } public void deleteMember(String nick) { members.remove(nick); } public int getState() { return state; } public void setState(int state){ this.state=state; if(state==2) { game = new Game(this); } } public void changeState(){ state += 1; } public void resetState(){ state = 0; } }