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.WebApplicationException;
import javax.ws.rs.core.MediaType;
import org.ntlab.radishforandroidstudio.framework.model3D.Position3D;
import org.ntlab.radishforandroidstudio.framework.model3D.Quaternion3D;
import cactusServer.entities.Bullet;
import cactusServer.entities.Instance;
import cactusServer.models.Instances;
import net.arnx.jsonic.JSON;
@Path("/instances/{instanceId}/bullets")
public class BulletsRest {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getBullets(@PathParam("instanceId") String instanceId) {
return JSON.encode(Instances.getInstance().getInstance(instanceId).getBullets());
}
@Path("/{playerId}")
@POST
@Produces(MediaType.TEXT_PLAIN)
public String createBullet(@PathParam("instanceId") String instanceId, @PathParam("playerId") String playerId,
@FormParam("bulletId") String bulletId, @FormParam("position") Position3D position, @FormParam("angle") Quaternion3D angle) {
Instance instance = Instances.getInstance().getInstance(instanceId);
HashMap<String, Bullet> idMap = instance.createBullet(playerId, bulletId, position, angle);
HashMap<String, Bullet> uriMap = new HashMap<>();
for (String id : idMap.keySet()) {
String uri = (InstancesRest.INSTANCES_URI + "/" + instanceId + "/bullets/") + playerId + "/" + id;
uriMap.put(uri, idMap.get(id));
}
return JSON.encode(uriMap);
}
@Path("/{playerId}/{bulletId}")
@PUT
@Produces(MediaType.TEXT_PLAIN)
public String updateBullet(@PathParam("instanceId") String instanceId, @PathParam("playerId") String playerId,
@PathParam("bulletId") String bulletId, @FormParam("position") Position3D position, @FormParam("angle") Quaternion3D angle) {
Instance instance = Instances.getInstance().getInstance(instanceId);
HashMap<String, Bullet> map = instance.getBullets().get(playerId);
if (map != null) {
Bullet bullet = map.get(bulletId);
if (bullet != null) {
bullet.update(position, angle);
}
return JSON.encode(map);
}
throw new WebApplicationException(400);
}
@Path("/{playerId}/{bulletId}")
@DELETE
@Produces(MediaType.TEXT_PLAIN)
public String destroyBullet(@PathParam("instanceId") String instanceId, @PathParam("playerId") String playerId,
@PathParam("bulletId") String bulletId) {
Instance instance = Instances.getInstance().getInstance(instanceId);
HashMap<String, Bullet> map = instance.getBullets().get(playerId);
if (map != null) {
Bullet removedBullet = map.remove(bulletId);
return JSON.encode(removedBullet);
}
throw new WebApplicationException(400);
}
}