diff --git a/.classpath b/.classpath index c8e2022..a874505 100644 --- a/.classpath +++ b/.classpath @@ -23,5 +23,6 @@ + diff --git a/lib/jetty/jetty-websocket-8.1.12.v20130726.jar b/lib/jetty/jetty-websocket-8.1.12.v20130726.jar new file mode 100644 index 0000000..1b93315 Binary files /dev/null and b/lib/jetty/jetty-websocket-8.1.12.v20130726.jar differ diff --git a/src/org/jpokemon/manager/PlayerManager.java b/src/org/jpokemon/manager/PlayerManager.java index b537064..28dece6 100644 --- a/src/org/jpokemon/manager/PlayerManager.java +++ b/src/org/jpokemon/manager/PlayerManager.java @@ -11,9 +11,10 @@ import java.util.Map; import java.util.Queue; -import org.jpokemon.server.JPokemonServer; import org.jpokemon.manager.component.OverworldActivity; import org.jpokemon.manager.message.Message; +import org.jpokemon.server.JPokemonServer; +import org.jpokemon.server.JPokemonWebSocket; import org.jpokemon.trainer.Player; import org.jpokemon.trainer.PokemonTrainer; import org.json.JSONArray; @@ -57,6 +58,24 @@ public static void activityRequest(JSONObject request) throws ServiceException { } } + public static void dispatchRequest(JPokemonWebSocket socket, JSONObject request) throws JSONException, ServiceException { + Player player = connections.get(socket); + String action = request.getString("action"); + + // TODO : Refactor all of this shit + if (action.equals("login")) { + String name = loadPlayer(request.getString("name")); + socket.sendLog("Loaded with name: " + name); + } + else if (action.equals("create")) { + String name = createPlayer(request.getString("name"), request.getString("rival")); + socket.sendLog("Loaded with name: " + name); + } + else { + // TODO : do stuff with the player + } + } + public static Player getPlayer(String id) { if (players.get(id) == null) throw new IllegalArgumentException("Could not retrieve PlayerID: " + id); @@ -162,6 +181,7 @@ private static String getUniquePlayerName(String attempt) { } private static Map players = new HashMap(); + private static Map connections = new HashMap(); private static Map activities = new HashMap(); private static Map> messageQueues = new HashMap>(); diff --git a/src/org/jpokemon/server/JPokemonServer.java b/src/org/jpokemon/server/JPokemonServer.java index 93fdef5..c25b54b 100644 --- a/src/org/jpokemon/server/JPokemonServer.java +++ b/src/org/jpokemon/server/JPokemonServer.java @@ -36,8 +36,7 @@ public static void main(String[] args) throws Exception { ServletContextHandler root = new ServletContextHandler(); root.setContextPath("/"); root.addServlet(new ServletHolder(new FileServlet()), "/*"); - root.addServlet(new ServletHolder(new PlayerServlet()), PlayerServlet.URL_PATH); - root.addServlet(new ServletHolder(new LoginServlet()), LoginServlet.URL_PATH); + root.addServlet(new ServletHolder(new JPokemonWebSocketServlet()), "/socket"); // Instantiate the server on the specified port logger.info("Server starting on port " + port); diff --git a/src/org/jpokemon/server/JPokemonWebSocket.java b/src/org/jpokemon/server/JPokemonWebSocket.java new file mode 100644 index 0000000..6ad6962 --- /dev/null +++ b/src/org/jpokemon/server/JPokemonWebSocket.java @@ -0,0 +1,53 @@ +package org.jpokemon.server; + +import java.io.IOException; + +import org.eclipse.jetty.websocket.WebSocket; +import org.jpokemon.manager.PlayerManager; +import org.jpokemon.manager.ServiceException; +import org.json.JSONException; +import org.json.JSONObject; + +public class JPokemonWebSocket implements WebSocket.OnTextMessage { + private Connection connection; + + @Override + public void onClose(int arg0, String arg1) { + } + + @Override + public void onOpen(Connection c) { + connection = c; + } + + @Override + public void onMessage(String arg0) { + try { + PlayerManager.dispatchRequest(this, new JSONObject(arg0)); + } catch (JSONException e) { + e.printStackTrace(); + } catch (ServiceException e) { + e.printStackTrace(); + } + } + + public void sendJson(JSONObject json) { + try { + connection.sendMessage(json.toString()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void sendLog(String message) { + JSONObject json = new JSONObject(); + + try { + json.put("action", "log"); + json.put("message", message); + } catch (JSONException e) { + } + + sendJson(json); + } +} \ No newline at end of file diff --git a/src/org/jpokemon/server/JPokemonWebSocketServlet.java b/src/org/jpokemon/server/JPokemonWebSocketServlet.java new file mode 100644 index 0000000..75d94d8 --- /dev/null +++ b/src/org/jpokemon/server/JPokemonWebSocketServlet.java @@ -0,0 +1,15 @@ +package org.jpokemon.server; + +import javax.servlet.http.HttpServletRequest; + +import org.eclipse.jetty.websocket.WebSocket; +import org.eclipse.jetty.websocket.WebSocketServlet; + +public class JPokemonWebSocketServlet extends WebSocketServlet { + private static final long serialVersionUID = 1L; + + @Override + public WebSocket doWebSocketConnect(HttpServletRequest arg0, String arg1) { + return new JPokemonWebSocket(); + } +} \ No newline at end of file diff --git a/web/index.html b/web/index.html new file mode 100644 index 0000000..b385157 --- /dev/null +++ b/web/index.html @@ -0,0 +1,17 @@ + + + + + +

JPokemon loaded successfully I think

+ + \ No newline at end of file