package cactusServer.models; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import cactusServer.entities.CameraState; import cactusServer.entities.EmoteState; import cactusServer.entities.IDAddressedEntity; import cactusServer.entities.Instance; import cactusServer.entities.Player; import cactusServer.utils.RandomStringGenerator; /** * インスタンスを管理するクラス * * @author r-isitani * */ public class Instances { private static Instances theInstance = null; private HashMap<String, Instance> instanceMap = new HashMap<>(); // instanceのIDと実体を管理 private HashMap<String, Player> playerMap = new HashMap<>(); private static final int UNIQUE_ID_LENGTH = 12; private Instances() { } /** * Instancesクラスを取得する (シングルトン) * * @return */ public static Instances getInstance() { if (theInstance == null) { theInstance = new Instances(); } return theInstance; } /** * 新しいInstanceクラスを作成してMapに追加する * * @param name * @param state * @param stageModel * @return */ public IDAddressedEntity createInstance(String name, Instance.State state, String stageModel) { String id = RandomStringGenerator.generateUniqueString(UNIQUE_ID_LENGTH, RandomStringGenerator.ALPHA_NUMERIC, instanceMap.keySet()); Instance instance = new Instance(name, state, stageModel); instanceMap.put(id, instance); return new IDAddressedEntity(id, instance); } public IDAddressedEntity createPlayer(String instanceURI, String characterURI, CameraState cameraState, EmoteState.EmoteType animationClassToStart) { String id = RandomStringGenerator.generateUniqueString(UNIQUE_ID_LENGTH, RandomStringGenerator.ALPHA_NUMERIC, playerMap.keySet()); Player player = new Player(instanceURI, characterURI, cameraState, animationClassToStart); playerMap.put(id, player); return new IDAddressedEntity(id, player); } /** * IDに対応するInstanceを取得して返す * * @param instanceId * 取得したいInstanceのID * @return IDに対応するInstance */ public Instance getInstance(String instanceId) { return instanceMap.get(instanceId); } public List<Player> getPlayers() { return new ArrayList<Player>(playerMap.values()); } public Player getPlayer(String playerId) { return playerMap.get(playerId); } /** * IDに対応するInstanceをMapから削除して返す * * @param instanceId * Instanceを識別する一意のID * @return Mapから削除されたInstance */ public Instance destroyInstance(String instanceId) { return instanceMap.remove(instanceId); } public Player destroyPlayer(String playerId) { return playerMap.remove(playerId); } }