From d6641e8264e4fc59e5b31a85a99477987d487066 Mon Sep 17 00:00:00 2001 From: "marcins78@gmail.com" Date: Thu, 15 Sep 2011 15:38:00 +0000 Subject: [PATCH] Introducing hall calls into ServerResource. --- .../lotro/game/DefaultLotroFormat.java | 7 ++ .../com/gempukku/lotro/game/LotroFormat.java | 4 ++ .../lotro/game/LotroGameMediator.java | 9 ++- .../gempukku/lotro/hall/AwaitingTable.java | 22 +++--- .../gempukku/lotro/hall/HallInfoVisitor.java | 9 +++ .../com/gempukku/lotro/hall/HallServer.java | 52 ++++++++++---- .../gempukku/lotro/server/ServerResource.java | 71 +++++++++++++++++++ 7 files changed, 152 insertions(+), 22 deletions(-) create mode 100644 gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallInfoVisitor.java diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/DefaultLotroFormat.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/DefaultLotroFormat.java index 273acb350..419546032 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/DefaultLotroFormat.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/DefaultLotroFormat.java @@ -1,5 +1,7 @@ package com.gempukku.lotro.game; +import com.gempukku.lotro.logic.vo.LotroDeck; + public class DefaultLotroFormat implements LotroFormat { private boolean _orderedSites; @@ -11,4 +13,9 @@ public class DefaultLotroFormat implements LotroFormat { public boolean isOrderedSites() { return _orderedSites; } + + @Override + public boolean validateDeck(LotroDeck deck) { + return (deck != null); + } } diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroFormat.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroFormat.java index 9d72149e7..e5a1fd62f 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroFormat.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroFormat.java @@ -1,5 +1,9 @@ package com.gempukku.lotro.game; +import com.gempukku.lotro.logic.vo.LotroDeck; + public interface LotroFormat { public boolean isOrderedSites(); + + public boolean validateDeck(LotroDeck deck); } 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 95fd02009..2c674e4fa 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 @@ -22,7 +22,6 @@ public class LotroGameMediator { private final int _channelInactivityTimeoutPeriod = 1000 * 60 * 5; // 5 minutes private final int _playerDecisionTimeoutPeriod = 1000 * 60 * 10; // 10 minutes - public LotroGameMediator(LotroFormat lotroFormat, LotroGameParticipant[] participants, LotroCardBlueprintLibrary library, GameResultListener gameResultListener) { if (participants.length < 1) throw new IllegalArgumentException("Game can't have less than one participant"); @@ -43,6 +42,14 @@ public class LotroGameMediator { return Collections.unmodifiableSet(_playersPlaying); } + public String getGameStatus() { + if (_lotroGame.getGameState() == null) + return "Preparation"; + if (_lotroGame.getGameState().getWinnerPlayerId() != null) + return "Finished"; + return "Playing"; + } + public synchronized String produceCardInfo(String participantId, int cardId) { StringBuilder sb = new StringBuilder(); diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/AwaitingTable.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/AwaitingTable.java index 745b0880f..477c4ca63 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/AwaitingTable.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/AwaitingTable.java @@ -1,16 +1,16 @@ package com.gempukku.lotro.hall; -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; +import com.gempukku.lotro.game.LotroGameParticipant; + +import java.util.*; public class AwaitingTable { - private Set _players = new HashSet(); + private Map _players = new HashMap(); private int _capacity = 2; - public boolean addPlayer(String playerId) { - _players.add(playerId); + public boolean addPlayer(LotroGameParticipant player) { + _players.put(player.getPlayerId(), player); return _players.size() == _capacity; } @@ -20,10 +20,14 @@ public class AwaitingTable { } public boolean hasPlayer(String playerId) { - return _players.contains(playerId); + return _players.containsKey(playerId); } - public Set getPlayers() { - return Collections.unmodifiableSet(_players); + public Set getPlayerNames() { + return Collections.unmodifiableSet(_players.keySet()); + } + + public Set getPlayers() { + return Collections.unmodifiableSet(new HashSet(_players.values())); } } 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 new file mode 100644 index 000000000..16ab95d2e --- /dev/null +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallInfoVisitor.java @@ -0,0 +1,9 @@ +package com.gempukku.lotro.hall; + +import java.util.Set; + +public interface HallInfoVisitor { + public void visitTable(String tableId, String tableStatus, Set playerIds); + + 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 86f14997b..e3547eaaf 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 @@ -2,10 +2,8 @@ package com.gempukku.lotro.hall; import com.gempukku.lotro.AbstractServer; import com.gempukku.lotro.chat.ChatServer; -import com.gempukku.lotro.game.DefaultLotroFormat; -import com.gempukku.lotro.game.LotroGameMediator; -import com.gempukku.lotro.game.LotroGameParticipant; -import com.gempukku.lotro.game.LotroServer; +import com.gempukku.lotro.game.*; +import com.gempukku.lotro.logic.vo.LotroDeck; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @@ -14,6 +12,8 @@ public class HallServer extends AbstractServer { private ChatServer _chatServer; private LotroServer _lotroServer; + private LotroFormat _lotroFormat = new DefaultLotroFormat(true); + private Map _awaitingTables = new ConcurrentHashMap(); private Map _runningTables = new ConcurrentHashMap(); private int _nextTableId = 1; @@ -38,8 +38,8 @@ public class HallServer extends AbstractServer { String tableId = String.valueOf(_nextTableId++); AwaitingTable table = new AwaitingTable(); - table.addPlayer(playerId); _awaitingTables.put(tableId, table); + joinTableAsPlayer(tableId, playerId); return true; } @@ -55,14 +55,19 @@ public class HallServer extends AbstractServer { AwaitingTable awaitingTable = _awaitingTables.get(tableId); if (awaitingTable == null) return false; - boolean tableFull = awaitingTable.addPlayer(playerId); - if (tableFull) { - Set players = awaitingTable.getPlayers(); - LotroGameParticipant[] participants = new LotroGameParticipant[players.size()]; - int index = 0; - for (String player : players) - participants[index] = new LotroGameParticipant(player, _lotroServer.getParticipantDeck(player)); + LotroDeck lotroDeck = _lotroServer.getParticipantDeck(playerId); + if (lotroDeck == null) + return false; + + boolean valid = _lotroFormat.validateDeck(lotroDeck); + if (!valid) + return false; + + boolean tableFull = awaitingTable.addPlayer(new LotroGameParticipant(playerId, lotroDeck)); + if (tableFull) { + Set players = awaitingTable.getPlayers(); + LotroGameParticipant[] participants = new LotroGameParticipant[players.size()]; String gameId = _lotroServer.createNewGame(new DefaultLotroFormat(true), participants); LotroGameMediator lotroGameMediator = _lotroServer.getGameById(gameId); lotroGameMediator.startGame(); @@ -82,6 +87,29 @@ public class HallServer extends AbstractServer { } } + public void processTables(String participantId, HallInfoVisitor visitor) { + Map copy = new HashMap(_awaitingTables); + for (Map.Entry table : copy.entrySet()) + visitor.visitTable(table.getKey(), "Waiting", table.getValue().getPlayerNames()); + + Map runningCopy = new HashMap(_runningTables); + for (Map.Entry runningGame : runningCopy.entrySet()) { + LotroGameMediator lotroGameMediator = _lotroServer.getGameById(runningGame.getValue()); + if (lotroGameMediator != null) + visitor.visitTable(runningGame.getKey(), lotroGameMediator.getGameStatus(), lotroGameMediator.getPlayersPlaying()); + } + + String playerTable = getPlayerTable(participantId); + if (playerTable != null) { + String gameId = _runningTables.get(playerTable); + if (gameId != null) { + LotroGameMediator lotroGameMediator = _lotroServer.getGameById(gameId); + if (lotroGameMediator != null && !lotroGameMediator.getGameStatus().equals("Finished")) + visitor.runningPlayerGame(gameId); + } + } + } + public String getPlayerTable(String playerId) { for (Map.Entry table : _awaitingTables.entrySet()) { if (table.getValue().hasPlayer(playerId)) diff --git a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ServerResource.java b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ServerResource.java index 5e56804b3..c19ac4b90 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ServerResource.java +++ b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ServerResource.java @@ -8,6 +8,7 @@ import com.gempukku.lotro.db.PlayerDAO; import com.gempukku.lotro.db.vo.Deck; import com.gempukku.lotro.db.vo.Player; import com.gempukku.lotro.game.*; +import com.gempukku.lotro.hall.HallInfoVisitor; import com.gempukku.lotro.hall.HallServer; import com.gempukku.lotro.logic.decisions.AwaitingDecision; import com.sun.jersey.spi.resource.Singleton; @@ -27,6 +28,7 @@ import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import java.util.List; import java.util.Map; +import java.util.Set; @Singleton @Path("/") @@ -374,6 +376,75 @@ public class ServerResource { return doc; } + @Path("/hall") + @GET + @Produces(MediaType.APPLICATION_XML) + public Document getHall( + @QueryParam("participantId") String participantId) throws ParserConfigurationException { + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); + + Document doc = documentBuilder.newDocument(); + + Element hall = doc.createElement("hall"); + + _hallServer.processTables(participantId, new SerializeHallInfoVisitor(doc, hall)); + + doc.appendChild(hall); + + return doc; + } + + @Path("/hall/{table}") + @POST + public void joinTable( + @PathParam("table") String tableId, + @FormParam("participantId") String participantId) throws ParserConfigurationException { + boolean joined = _hallServer.joinTableAsPlayer(tableId, participantId); + } + + @Path("/hall") + @POST + public void createTable( + @FormParam("participantId") String participantId) throws ParserConfigurationException { + boolean created = _hallServer.createNewTable(participantId); + } + + private class SerializeHallInfoVisitor implements HallInfoVisitor { + private Document _doc; + private Element _hall; + + public SerializeHallInfoVisitor(Document doc, Element hall) { + _doc = doc; + _hall = hall; + } + + @Override + public void visitTable(String tableId, String tableStatus, Set playerIds) { + Element table = _doc.createElement("table"); + table.setAttribute("id", tableId); + table.setAttribute("status", tableStatus); + table.setAttribute("players", mergeStrings(playerIds)); + _hall.appendChild(table); + } + + @Override + public void runningPlayerGame(String gameId) { + Element runningGame = _doc.createElement("game"); + runningGame.setAttribute("id", gameId); + _hall.appendChild(runningGame); + } + } + + private String mergeStrings(Set strings) { + StringBuilder sb = new StringBuilder(); + for (String string : strings) + sb.append(string).append(","); + if (sb.length() > 0) + return sb.deleteCharAt(sb.length() - 1).toString(); + return sb.toString(); + } + private void serializeChatRoomData(String room, List chatMessages, Document doc) { Element chatElem = doc.createElement("chat"); chatElem.setAttribute("roomName", room);