package cactusServer.models; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.ntlab.radishforandroidstudio.framework.model3D.Position3D; import cactusServer.entities.Angle; 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 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(Instance.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(Player.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); } /** * 全playerを返す * @return */ public List<Player> getPlayers() { return new ArrayList<Player>(playerMap.values()); } /** * IDに対応するinstanceにいる全playerを返す * @param instanceId * @return */ public List<Player> getPlayers(String instanceId) { if (instanceId == null || instanceId.isEmpty()) { return getPlayers(); } List<Player> responsePlayers = new ArrayList<>(); for (Player player : playerMap.values()) { String[] instanceURISplit = player.getInstanceURI().split("/"); // …/instances/{instanceId} if (instanceId.equals(instanceURISplit[instanceURISplit.length - 1])) { responsePlayers.add(player); } } return responsePlayers; } public Player getPlayer(String playerId) { return playerMap.get(playerId); } public Player updatePlayer(String playerId, String characterURI, Position3D position, Angle angle, CameraState cameraState, EmoteState.EmoteType animationClassToStart) { Player player = playerMap.get(playerId); player.update(characterURI, position, angle, cameraState, animationClassToStart); return player; } /** * IDに対応するInstanceをMapから削除して返す * * @param instanceId * Instanceを識別する一意のID * @return Mapから削除されたInstance */ public Instance destroyInstance(String instanceId) { return instanceMap.remove(instanceId); } public Player destroyPlayer(String playerId) { Player player = playerMap.get(playerId); player.destroy(); return playerMap.remove(playerId); } }