Adding games played to league results.
This commit is contained in:
@@ -8,6 +8,8 @@ import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class LeaguePointsDAO {
|
||||
@@ -69,21 +71,22 @@ public class LeaguePointsDAO {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, Integer> getSerieStandings(League league, LeagueSerie serie) {
|
||||
public List<Standing> getSerieStandings(League league, LeagueSerie serie) {
|
||||
try {
|
||||
Connection conn = _dbAccess.getDataSource().getConnection();
|
||||
try {
|
||||
PreparedStatement statement = conn.prepareStatement("select player_name, sum(points) from league_points where league_type=? and season_type=? group by player_name order by 2 desc");
|
||||
PreparedStatement statement = conn.prepareStatement("select player_name, sum(points), count(*) from league_points where league_type=? and season_type=? group by player_name order by 2 desc, 3 asc");
|
||||
try {
|
||||
statement.setString(1, league.getType());
|
||||
statement.setString(2, serie.getType());
|
||||
ResultSet rs = statement.executeQuery();
|
||||
try {
|
||||
Map<String, Integer> result = new LinkedHashMap<String, Integer>();
|
||||
List<Standing> result = new LinkedList<Standing>();
|
||||
while (rs.next()) {
|
||||
String playerName = rs.getString(1);
|
||||
int sumPoints = rs.getInt(2);
|
||||
result.put(playerName, sumPoints);
|
||||
int gamesPlayed = rs.getInt(3);
|
||||
result.add(new Standing(playerName, sumPoints, gamesPlayed));
|
||||
}
|
||||
return result;
|
||||
} finally {
|
||||
@@ -99,4 +102,28 @@ public class LeaguePointsDAO {
|
||||
throw new RuntimeException(exp);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Standing {
|
||||
private String _player;
|
||||
private int _points;
|
||||
private int _gamesPlayed;
|
||||
|
||||
public Standing(String player, int points, int gamesPlayed) {
|
||||
_player = player;
|
||||
_points = points;
|
||||
_gamesPlayed = gamesPlayed;
|
||||
}
|
||||
|
||||
public int getGamesPlayed() {
|
||||
return _gamesPlayed;
|
||||
}
|
||||
|
||||
public String getPlayer() {
|
||||
return _player;
|
||||
}
|
||||
|
||||
public int getPoints() {
|
||||
return _points;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@ import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Singleton
|
||||
@Path("/league")
|
||||
@@ -51,12 +50,13 @@ public class LeagueResource extends AbstractResource {
|
||||
serieElem.setAttribute("start", String.valueOf(serie.getStart()));
|
||||
serieElem.setAttribute("end", String.valueOf(serie.getEnd()));
|
||||
|
||||
Map<String, Integer> standings = _leaguePointsDao.getSerieStandings(league, serie);
|
||||
for (Map.Entry<String, Integer> playerPoints : standings.entrySet()) {
|
||||
Element standing = doc.createElement("standing");
|
||||
standing.setAttribute("player", playerPoints.getKey());
|
||||
standing.setAttribute("points", playerPoints.getValue().toString());
|
||||
serieElem.appendChild(standing);
|
||||
final List<LeaguePointsDAO.Standing> standings = _leaguePointsDao.getSerieStandings(league, serie);
|
||||
for (LeaguePointsDAO.Standing standing : standings) {
|
||||
Element standingElem = doc.createElement("standing");
|
||||
standingElem.setAttribute("player", standing.getPlayer());
|
||||
standingElem.setAttribute("points", String.valueOf(standing.getPoints()));
|
||||
standingElem.setAttribute("gamesPlayed", String.valueOf(standing.getGamesPlayed()));
|
||||
serieElem.appendChild(standingElem);
|
||||
}
|
||||
|
||||
leagueElem.appendChild(serieElem);
|
||||
|
||||
@@ -50,21 +50,24 @@ var LeagueResultsUI = Class.extend({
|
||||
if (standings.length > 0) {
|
||||
var standingsTable = $("<table class='standings'></table>");
|
||||
|
||||
standingsTable.append("<tr><th>Standing</th><th>Player</th><th>Points</th></tr>");
|
||||
standingsTable.append("<tr><th>Standing</th><th>Player</th><th>Points</th><th>Games played</th></tr>");
|
||||
|
||||
var lastPoints = -1;
|
||||
var lastGamesPlayed = -1;
|
||||
var currentStanding = -1;
|
||||
|
||||
for (var k = 0; k < standings.length; k++) {
|
||||
var standing = standings[k];
|
||||
var player = standing.getAttribute("player");
|
||||
var points = parseInt(standing.getAttribute("points"));
|
||||
if (points != lastPoints) {
|
||||
var gamesPlayed = parseInt(standing.getAttribute("gamesPlayed"));
|
||||
if (points != lastPoints || gamesPlayed != lastGamesPlayed) {
|
||||
lastPoints = points;
|
||||
lastGamesPlayed = gamesPlayed;
|
||||
currentStanding = k + 1;
|
||||
}
|
||||
|
||||
standingsTable.append("<tr><td>" + currentStanding + "</td><td>" + player + "</td><td>" + points + "</td></tr>");
|
||||
standingsTable.append("<tr><td>" + currentStanding + "</td><td>" + player + "</td><td>" + points + "</td><td>" + gamesPlayed + "</td></tr>");
|
||||
}
|
||||
|
||||
$("#leagueResults").append(standingsTable);
|
||||
|
||||
Reference in New Issue
Block a user