From 45e5705e561f26f05b74de0ceeb4245ee43a0b02 Mon Sep 17 00:00:00 2001 From: Andy Janata Date: Thu, 12 Jan 2012 13:35:53 -0800 Subject: [PATCH] move Type and DisconnectReason enums to a single file, preparing to do the same for all the scattered strings for operations --- src/net/socialgamer/cah/Constants.java | 27 +++++++++++++++++ .../socialgamer/cah/data/ConnectedUsers.java | 16 +++++----- src/net/socialgamer/cah/data/Game.java | 8 ++--- .../socialgamer/cah/data/QueuedMessage.java | 30 ++++--------------- src/net/socialgamer/cah/data/User.java | 4 --- .../socialgamer/cah/handlers/ChatHandler.java | 4 +-- .../cah/handlers/LogoutHandler.java | 2 +- test/net/socialgamer/cah/data/GameTest.java | 4 +-- 8 files changed, 49 insertions(+), 46 deletions(-) create mode 100644 src/net/socialgamer/cah/Constants.java diff --git a/src/net/socialgamer/cah/Constants.java b/src/net/socialgamer/cah/Constants.java new file mode 100644 index 0000000..c03b21c --- /dev/null +++ b/src/net/socialgamer/cah/Constants.java @@ -0,0 +1,27 @@ +package net.socialgamer.cah; + +public class Constants { + public enum DisconnectReason { + MANUAL, PING_TIMEOUT, KICKED + } + + /** + * 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/data/ConnectedUsers.java b/src/net/socialgamer/cah/data/ConnectedUsers.java index 0cc7175..e405c04 100644 --- a/src/net/socialgamer/cah/data/ConnectedUsers.java +++ b/src/net/socialgamer/cah/data/ConnectedUsers.java @@ -6,8 +6,8 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import net.socialgamer.cah.data.QueuedMessage.Type; -import net.socialgamer.cah.data.User.DisconnectReason; +import net.socialgamer.cah.Constants.DisconnectReason; +import net.socialgamer.cah.Constants.MessageType; /** @@ -37,24 +37,24 @@ public class ConnectedUsers { final HashMap data = new HashMap(); data.put("event", "new_player"); data.put("nickname", user.getNickname()); - broadcastToAll(Type.PLAYER_EVENT, data); + broadcastToAll(MessageType.PLAYER_EVENT, data); } } - public void removeUser(final User user, final User.DisconnectReason reason) { + public void removeUser(final User user, final DisconnectReason reason) { synchronized (users) { users.remove(user.getNickname()); notifyRemoveUser(user, reason); } } - private void notifyRemoveUser(final User user, final User.DisconnectReason reason) { + private void notifyRemoveUser(final User user, final DisconnectReason reason) { // We might also have to tell games about this directly, probably with a listener system. final HashMap data = new HashMap(); data.put("event", "player_leave"); data.put("nickname", user.getNickname()); data.put("reason", reason.toString()); - broadcastToAll(Type.PLAYER_EVENT, data); + broadcastToAll(MessageType.PLAYER_EVENT, data); } public void checkForPingTimeouts() { @@ -77,7 +77,7 @@ public class ConnectedUsers { * @param type * @param masterData */ - public void broadcastToAll(final Type type, final HashMap masterData) { + public void broadcastToAll(final MessageType type, final HashMap masterData) { broadcastToList(users.values(), type, masterData); } @@ -88,7 +88,7 @@ public class ConnectedUsers { * @param type * @param masterData */ - public void broadcastToList(final Collection broadcastTo, final Type type, + public void broadcastToList(final Collection broadcastTo, final MessageType type, final HashMap masterData) { synchronized (users) { for (final User u : broadcastTo) { diff --git a/src/net/socialgamer/cah/data/Game.java b/src/net/socialgamer/cah/data/Game.java index 25c8264..430e479 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.data.QueuedMessage.Type; +import net.socialgamer.cah.Constants.MessageType; public class Game { @@ -41,7 +41,7 @@ public class Game { data.put("event", "game_player_join"); data.put("game_id", id); data.put("nickname", user.getNickname()); - broadcastToPlayers(Type.GAME_PLAYER_EVENT, data); + broadcastToPlayers(MessageType.GAME_PLAYER_EVENT, data); } /** @@ -60,7 +60,7 @@ public class Game { data.put("event", "game_player_leave"); data.put("game_id", id); data.put("nickname", user.getNickname()); - broadcastToPlayers(Type.GAME_PLAYER_EVENT, data); + broadcastToPlayers(MessageType.GAME_PLAYER_EVENT, data); if (host == player) { if (players.size() > 0) { host = players.get(0); @@ -75,7 +75,7 @@ public class Game { } } - public void broadcastToPlayers(final Type type, final HashMap masterData) { + public void broadcastToPlayers(final MessageType type, final HashMap masterData) { connectedUsers.broadcastToList(playersToUsers(), type, masterData); } diff --git a/src/net/socialgamer/cah/data/QueuedMessage.java b/src/net/socialgamer/cah/data/QueuedMessage.java index 0db2345..b9d9def 100644 --- a/src/net/socialgamer/cah/data/QueuedMessage.java +++ b/src/net/socialgamer/cah/data/QueuedMessage.java @@ -2,18 +2,20 @@ package net.socialgamer.cah.data; import java.util.Map; +import net.socialgamer.cah.Constants.MessageType; + public class QueuedMessage implements Comparable { - private final Type messageType; + private final MessageType messageType; private final Map data; - public QueuedMessage(final Type messageType, final Map data) { + public QueuedMessage(final MessageType messageType, final Map data) { this.messageType = messageType; this.data = data; } - public Type getMessageType() { + public MessageType getMessageType() { return messageType; } @@ -29,26 +31,4 @@ 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 Type { - PLAYER_EVENT(3), GAME_PLAYER_EVENT(4), CHAT(5); - - private final int weight; - - Type(final int weight) { - this.weight = weight; - } - - public int getWeight() { - return weight; - } - } - } diff --git a/src/net/socialgamer/cah/data/User.java b/src/net/socialgamer/cah/data/User.java index 73e9d98..e0d23f3 100644 --- a/src/net/socialgamer/cah/data/User.java +++ b/src/net/socialgamer/cah/data/User.java @@ -7,10 +7,6 @@ import java.util.concurrent.PriorityBlockingQueue; public class User { - public enum DisconnectReason { - MANUAL, PING_TIMEOUT, KICKED - } - private final String nickname; private final PriorityBlockingQueue queuedMessages; diff --git a/src/net/socialgamer/cah/handlers/ChatHandler.java b/src/net/socialgamer/cah/handlers/ChatHandler.java index df447a1..54b31af 100644 --- a/src/net/socialgamer/cah/handlers/ChatHandler.java +++ b/src/net/socialgamer/cah/handlers/ChatHandler.java @@ -5,9 +5,9 @@ import java.util.Map; import javax.servlet.http.HttpSession; +import net.socialgamer.cah.Constants.MessageType; import net.socialgamer.cah.Server; import net.socialgamer.cah.data.ConnectedUsers; -import net.socialgamer.cah.data.QueuedMessage.Type; import net.socialgamer.cah.data.User; import com.google.inject.Inject; @@ -45,7 +45,7 @@ public class ChatHandler extends Handler { broadcastData.put("message", message); // TODO once there are multiple chat channels, put the destination here // TODO once there are games and they have their own chat, make it only send to participants - users.broadcastToAll(Type.CHAT, broadcastData); + users.broadcastToAll(MessageType.CHAT, broadcastData); } } diff --git a/src/net/socialgamer/cah/handlers/LogoutHandler.java b/src/net/socialgamer/cah/handlers/LogoutHandler.java index f508bbe..5e6bbae 100644 --- a/src/net/socialgamer/cah/handlers/LogoutHandler.java +++ b/src/net/socialgamer/cah/handlers/LogoutHandler.java @@ -5,10 +5,10 @@ import java.util.Map; import javax.servlet.http.HttpSession; +import net.socialgamer.cah.Constants.DisconnectReason; import net.socialgamer.cah.Server; import net.socialgamer.cah.data.ConnectedUsers; import net.socialgamer.cah.data.User; -import net.socialgamer.cah.data.User.DisconnectReason; import com.google.inject.Inject; diff --git a/test/net/socialgamer/cah/data/GameTest.java b/test/net/socialgamer/cah/data/GameTest.java index 3d22c72..265d703 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.data.QueuedMessage.Type; +import net.socialgamer.cah.Constants.MessageType; import org.junit.Before; import org.junit.Test; @@ -33,7 +33,7 @@ public class GameTest { @SuppressWarnings("unchecked") @Test public void testRemovePlayer() { - cmMock.broadcastToList(anyObject(Collection.class), eq(Type.GAME_PLAYER_EVENT), + cmMock.broadcastToList(anyObject(Collection.class), eq(MessageType.GAME_PLAYER_EVENT), anyObject(HashMap.class)); expectLastCall().times(4); replay(cmMock);