From 8da78ebac7ef2af8fbfc4b6da2d289288e009ff0 Mon Sep 17 00:00:00 2001 From: Andy Janata Date: Thu, 12 Jan 2012 20:05:39 -0800 Subject: [PATCH] move MessageType back inside QueuedMessage since the client doesn't need it make converter to produce js enums out of java enums make everything use the constants on both sides for ajax operation code and disconnect reason move the handler update out of the subpackage that it generates into make the enum update and handler update run at build --- .../Update Handlers [Builder].launch | 11 +++ .../Update JS Constants [Builder].launch | 11 +++ .project | 28 +++++++ .settings/org.eclipse.jdt.core.prefs | 6 +- WebContent/index.jsp | 1 + WebContent/js/cah.ajax.handlers.js | 2 +- WebContent/js/cah.ajax.js | 2 +- WebContent/js/cah.app.js | 8 +- WebContent/js/cah.constants.js | 22 ++++++ WebContent/js/cah.longpoll.handlers.js | 10 +-- src/net/socialgamer/cah/Constants.java | 38 ++++++---- src/net/socialgamer/cah/UpdateHandlerList.cmd | 1 + .../cah/{handlers => }/UpdateHandlerList.java | 2 +- src/net/socialgamer/cah/UpdateJsConstants.cmd | 1 + .../socialgamer/cah/UpdateJsConstants.java | 76 +++++++++++++++++++ .../socialgamer/cah/data/ConnectedUsers.java | 2 +- src/net/socialgamer/cah/data/Game.java | 2 +- .../socialgamer/cah/data/QueuedMessage.java | 22 +++++- .../socialgamer/cah/handlers/ChatHandler.java | 5 +- .../cah/handlers/FirstLoadHandler.java | 3 +- .../socialgamer/cah/handlers/Handlers.java | 1 - .../cah/handlers/LogoutHandler.java | 3 +- .../cah/handlers/NamesHandler.java | 3 +- .../cah/handlers/RegisterHandler.java | 3 +- .../socialgamer/cah/handlers/TestHandler.java | 18 ----- .../cah/handlers/UpdateHandlerList.cmd | 1 - test/net/socialgamer/cah/data/GameTest.java | 2 +- 27 files changed, 224 insertions(+), 60 deletions(-) create mode 100644 .externalToolBuilders/Update Handlers [Builder].launch create mode 100644 .externalToolBuilders/Update JS Constants [Builder].launch create mode 100644 WebContent/js/cah.constants.js create mode 100644 src/net/socialgamer/cah/UpdateHandlerList.cmd rename src/net/socialgamer/cah/{handlers => }/UpdateHandlerList.java (98%) create mode 100644 src/net/socialgamer/cah/UpdateJsConstants.cmd create mode 100644 src/net/socialgamer/cah/UpdateJsConstants.java delete mode 100644 src/net/socialgamer/cah/handlers/TestHandler.java delete mode 100644 src/net/socialgamer/cah/handlers/UpdateHandlerList.cmd diff --git a/.externalToolBuilders/Update Handlers [Builder].launch b/.externalToolBuilders/Update Handlers [Builder].launch new file mode 100644 index 0000000..3a3ddab --- /dev/null +++ b/.externalToolBuilders/Update Handlers [Builder].launch @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/.externalToolBuilders/Update JS Constants [Builder].launch b/.externalToolBuilders/Update JS Constants [Builder].launch new file mode 100644 index 0000000..d6c5f98 --- /dev/null +++ b/.externalToolBuilders/Update JS Constants [Builder].launch @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/.project b/.project index a541a3c..fa06ee8 100644 --- a/.project +++ b/.project @@ -5,6 +5,34 @@ + + org.eclipse.ui.externaltools.ExternalToolBuilder + auto,full,incremental, + + + LaunchConfigHandle + <project>/.externalToolBuilders/Update Handlers [Builder].launch + + + incclean + true + + + + + org.eclipse.ui.externaltools.ExternalToolBuilder + auto,full,incremental, + + + LaunchConfigHandle + <project>/.externalToolBuilders/Update JS Constants [Builder].launch + + + incclean + true + + + org.eclipse.wst.jsdt.core.javascriptValidator diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index c9744bc..4bacbc9 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,8 +1,12 @@ -#Fri Dec 09 20:46:02 PST 2011 +#Thu Jan 12 19:45:21 PST 2012 eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.source=1.6 diff --git a/WebContent/index.jsp b/WebContent/index.jsp index c5bd08f..4a26cfd 100644 --- a/WebContent/index.jsp +++ b/WebContent/index.jsp @@ -10,6 +10,7 @@ <%-- cah must be first, ajax must be before app. app probably has to be last. --%> <%-- TODO make this be dynamic with looking at the filesystem and using jquery --%> + diff --git a/WebContent/js/cah.ajax.handlers.js b/WebContent/js/cah.ajax.handlers.js index 8f78a2c..f733916 100644 --- a/WebContent/js/cah.ajax.handlers.js +++ b/WebContent/js/cah.ajax.handlers.js @@ -43,7 +43,7 @@ cah.ajax.ErrorHandlers.firstload = function(data) { cah.ajax.after_registered = function() { cah.log.debug("done registering"); // TODO once there are channels, this needs to specify the global channel - cah.Ajax.build("names").run(); + cah.Ajax.build(cah.$.AjaxOperation.NAMES).run(); cah.longpoll.longPoll(); }; diff --git a/WebContent/js/cah.ajax.js b/WebContent/js/cah.ajax.js index aaa04a0..c5c97cf 100644 --- a/WebContent/js/cah.ajax.js +++ b/WebContent/js/cah.ajax.js @@ -26,7 +26,7 @@ $(document).ready(function() { /** * Singleton instance for ajax utility. * - * @type {cah.ajax.lib} + * @type {cah.Ajax} */ cah.Ajax.instance = new cah.Ajax(); $.ajaxSetup({ diff --git a/WebContent/js/cah.app.js b/WebContent/js/cah.app.js index 6b25247..ece0152 100644 --- a/WebContent/js/cah.app.js +++ b/WebContent/js/cah.app.js @@ -6,7 +6,7 @@ $(document).ready(function() { // see if we already exist on the server so we can resume - cah.Ajax.build("firstload").run(); + cah.Ajax.build(cah.$.AjaxOperation.FIRST_LOAD).run(); // TODO see if we have a stored nickname somewhere $("#nicknameconfirm").click(nicknameconfirm_click); @@ -31,7 +31,7 @@ function nickbox_keyup(e) { function nicknameconfirm_click(e) { var nickname = $.trim($("#nickname").val()); - cah.Ajax.build("register").withNickname(nickname).run(); + cah.Ajax.build(cah.$.AjaxOperation.REGISTER).withNickname(nickname).run(); } function chat_keyup(e) { @@ -44,12 +44,12 @@ function chat_keyup(e) { function chatsubmit_click(e) { var text = $.trim($("#chat").val()); // TODO when I get multiple channels working, this needs to know active and pass it - cah.Ajax.build("chat").withMessage(text).run(); + cah.Ajax.build(cah.$.AjaxOperation.CHAT).withMessage(text).run(); cah.log.status("<" + cah.nickname + "> " + text); $("#chat").val(""); $("#chat").focus(); } function logout_click(e) { - cah.Ajax.build("logout").run(); + cah.Ajax.build(cah.$.AjaxOperation.LOG_OUT).run(); } diff --git a/WebContent/js/cah.constants.js b/WebContent/js/cah.constants.js new file mode 100644 index 0000000..3ed7df7 --- /dev/null +++ b/WebContent/js/cah.constants.js @@ -0,0 +1,22 @@ +// This file is automatically generated. Do not edit. + +cah.$ = {}; + +cah.$.AjaxOperation = function() { + // pass +}; +cah.$.AjaxOperation.prototype.dummy = undefined; +cah.$.AjaxOperation.FIRST_LOAD = "firstload"; +cah.$.AjaxOperation.LOG_OUT = "logout"; +cah.$.AjaxOperation.REGISTER = "register"; +cah.$.AjaxOperation.CHAT = "chat"; +cah.$.AjaxOperation.NAMES = "names"; + +cah.$.DisconnectReason = function() { + // pass +}; +cah.$.DisconnectReason.prototype.dummy = undefined; +cah.$.DisconnectReason.PING_TIMEOUT = "ping_timeout"; +cah.$.DisconnectReason.KICKED = "kicked"; +cah.$.DisconnectReason.MANUAL = "manual"; + diff --git a/WebContent/js/cah.longpoll.handlers.js b/WebContent/js/cah.longpoll.handlers.js index 6a4121e..3c2e2e8 100644 --- a/WebContent/js/cah.longpoll.handlers.js +++ b/WebContent/js/cah.longpoll.handlers.js @@ -22,15 +22,15 @@ cah.longpoll.EventHandlers.player_leave = function(data) { var friendly_reason = "Leaving"; // see net.socialgamer.cah.data.User.DisconnectReason switch (data.reason) { - case "MANUAL": + case cah.$.DisconnectReason.KICKED: + friendly_reason = "Kicked by server"; + break; + case cah.$.DisconnectReason.MANUAL: friendly_reason = "Leaving"; break; - case "PING_TIMEOUT": + case cah.$.DisconnectReason.PING_TIMEOUT: friendly_reason = "Ping timeout"; break; - case "KICKED": - friendly_reason = "Kicked by server"; - break; } cah.log.status(data.nickname + " has disconnected (" + friendly_reason + ")."); }; diff --git a/src/net/socialgamer/cah/Constants.java b/src/net/socialgamer/cah/Constants.java index c03b21c..86da315 100644 --- a/src/net/socialgamer/cah/Constants.java +++ b/src/net/socialgamer/cah/Constants.java @@ -2,26 +2,32 @@ package net.socialgamer.cah; public class Constants { public enum DisconnectReason { - MANUAL, PING_TIMEOUT, KICKED - } + KICKED("kicked"), MANUAL("manual"), PING_TIMEOUT("ping_timeout"); - /** - * Types of messages that can be queued. The numerical value is the priority that this message - * should be delivered (lower = more important) compared to other queued messages. - * - * @author ajanata - */ - public enum MessageType { - PLAYER_EVENT(3), GAME_PLAYER_EVENT(4), CHAT(5); + private final String reason; - private final int weight; - - MessageType(final int weight) { - this.weight = weight; + DisconnectReason(final String reason) { + this.reason = reason; } - public int getWeight() { - return weight; + @Override + public String toString() { + return reason; + } + } + + public enum AjaxOperation { + CHAT("chat"), FIRST_LOAD("firstload"), LOG_OUT("logout"), NAMES("names"), REGISTER("register"); + + private final String op; + + AjaxOperation(final String op) { + this.op = op; + } + + @Override + public String toString() { + return op; } } } diff --git a/src/net/socialgamer/cah/UpdateHandlerList.cmd b/src/net/socialgamer/cah/UpdateHandlerList.cmd new file mode 100644 index 0000000..982569b --- /dev/null +++ b/src/net/socialgamer/cah/UpdateHandlerList.cmd @@ -0,0 +1 @@ +java -cp build\classes net.socialgamer.cah.UpdateHandlerList \ No newline at end of file diff --git a/src/net/socialgamer/cah/handlers/UpdateHandlerList.java b/src/net/socialgamer/cah/UpdateHandlerList.java similarity index 98% rename from src/net/socialgamer/cah/handlers/UpdateHandlerList.java rename to src/net/socialgamer/cah/UpdateHandlerList.java index 593d118..f232cf3 100644 --- a/src/net/socialgamer/cah/handlers/UpdateHandlerList.java +++ b/src/net/socialgamer/cah/UpdateHandlerList.java @@ -1,4 +1,4 @@ -package net.socialgamer.cah.handlers; +package net.socialgamer.cah; import java.io.File; import java.io.PrintWriter; diff --git a/src/net/socialgamer/cah/UpdateJsConstants.cmd b/src/net/socialgamer/cah/UpdateJsConstants.cmd new file mode 100644 index 0000000..77678bc --- /dev/null +++ b/src/net/socialgamer/cah/UpdateJsConstants.cmd @@ -0,0 +1 @@ +java -cp build\classes net.socialgamer.cah.UpdateJsConstants \ No newline at end of file diff --git a/src/net/socialgamer/cah/UpdateJsConstants.java b/src/net/socialgamer/cah/UpdateJsConstants.java new file mode 100644 index 0000000..2c4a7dc --- /dev/null +++ b/src/net/socialgamer/cah/UpdateJsConstants.java @@ -0,0 +1,76 @@ +package net.socialgamer.cah; + +import java.io.File; +import java.io.PrintWriter; +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; + + +public class UpdateJsConstants { + + private static final String enumHeaderFmt = "cah.$.%s = function() {\r\n // pass\r\n};\r\n"; + private static final String enumDummyFmt = "cah.$.%s.prototype.dummy = undefined;\r\n"; + private static final String enumValueFmt = "cah.$.%s.%s = \"%s\";\r\n"; + + /** + * @param args + */ + @SuppressWarnings("rawtypes") + public static void main(final String[] args) throws Exception { + final String dir = "WebContent/js/"; + final File outFile = new File(dir + "cah.constants.js"); + assert outFile.canWrite(); + assert outFile.delete(); + assert outFile.createNewFile(); + final PrintWriter writer = new PrintWriter(outFile); + + writer.println("// This file is automatically generated. Do not edit."); + writer.println(); + writer.println("cah.$ = {};"); + writer.println(); + + final Class[] classes = Constants.class.getClasses(); + for (final Class c : classes) { + final String cName = c.getName().split("\\$")[1]; + System.out.println(cName); + writer.format(enumHeaderFmt, cName); + writer.format(enumDummyFmt, cName); + final Map values = getEnumValues(c); + for (final String key : values.keySet()) { + final String value = values.get(key); + writer.format(enumValueFmt, cName, key, value); + } + writer.println(); + } + writer.flush(); + writer.close(); + } + + @SuppressWarnings("rawtypes") + private static Map getEnumValues(final Class enumClass) + throws IllegalArgumentException, IllegalAccessException { + if (!enumClass.isEnum()) { + throw new IllegalArgumentException(enumClass.getName() + " is not an enum"); + } + + final Field[] flds = enumClass.getDeclaredFields(); + final HashMap enumMap = new HashMap(); + for (final Field f : flds) { + if (f.isEnumConstant()) { + enumMap.put(f.getName(), f.get(null).toString()); + } + } + return enumMap; + } +} + +////Automatically generated file. Do not edit! +// +//cah.$ = {}; +// +//cah.$.DisconnectReason = {}; +//cah.$.DisconnectReason.prototype.dummy = undefined; +//cah.$.DisconnectReason.KICKED = "kicked"; +//cah.$.DisconnectReason.MANUAL = "manual"; +//cah.$.DisconnectReason.PING_TIMEOUT = "ping_timeout"; diff --git a/src/net/socialgamer/cah/data/ConnectedUsers.java b/src/net/socialgamer/cah/data/ConnectedUsers.java index e405c04..673a5ae 100644 --- a/src/net/socialgamer/cah/data/ConnectedUsers.java +++ b/src/net/socialgamer/cah/data/ConnectedUsers.java @@ -7,7 +7,7 @@ import java.util.Iterator; import java.util.Map; import net.socialgamer.cah.Constants.DisconnectReason; -import net.socialgamer.cah.Constants.MessageType; +import net.socialgamer.cah.data.QueuedMessage.MessageType; /** diff --git a/src/net/socialgamer/cah/data/Game.java b/src/net/socialgamer/cah/data/Game.java index 430e479..8b1e5e2 100644 --- a/src/net/socialgamer/cah/data/Game.java +++ b/src/net/socialgamer/cah/data/Game.java @@ -5,7 +5,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; -import net.socialgamer.cah.Constants.MessageType; +import net.socialgamer.cah.data.QueuedMessage.MessageType; public class Game { diff --git a/src/net/socialgamer/cah/data/QueuedMessage.java b/src/net/socialgamer/cah/data/QueuedMessage.java index b9d9def..d9a12db 100644 --- a/src/net/socialgamer/cah/data/QueuedMessage.java +++ b/src/net/socialgamer/cah/data/QueuedMessage.java @@ -2,8 +2,6 @@ package net.socialgamer.cah.data; import java.util.Map; -import net.socialgamer.cah.Constants.MessageType; - public class QueuedMessage implements Comparable { @@ -31,4 +29,24 @@ public class QueuedMessage implements Comparable { public int compareTo(final QueuedMessage qm) { return this.messageType.getWeight() - qm.messageType.getWeight(); } + + /** + * Types of messages that can be queued. The numerical value is the priority that this message + * should be delivered (lower = more important) compared to other queued messages. + * + * @author ajanata + */ + public enum MessageType { + PLAYER_EVENT(3), GAME_PLAYER_EVENT(4), CHAT(5); + + private final int weight; + + MessageType(final int weight) { + this.weight = weight; + } + + public int getWeight() { + return weight; + } + } } diff --git a/src/net/socialgamer/cah/handlers/ChatHandler.java b/src/net/socialgamer/cah/handlers/ChatHandler.java index 54b31af..a192fba 100644 --- a/src/net/socialgamer/cah/handlers/ChatHandler.java +++ b/src/net/socialgamer/cah/handlers/ChatHandler.java @@ -5,9 +5,10 @@ import java.util.Map; import javax.servlet.http.HttpSession; -import net.socialgamer.cah.Constants.MessageType; +import net.socialgamer.cah.Constants.AjaxOperation; import net.socialgamer.cah.Server; import net.socialgamer.cah.data.ConnectedUsers; +import net.socialgamer.cah.data.QueuedMessage.MessageType; import net.socialgamer.cah.data.User; import com.google.inject.Inject; @@ -15,7 +16,7 @@ import com.google.inject.Inject; public class ChatHandler extends Handler { - public static final String OP = "chat"; + public static final String OP = AjaxOperation.CHAT.toString(); private final ConnectedUsers users; diff --git a/src/net/socialgamer/cah/handlers/FirstLoadHandler.java b/src/net/socialgamer/cah/handlers/FirstLoadHandler.java index 74851e4..cc3d60e 100644 --- a/src/net/socialgamer/cah/handlers/FirstLoadHandler.java +++ b/src/net/socialgamer/cah/handlers/FirstLoadHandler.java @@ -5,6 +5,7 @@ import java.util.Map; import javax.servlet.http.HttpSession; +import net.socialgamer.cah.Constants.AjaxOperation; import net.socialgamer.cah.data.User; @@ -17,7 +18,7 @@ import net.socialgamer.cah.data.User; */ public class FirstLoadHandler extends Handler { - public static final String OP = "firstload"; + public static final String OP = AjaxOperation.FIRST_LOAD.toString(); @Override public Map handle(final Map parameters, diff --git a/src/net/socialgamer/cah/handlers/Handlers.java b/src/net/socialgamer/cah/handlers/Handlers.java index 85e33a8..36c0cce 100644 --- a/src/net/socialgamer/cah/handlers/Handlers.java +++ b/src/net/socialgamer/cah/handlers/Handlers.java @@ -15,6 +15,5 @@ public class Handlers { LIST.put(LogoutHandler.OP, LogoutHandler.class); LIST.put(NamesHandler.OP, NamesHandler.class); LIST.put(RegisterHandler.OP, RegisterHandler.class); - LIST.put(TestHandler.OP, TestHandler.class); } } diff --git a/src/net/socialgamer/cah/handlers/LogoutHandler.java b/src/net/socialgamer/cah/handlers/LogoutHandler.java index 5e6bbae..efcf542 100644 --- a/src/net/socialgamer/cah/handlers/LogoutHandler.java +++ b/src/net/socialgamer/cah/handlers/LogoutHandler.java @@ -5,6 +5,7 @@ import java.util.Map; import javax.servlet.http.HttpSession; +import net.socialgamer.cah.Constants.AjaxOperation; import net.socialgamer.cah.Constants.DisconnectReason; import net.socialgamer.cah.Server; import net.socialgamer.cah.data.ConnectedUsers; @@ -15,7 +16,7 @@ import com.google.inject.Inject; public class LogoutHandler extends Handler { - public final static String OP = "logout"; + public final static String OP = AjaxOperation.LOG_OUT.toString(); private final ConnectedUsers users; diff --git a/src/net/socialgamer/cah/handlers/NamesHandler.java b/src/net/socialgamer/cah/handlers/NamesHandler.java index 4256b4c..cd0668b 100644 --- a/src/net/socialgamer/cah/handlers/NamesHandler.java +++ b/src/net/socialgamer/cah/handlers/NamesHandler.java @@ -8,6 +8,7 @@ import java.util.Map; import javax.servlet.http.HttpSession; +import net.socialgamer.cah.Constants.AjaxOperation; import net.socialgamer.cah.Server; import net.socialgamer.cah.data.ConnectedUsers; import net.socialgamer.cah.data.User; @@ -17,7 +18,7 @@ import com.google.inject.Inject; public class NamesHandler extends Handler { - public static final String OP = "names"; + public static final String OP = AjaxOperation.NAMES.toString(); private final ConnectedUsers users; diff --git a/src/net/socialgamer/cah/handlers/RegisterHandler.java b/src/net/socialgamer/cah/handlers/RegisterHandler.java index f206a1d..e4378a7 100644 --- a/src/net/socialgamer/cah/handlers/RegisterHandler.java +++ b/src/net/socialgamer/cah/handlers/RegisterHandler.java @@ -6,6 +6,7 @@ import java.util.regex.Pattern; import javax.servlet.http.HttpSession; +import net.socialgamer.cah.Constants.AjaxOperation; import net.socialgamer.cah.Server; import net.socialgamer.cah.data.ConnectedUsers; import net.socialgamer.cah.data.User; @@ -15,7 +16,7 @@ import com.google.inject.Inject; public class RegisterHandler extends Handler { - public static final String OP = "register"; + public static final String OP = AjaxOperation.REGISTER.toString(); private static final Pattern validName = Pattern.compile("[a-zA-Z_][a-zA-Z0-9_]{2,29}"); diff --git a/src/net/socialgamer/cah/handlers/TestHandler.java b/src/net/socialgamer/cah/handlers/TestHandler.java deleted file mode 100644 index 574310a..0000000 --- a/src/net/socialgamer/cah/handlers/TestHandler.java +++ /dev/null @@ -1,18 +0,0 @@ -package net.socialgamer.cah.handlers; - -import java.util.Map; - -import javax.servlet.http.HttpSession; - - -public class TestHandler extends Handler { - - public static final String OP = "test"; - - @Override - public Map handle(final Map parameters, - final HttpSession session) { - // TODO Auto-generated method stub - return null; - } -} diff --git a/src/net/socialgamer/cah/handlers/UpdateHandlerList.cmd b/src/net/socialgamer/cah/handlers/UpdateHandlerList.cmd deleted file mode 100644 index a07b825..0000000 --- a/src/net/socialgamer/cah/handlers/UpdateHandlerList.cmd +++ /dev/null @@ -1 +0,0 @@ -java -cp build\classes net.socialgamer.cah.handlers.UpdateHandlerList \ No newline at end of file diff --git a/test/net/socialgamer/cah/data/GameTest.java b/test/net/socialgamer/cah/data/GameTest.java index 265d703..0396864 100644 --- a/test/net/socialgamer/cah/data/GameTest.java +++ b/test/net/socialgamer/cah/data/GameTest.java @@ -13,7 +13,7 @@ import static org.junit.Assert.assertTrue; import java.util.Collection; import java.util.HashMap; -import net.socialgamer.cah.Constants.MessageType; +import net.socialgamer.cah.data.QueuedMessage.MessageType; import org.junit.Before; import org.junit.Test;