move Type and DisconnectReason enums to a single file, preparing to do the same for all the scattered strings for operations

This commit is contained in:
Andy Janata 2012-01-12 13:35:53 -08:00
parent de1be43753
commit 45e5705e56
8 changed files with 49 additions and 46 deletions

View File

@ -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;
}
}
}

View File

@ -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<String, Object> data = new HashMap<String, Object>();
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<String, Object> data = new HashMap<String, Object>();
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<String, Object> masterData) {
public void broadcastToAll(final MessageType type, final HashMap<String, Object> masterData) {
broadcastToList(users.values(), type, masterData);
}
@ -88,7 +88,7 @@ public class ConnectedUsers {
* @param type
* @param masterData
*/
public void broadcastToList(final Collection<User> broadcastTo, final Type type,
public void broadcastToList(final Collection<User> broadcastTo, final MessageType type,
final HashMap<String, Object> masterData) {
synchronized (users) {
for (final User u : broadcastTo) {

View File

@ -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<String, Object> masterData) {
public void broadcastToPlayers(final MessageType type, final HashMap<String, Object> masterData) {
connectedUsers.broadcastToList(playersToUsers(), type, masterData);
}

View File

@ -2,18 +2,20 @@ package net.socialgamer.cah.data;
import java.util.Map;
import net.socialgamer.cah.Constants.MessageType;
public class QueuedMessage implements Comparable<QueuedMessage> {
private final Type messageType;
private final MessageType messageType;
private final Map<String, Object> data;
public QueuedMessage(final Type messageType, final Map<String, Object> data) {
public QueuedMessage(final MessageType messageType, final Map<String, Object> data) {
this.messageType = messageType;
this.data = data;
}
public Type getMessageType() {
public MessageType getMessageType() {
return messageType;
}
@ -29,26 +31,4 @@ public class QueuedMessage implements Comparable<QueuedMessage> {
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;
}
}
}

View File

@ -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<QueuedMessage> queuedMessages;

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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);