Added PlayerStatsRequestHandler.

This commit is contained in:
marcins78
2013-01-09 16:42:37 +00:00
parent 174a262611
commit 514c8c4ca8
2 changed files with 79 additions and 0 deletions

View File

@@ -0,0 +1,75 @@
package com.gempukku.lotro.async.handler;
import com.gempukku.lotro.async.ResponseWriter;
import com.gempukku.lotro.db.PlayerStatistic;
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.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.lang.reflect.Type;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
public class PlayerStatsRequestHandler extends LotroServerRequestHandler implements UriRequestHandler {
private GameHistoryService _gameHistoryService;
public PlayerStatsRequestHandler(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");
Player resourceOwner = getResourceOwnerSafely(request, participantId);
List<PlayerStatistic> casualStatistics = _gameHistoryService.getCasualPlayerStatistics(resourceOwner);
List<PlayerStatistic> competitiveStatistics = _gameHistoryService.getCompetitivePlayerStatistics(resourceOwner);
DecimalFormat percFormat = new DecimalFormat("#0.0%");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.newDocument();
Element stats = doc.createElement("playerStats");
Element casual = doc.createElement("casual");
appendStatistics(casualStatistics, percFormat, doc, casual);
stats.appendChild(casual);
Element competitive = doc.createElement("competitive");
appendStatistics(competitiveStatistics, percFormat, doc, competitive);
stats.appendChild(competitive);
doc.appendChild(stats);
responseWriter.writeResponse(doc);
} else {
responseWriter.writeError(404);
}
}
private void appendStatistics(List<PlayerStatistic> statistics, DecimalFormat percFormat, Document doc, Element type) {
for (PlayerStatistic casualStatistic : statistics) {
Element entry = doc.createElement("entry");
entry.setAttribute("deckName", casualStatistic.getDeckName());
entry.setAttribute("format", casualStatistic.getFormatName());
entry.setAttribute("wins", String.valueOf(casualStatistic.getWins()));
entry.setAttribute("losses", String.valueOf(casualStatistic.getLosses()));
entry.setAttribute("perc", percFormat.format(1f * casualStatistic.getWins() / (casualStatistic.getLosses() + casualStatistic.getWins())));
type.appendChild(entry);
}
}
}

View File

@@ -27,6 +27,7 @@ public class RootUriRequestHandler implements UriRequestHandler {
private ReplayRequestHandler _replayRequestHandler;
private GameHistoryRequestHandler _gameHistoryRequestHandler;
private ServerStatsRequestHandler _serverStatsRequestHandler;
private PlayerStatsRequestHandler _playerStatsRequestHandler;
public RootUriRequestHandler(Map<Type, Object> context) {
_hallRequestHandler = new HallRequestHandler(context);
@@ -44,6 +45,7 @@ public class RootUriRequestHandler implements UriRequestHandler {
_replayRequestHandler = new ReplayRequestHandler(context);
_gameHistoryRequestHandler = new GameHistoryRequestHandler(context);
_serverStatsRequestHandler = new ServerStatsRequestHandler(context);
_playerStatsRequestHandler = new PlayerStatsRequestHandler(context);
}
@Override
@@ -66,6 +68,8 @@ public class RootUriRequestHandler implements UriRequestHandler {
_gameHistoryRequestHandler.handleRequest(uri.substring(_serverContextPath.length()+11), request, context, responseWriter, e);
} else if (uri.startsWith(_serverContextPath+"stats")) {
_serverStatsRequestHandler.handleRequest(uri.substring(_serverContextPath.length()+5), request, context, responseWriter, e);
} else if (uri.startsWith(_serverContextPath+"playerStats")) {
_playerStatsRequestHandler.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")) {