Introducing hall calls into ServerResource.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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<String> _players = new HashSet<String>();
|
||||
private Map<String, LotroGameParticipant> _players = new HashMap<String, LotroGameParticipant>();
|
||||
|
||||
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<String> getPlayers() {
|
||||
return Collections.unmodifiableSet(_players);
|
||||
public Set<String> getPlayerNames() {
|
||||
return Collections.unmodifiableSet(_players.keySet());
|
||||
}
|
||||
|
||||
public Set<LotroGameParticipant> getPlayers() {
|
||||
return Collections.unmodifiableSet(new HashSet<LotroGameParticipant>(_players.values()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.gempukku.lotro.hall;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public interface HallInfoVisitor {
|
||||
public void visitTable(String tableId, String tableStatus, Set<String> playerIds);
|
||||
|
||||
public void runningPlayerGame(String gameId);
|
||||
}
|
||||
@@ -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<String, AwaitingTable> _awaitingTables = new ConcurrentHashMap<String, AwaitingTable>();
|
||||
private Map<String, String> _runningTables = new ConcurrentHashMap<String, String>();
|
||||
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<String> 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<LotroGameParticipant> 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<String, AwaitingTable> copy = new HashMap<String, AwaitingTable>(_awaitingTables);
|
||||
for (Map.Entry<String, AwaitingTable> table : copy.entrySet())
|
||||
visitor.visitTable(table.getKey(), "Waiting", table.getValue().getPlayerNames());
|
||||
|
||||
Map<String, String> runningCopy = new HashMap<String, String>(_runningTables);
|
||||
for (Map.Entry<String, String> 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<String, AwaitingTable> table : _awaitingTables.entrySet()) {
|
||||
if (table.getValue().hasPlayer(playerId))
|
||||
|
||||
@@ -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<String> 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<String> 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<ChatMessage> chatMessages, Document doc) {
|
||||
Element chatElem = doc.createElement("chat");
|
||||
chatElem.setAttribute("roomName", room);
|
||||
|
||||
Reference in New Issue
Block a user