Newer
Older
CactusServer / src / main / java / cactusServer / resources / CharactersRest.java
package cactusServer.resources;

import java.util.HashMap;

import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;

import org.ntlab.radishforandroidstudio.framework.model3D.Position3D;
import org.ntlab.radishforandroidstudio.framework.model3D.Quaternion3D;

import cactusServer.entities.Character;
import cactusServer.entities.Instance;
import cactusServer.models.Instances;
import net.arnx.jsonic.JSON;

@Path("/instances/{instanceId}/characters")
public class CharactersRest {

	@Path("/{characterId}")
	@GET
	//@Produces(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_PLAIN)
	public String getCharacter(@PathParam("instanceId") String instanceId,
			@PathParam("characterId") String characterId) {
		Instance instance = Instances.getInstance().getInstance(instanceId);
		if (instance != null) {
			Character character = instance.getCharacter(characterId);
			System.out.println(JSON.encode(character, true));
			return JSON.encode(character);
		}
		return null;
	}

	@GET
	//@Produces(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_PLAIN)
	public String getCharacters(@PathParam("instanceId") String instanceId, @QueryParam("accountID") String accountID) {
		return JSON.encode(Instances.getInstance().getInstance(instanceId).getCharacters(accountID));
	}

	@POST
	//@Produces(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_PLAIN)
	public String createCharacter(@PathParam("instanceId") String instanceId,
			@FormParam("accountURI") String accountURI, @FormParam("name") String name,
			@FormParam("position") Position3D position, @FormParam("angle") Quaternion3D angle,
			@FormParam("modelID") int modelID) {
		Instance instance = Instances.getInstance().getInstance(instanceId);
		HashMap<String, Character> idMap = instance.createCharacter(instanceId, accountURI, name, position, angle,
				modelID);
		HashMap<String, Character> uriMap = new HashMap<>();
		for (String id : idMap.keySet()) {
			String uri = (InstancesRest.INSTANCES_URI + "/" + instanceId + "/characters/") + id;
			uriMap.put(uri, idMap.get(id));
		}
		return JSON.encode(uriMap);
	}

	@Path("/{characterId}")
	@PUT
	//@Produces(MediaType.APPLICATION_JSON)
	//@Produces(MediaType.TEXT_PLAIN)
	public void updateCharacter(@PathParam("instanceId") String instanceId, @PathParam("characterId") String characterId,
			@FormParam("position") Position3D position, @FormParam("angle") Quaternion3D angle,
			@FormParam("modelID") int modelID) {
		Instance instance = Instances.getInstance().getInstance(instanceId);
		Character character = instance.updateCharacter(characterId, position, angle, modelID);
		if (character == null) {
			throw new WebApplicationException(400);
		}
	}
}