Newer
Older
CactusServer / src / main / java / cactusServer / resources / BulletsRest.java
  1. package cactusServer.resources;
  2.  
  3. import java.util.HashMap;
  4.  
  5. import javax.ws.rs.DELETE;
  6. import javax.ws.rs.FormParam;
  7. import javax.ws.rs.GET;
  8. import javax.ws.rs.POST;
  9. import javax.ws.rs.PUT;
  10. import javax.ws.rs.Path;
  11. import javax.ws.rs.PathParam;
  12. import javax.ws.rs.Produces;
  13. import javax.ws.rs.WebApplicationException;
  14. import javax.ws.rs.core.MediaType;
  15.  
  16. import org.ntlab.radishforandroidstudio.framework.model3D.Position3D;
  17. import org.ntlab.radishforandroidstudio.framework.model3D.Quaternion3D;
  18.  
  19. import cactusServer.entities.Bullet;
  20. import cactusServer.entities.Instance;
  21. import cactusServer.models.Instances;
  22. import net.arnx.jsonic.JSON;
  23.  
  24. @Path("/instances/{instanceId}/bullets")
  25. public class BulletsRest {
  26. @GET
  27. @Produces(MediaType.TEXT_PLAIN)
  28. public String getBullets(@PathParam("instanceId") String instanceId) {
  29. return JSON.encode(Instances.getInstance().getInstance(instanceId).getBullets());
  30. }
  31.  
  32. @Path("/{playerId}")
  33. @POST
  34. @Produces(MediaType.TEXT_PLAIN)
  35. public String createBullet(@PathParam("instanceId") String instanceId, @PathParam("playerId") String playerId,
  36. @FormParam("bulletId") String bulletId, @FormParam("position") Position3D position, @FormParam("angle") Quaternion3D angle) {
  37. Instance instance = Instances.getInstance().getInstance(instanceId);
  38. HashMap<String, Bullet> idMap = instance.createBullet(playerId, bulletId, position, angle);
  39. HashMap<String, Bullet> uriMap = new HashMap<>();
  40. for (String id : idMap.keySet()) {
  41. String uri = (InstancesRest.INSTANCES_URI + "/" + instanceId + "/bullets/") + playerId + "/" + id;
  42. uriMap.put(uri, idMap.get(id));
  43. }
  44. return JSON.encode(uriMap);
  45. }
  46.  
  47. @Path("/{playerId}/{bulletId}")
  48. @PUT
  49. @Produces(MediaType.TEXT_PLAIN)
  50. public String updateBullet(@PathParam("instanceId") String instanceId, @PathParam("playerId") String playerId,
  51. @PathParam("bulletId") String bulletId, @FormParam("position") Position3D position, @FormParam("angle") Quaternion3D angle) {
  52. Instance instance = Instances.getInstance().getInstance(instanceId);
  53. HashMap<String, Bullet> map = instance.getBullets().get(playerId);
  54. if (map != null) {
  55. Bullet bullet = map.get(bulletId);
  56. if (bullet != null) {
  57. bullet.update(position, angle);
  58. }
  59. return JSON.encode(map);
  60. }
  61. throw new WebApplicationException(400);
  62. }
  63. @Path("/{playerId}/{bulletId}")
  64. @DELETE
  65. @Produces(MediaType.TEXT_PLAIN)
  66. public String destroyBullet(@PathParam("instanceId") String instanceId, @PathParam("playerId") String playerId,
  67. @PathParam("bulletId") String bulletId) {
  68. Instance instance = Instances.getInstance().getInstance(instanceId);
  69. HashMap<String, Bullet> map = instance.getBullets().get(playerId);
  70. if (map != null) {
  71. Bullet removedBullet = map.remove(bulletId);
  72. return JSON.encode(removedBullet);
  73. }
  74. throw new WebApplicationException(400);
  75. }
  76. }