Added GameHistoryRequestHandler.

This commit is contained in:
marcins78
2013-01-09 16:36:36 +00:00
parent 31b11c3869
commit a5efeac059
3 changed files with 93 additions and 1 deletions

View File

@@ -0,0 +1,88 @@
package com.gempukku.lotro.async.handler;
import com.gempukku.lotro.async.ResponseWriter;
import com.gempukku.lotro.db.vo.GameHistoryEntry;
import com.gempukku.lotro.game.GameHistoryService;
import com.gempukku.lotro.game.Player;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.QueryStringDecoder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
public class GameHistoryRequestHandler extends LotroServerRequestHandler implements UriRequestHandler {
private GameHistoryService _gameHistoryService;
public GameHistoryRequestHandler(Map<Type, Object> context) {
super(context);
_gameHistoryService = extractObject(context, GameHistoryService.class);
}
@Override
public void handleRequest(String uri, HttpRequest request, Map<Type, Object> context, ResponseWriter responseWriter, MessageEvent e) throws Exception {
if (uri.equals("") && request.getMethod() == HttpMethod.GET) {
QueryStringDecoder queryDecoder = new QueryStringDecoder(request.getUri());
String participantId = getQueryParameterSafely(queryDecoder, "participantId");
int start = Integer.parseInt(getQueryParameterSafely(queryDecoder, "start"));
int count = Integer.parseInt(getQueryParameterSafely(queryDecoder, "count"));
if (start < 0 || count < 1 || count > 100)
throw new WebApplicationException(Response.Status.BAD_REQUEST);
Player resourceOwner = getResourceOwnerSafely(request, participantId);
final List<GameHistoryEntry> playerGameHistory = _gameHistoryService.getGameHistoryForPlayer(resourceOwner, start, count);
int recordCount = _gameHistoryService.getGameHistoryForPlayerCount(resourceOwner);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.newDocument();
Element gameHistory = doc.createElement("gameHistory");
gameHistory.setAttribute("count", String.valueOf(recordCount));
gameHistory.setAttribute("playerId", resourceOwner.getName());
for (GameHistoryEntry gameHistoryEntry : playerGameHistory) {
Element historyEntry = doc.createElement("historyEntry");
historyEntry.setAttribute("winner", gameHistoryEntry.getWinner());
historyEntry.setAttribute("loser", gameHistoryEntry.getLoser());
historyEntry.setAttribute("winReason", gameHistoryEntry.getWinReason());
historyEntry.setAttribute("loseReason", gameHistoryEntry.getLoseReason());
historyEntry.setAttribute("formatName", gameHistoryEntry.getFormatName());
String tournament = gameHistoryEntry.getTournament();
if (tournament != null)
historyEntry.setAttribute("tournament", tournament);
if (gameHistoryEntry.getWinner().equals(resourceOwner.getName()) && gameHistoryEntry.getWinnerRecording() != null) {
historyEntry.setAttribute("gameRecordingId", gameHistoryEntry.getWinnerRecording());
historyEntry.setAttribute("deckName", gameHistoryEntry.getWinnerDeckName());
} else if (gameHistoryEntry.getLoser().equals(resourceOwner.getName()) && gameHistoryEntry.getLoserRecording() != null) {
historyEntry.setAttribute("gameRecordingId", gameHistoryEntry.getLoserRecording());
historyEntry.setAttribute("deckName", gameHistoryEntry.getLoserDeckName());
}
historyEntry.setAttribute("startTime", String.valueOf(gameHistoryEntry.getStartTime().getTime()));
historyEntry.setAttribute("endTime", String.valueOf(gameHistoryEntry.getEndTime().getTime()));
gameHistory.appendChild(historyEntry);
}
doc.appendChild(gameHistory);
responseWriter.writeResponse(doc);
} else {
responseWriter.writeError(404);
}
}
}

View File

@@ -25,6 +25,7 @@ public class RootUriRequestHandler implements UriRequestHandler {
private MerchantRequestHandler _merchantRequestHandler;
private RegisterRequestHandler _registerRequestHandler;
private ReplayRequestHandler _replayRequestHandler;
private GameHistoryRequestHandler _gameHistoryRequestHandler;
public RootUriRequestHandler(Map<Type, Object> context) {
_hallRequestHandler = new HallRequestHandler(context);
@@ -40,6 +41,7 @@ public class RootUriRequestHandler implements UriRequestHandler {
_merchantRequestHandler = new MerchantRequestHandler(context);
_registerRequestHandler = new RegisterRequestHandler(context);
_replayRequestHandler = new ReplayRequestHandler(context);
_gameHistoryRequestHandler = new GameHistoryRequestHandler(context);
}
@Override
@@ -58,6 +60,8 @@ public class RootUriRequestHandler implements UriRequestHandler {
_registerRequestHandler.handleRequest(uri.substring(_serverContextPath.length()+8), request, context, responseWriter, e);
} else if (uri.startsWith(_serverContextPath+"replay")) {
_replayRequestHandler.handleRequest(uri.substring(_serverContextPath.length()+6), request, context, responseWriter, e);
} else if (uri.startsWith(_serverContextPath+"gameHistory")) {
_gameHistoryRequestHandler.handleRequest(uri.substring(_serverContextPath.length()+11), request, context, responseWriter, e);
} else if (uri.startsWith(_serverContextPath+"admin")) {
_adminRequestHandler.handleRequest(uri.substring(_serverContextPath.length()+5), request, context, responseWriter, e);
} else if (uri.startsWith(_serverContextPath + "chat")) {

View File

@@ -254,7 +254,7 @@ public class ServerResource extends AbstractResource {
@Path("/playerStats")
@GET
@Produces(MediaType.APPLICATION_XML)
public Document getServerStats(
public Document getPlayerStats(
@QueryParam("participantId") String participantId,
@Context HttpServletRequest request) throws ParserConfigurationException {
Player resourceOwner = getResourceOwnerSafely(request, participantId);