diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/GameHistoryDAO.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/GameHistoryDAO.java index bfb0d76a5..43be11425 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/GameHistoryDAO.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/GameHistoryDAO.java @@ -236,8 +236,67 @@ public class GameHistoryDAO { } } - // Statistics - // select deck_name, format_name, sum(win), sum(lose) from - // (select winner_deck_name as deck_name, format_name, 1 as win, 0 as lose from game_history where winner='hsiale' union all select loser_deck_name as deck_name, format_name, 0 as win, 1 as lose from game_history where loser='hsiale') as u - // group by deck_name, format_name order by format_name + public List getCasualPlayerStatistics(Player player) { + try { + Connection connection = _dbAccess.getDataSource().getConnection(); + try { + PreparedStatement statement = connection.prepareStatement( + "select deck_name, format_name, sum(win), sum(lose) from" + + " (select winner_deck_name as deck_name, format_name, 1 as win, 0 as lose from game_history where winner=? and (tournament is null or tournament = 'Casual')" + + " union all select loser_deck_name as deck_name, format_name, 0 as win, 1 as lose from game_history where loser=? and (tournament is null or tournament = 'Casual')) as u" + + " group by deck_name, format_name order by format_name, deck_name"); + try { + statement.setString(1, player.getName()); + statement.setString(2, player.getName()); + ResultSet rs = statement.executeQuery(); + List result = new LinkedList(); + try { + while (rs.next()) + result.add(new PlayerStatistic(rs.getString(1), rs.getString(2), rs.getInt(3), rs.getInt(4))); + } finally { + rs.close(); + } + return result; + } finally { + statement.close(); + } + } finally { + connection.close(); + } + } catch (SQLException exp) { + throw new RuntimeException("Unable to get count of games played", exp); + } + } + + public List getCompetetivePlayerStatistics(Player player) { + try { + Connection connection = _dbAccess.getDataSource().getConnection(); + try { + PreparedStatement statement = connection.prepareStatement( + "select deck_name, format_name, sum(win), sum(lose) from" + + " (select winner_deck_name as deck_name, format_name, 1 as win, 0 as lose from game_history where winner=? and (tournament is not null and tournament <> 'Casual')" + + " union all select loser_deck_name as deck_name, format_name, 0 as win, 1 as lose from game_history where loser=? and (tournament is not null and tournament <> 'Casual')) as u" + + " group by deck_name, format_name order by format_name, deck_name"); + try { + statement.setString(1, player.getName()); + statement.setString(2, player.getName()); + ResultSet rs = statement.executeQuery(); + List result = new LinkedList(); + try { + while (rs.next()) + result.add(new PlayerStatistic(rs.getString(1), rs.getString(2), rs.getInt(3), rs.getInt(4))); + } finally { + rs.close(); + } + return result; + } finally { + statement.close(); + } + } finally { + connection.close(); + } + } catch (SQLException exp) { + throw new RuntimeException("Unable to get count of games played", exp); + } + } } diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/PlayerStatistic.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/PlayerStatistic.java new file mode 100644 index 000000000..102d8c544 --- /dev/null +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/PlayerStatistic.java @@ -0,0 +1,31 @@ +package com.gempukku.lotro.db; + +public class PlayerStatistic { + private String _deckName; + private String _formatName; + private int _wins; + private int _losses; + + public PlayerStatistic(String deckName, String formatName, int wins, int losses) { + _deckName = deckName; + _formatName = formatName; + _wins = wins; + _losses = losses; + } + + public String getDeckName() { + return _deckName; + } + + public String getFormatName() { + return _formatName; + } + + public int getWins() { + return _wins; + } + + public int getLosses() { + return _losses; + } +} diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/GameHistoryService.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/GameHistoryService.java index 6e6eada3a..f4377314b 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/GameHistoryService.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/GameHistoryService.java @@ -1,6 +1,7 @@ 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; @@ -56,4 +57,12 @@ public class GameHistoryService { stats.init(_gameHistoryDAO); return stats; } + + public List getCasualPlayerStatistics(Player player) { + return _gameHistoryDAO.getCasualPlayerStatistics(player); + } + + public List getCompetetivePlayerStatistics(Player player) { + return _gameHistoryDAO.getCompetetivePlayerStatistics(player); + } } diff --git a/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/ServerResource.java b/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/ServerResource.java index 90737ac41..8cb983708 100644 --- a/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/ServerResource.java +++ b/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/ServerResource.java @@ -2,6 +2,7 @@ package com.gempukku.lotro.server; import com.gempukku.lotro.chat.ChatServer; import com.gempukku.lotro.common.ApplicationRoot; +import com.gempukku.lotro.db.PlayerStatistic; import com.gempukku.lotro.db.vo.GameHistoryEntry; import com.gempukku.lotro.game.*; import com.gempukku.lotro.hall.HallServer; @@ -220,7 +221,7 @@ public class ServerResource extends AbstractResource { @Path("/stats") @GET @Produces(MediaType.APPLICATION_XML) - public Document getGameState( + public Document getServerStats( @QueryParam("startDay") String startDay, @QueryParam("length") String length, @QueryParam("participantId") String participantId, @@ -273,6 +274,48 @@ public class ServerResource extends AbstractResource { } } + @Path("/playerStats") + @GET + @Produces(MediaType.APPLICATION_XML) + public Document getServerStats( + @QueryParam("participantId") String participantId, + @Context HttpServletRequest request) throws ParserConfigurationException { + Player resourceOwner = getResourceOwnerSafely(request, participantId); + + List casualStatistics = _gameHistoryService.getCasualPlayerStatistics(resourceOwner); + List competetiveStatistics = _gameHistoryService.getCompetetivePlayerStatistics(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 competetive = doc.createElement("competetive"); + appendStatistics(competetiveStatistics, percFormat, doc, competetive); + stats.appendChild(competetive); + + doc.appendChild(stats); + return doc; + } + + private void appendStatistics(List 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())); + type.appendChild(entry); + } + } + private void logUser(HttpServletRequest request, String login) { request.getSession().setAttribute("logged", login); }