Allowing to view recent games

This commit is contained in:
marcin.sciesinski
2019-08-14 13:54:06 -07:00
parent a5e9032d44
commit 26f2b692c2
4 changed files with 155 additions and 64 deletions

View File

@@ -15,6 +15,9 @@ 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;
@@ -79,6 +82,41 @@ public class GameHistoryRequestHandler extends LotroServerRequestHandler impleme
doc.appendChild(gameHistory);
responseWriter.writeXmlResponse(doc);
} else if (uri.equals("/list") && request.getMethod() == HttpMethod.GET) {
final List<GameHistoryEntry> playerGameHistory = _gameHistoryService.getTrackableGames(50);
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 (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());
historyEntry.setAttribute("winnerRecordingLink", "http://www.gempukku.com/gemp-lotr/game.html?replayId="+gameHistoryEntry.getWinner()+"$"+gameHistoryEntry.getWinnerRecording());
historyEntry.setAttribute("winnerDeckName", gameHistoryEntry.getWinnerDeckName());
historyEntry.setAttribute("loserRecordingLink", "http://www.gempukku.com/gemp-lotr/game.html?replayId="+gameHistoryEntry.getLoser()+"$"+gameHistoryEntry.getLoserRecording());
historyEntry.setAttribute("loserDeckName", gameHistoryEntry.getLoserDeckName());
historyEntry.setAttribute("startTime", dateFormat.format(new Date(gameHistoryEntry.getStartTime().getTime())));
historyEntry.setAttribute("endTime", dateFormat.format(new Date(gameHistoryEntry.getEndTime().getTime())));
gameHistory.appendChild(historyEntry);
}
doc.appendChild(gameHistory);
responseWriter.writeXmlResponse(doc);
} else {
throw new HttpProcessingException(404);

View File

@@ -240,6 +240,53 @@ public class DbGameHistoryDAO implements GameHistoryDAO {
}
}
@Override
public List<GameHistoryEntry> getLastGames(String requestedFormatName, int count) {
try {
Connection connection = _dbAccess.getDataSource().getConnection();
try {
PreparedStatement statement = connection.prepareStatement(
"select winner, loser, win_reason, lose_reason, win_recording_id, lose_recording_id, format_name, " +
"tournament, winner_deck_name, loser_deck_name, start_date, end_date from game_history " +
"where format_name=? order by end_date desc limit ?");
try {
statement.setString(1, requestedFormatName);
statement.setInt(2, count);
ResultSet rs = statement.executeQuery();
try {
List<GameHistoryEntry> result = new LinkedList<GameHistoryEntry>();
while (rs.next()) {
String winner = rs.getString(1);
String loser = rs.getString(2);
String winReason = rs.getString(3);
String loseReason = rs.getString(4);
String winRecordingId = rs.getString(5);
String loseRecordingId = rs.getString(6);
String formatName = rs.getString(7);
String tournament = rs.getString(8);
String winnerDeckName = rs.getString(9);
String loserDeckName = rs.getString(10);
Date startDate = new Date(rs.getLong(11));
Date endDate = new Date(rs.getLong(12));
GameHistoryEntry entry = new GameHistoryEntry(winner, winReason, winRecordingId, loser, loseReason, loseRecordingId, formatName, tournament, winnerDeckName, loserDeckName, startDate, endDate);
result.add(entry);
}
return result;
} finally {
rs.close();
}
} finally {
statement.close();
}
} finally {
connection.close();
}
} catch (SQLException exp) {
throw new RuntimeException("Unable to get list of games", exp);
}
}
public List<PlayerStatistic> getCompetitivePlayerStatistics(Player player) {
try {
Connection connection = _dbAccess.getDataSource().getConnection();

View File

@@ -23,4 +23,6 @@ public interface GameHistoryDAO {
public List<PlayerStatistic> getCasualPlayerStatistics(Player player);
public List<PlayerStatistic> getCompetitivePlayerStatistics(Player player);
List<GameHistoryEntry> getLastGames(String formatName, int count);
}

View File

@@ -1,64 +1,68 @@
package com.gempukku.lotro.game;
import com.gempukku.lotro.db.GameHistoryDAO;
import com.gempukku.lotro.db.PlayerStatistic;
import com.gempukku.lotro.db.vo.GameHistoryEntry;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class GameHistoryService {
private GameHistoryDAO _gameHistoryDAO;
private Map<String, Integer> _playerGameCount = new ConcurrentHashMap<String, Integer>();
public GameHistoryService(GameHistoryDAO gameHistoryDAO) {
_gameHistoryDAO = gameHistoryDAO;
}
public void addGameHistory(String winner, String loser, String winReason, String loseReason, String winRecordingId, String loseRecordingId, String formatName, String tournament, String winnerDeckName, String loserDeckName, Date startDate, Date endDate) {
_gameHistoryDAO.addGameHistory(winner, loser, winReason, loseReason, winRecordingId, loseRecordingId, formatName, tournament, winnerDeckName, loserDeckName, startDate, endDate);
Integer winnerCount = _playerGameCount.get(winner);
Integer loserCount = _playerGameCount.get(loser);
if (winnerCount != null)
_playerGameCount.put(winner, winnerCount + 1);
if (loserCount != null)
_playerGameCount.put(loser, loserCount + 1);
}
public int getGameHistoryForPlayerCount(Player player) {
Integer result = _playerGameCount.get(player.getName());
if (result != null)
return result;
int count = _gameHistoryDAO.getGameHistoryForPlayerCount(player);
_playerGameCount.put(player.getName(), count);
return count;
}
public List<GameHistoryEntry> getGameHistoryForPlayer(Player player, int start, int count) {
return _gameHistoryDAO.getGameHistoryForPlayer(player, start, count);
}
public int getActivePlayersCount(long from, long duration) {
return _gameHistoryDAO.getActivePlayersCount(from, duration);
}
public int getGamesPlayedCount(long from, long duration) {
return _gameHistoryDAO.getGamesPlayedCount(from, duration);
}
public GameHistoryStatistics getGameHistoryStatistics(long from, long duration) {
GameHistoryStatistics stats = new GameHistoryStatistics(from, duration);
stats.init(_gameHistoryDAO);
return stats;
}
public List<PlayerStatistic> getCasualPlayerStatistics(Player player) {
return _gameHistoryDAO.getCasualPlayerStatistics(player);
}
public List<PlayerStatistic> getCompetitivePlayerStatistics(Player player) {
return _gameHistoryDAO.getCompetitivePlayerStatistics(player);
}
}
package com.gempukku.lotro.game;
import com.gempukku.lotro.db.GameHistoryDAO;
import com.gempukku.lotro.db.PlayerStatistic;
import com.gempukku.lotro.db.vo.GameHistoryEntry;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class GameHistoryService {
private GameHistoryDAO _gameHistoryDAO;
private Map<String, Integer> _playerGameCount = new ConcurrentHashMap<String, Integer>();
public GameHistoryService(GameHistoryDAO gameHistoryDAO) {
_gameHistoryDAO = gameHistoryDAO;
}
public void addGameHistory(String winner, String loser, String winReason, String loseReason, String winRecordingId, String loseRecordingId, String formatName, String tournament, String winnerDeckName, String loserDeckName, Date startDate, Date endDate) {
_gameHistoryDAO.addGameHistory(winner, loser, winReason, loseReason, winRecordingId, loseRecordingId, formatName, tournament, winnerDeckName, loserDeckName, startDate, endDate);
Integer winnerCount = _playerGameCount.get(winner);
Integer loserCount = _playerGameCount.get(loser);
if (winnerCount != null)
_playerGameCount.put(winner, winnerCount + 1);
if (loserCount != null)
_playerGameCount.put(loser, loserCount + 1);
}
public int getGameHistoryForPlayerCount(Player player) {
Integer result = _playerGameCount.get(player.getName());
if (result != null)
return result;
int count = _gameHistoryDAO.getGameHistoryForPlayerCount(player);
_playerGameCount.put(player.getName(), count);
return count;
}
public List<GameHistoryEntry> getGameHistoryForPlayer(Player player, int start, int count) {
return _gameHistoryDAO.getGameHistoryForPlayer(player, start, count);
}
public List<GameHistoryEntry> getTrackableGames(int count) {
return _gameHistoryDAO.getLastGames("Second Edition", count);
}
public int getActivePlayersCount(long from, long duration) {
return _gameHistoryDAO.getActivePlayersCount(from, duration);
}
public int getGamesPlayedCount(long from, long duration) {
return _gameHistoryDAO.getGamesPlayedCount(from, duration);
}
public GameHistoryStatistics getGameHistoryStatistics(long from, long duration) {
GameHistoryStatistics stats = new GameHistoryStatistics(from, duration);
stats.init(_gameHistoryDAO);
return stats;
}
public List<PlayerStatistic> getCasualPlayerStatistics(Player player) {
return _gameHistoryDAO.getCasualPlayerStatistics(player);
}
public List<PlayerStatistic> getCompetitivePlayerStatistics(Player player) {
return _gameHistoryDAO.getCompetitivePlayerStatistics(player);
}
}