From 2a6b81e4dd9fa96f5af5cf550cad4695b53387d1 Mon Sep 17 00:00:00 2001 From: uecasm Date: Thu, 3 Oct 2013 00:28:16 +1300 Subject: [PATCH] Add /me command to support emotes. --- WebContent/js/cah.ajax.builder.js | 11 +++++++++++ WebContent/js/cah.app.js | 13 +++++++++++-- WebContent/js/cah.constants.js | 2 ++ WebContent/js/cah.longpoll.handlers.js | 8 ++++++-- src/net/socialgamer/cah/Constants.java | 3 +++ src/net/socialgamer/cah/handlers/ChatHandler.java | 5 +++++ .../socialgamer/cah/handlers/GameChatHandler.java | 3 +++ 7 files changed, 41 insertions(+), 4 deletions(-) diff --git a/WebContent/js/cah.ajax.builder.js b/WebContent/js/cah.ajax.builder.js index bcc9bb7..0b02f1f 100644 --- a/WebContent/js/cah.ajax.builder.js +++ b/WebContent/js/cah.ajax.builder.js @@ -199,6 +199,17 @@ cah.ajax.Builder.prototype.withWall = function(wall) { return this; }; +/** + * @param {boolean} + * emote Whether or not this is an emote + * @returns {cah.ajax.Builder} This object. + */ +cah.ajax.Builder.prototype.withEmote = function(emote) { + this.assertNotExecuted_(); + this.data[cah.$.AjaxRequest.EMOTE] = emote; + return this; +}; + /** * Assert that the request from this builder has not already run. Throws an exception if it has. * diff --git a/WebContent/js/cah.app.js b/WebContent/js/cah.app.js index 5d1716c..4940d9d 100644 --- a/WebContent/js/cah.app.js +++ b/WebContent/js/cah.app.js @@ -146,13 +146,22 @@ function chatsubmit_click(game_id, parent_element) { // TODO support an /ignore command case '': if (game_id !== null) { - ajax = cah.Ajax.build(cah.$.AjaxOperation.GAME_CHAT).withGameId(game_id); + ajax = cah.Ajax.build(cah.$.AjaxOperation.GAME_CHAT).withGameId(game_id).withEmote(false); } else { - ajax = cah.Ajax.build(cah.$.AjaxOperation.CHAT); + ajax = cah.Ajax.build(cah.$.AjaxOperation.CHAT).withEmote(false); } ajax = ajax.withMessage(text); cah.log.status_with_game(game_id, "<" + cah.nickname + "> " + text); break; + case 'me': + if (game_id !== null) { + ajax = cah.Ajax.build(cah.$.AjaxOperation.GAME_CHAT).withGameId(game_id).withEmote(true); + } else { + ajax = cah.Ajax.build(cah.$.AjaxOperation.CHAT).withEmote(true); + } + ajax = ajax.withMessage(text); + cah.log.status_with_game(game_id, "* " + cah.nickname + " " + text); + break; case 'wall': ajax = cah.Ajax.build(cah.$.AjaxOperation.CHAT).withWall(true).withMessage(text); break; diff --git a/WebContent/js/cah.constants.js b/WebContent/js/cah.constants.js index 01e8c1c..c2b64e3 100644 --- a/WebContent/js/cah.constants.js +++ b/WebContent/js/cah.constants.js @@ -35,6 +35,7 @@ cah.$.AjaxRequest.MESSAGE = "m"; cah.$.AjaxRequest.CARD_ID = "cid"; cah.$.AjaxRequest.USE_TIMER = "ut"; cah.$.AjaxRequest.GAME_ID = "gid"; +cah.$.AjaxRequest.EMOTE = "me"; cah.$.AjaxRequest.CARD_SETS = "css"; cah.$.AjaxRequest.SERIAL = "s"; cah.$.AjaxRequest.PLAYER_LIMIT = "pL"; @@ -269,6 +270,7 @@ cah.$.LongPollResponse.WALL = "wall"; cah.$.LongPollResponse.WHITE_CARDS = "wc"; cah.$.LongPollResponse.REASON = "qr"; cah.$.LongPollResponse.GAME_ID = "gid"; +cah.$.LongPollResponse.EMOTE = "me"; cah.$.LongPollResponse.HAND = "h"; cah.$.LongPollResponse.INTERMISSION = "i"; cah.$.LongPollResponse.PLAYER_INFO = "pi"; diff --git a/WebContent/js/cah.longpoll.handlers.js b/WebContent/js/cah.longpoll.handlers.js index eac8491..c8a9136 100644 --- a/WebContent/js/cah.longpoll.handlers.js +++ b/WebContent/js/cah.longpoll.handlers.js @@ -107,8 +107,12 @@ cah.longpoll.EventHandlers[cah.$.LongPollEvent.CHAT] = function(data) { // don't display our own chat if (from != cah.nickname && show) { - cah.log.status_with_game(game, "<" + data[cah.$.LongPollResponse.FROM] + "> " - + data[cah.$.LongPollResponse.MESSAGE], clazz); + var message = data[cah.$.LongPollResponse.MESSAGE]; + if (data[cah.$.LongPollResponse.EMOTE]) { + cah.log.status_with_game(game, "* " + from + " " + message, clazz); + } else { + cah.log.status_with_game(game, "<" + from + "> " + message, clazz); + } } } }; diff --git a/src/net/socialgamer/cah/Constants.java b/src/net/socialgamer/cah/Constants.java index 12253de..0ba6013 100644 --- a/src/net/socialgamer/cah/Constants.java +++ b/src/net/socialgamer/cah/Constants.java @@ -202,6 +202,7 @@ public class Constants { public enum AjaxRequest { CARD_ID("cid"), CARD_SETS("css"), + EMOTE("me"), GAME_ID("gid"), MESSAGE("m"), NICKNAME("n"), @@ -410,6 +411,8 @@ public class Constants { @DuplicationAllowed BLACK_CARD(AjaxResponse.BLACK_CARD), @DuplicationAllowed + EMOTE(AjaxRequest.EMOTE), + @DuplicationAllowed ERROR(AjaxResponse.ERROR), @DuplicationAllowed ERROR_CODE(AjaxResponse.ERROR_CODE), diff --git a/src/net/socialgamer/cah/handlers/ChatHandler.java b/src/net/socialgamer/cah/handlers/ChatHandler.java index d85b435..12d86e9 100644 --- a/src/net/socialgamer/cah/handlers/ChatHandler.java +++ b/src/net/socialgamer/cah/handlers/ChatHandler.java @@ -69,6 +69,8 @@ public class ChatHandler extends Handler { assert (user != null); final boolean wall = request.getParameter(AjaxRequest.WALL) != null && Boolean.valueOf(request.getParameter(AjaxRequest.WALL)); + final boolean emote = request.getParameter(AjaxRequest.EMOTE) != null + && Boolean.valueOf(request.getParameter(AjaxRequest.EMOTE)); if (request.getParameter(AjaxRequest.MESSAGE) == null) { return error(ErrorCode.NO_MSG_SPECIFIED); @@ -103,6 +105,9 @@ public class ChatHandler extends Handler { if (wall) { broadcastData.put(LongPollResponse.WALL, true); } + if (emote) { + broadcastData.put(LongPollResponse.EMOTE, true); + } users.broadcastToAll(MessageType.CHAT, broadcastData); } } diff --git a/src/net/socialgamer/cah/handlers/GameChatHandler.java b/src/net/socialgamer/cah/handlers/GameChatHandler.java index 58575b8..3256e1b 100644 --- a/src/net/socialgamer/cah/handlers/GameChatHandler.java +++ b/src/net/socialgamer/cah/handlers/GameChatHandler.java @@ -62,6 +62,8 @@ public class GameChatHandler extends GameWithPlayerHandler { public Map handleWithUserInGame(final RequestWrapper request, final HttpSession session, final User user, final Game game) { final Map data = new HashMap(); + final boolean emote = request.getParameter(AjaxRequest.EMOTE) != null + && Boolean.valueOf(request.getParameter(AjaxRequest.EMOTE)); if (request.getParameter(AjaxRequest.MESSAGE) == null) { return error(ErrorCode.NO_MSG_SPECIFIED); @@ -90,6 +92,7 @@ public class GameChatHandler extends GameWithPlayerHandler { broadcastData.put(LongPollResponse.MESSAGE, message); broadcastData.put(LongPollResponse.FROM_ADMIN, user.isAdmin()); broadcastData.put(LongPollResponse.GAME_ID, game.getId()); + broadcastData.put(LongPollResponse.EMOTE, emote); game.broadcastToPlayers(MessageType.CHAT, broadcastData); } }