From 21d9ec5d4c1ba3a212ffc727310af37f897aac00 Mon Sep 17 00:00:00 2001 From: "marcins78@gmail.com" Date: Wed, 14 Mar 2012 15:50:27 +0000 Subject: [PATCH] Introducing possibility of no-spectator games. --- .../gempukku/lotro/chat/ChatRoomMediator.java | 18 +++++++++++- .../com/gempukku/lotro/chat/ChatServer.java | 8 ++++++ .../lotro/game/LotroGameMediator.java | 28 +++++++++++++++---- .../com/gempukku/lotro/game/LotroServer.java | 13 +++++++-- .../gempukku/lotro/hall/HallInfoVisitor.java | 2 +- .../com/gempukku/lotro/hall/HallServer.java | 6 ++-- .../gempukku/lotro/server/ChatResource.java | 11 +++++--- .../gempukku/lotro/server/GameResource.java | 6 ++-- .../gempukku/lotro/server/HallResource.java | 3 +- 9 files changed, 75 insertions(+), 20 deletions(-) diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/chat/ChatRoomMediator.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/chat/ChatRoomMediator.java index 5754b7609..a9dff29f4 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/chat/ChatRoomMediator.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/chat/ChatRoomMediator.java @@ -13,12 +13,21 @@ public class ChatRoomMediator { private Map _listeners = new HashMap(); private int _channelInactivityTimeoutPeriod = 1000 * 10; // 10 seconds + private Set _allowedPlayers; public ChatRoomMediator(int secondsTimeoutPeriod) { + this(secondsTimeoutPeriod, null); + } + + public ChatRoomMediator(int secondsTimeoutPeriod, Set allowedPlayers) { + _allowedPlayers = allowedPlayers; _channelInactivityTimeoutPeriod = 1000 * secondsTimeoutPeriod; } public synchronized List 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 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() { diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/chat/ChatServer.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/chat/ChatServer.java index fa579517e..a73c54a39 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/chat/ChatServer.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/chat/ChatServer.java @@ -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 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); } diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroGameMediator.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroGameMediator.java index 98e77756e..89a280883 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroGameMediator.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroGameMediator.java @@ -27,6 +27,7 @@ public class LotroGameMediator { private Set _playersPlaying = new HashSet(); 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 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() { diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroServer.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroServer.java index 215359d87..c2a645d4f 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroServer.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroServer.java @@ -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 allowedUsers = new HashSet(); + 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 diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallInfoVisitor.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallInfoVisitor.java index ac96f747a..0f6e7d78c 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallInfoVisitor.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallInfoVisitor.java @@ -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 playerIds, String winner); + public void visitTable(String tableId, String gameId, boolean noSpectators, String tableStatus, String formatName, String tournamentName, Set playerIds, String winner); public void runningPlayerGame(String gameId); } diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallServer.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallServer.java index 56cbb2545..0ed249022 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallServer.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallServer.java @@ -176,7 +176,7 @@ public class HallServer extends AbstractServer { for (Map.Entry 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()); diff --git a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ChatResource.java b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ChatResource.java index d9aba062f..a119ca125 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ChatResource.java +++ b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ChatResource.java @@ -40,6 +40,8 @@ public class ChatResource extends AbstractResource { sendError(Response.Status.NOT_FOUND); List chatMessages = chatRoom.joinUser(resourceOwner.getName()); + if (chatMessages == null) + sendError(Response.Status.FORBIDDEN); Set 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 chatMessages = chatRoom.getPendingMessages(resourceOwner.getName()); - Set usersInRoom = chatRoom.getUsersInRoom(); - if (chatMessages == null) - sendError(Response.Status.NOT_FOUND); + sendError(Response.Status.FORBIDDEN); + + Set usersInRoom = chatRoom.getUsersInRoom(); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); diff --git a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/GameResource.java b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/GameResource.java index 4afdb5270..0b434cff1 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/GameResource.java +++ b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/GameResource.java @@ -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); diff --git a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/HallResource.java b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/HallResource.java index 3910dc9fd..afd79b2ce 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/HallResource.java +++ b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/HallResource.java @@ -145,12 +145,13 @@ public class HallResource extends AbstractResource { } @Override - public void visitTable(String tableId, String gameId, String tableStatus, String formatName, String tournamentName, Set playerIds, String winner) { + public void visitTable(String tableId, String gameId, boolean noSpectators, String tableStatus, String formatName, String tournamentName, Set 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));