Adding player stats.

This commit is contained in:
marcins78@gmail.com
2012-07-11 09:36:40 +00:00
parent a1fe12b328
commit 6dd7cf7d8c
4 changed files with 147 additions and 5 deletions

View File

@@ -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<PlayerStatistic> 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<PlayerStatistic> result = new LinkedList<PlayerStatistic>();
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<PlayerStatistic> 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<PlayerStatistic> result = new LinkedList<PlayerStatistic>();
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);
}
}
}

View File

@@ -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;
}
}

View File

@@ -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<PlayerStatistic> getCasualPlayerStatistics(Player player) {
return _gameHistoryDAO.getCasualPlayerStatistics(player);
}
public List<PlayerStatistic> getCompetetivePlayerStatistics(Player player) {
return _gameHistoryDAO.getCompetetivePlayerStatistics(player);
}
}

View File

@@ -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<PlayerStatistic> casualStatistics = _gameHistoryService.getCasualPlayerStatistics(resourceOwner);
List<PlayerStatistic> 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<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()));
type.appendChild(entry);
}
}
private void logUser(HttpServletRequest request, String login) {
request.getSession().setAttribute("logged", login);
}