package cactusServer.entities; import java.net.URI; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import cactusServer.entities.Object; import cactusServer.utils.RandomStringGenerator; import cactusServer.entities.Character; import framework.model3D.Position3D; import net.arnx.jsonic.JSONHint; /** * インスタンス * @author ryo isitani * */ public class Instance { private String name; private State state; // private StageModel stage; @JSONHint(ignore = true) private HashMap<String, Area> areaMap = new HashMap<>(); @JSONHint(ignore = true) private HashMap<String, Object> objMap = new HashMap<>(); @JSONHint(ignore = true) private HashMap<String, Character> characterMap = new HashMap<>(); @JSONHint(ignore = true) private static final int UNIQUE_ID_LENGTH = 12; /** * (引数は現時点での仮のもの) * @param name インスタンス名 */ public Instance(String name, State state) { this.name = name; this.state = state; } public Instance(String name) { this.name = name; this.state = State.AVAILABLE; } public String getName() { return name; } public State getState() { return state; } @JSONHint(ignore = true) public Set<String> getAreaIdSet() { return areaMap.keySet(); } public Area getArea(String areaId) { return areaMap.get(areaId); } @JSONHint(ignore = true) public Set<String> getObjIdSet() { return objMap.keySet(); } public Object getObject(String objId) { return objMap.get(objId); } @JSONHint(ignore = true) public List<Character> getActiveCharacters() { List<Character> activeCharacterList = new ArrayList<>(); for (Map.Entry<String, Character> entry : characterMap.entrySet()) { Character character = entry.getValue(); if (character.isActive()) { activeCharacterList.add(character); } } return activeCharacterList; } public List<Character> getCharacters(String accountUniqueId) { List<Character> accountCharacterList = new ArrayList<>(); for (Map.Entry<String, Character> entry : characterMap.entrySet()) { Character character = entry.getValue(); URI accountURI = character.getAccountURI(); if (accountUniqueId.equals("")) { // if文の条件にはaccountURIに含まれるaccountIdと引数のidとの照合が入る accountCharacterList.add(character); } } return accountCharacterList; } public Character getCharacter(String characterId) { return characterMap.get(characterId); } public void setName(String name) { this.name = name; } public void setState(State state) { this.state = state; } public Map<String, Area> createArea(Area area) { String id = RandomStringGenerator.generateUniqueString(UNIQUE_ID_LENGTH, RandomStringGenerator.ALPHA_NUMERIC, areaMap.keySet()); areaMap.put(id, area); return new HashMap<String, Area>() { {put(id, area);} }; } public Map<String, Object> createObject(Object object) { String id = RandomStringGenerator.generateUniqueString(UNIQUE_ID_LENGTH, RandomStringGenerator.ALPHA_NUMERIC, objMap.keySet()); objMap.put(id, object); return new HashMap<String, Object>() { {put(id, object);} }; } public Map<String, Character> createCharacter(Character character) { String id = RandomStringGenerator.generateUniqueString(UNIQUE_ID_LENGTH, RandomStringGenerator.ALPHA_NUMERIC, characterMap.keySet()); characterMap.put(id, character); return new HashMap<String, Character>() { {put(id, character);} }; } public Character updateCharacter(String characterId, Position3D position) { Character character = characterMap.get(characterId); character.setPosition(position); return character; } public Character destroyCharacter(String characterId) { return characterMap.remove(characterId); } public Area destroyArea(String areaId) { return areaMap.remove(areaId); } public Object destroyObject(String objId) { return objMap.remove(objId); } public static enum State { AVAILABLE, MAINTENANCE; } }