diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..c0f4868 --- /dev/null +++ b/.classpath @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target diff --git a/.project b/.project new file mode 100644 index 0000000..2cb22d7 --- /dev/null +++ b/.project @@ -0,0 +1,37 @@ + + + TagServer + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + org.eclipse.wst.common.project.facet.core.nature + org.eclipse.wst.jsdt.core.jsNature + + diff --git a/.settings/.jsdtscope b/.settings/.jsdtscope new file mode 100644 index 0000000..f179e11 --- /dev/null +++ b/.settings/.jsdtscope @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..abdea9a --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/main/resources=UTF-8 +encoding/=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..443e085 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,8 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component new file mode 100644 index 0000000..cc64bfa --- /dev/null +++ b/.settings/org.eclipse.wst.common.component @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/.settings/org.eclipse.wst.common.project.facet.core.prefs.xml b/.settings/org.eclipse.wst.common.project.facet.core.prefs.xml new file mode 100644 index 0000000..cc81385 --- /dev/null +++ b/.settings/org.eclipse.wst.common.project.facet.core.prefs.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/.settings/org.eclipse.wst.common.project.facet.core.xml b/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 0000000..2dcd63f --- /dev/null +++ b/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/.settings/org.eclipse.wst.jsdt.ui.superType.container b/.settings/org.eclipse.wst.jsdt.ui.superType.container new file mode 100644 index 0000000..3bd5d0a --- /dev/null +++ b/.settings/org.eclipse.wst.jsdt.ui.superType.container @@ -0,0 +1 @@ +org.eclipse.wst.jsdt.launching.baseBrowserLibrary \ No newline at end of file diff --git a/.settings/org.eclipse.wst.jsdt.ui.superType.name b/.settings/org.eclipse.wst.jsdt.ui.superType.name new file mode 100644 index 0000000..05bd71b --- /dev/null +++ b/.settings/org.eclipse.wst.jsdt.ui.superType.name @@ -0,0 +1 @@ +Window \ No newline at end of file diff --git a/.settings/org.eclipse.wst.validation.prefs b/.settings/org.eclipse.wst.validation.prefs new file mode 100644 index 0000000..04cad8c --- /dev/null +++ b/.settings/org.eclipse.wst.validation.prefs @@ -0,0 +1,2 @@ +disabled=06target +eclipse.preferences.version=1 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..e18e090 --- /dev/null +++ b/pom.xml @@ -0,0 +1,64 @@ + + + 4.0.0 + + org.ntlab + TagServer + war + 0.0.1-SNAPSHOT + TagServer + + + TagServer + + + org.apache.maven.plugins + maven-compiler-plugin + 2.5.1 + true + + 1.7 + 1.7 + + + + + + + + + org.glassfish.jersey + jersey-bom + ${jersey.version} + pom + import + + + + + + + org.glassfish.jersey.containers + jersey-container-servlet-core + + + + + + + net.arnx + jsonic + 1.3.0 + + + + 2.26-b03 + UTF-8 + + diff --git a/src/main/java/org/ntlab/TagServer/Constant.java b/src/main/java/org/ntlab/TagServer/Constant.java new file mode 100644 index 0000000..f87f2d9 --- /dev/null +++ b/src/main/java/org/ntlab/TagServer/Constant.java @@ -0,0 +1,6 @@ +package org.ntlab.TagServer; + +public class Constant { + public static int MAX_ROOM_NUM = 2; + public static boolean DEVELOP = false; +} diff --git a/src/main/java/org/ntlab/TagServer/Develop.java b/src/main/java/org/ntlab/TagServer/Develop.java new file mode 100644 index 0000000..56e2a5d --- /dev/null +++ b/src/main/java/org/ntlab/TagServer/Develop.java @@ -0,0 +1,41 @@ +package org.ntlab.TagServer; + +import org.ntlab.TagServer.Rooms.Rooms; +import org.ntlab.TagServer.accounts.Account; +import org.ntlab.TagServer.accounts.Accounts; + +public class Develop { + public static void createDummyData() { + if (Constant.DEVELOP) { + if (Accounts.getInstance().accountList.size() == 0) { + createDummyAccounts(); + createDummyRooms(); + createDummyTags(); + } + } + } + + // アカウントのダミーデータを作成 + private static void createDummyAccounts() { + for (int i = 0; i < 20; i++) { + Account account = new Account(Accounts.getInstance().getUserId(), "User" + String.valueOf(i)); + Accounts.getInstance().accountList.add(account); + Accounts.getInstance().setUserId(Accounts.getInstance().getUserId() + 1); + } + } + + // ルームのダミーデータを作成 + private static void createDummyRooms() { + Rooms.getInstance().createRoom(0, "testRoom"); + if (Constant.MAX_ROOM_NUM == 1) { + return; + } + Rooms.getInstance().enterRoom(1, 0); + Rooms.getInstance().updateReady(1, 0, false, true); + } + + // 鬼ごっこのダミーデータを作成 + private static void createDummyTags() { + Rooms.getInstance().updateReady(0, 0, true, true); + } +} diff --git a/src/main/java/org/ntlab/TagServer/MyResource.java b/src/main/java/org/ntlab/TagServer/MyResource.java new file mode 100644 index 0000000..e9a1b99 --- /dev/null +++ b/src/main/java/org/ntlab/TagServer/MyResource.java @@ -0,0 +1,25 @@ +package org.ntlab.TagServer; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +/** + * Root resource (exposed at "myresource" path) + */ +@Path("myresource") +public class MyResource { + + /** + * Method handling HTTP GET requests. The returned object will be sent to + * the client as "text/plain" media type. + * + * @return String that will be returned as a text/plain response. + */ + @GET + @Produces(MediaType.TEXT_PLAIN) + public String getIt() { + return "Got ittttttttttttttt!"; + } +} \ No newline at end of file diff --git a/src/main/java/org/ntlab/TagServer/Rooms/Room.java b/src/main/java/org/ntlab/TagServer/Rooms/Room.java new file mode 100644 index 0000000..30ffc6e --- /dev/null +++ b/src/main/java/org/ntlab/TagServer/Rooms/Room.java @@ -0,0 +1,157 @@ +package org.ntlab.TagServer.Rooms; + +import java.util.ArrayList; + +import org.ntlab.TagServer.Constant; +import org.ntlab.TagServer.accounts.Accounts; + +public class Room { + // 入室成功ならtrue, 人数オーバーにより失敗ならfalse + private boolean start = false; + private boolean success = true; + private boolean allReady = false; + private int roomId = -1; + private int hostUserId = -1; + private String hostUserName = ""; + private String roomName = ""; + private int tagId = -1; + private int maxRoomNum = Constant.MAX_ROOM_NUM; + // ルームのプレイヤー情報 + private ArrayList roomPlayerList = new ArrayList<>(); + + public Room(int roomId, int userId, String roomName) { + this.roomId = roomId; + this.roomName = roomName; + this.hostUserId = userId; + this.hostUserName = Accounts.getInstance().getUserName(userId); + roomPlayerList.add(new RoomPlayer(userId, true, true)); + } + + public Room() { + this.success = false; + } + + // 入室 + public Boolean enterPlayer(int userId) { + if (this.roomPlayerList.size() == Constant.MAX_ROOM_NUM) { + return false; + } + roomPlayerList.add(new RoomPlayer(userId, false, false)); + return true; + } + + // 退出 + public void getOutPlayer(int userId) { + this.roomPlayerList.remove(findPlayer(userId)); + } + + // スタート + public void gameStart(boolean start) { + this.start = start; + } + + // プレイヤーのreadyの状態を調べる + public void checkReady() { + if (this.roomPlayerList.size() != Constant.MAX_ROOM_NUM) { + this.allReady = false; + return; + } + for (RoomPlayer player : this.roomPlayerList) { + if (!player.isReady()) { + this.allReady = false; + return; + } + } + this.allReady = true; + } + + // プレイヤー検索 + public RoomPlayer findPlayer(int userId) { + for (RoomPlayer player : this.roomPlayerList) { + if (userId == player.getUserId()) { + return player; + } + } + return null; + } + + public int getRoomId() { + return roomId; + } + + public void setRoomId(int roomId) { + this.roomId = roomId; + } + + public int getHostUserId() { + return hostUserId; + } + + public void setHostUserId(int hostUserId) { + this.hostUserId = hostUserId; + } + + public String getRoomName() { + return roomName; + } + + public void setRoomName(String roomName) { + this.roomName = roomName; + } + + public ArrayList getRoomPlayerList() { + return roomPlayerList; + } + + public void setRoomPlayerList(ArrayList roomPlayerList) { + this.roomPlayerList = roomPlayerList; + } + + public int getTagId() { + return tagId; + } + + public void setTagId(int tagId) { + this.tagId = tagId; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public boolean isAllReady() { + return allReady; + } + + public void setAllReady(boolean allReady) { + this.allReady = allReady; + } + + public boolean isStart() { + return start; + } + + public void setStart(boolean start) { + this.start = start; + } + + public String getHostUserName() { + return hostUserName; + } + + public void setHostUserName(String hostUserName) { + this.hostUserName = hostUserName; + } + + public int getMaxRoomNum() { + return maxRoomNum; + } + + public void setMaxRoomNum(int maxRoomNum) { + this.maxRoomNum = maxRoomNum; + } +} diff --git a/src/main/java/org/ntlab/TagServer/Rooms/RoomPlayer.java b/src/main/java/org/ntlab/TagServer/Rooms/RoomPlayer.java new file mode 100644 index 0000000..784ed54 --- /dev/null +++ b/src/main/java/org/ntlab/TagServer/Rooms/RoomPlayer.java @@ -0,0 +1,55 @@ +package org.ntlab.TagServer.Rooms; + +import org.ntlab.TagServer.accounts.Accounts; + +public class RoomPlayer { + private int userId; + private String userName; + private boolean isHost = false; + private boolean ready = false; + + public RoomPlayer(int userId, boolean isHost, boolean ready) { + this.userId = userId; + this.userName = Accounts.getInstance().getUserName(userId); + this.isHost = isHost; + this.ready = ready; + } + + // 準備状態更新 + public void updateReady(boolean ready) { + this.ready = ready; + } + + public int getUserId() { + return userId; + } + + public void setUserId(int userId) { + this.userId = userId; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public boolean isHost() { + return isHost; + } + + public void setHost(boolean isHost) { + this.isHost = isHost; + } + + public boolean isReady() { + return ready; + } + + public void setReady(boolean ready) { + this.ready = ready; + } + +} diff --git a/src/main/java/org/ntlab/TagServer/Rooms/Rooms.java b/src/main/java/org/ntlab/TagServer/Rooms/Rooms.java new file mode 100644 index 0000000..72fcb32 --- /dev/null +++ b/src/main/java/org/ntlab/TagServer/Rooms/Rooms.java @@ -0,0 +1,121 @@ +package org.ntlab.TagServer.Rooms; + +import java.util.ArrayList; + +import javax.inject.Inject; +import javax.inject.Singleton; +import javax.ws.rs.DELETE; +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 org.ntlab.TagServer.Tags.Tags; +import org.ntlab.TagServer.accounts.Accounts; + +@Path("rooms") +@Singleton +public class Rooms { + + @Inject + Accounts accounts; + + // 全ルーム情報 + public static ArrayList roomList = new ArrayList<>(); + private static Rooms rooms = null; + private static int roomId = 0; + + public static Rooms getInstance() { + + if (rooms == null) { + rooms = new Rooms(); + } + return rooms; + } + + // ルーム一覧 + @GET + @Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON) + public ArrayList getAllRooms() { + return this.roomList; + } + + // ルームを作成 + @POST + @Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON) + public Room createRoom(@QueryParam("userId") Integer userId, @QueryParam("roomName") String roomName) { + Room room = new Room(roomId, userId, roomName); + roomList.add(room); + roomId++; + return room; + } + + // ルームを削除 + @DELETE + @Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON) + public void deleteRoom(@QueryParam("roomId") Integer roomId) { + if (roomList.size() == 0) { + return; + } + this.roomList.remove(findRoom(roomId)); + return; + } + + // ルームに入室 + @PUT + @Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON) + public Room enterRoom(@QueryParam("userId") Integer userId, @QueryParam("roomId") Integer roomId) { + boolean enterJudge = findRoom(roomId).enterPlayer(userId); + if (enterJudge) { + return findRoom(roomId); + } + // 入室失敗のルームインスタンスを作成 success=false + Room room = new Room(); + return room; + } + + // 部屋の状態取得 + @GET + @Path("{roomId}") + @Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON) + public Room getInRoom(@QueryParam("userId") Integer userId, @PathParam("roomId") Integer roomId) { + findRoom(roomId).checkReady(); + return findRoom(roomId); + } + + // 退出 + @PUT + @Path("{roomId}/{userId}") + @Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON) + public void updateReady(@PathParam("userId") Integer userId, @PathParam("roomId") Integer roomId) { + findRoom(roomId).getOutPlayer(userId); + } + + // 状態更新 + @PUT + @Path("{roomId}") + @Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON) + public Room updateReady(@QueryParam("userId") Integer userId, @PathParam("roomId") Integer roomId, + @QueryParam("start") Boolean start, @QueryParam("ready") Boolean ready) { + findRoom(roomId).findPlayer(userId).updateReady(ready); + // ホストからの状態更新が来た時 + if (userId == findRoom(roomId).getHostUserId()) { + findRoom(roomId).gameStart(start); + findRoom(roomId).setTagId(Tags.getInstance().createTag(findRoom(roomId))); + } + return findRoom(roomId); + } + + // ルーム検索 + public Room findRoom(int roomId) { + for (Room room : this.roomList) { + if (roomId == room.getRoomId()) { + return room; + } + } + return null; + } +} diff --git a/src/main/java/org/ntlab/TagServer/Tags/Tag.java b/src/main/java/org/ntlab/TagServer/Tags/Tag.java new file mode 100644 index 0000000..27c2382 --- /dev/null +++ b/src/main/java/org/ntlab/TagServer/Tags/Tag.java @@ -0,0 +1,171 @@ +package org.ntlab.TagServer.Tags; + +import java.util.ArrayList; +import java.util.Random; + +import org.ntlab.TagServer.Constant; +import org.ntlab.TagServer.Rooms.Room; +import org.ntlab.TagServer.Rooms.RoomPlayer; +import org.ntlab.TagServer.utils.DemonUtil; + +public class Tag { + private int tagId = -1; + // プレイヤー情報を保持 + private ArrayList playerList = new ArrayList<>(); + // プレイヤー全ての緯度経度が送信されたらtrue + private boolean load = false; + private double time = 600; // s + private final long TIME_INTERVAL = 250; // ms + private boolean demonChangeFlug = true; + private final int demonChangeInterval = 5; // s + + public Tag(int tagId, Room room) { + this.tagId = tagId; + for (RoomPlayer player : room.getRoomPlayerList()) { + playerList.add(new TagPlayer(player.getUserId())); + } + } + + // プレイヤー情報更新 + public void updateTagPlayer(TagPlayerPojo player) { + findTagPlayer(player.getUserId()).update(player); + load = checkLoad(); + } + + // 全プレイヤーのロード状態を確認 + public boolean checkLoad() { + if (load) { + return true; + } + for (TagPlayer tagPlayer : playerList) { + if (tagPlayer.getLatitude() == 0) { + return false; + } + } + startTimer(); + load = true; + + int demonNumber = new Random().nextInt(Constant.MAX_ROOM_NUM); + this.playerList.get(demonNumber).setDemon(true); + + return true; + } + + public void hoge() { + + } + + // 鬼の入れ替わり判定 + public void checkChangeDemon(int userId) { + System.out.println(demonChangeFlug); + if (!demonChangeFlug) + return; + TagPlayer mine = findTagPlayer(userId); + for (TagPlayer player : playerList) { + if (player.getUserId() == mine.getUserId()) { + continue; + } + System.out.println(DemonUtil.getDistance(mine.getLatitude(), mine.getLongitude(), player.getLatitude(), + player.getLongitude(), 8)); + if (DemonUtil.getDistance(mine.getLatitude(), mine.getLongitude(), player.getLatitude(), + player.getLongitude(), 8) >= 0 + && DemonUtil.getDistance(mine.getLatitude(), mine.getLongitude(), player.getLatitude(), + player.getLongitude(), 8) < 10.0) { + + if (mine.isDemon()) { + mine.setDemon(false); + findTagPlayer(player.getUserId()).setDemon(true); + setDemonChangeInterval(); + return; + } else if (player.isDemon()) { + findTagPlayer(player.getUserId()).setDemon(false); + mine.setDemon(true); + setDemonChangeInterval(); + return; + } else { + continue; + } + } + } + + } + + // 鬼の入れ替わりのインターバル調整 + public void setDemonChangeInterval() { + demonChangeFlug = false; + System.out.println("インターバル調整"); + new Thread(new Runnable() { + @Override + public void run() { + int interval = 0; + while (interval != demonChangeInterval) { + try { + Thread.sleep(1000); + } catch (Exception e) { + } + System.out.println("インターバル内"); + interval++; + } + demonChangeFlug = true; + } + }).start(); + } + + // プレイヤー検索 + public TagPlayer findTagPlayer(int userId) { + for (TagPlayer player : this.playerList) { + if (userId == player.getUserId()) { + return player; + } + } + return null; + } + + public void startTimer() { + new Thread(new Runnable() { + @Override + public void run() { + while (true) { + try { + Thread.sleep(TIME_INTERVAL); + } catch (Exception e) { + + } + time -= 0.25; + } + } + }).start(); + } + + public double getTime() { + return time; + } + + public void setTime(double time) { + this.time = time; + } + + public int getTagId() { + return tagId; + } + + public void setTagId(int tagId) { + this.tagId = tagId; + } + + public ArrayList getPlayerList() { + return playerList; + } + + public void setPlayerList(ArrayList playerList) { + this.playerList = playerList; + } + + public boolean isLoad() { + return load; + } + + public void setLoad(boolean load) { + this.load = load; + } +} diff --git a/src/main/java/org/ntlab/TagServer/Tags/TagPlayer.java b/src/main/java/org/ntlab/TagServer/Tags/TagPlayer.java new file mode 100644 index 0000000..68d49a6 --- /dev/null +++ b/src/main/java/org/ntlab/TagServer/Tags/TagPlayer.java @@ -0,0 +1,65 @@ +package org.ntlab.TagServer.Tags; + +import org.ntlab.TagServer.accounts.Accounts; + +public class TagPlayer { + // プレイヤーGps情報が送られていない時はnull値を入れる + private int userId = -1; + private String userName = ""; + private double latitude = 0; + private double longitude = 0; + private boolean demon = false; + + // プレイヤー情報更新 + public void update(TagPlayerPojo tagPlayer) { + System.out.println("update"); + this.latitude = tagPlayer.getLatitude(); + this.longitude = tagPlayer.getLongitude(); + } + + public TagPlayer(int userId) { + this.userId = userId; + this.userName = Accounts.getInstance().getUserName(userId); + } + + public int getUserId() { + return userId; + } + + public void setUserId(int userId) { + this.userId = userId; + } + + public double getLatitude() { + return latitude; + } + + public void setLatitude(double latitude) { + this.latitude = latitude; + } + + public double getLongitude() { + return longitude; + } + + public void setLongitude(double longitude) { + this.longitude = longitude; + } + + public boolean isDemon() { + return demon; + } + + public void setDemon(boolean demon) { + this.demon = demon; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + +} diff --git a/src/main/java/org/ntlab/TagServer/Tags/TagPlayerPojo.java b/src/main/java/org/ntlab/TagServer/Tags/TagPlayerPojo.java new file mode 100644 index 0000000..1ddc0c5 --- /dev/null +++ b/src/main/java/org/ntlab/TagServer/Tags/TagPlayerPojo.java @@ -0,0 +1,41 @@ +package org.ntlab.TagServer.Tags; + +public class TagPlayerPojo { + private int userId; + private double latitude; + private double longitude; + private boolean demon; + + public int getUserId() { + return userId; + } + + public void setUserId(int userId) { + this.userId = userId; + } + + public double getLatitude() { + return latitude; + } + + public void setLatitude(double latitude) { + this.latitude = latitude; + } + + public double getLongitude() { + return longitude; + } + + public void setLongitude(double longitude) { + this.longitude = longitude; + } + + public boolean isDemon() { + return demon; + } + + public void setDemon(boolean demon) { + this.demon = demon; + } + +} diff --git a/src/main/java/org/ntlab/TagServer/Tags/Tags.java b/src/main/java/org/ntlab/TagServer/Tags/Tags.java new file mode 100644 index 0000000..b7a0f08 --- /dev/null +++ b/src/main/java/org/ntlab/TagServer/Tags/Tags.java @@ -0,0 +1,96 @@ +package org.ntlab.TagServer.Tags; + +import java.util.ArrayList; + +import javax.inject.Singleton; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +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 org.ntlab.TagServer.Develop; +import org.ntlab.TagServer.Rooms.Room; + +@Path("tags") +@Singleton +public class Tags { + // 全Tag情報を保持 + public static ArrayList tagList = new ArrayList<>(); + private static Tags instance = null; + private static int tagId = 0; + // プレイヤー情報を保持 + + public static Tags getInstance() { + if (instance == null) { + instance = new Tags(); + } + return instance; + } + + // ゲーム作成 + public int createTag(Room room) { + int tmpTagId = this.tagId; + this.tagId++; + this.tagList.add(new Tag(tmpTagId, room)); + return tmpTagId; + } + + // ゲーム一覧 + @GET + @Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON) + public ArrayList getAllTags() { + Develop.createDummyData(); + return this.tagList; + } + + // ゲーム情報取得 + @Path("{tagId}") + @GET + @Consumes("application/json") + @Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON) + public Tag getTag(@PathParam("tagId") Integer tagId) { + return findTag(tagId); + } + + // プレイヤー情報更新 + @Path("{tagId}") + @PUT + @Consumes("application/json") + @Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON) + public Tag update(@PathParam("tagId") Integer tagId, @QueryParam("userId") Integer userId, + TagPlayerPojo tagPlayer) { + if (tagPlayer != null) { + findTag(tagId).updateTagPlayer(tagPlayer); + } else { + System.out.println("null"); + } + findTag(tagId).checkChangeDemon(userId); + return findTag(tagId); + } + + @PUT + @Path("test") + @Consumes("application/json") + @Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON) + public TagPlayerPojo test(TagPlayerPojo jsonTest) { + if (jsonTest != null) { + return jsonTest; + } else { + System.out.println("null"); + } + return null; + } + + // Tag検索 + public Tag findTag(int tagId) { + for (Tag tag : this.tagList) { + if (tagId == tag.getTagId()) { + return tag; + } + } + return null; + } +} diff --git a/src/main/java/org/ntlab/TagServer/accounts/Account.java b/src/main/java/org/ntlab/TagServer/accounts/Account.java new file mode 100644 index 0000000..7870eb9 --- /dev/null +++ b/src/main/java/org/ntlab/TagServer/accounts/Account.java @@ -0,0 +1,27 @@ +package org.ntlab.TagServer.accounts; + +public class Account { + private int userId; + private String userName; + + public Account(int userId, String userName) { + this.userId = userId; + this.userName = userName; + } + + public int getUserId() { + return userId; + } + + public void setUserId(int userId) { + this.userId = userId; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } +} diff --git a/src/main/java/org/ntlab/TagServer/accounts/Accounts.java b/src/main/java/org/ntlab/TagServer/accounts/Accounts.java new file mode 100644 index 0000000..e29884e --- /dev/null +++ b/src/main/java/org/ntlab/TagServer/accounts/Accounts.java @@ -0,0 +1,69 @@ +package org.ntlab.TagServer.accounts; + +import java.util.ArrayList; + +import javax.inject.Singleton; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; + +import org.ntlab.TagServer.Develop; + +@Path("accounts") +@Singleton +public class Accounts { + + // 全アカウント情報を保持 + public static ArrayList accountList = new ArrayList<>(); + private static Accounts accounts = null; + private static int userId = 0; + + public static Accounts getInstance() { + if (accounts == null) { + accounts = new Accounts(); + } + return accounts; + } + + // ユーザー一覧 + @GET + @Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON) + public ArrayList getAllAccounts() { + Develop.createDummyData(); + return this.accountList; + } + + // 新規ユーザー登録 + @POST + @Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON) + public Account createAccount(@QueryParam("userName") String userName) { + Account account = new Account(this.userId, userName); + accountList.add(account); + this.userId++; + return account; + } + + public String getUserName(int userId) { + return accountList.get(userId).getUserName(); + } + + // @Inject + public ArrayList getAccountList() { + return accountList; + } + + public void setAccountList(ArrayList accountList) { + this.accountList = accountList; + } + + public static int getUserId() { + return userId; + } + + public static void setUserId(int userId) { + Accounts.userId = userId; + } + +} diff --git a/src/main/java/org/ntlab/TagServer/utils/DemonUtil.java b/src/main/java/org/ntlab/TagServer/utils/DemonUtil.java new file mode 100644 index 0000000..b50df4f --- /dev/null +++ b/src/main/java/org/ntlab/TagServer/utils/DemonUtil.java @@ -0,0 +1,16 @@ +package org.ntlab.TagServer.utils; + +public class DemonUtil { + public static float getDistance(double lat1, double lng1, double lat2, double lng2, int precision) { + double R = 6378.137; // km + double lat = Math.toRadians(lat2 - lat1); + double lng = Math.toRadians(lng2 - lng1); + double A = Math.sin(lat / 2) * Math.sin(lat / 2) + Math.cos(Math.toRadians(lat1)) + * Math.cos(Math.toRadians(lat2)) * Math.sin(lng / 2) * Math.sin(lng / 2); + double C = 2 * Math.atan2(Math.sqrt(A), Math.sqrt(1 - A)); + double decimalNo = Math.pow(10, precision); + double distance = R * C; + distance = Math.round(decimalNo * distance / 1) / decimalNo; + return (float) distance * 1000; + } +} \ No newline at end of file diff --git a/src/main/java/org/ntlab/TagServer/utils/JSONConsumer.java b/src/main/java/org/ntlab/TagServer/utils/JSONConsumer.java new file mode 100644 index 0000000..0d61cbd --- /dev/null +++ b/src/main/java/org/ntlab/TagServer/utils/JSONConsumer.java @@ -0,0 +1,43 @@ +package org.ntlab.TagServer.utils; + +import java.io.IOException; +import java.io.InputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +import javax.ws.rs.Consumes; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyReader; +import javax.ws.rs.ext.Provider; + +import net.arnx.jsonic.JSON; +import net.arnx.jsonic.JSONException; + +@Provider +@Consumes(MediaType.APPLICATION_JSON) +public class JSONConsumer implements MessageBodyReader { + + @Override + public boolean isReadable(java.lang.Class type, java.lang.reflect.Type genericType, + java.lang.annotation.Annotation[] annotations, MediaType mediaType) { + return true; + } + + @Override + public Object readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, + MultivaluedMap httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { + // TODO Auto-generated method stub + try { + return JSON.decode(entityStream, genericType); + } catch (JSONException e) { + // TODO Auto-generated catch block + return null; + } catch (IOException e) { + // TODO Auto-generated catch block + return null; + } + } + +} \ No newline at end of file diff --git a/src/main/java/org/ntlab/TagServer/utils/JSONProvider.java b/src/main/java/org/ntlab/TagServer/utils/JSONProvider.java new file mode 100644 index 0000000..38ed438 --- /dev/null +++ b/src/main/java/org/ntlab/TagServer/utils/JSONProvider.java @@ -0,0 +1,39 @@ +package org.ntlab.TagServer.utils; + +import java.io.IOException; +import java.io.OutputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyWriter; +import javax.ws.rs.ext.Provider; + +import net.arnx.jsonic.JSON; + +@Provider() +@Produces(MediaType.APPLICATION_JSON) +public class JSONProvider implements MessageBodyWriter { + @Override + public long getSize(Object arg0, Class arg1, Type arg2, Annotation[] arg3, MediaType arg4) { + try { + return JSON.encode(arg0).getBytes("UTF-8").length; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public boolean isWriteable(Class arg0, Type arg1, Annotation[] arg2, MediaType arg3) { + return true; + } + + @Override + public void writeTo(Object arg0, Class arg1, Type arg2, Annotation[] arg3, MediaType arg4, MultivaluedMap arg5, + OutputStream arg6) throws IOException, WebApplicationException { + JSON.encode(arg0, arg6); + } +} \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..c871cc1 --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,18 @@ + + + + + Jersey Web Application + org.glassfish.jersey.servlet.ServletContainer + + jersey.config.server.provider.packages + org.ntlab.TagServer + + 1 + + + Jersey Web Application + /webapi/* + + diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp new file mode 100644 index 0000000..a064b45 --- /dev/null +++ b/src/main/webapp/index.jsp @@ -0,0 +1,8 @@ + + +

Jersey RESTful Web Application!

+

Jersey resource +

Visit Project Jersey website + for more information on Jersey! + +