Newer
Older
CactusServer / src / main / java / cactusServer / entities / Instance.java
package cactusServer.entities;

import java.net.URI;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;

import cactusServer.entities.Area.Allowed;
import cactusServer.entities.Object;
import cactusServer.entities.Character;
import framework.model3D.Position3D;
import framework.physics.AngularVelocity3D;
import framework.physics.Velocity3D;
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 String getName() {
		return name;
	}
	
	public State getState() {
		return state;
	}
	
	public Set<String> getAreaIdSet() {
		return areaMap.keySet();
	}
	
	public HashMap<String, Area> getAreaMap() {
		return areaMap;
	}
	
	public Area getArea(String areaId) {
		return areaMap.get(areaId);
	}
	
	public Set<String> getObjIdSet() {
		return objMap.keySet();
	}
	
	public HashMap<String, Object> getObjMap() {
		return objMap;
	}
	
	public Object getObject(String objId) {
		return objMap.get(objId);
	}
	
	public Set<String> getCharacterIdSet() {
		return characterMap.keySet();
	}
	
	public HashMap<String, Character> getCharacterMap() {
		return characterMap;
	}
	
	public Character getCharacter(String characterId) {
		return characterMap.get(characterId);
	}
	
	public void setState(State state) {
		this.state = state;
	}

	public Map<String, Area> createArea(String areaId, String name, Plain[] regions, HashSet<Allowed> permissions) {
		String id = createUniqueId(UNIQUE_ID_LENGTH, areaMap.keySet());
		Area area = new Area(name, regions, permissions);
		areaMap.put(id, area);
		return new HashMap<String, Area>() {
			{put(id, area);}
		};
	}
	
	public Map<String, Object> createObject(String objId, Position3D position, Velocity3D velocity, AngularVelocity3D angularVelocity, Object.Attribute attribute) {
		String id = createUniqueId(UNIQUE_ID_LENGTH, objMap.keySet());
		Object object = new Object(position, velocity, angularVelocity, attribute);
		objMap.put(id, object);
		return new HashMap<String, Object>() {
			{put(id, object);}
		};
	}
	
	public Map<String, Character> createCharacter(URI accountURI, String name, Position3D position, String areaId, boolean isActive) {
		String id = createUniqueId(UNIQUE_ID_LENGTH, characterMap.keySet());
		Character character = new Character(accountURI, name, position, areaId, isActive);
		characterMap.put(id, character);
		return new HashMap<String, Character>() {
			{put(id, 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);
	}
	
	private String createUniqueId(int length, Set<String> idSet) {
		String id;
		do {
			id = createRandomId(length);
		} while (checkId(id, idSet));
		return id;
	}
	
	private boolean checkId(String id, Set<String> idSet) {
		return idSet.contains(id);
	}
	
	private String createRandomId(int length) {
		StringBuilder id = new StringBuilder("");
		do {
			id.append(UUID.randomUUID().toString());
		} while (id.length() < length);
		return id.substring(0, length);
	}

	public static enum State {
		AVAILABLE, MAINTENANCE;
	}
}