Introducing possibility of no-spectator games.

This commit is contained in:
marcins78@gmail.com
2012-03-14 15:50:27 +00:00
parent 20c499a57b
commit 21d9ec5d4c
9 changed files with 75 additions and 20 deletions

View File

@@ -13,12 +13,21 @@ public class ChatRoomMediator {
private Map<String, GatheringChatRoomListener> _listeners = new HashMap<String, GatheringChatRoomListener>();
private int _channelInactivityTimeoutPeriod = 1000 * 10; // 10 seconds
private Set<String> _allowedPlayers;
public ChatRoomMediator(int secondsTimeoutPeriod) {
this(secondsTimeoutPeriod, null);
}
public ChatRoomMediator(int secondsTimeoutPeriod, Set<String> allowedPlayers) {
_allowedPlayers = allowedPlayers;
_channelInactivityTimeoutPeriod = 1000 * secondsTimeoutPeriod;
}
public synchronized List<ChatMessage> joinUser(String playerId) {
if (_allowedPlayers != null && !_allowedPlayers.contains(playerId))
return null;
GatheringChatRoomListener value = new GatheringChatRoomListener();
_listeners.put(playerId, value);
_chatRoom.joinChatRoom(playerId, value);
@@ -26,6 +35,9 @@ public class ChatRoomMediator {
}
public synchronized List<ChatMessage> getPendingMessages(String playerId) {
if (_allowedPlayers != null && !_allowedPlayers.contains(playerId))
return null;
GatheringChatRoomListener gatheringChatRoomListener = _listeners.get(playerId);
if (gatheringChatRoomListener == null)
return null;
@@ -37,8 +49,12 @@ public class ChatRoomMediator {
_listeners.remove(playerId);
}
public synchronized void sendMessage(String playerId, String message) {
public synchronized boolean sendMessage(String playerId, String message) {
if (_allowedPlayers != null && !_allowedPlayers.contains(playerId))
return false;
_chatRoom.postMessage(playerId, message);
return true;
}
public synchronized void cleanup() {

View File

@@ -3,6 +3,7 @@ package com.gempukku.lotro.chat;
import com.gempukku.lotro.AbstractServer;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class ChatServer extends AbstractServer {
@@ -15,6 +16,13 @@ public class ChatServer extends AbstractServer {
return chatRoom;
}
public ChatRoomMediator createPrivateChatRoom(String name, Set<String> allowedUsers, int secondsTimeoutPeriod) {
ChatRoomMediator chatRoom = new ChatRoomMediator(secondsTimeoutPeriod, allowedUsers);
chatRoom.sendMessage("System", "Welcome to private room: " + name);
_chatRooms.put(name, chatRoom);
return chatRoom;
}
public ChatRoomMediator getChatRoom(String name) {
return _chatRooms.get(name);
}

View File

@@ -27,6 +27,7 @@ public class LotroGameMediator {
private Set<String> _playersPlaying = new HashSet<String>();
private int _maxSecondsForGamePerPlayer = 60 * 80; // 80 minutes
private boolean _noSpectators;
// private final int _maxSecondsForGamePerPlayer = 60 * 40; // 40 minutes
private final int _channelInactivityTimeoutPeriod = 1000 * 60 * 5; // 5 minutes
private final int _playerDecisionTimeoutPeriod = 1000 * 60 * 10; // 10 minutes
@@ -35,8 +36,10 @@ public class LotroGameMediator {
private ReentrantReadWriteLock.ReadLock _readLock = _lock.readLock();
private ReentrantReadWriteLock.WriteLock _writeLock = _lock.writeLock();
public LotroGameMediator(LotroFormat lotroFormat, LotroGameParticipant[] participants, LotroCardBlueprintLibrary library, int maxSecondsForGamePerPlayer) {
public LotroGameMediator(LotroFormat lotroFormat, LotroGameParticipant[] participants, LotroCardBlueprintLibrary library, int maxSecondsForGamePerPlayer,
boolean noSpectators) {
_maxSecondsForGamePerPlayer = maxSecondsForGamePerPlayer;
_noSpectators = noSpectators;
if (participants.length < 1)
throw new IllegalArgumentException("Game can't have less than one participant");
@@ -54,6 +57,10 @@ public class LotroGameMediator {
_userFeedback.setGame(_lotroGame);
}
public boolean isNoSpectators() {
return _noSpectators;
}
public void setPlayerAutoPassSettings(String playerId, Set<Phase> phases) {
if (_playersPlaying.contains(playerId)) {
_lotroGame.setPlayerAutoPassSettings(playerId, phases);
@@ -280,8 +287,11 @@ public class LotroGameMediator {
}
}
public void processCommunicationChannel(Player player, ParticipantCommunicationVisitor visitor) {
public boolean processCommunicationChannel(Player player, ParticipantCommunicationVisitor visitor) {
String playerName = player.getName();
if (_noSpectators && !_playersPlaying.contains(playerName))
return false;
_readLock.lock();
try {
GatheringParticipantCommunicationChannel communicationChannel = _communicationChannels.get(playerName);
@@ -305,15 +315,20 @@ public class LotroGameMediator {
} finally {
_readLock.unlock();
}
return true;
}
public void singupUserForGame(Player player, ParticipantCommunicationVisitor visitor) {
public boolean singupUserForGame(Player player, ParticipantCommunicationVisitor visitor) {
String playerName = player.getName();
if (_noSpectators && !_playersPlaying.contains(playerName))
return false;
_readLock.lock();
try {
GatheringParticipantCommunicationChannel participantCommunicationChannel = new GatheringParticipantCommunicationChannel(player.getName());
_communicationChannels.put(player.getName(), participantCommunicationChannel);
GatheringParticipantCommunicationChannel participantCommunicationChannel = new GatheringParticipantCommunicationChannel(playerName);
_communicationChannels.put(playerName, participantCommunicationChannel);
_lotroGame.addGameStateListener(player.getName(), participantCommunicationChannel);
_lotroGame.addGameStateListener(playerName, participantCommunicationChannel);
for (GameEvent gameEvent : participantCommunicationChannel.consumeGameEvents())
visitor.visitGameEvent(gameEvent);
@@ -327,6 +342,7 @@ public class LotroGameMediator {
} finally {
_readLock.unlock();
}
return true;
}
private void startClocksForUsersPendingDecision() {

View File

@@ -136,9 +136,18 @@ public class LotroServer extends AbstractServer {
throw new IllegalArgumentException("There has to be at least two players");
final String gameId = String.valueOf(_nextGameId);
_chatServer.createChatRoom(getChatRoomName(gameId), 30);
boolean privateGame = false;
LotroGameMediator lotroGameMediator = new LotroGameMediator(lotroFormat, participants, _lotroCardBlueprintLibrary, competetive ? 60 * 40 : 60 * 80);
if (privateGame) {
Set<String> allowedUsers = new HashSet<String>();
for (LotroGameParticipant participant : participants)
allowedUsers.add(participant.getPlayerId());
_chatServer.createPrivateChatRoom(getChatRoomName(gameId), allowedUsers, 30);
} else
_chatServer.createChatRoom(getChatRoomName(gameId), 30);
LotroGameMediator lotroGameMediator = new LotroGameMediator(lotroFormat, participants, _lotroCardBlueprintLibrary,
competetive ? 60 * 40 : 60 * 80, privateGame);
lotroGameMediator.addGameResultListener(
new GameResultListener() {
@Override

View File

@@ -5,7 +5,7 @@ import java.util.Set;
public interface HallInfoVisitor {
public void playerIsWaiting(boolean waiting);
public void visitTable(String tableId, String gameId, String tableStatus, String formatName, String tournamentName, Set<String> playerIds, String winner);
public void visitTable(String tableId, String gameId, boolean noSpectators, String tableStatus, String formatName, String tournamentName, Set<String> playerIds, String winner);
public void runningPlayerGame(String gameId);
}

View File

@@ -176,7 +176,7 @@ public class HallServer extends AbstractServer {
for (Map.Entry<String, AwaitingTable> tableInformation : _awaitingTables.entrySet()) {
final AwaitingTable table = tableInformation.getValue();
visitor.visitTable(tableInformation.getKey(), null, "Waiting", table.getLotroFormat().getName(), getTournamentName(table), table.getPlayerNames(), null);
visitor.visitTable(tableInformation.getKey(), null, false, "Waiting", table.getLotroFormat().getName(), getTournamentName(table), table.getPlayerNames(), null);
}
// Then non-finished
@@ -188,7 +188,7 @@ public class HallServer extends AbstractServer {
if (lotroGameMediator != null) {
String gameStatus = lotroGameMediator.getGameStatus();
if (!gameStatus.equals("Finished"))
visitor.visitTable(runningGame.getKey(), runningTable.getGameId(), gameStatus, runningTable.getFormatName(), runningTable.getTournamentName(), lotroGameMediator.getPlayersPlaying(), lotroGameMediator.getWinner());
visitor.visitTable(runningGame.getKey(), runningTable.getGameId(), lotroGameMediator.isNoSpectators(), gameStatus, runningTable.getFormatName(), runningTable.getTournamentName(), lotroGameMediator.getPlayersPlaying(), lotroGameMediator.getWinner());
else
finishedTables.put(runningGame.getKey(), runningTable);
}
@@ -199,7 +199,7 @@ public class HallServer extends AbstractServer {
final RunningTable runningTable = nonPlayingGame.getValue();
LotroGameMediator lotroGameMediator = _lotroServer.getGameById(runningTable.getGameId());
if (lotroGameMediator != null)
visitor.visitTable(nonPlayingGame.getKey(), runningTable.getGameId(), lotroGameMediator.getGameStatus(), runningTable.getFormatName(), runningTable.getTournamentName(), lotroGameMediator.getPlayersPlaying(), lotroGameMediator.getWinner());
visitor.visitTable(nonPlayingGame.getKey(), runningTable.getGameId(), lotroGameMediator.isNoSpectators(), lotroGameMediator.getGameStatus(), runningTable.getFormatName(), runningTable.getTournamentName(), lotroGameMediator.getPlayersPlaying(), lotroGameMediator.getWinner());
}
String gameId = getPlayingPlayerGameId(player.getName());

View File

@@ -40,6 +40,8 @@ public class ChatResource extends AbstractResource {
sendError(Response.Status.NOT_FOUND);
List<ChatMessage> chatMessages = chatRoom.joinUser(resourceOwner.getName());
if (chatMessages == null)
sendError(Response.Status.FORBIDDEN);
Set<String> usersInRoom = chatRoom.getUsersInRoom();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
@@ -69,15 +71,16 @@ public class ChatResource extends AbstractResource {
if (messages != null) {
for (String message : messages) {
if (message != null && message.trim().length() > 0)
chatRoom.sendMessage(resourceOwner.getName(), StringEscapeUtils.escapeHtml(message));
if (!chatRoom.sendMessage(resourceOwner.getName(), StringEscapeUtils.escapeHtml(message)))
sendError(Response.Status.FORBIDDEN);
}
}
List<ChatMessage> chatMessages = chatRoom.getPendingMessages(resourceOwner.getName());
Set<String> usersInRoom = chatRoom.getUsersInRoom();
if (chatMessages == null)
sendError(Response.Status.NOT_FOUND);
sendError(Response.Status.FORBIDDEN);
Set<String> usersInRoom = chatRoom.getUsersInRoom();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

View File

@@ -67,7 +67,8 @@ public class GameResource extends AbstractResource {
Document doc = documentBuilder.newDocument();
Element gameState = doc.createElement("gameState");
gameMediator.singupUserForGame(resourceOwner, new SerializationVisitor(doc, gameState));
if (!gameMediator.singupUserForGame(resourceOwner, new SerializationVisitor(doc, gameState)))
sendError(Response.Status.FORBIDDEN);
doc.appendChild(gameState);
return doc;
@@ -157,7 +158,8 @@ public class GameResource extends AbstractResource {
Document doc = documentBuilder.newDocument();
Element update = doc.createElement("update");
gameMediator.processCommunicationChannel(resourceOwner, new SerializationVisitor(doc, update));
if (!gameMediator.processCommunicationChannel(resourceOwner, new SerializationVisitor(doc, update)))
sendError(Response.Status.FORBIDDEN);
doc.appendChild(update);

View File

@@ -145,12 +145,13 @@ public class HallResource extends AbstractResource {
}
@Override
public void visitTable(String tableId, String gameId, String tableStatus, String formatName, String tournamentName, Set<String> playerIds, String winner) {
public void visitTable(String tableId, String gameId, boolean noSpectators, String tableStatus, String formatName, String tournamentName, Set<String> playerIds, String winner) {
Element table = _doc.createElement("table");
table.setAttribute("id", tableId);
if (gameId != null)
table.setAttribute("gameId", gameId);
table.setAttribute("status", tableStatus);
table.setAttribute("noSpectators", String.valueOf(noSpectators));
table.setAttribute("format", formatName);
table.setAttribute("tournament", tournamentName);
table.setAttribute("players", mergeStrings(playerIds));