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

import java.util.HashMap;

import javax.ws.rs.DELETE;
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.CameraState;
import cactusServer.entities.EmoteState;
import cactusServer.entities.Player;
import cactusServer.models.Instances;
import cactusServer.utils.App;
import net.arnx.jsonic.JSON;

@Path("/instances/players")
public class PlayersRest {
	@POST
	// @Produces(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_PLAIN)
	public String createPlayer(@FormParam("instanceID") String instanceID,
			@FormParam("characterID") String characterID) {
		Instances instances = Instances.getInstance();
		HashMap<String, Player> idMap = instances.createPlayer(instanceID, characterID, null,
				null);
		HashMap<String, Player> uriMap = new HashMap<>();
		for (String id : idMap.keySet()) {
			String uri = (InstancesRest.INSTANCES_URI + "/players/") + id;
			uriMap.put(uri, idMap.get(id));
		}
		return JSON.encode(uriMap);
	}

	@Path("/{playerId}")
	@GET
	// @Produces(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_PLAIN)
	public String getPlayer(@PathParam("playerId") String playerId) {
		return JSON.encode(Instances.getInstance().getPlayer(playerId));
	}

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

	@Path("/{playerId}")
	@PUT
	// @Produces(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_PLAIN)
	public String updatePlayer(@PathParam("playerId") String playerId, @FormParam("characterID") String characterID,
			@FormParam("position") Position3D position, @FormParam("angle") Quaternion3D angle,
			@FormParam("cameraState") CameraState cameraState,
			@FormParam("animationClassToStart") EmoteState.EmoteType animationClassToStart) {
		Player player = Instances.getInstance().updatePlayer(playerId, characterID, position, angle, cameraState,
				animationClassToStart);
		if (player != null) {
			return JSON.encode(player);
		}
		throw new WebApplicationException(400);
	}
	
	@Path("/{playerId}")
	@DELETE
	@Produces(MediaType.TEXT_PLAIN)
	public void deletePlayer(@PathParam("playerId") String playerId) {
		Instances.getInstance().destroyPlayer(playerId);
	}
}