Deleting obsolete stats and Second Edition history stuff

This commit is contained in:
Christian 'ketura' McCarty
2023-03-12 23:21:51 -05:00
parent bb80552044
commit c7d33a4f79
2 changed files with 55 additions and 238 deletions

View File

@@ -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<Type, Object> 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<DBDefs.GameHistory> 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<DBDefs.GameHistory> 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<DBDefs.GameHistory> playerGameHistory = _gameHistoryService.getTrackableGames(100);
StringBuilder sb = new StringBuilder();
sb.append("<html><body><table>");
sb.append("<tr>");
sb.append("<th rowspan='2'>Start time</th><th rowspan='2'>End time</th>");
sb.append("<th>Winner</th><th>Reason</th><th>Deck name</th><th>Replay</th>");
sb.append("</tr>");
sb.append("<tr>");
sb.append("<th>Loser</th><th>Reason</th><th>Deck name</th><th>Replay</th>");
sb.append("</tr>");
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("<tr>");
sb.append("<td rowspan='2'>" + dateFormat.format(new Date(game.GetStartDate().getTime())) + "</td><td rowspan='2'>" + dateFormat.format(new Date(game.GetEndDate().getTime())) + "</td>");
sb.append("<td>" + game.winner + "</td><td>" + game.win_reason + "</td><td>" + game.winner_deck_name + "</td><td><a target='_blank' href='" + winnerLink + "'>Replay</a></td>");
sb.append("</tr>");
sb.append("<tr>");
sb.append("<td>" + game.loser + "</td><td>" + game.lose_reason + "</td><td>" + game.lose_reason + "</td><td><a target='_blank' href='" + loserLink + "'>Replay</a></td>");
sb.append("</tr>");
}
sb.append("</table></body></html>");
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<DBDefs.GameHistory> 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);
}
}

View File

@@ -1,119 +0,0 @@
package com.gempukku.lotro.game;
import com.gempukku.lotro.db.GameHistoryDAO;
import java.util.*;
public class GameHistoryStatistics {
private List<FormatStat> _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<String, Integer> countsPerFormat = gameHistoryDao.getCasualGamesPlayedPerFormat(_start, _duration);
Map<String, Integer> result = new HashMap<>();
for (Map.Entry<String, Integer> 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<FormatStat> stats = new ArrayList<>();
for (Map.Entry<String, Integer> 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<String, Integer> 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<FormatStat> 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;
}
}
}