diff --git a/gemp-lotr/gemp-lotr-async/src/main/java/com/gempukku/lotro/async/handler/GameHistoryRequestHandler.java b/gemp-lotr/gemp-lotr-async/src/main/java/com/gempukku/lotro/async/handler/GameHistoryRequestHandler.java index 429804237..2409b5bfc 100644 --- a/gemp-lotr/gemp-lotr-async/src/main/java/com/gempukku/lotro/async/handler/GameHistoryRequestHandler.java +++ b/gemp-lotr/gemp-lotr-async/src/main/java/com/gempukku/lotro/async/handler/GameHistoryRequestHandler.java @@ -15,9 +15,6 @@ import org.w3c.dom.Element; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.lang.reflect.Type; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; import java.util.List; import java.util.Map; @@ -35,124 +32,63 @@ public class GameHistoryRequestHandler extends LotroServerRequestHandler impleme @Override public void handleRequest(String uri, HttpRequest request, Map context, ResponseWriter responseWriter, String remoteIp) throws Exception { if (uri.equals("") && request.method() == HttpMethod.GET) { - QueryStringDecoder queryDecoder = new QueryStringDecoder(request.uri()); - 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 HttpProcessingException(400); - - Player resourceOwner = getResourceOwnerSafely(request, participantId); - - final List 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 (DBDefs.GameHistory game : playerGameHistory) { - Element historyEntry = doc.createElement("historyEntry"); - historyEntry.setAttribute("winner", game.winner); - historyEntry.setAttribute("loser", game.loser); - - historyEntry.setAttribute("winReason", game.win_reason); - historyEntry.setAttribute("loseReason", game.lose_reason); - - historyEntry.setAttribute("formatName", game.format_name); - String tournament = game.tournament; - if (tournament != null) - historyEntry.setAttribute("tournament", tournament); - - if (game.winner.equals(resourceOwner.getName()) && game.win_recording_id != null) { - historyEntry.setAttribute("gameRecordingId", game.win_recording_id); - historyEntry.setAttribute("deckName", game.winner_deck_name); - } else if (game.loser.equals(resourceOwner.getName()) && game.lose_recording_id != null) { - historyEntry.setAttribute("gameRecordingId", game.lose_recording_id); - historyEntry.setAttribute("deckName", game.loser_deck_name); - } - - historyEntry.setAttribute("startTime", String.valueOf(game.GetStartDate().getTime())); - historyEntry.setAttribute("endTime", String.valueOf(game.GetEndDate().getTime())); - - gameHistory.appendChild(historyEntry); - } - - doc.appendChild(gameHistory); - - responseWriter.writeXmlResponse(doc); - } else if (uri.equals("/list") && request.method() == HttpMethod.GET) { - final List playerGameHistory = _gameHistoryService.getTrackableGames(100); - - DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); - DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); - Document doc = documentBuilder.newDocument(); - Element gameHistory = doc.createElement("gameHistory"); - - DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - - for (DBDefs.GameHistory game : playerGameHistory) { - Element historyEntry = doc.createElement("historyEntry"); - historyEntry.setAttribute("winner", game.winner); - historyEntry.setAttribute("loser", game.loser); - - historyEntry.setAttribute("winReason", game.win_reason); - historyEntry.setAttribute("loseReason", game.lose_reason); - - historyEntry.setAttribute("formatName", game.format_name); - - historyEntry.setAttribute("winnerRecordingLink", "http://www.gempukku.com/gemp-lotr/game.html?replayId="+game.winner+"$"+game.win_recording_id); - historyEntry.setAttribute("winnerDeckName", game.winner_deck_name); - - historyEntry.setAttribute("loserRecordingLink", "http://www.gempukku.com/gemp-lotr/game.html?replayId="+game.loser+"$"+game.lose_recording_id); - historyEntry.setAttribute("loserDeckName", game.loser_deck_name); - - historyEntry.setAttribute("startTime", dateFormat.format(new Date(game.GetStartDate().getTime()))); - historyEntry.setAttribute("endTime", dateFormat.format(new Date(game.GetEndDate().getTime()))); - - gameHistory.appendChild(historyEntry); - } - - doc.appendChild(gameHistory); - - responseWriter.writeXmlResponse(doc); - } else if (uri.equals("/list/html") && request.method() == HttpMethod.GET) { - final List playerGameHistory = _gameHistoryService.getTrackableGames(100); - - StringBuilder sb = new StringBuilder(); - sb.append(""); - - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(""); - - DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - - for (DBDefs.GameHistory game : playerGameHistory) { - String winnerLink = "http://www.gempukku.com/gemp-lotr/game.html?replayId=" + game.winner + "$" + game.win_recording_id; - String loserLink = "http://www.gempukku.com/gemp-lotr/game.html?replayId=" + game.loser + "$" + game.lose_recording_id; - - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(""); - sb.append(""); - } - - sb.append("
Start timeEnd timeWinnerReasonDeck nameReplay
LoserReasonDeck nameReplay
" + dateFormat.format(new Date(game.GetStartDate().getTime())) + "" + dateFormat.format(new Date(game.GetEndDate().getTime())) + "" + game.winner + "" + game.win_reason + "" + game.winner_deck_name + "Replay
" + game.loser + "" + game.lose_reason + "" + game.lose_reason + "Replay
"); - responseWriter.writeHtmlResponse(sb.toString()); + getGameHistory(request, responseWriter); } else { throw new HttpProcessingException(404); } } + + private void getGameHistory(HttpRequest request, ResponseWriter responseWriter) throws Exception { + //HttpPostRequestDecoder postDecoder = new HttpPostRequestDecoder(request); + QueryStringDecoder queryDecoder = new QueryStringDecoder(request.uri()); + 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 HttpProcessingException(400); + + Player resourceOwner = getResourceOwnerSafely(request, participantId); + + final List 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 (DBDefs.GameHistory game : playerGameHistory) { + Element historyEntry = doc.createElement("historyEntry"); + historyEntry.setAttribute("winner", game.winner); + historyEntry.setAttribute("loser", game.loser); + + historyEntry.setAttribute("winReason", game.win_reason); + historyEntry.setAttribute("loseReason", game.lose_reason); + + historyEntry.setAttribute("formatName", game.format_name); + String tournament = game.tournament; + if (tournament != null) + historyEntry.setAttribute("tournament", tournament); + + if (game.winner.equals(resourceOwner.getName()) && game.win_recording_id != null) { + historyEntry.setAttribute("gameRecordingId", game.win_recording_id); + historyEntry.setAttribute("deckName", game.winner_deck_name); + } else if (game.loser.equals(resourceOwner.getName()) && game.lose_recording_id != null) { + historyEntry.setAttribute("gameRecordingId", game.lose_recording_id); + historyEntry.setAttribute("deckName", game.loser_deck_name); + } + + historyEntry.setAttribute("startTime", game.start_date.toLocalDate().toString()); + historyEntry.setAttribute("endTime", game.end_date.toLocalDate().toString()); + + gameHistory.appendChild(historyEntry); + } + + doc.appendChild(gameHistory); + + responseWriter.writeXmlResponse(doc); + } } diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/GameHistoryStatistics.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/GameHistoryStatistics.java deleted file mode 100644 index 82f1ded92..000000000 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/GameHistoryStatistics.java +++ /dev/null @@ -1,119 +0,0 @@ -package com.gempukku.lotro.game; - -import com.gempukku.lotro.db.GameHistoryDAO; - -import java.util.*; - -public class GameHistoryStatistics { - private List _formatStats; - private final long _start; - private final long _duration; - - public GameHistoryStatistics(long start, long duration) { - _start = start; - _duration = duration; - } - - public void init(GameHistoryDAO gameHistoryDao) { - Map countsPerFormat = gameHistoryDao.getCasualGamesPlayedPerFormat(_start, _duration); - Map result = new HashMap<>(); - for (Map.Entry formatCount : countsPerFormat.entrySet()) { - String format = formatCount.getKey(); - if (!isIgnorable(format)) { - incrementResult(result, getMainFormat(format), formatCount.getValue()); - } - } - - int sum = 0; - for (Integer integer : result.values()) - sum += integer; - - List stats = new ArrayList<>(); - for (Map.Entry formatCount : result.entrySet()) - stats.add(new FormatStat(formatCount.getKey(), formatCount.getValue(), 1f * formatCount.getValue() / sum)); - - stats.sort(new Comparator<>() { - @Override - public int compare(FormatStat o1, FormatStat o2) { - return o2.getCount() - o1.getCount(); - } - }); - - _formatStats = stats; - } - - private void incrementResult(Map result, String format, int value) { - Integer previous = result.get(format); - if (previous == null) - previous = 0; - result.put(format, previous + value); - } - - private String getMainFormat(String format) { - if (format == null) - return "Fellowship block"; - if (format.equals("Community Fellowship block")) - return "Fellowship block"; - if (format.equals("Community Two Towers block")) - return "Towers block"; - if (format.equals("Community War of the Ring block")) - return "War of the Ring block"; - if (format.equals("Two Towers block")) - return "Towers block"; - if (format.equals("Towers Standard")) - return "Towers standard"; - return format; - } - - private boolean isIgnorable(String format) { - if (format == null) - return false; - if (format.startsWith("Fellowship block - ")) - return true; - if (format.equals("Format for testing")) - return true; - if (format.equals("Sealed FotR League (Dec 2011)")) - return true; - if (format.startsWith("Test league")) - return true; - if (format.startsWith("Towers block - ")) - return true; - return false; - } - - public List getFormatStats() { - return Collections.unmodifiableList(_formatStats); - } - - public long getStart() { - return _start; - } - - public long getDuration() { - return _duration; - } - - public static class FormatStat { - private final String _format; - private final int _count; - private final float _percentage; - - public FormatStat(String format, int count, float percentage) { - _format = format; - _count = count; - _percentage = percentage; - } - - public int getCount() { - return _count; - } - - public String getFormat() { - return _format; - } - - public float getPercentage() { - return _percentage; - } - } -}