diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/LeaguePointsDAO.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/LeaguePointsDAO.java index b22f9b695..b01a41fa9 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/LeaguePointsDAO.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/LeaguePointsDAO.java @@ -7,26 +7,29 @@ import java.sql.Connection; 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; +import java.util.concurrent.ConcurrentHashMap; public class LeaguePointsDAO { private DbAccess _dbAccess; + private Map> _leagueStandings = new ConcurrentHashMap>(); + private Map> _serieStandings = new ConcurrentHashMap>(); + public LeaguePointsDAO(DbAccess dbAccess) { _dbAccess = dbAccess; } - public void addPoints(League league, LeagueSerie season, String playerName, int points) { + public synchronized void addPoints(League league, LeagueSerie serie, String playerName, int points) { try { Connection conn = _dbAccess.getDataSource().getConnection(); try { PreparedStatement statement = conn.prepareStatement("insert into league_points (league_type, season_type, player_name, points) values (?, ?, ?, ?)"); try { statement.setString(1, league.getType()); - statement.setString(2, season.getType()); + statement.setString(2, serie.getType()); statement.setString(3, playerName); statement.setInt(4, points); statement.execute(); @@ -39,22 +42,37 @@ public class LeaguePointsDAO { } catch (SQLException exp) { throw new RuntimeException(exp); } + + _leagueStandings.remove(league); + _serieStandings.remove(serie); } - public Map getLeagueStandings(League league) { + public List getLeagueStandings(League league) { + List standings = _leagueStandings.get(league); + if (standings == null) { + synchronized (this) { + standings = loadLeagueStandings(league); + _leagueStandings.put(league, standings); + } + } + return standings; + } + + private List loadLeagueStandings(League league) { try { Connection conn = _dbAccess.getDataSource().getConnection(); try { - PreparedStatement statement = conn.prepareStatement("select player_name, sum(points) from league_points where league_type=? order by 2 desc"); + PreparedStatement statement = conn.prepareStatement("select player_name, sum(points), count(*) from league_points where league_type=? order by 2 desc"); try { statement.setString(1, league.getType()); ResultSet rs = statement.executeQuery(); try { - Map result = new LinkedHashMap(); + List result = new LinkedList(); 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 { @@ -72,6 +90,17 @@ public class LeaguePointsDAO { } public List getSerieStandings(League league, LeagueSerie serie) { + List standings = _serieStandings.get(serie); + if (standings == null) { + synchronized (this) { + standings = loadSerieStandings(league, serie); + _serieStandings.put(serie, standings); + } + } + return standings; + } + + private List loadSerieStandings(League league, LeagueSerie serie) { try { Connection conn = _dbAccess.getDataSource().getConnection(); try { diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/vo/League.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/vo/League.java index 81849c444..e9f86ce75 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/vo/League.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/vo/League.java @@ -42,4 +42,21 @@ public class League { public int getStart() { return _start; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + League league = (League) o; + + if (_id != league._id) return false; + + return true; + } + + @Override + public int hashCode() { + return _id; + } } diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/vo/LeagueSerie.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/vo/LeagueSerie.java index 28af3183f..51662442f 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/vo/LeagueSerie.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/vo/LeagueSerie.java @@ -28,4 +28,21 @@ public class LeagueSerie { public int getStart() { return _start; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + LeagueSerie that = (LeagueSerie) o; + + if (_type != null ? !_type.equals(that._type) : that._type != null) return false; + + return true; + } + + @Override + public int hashCode() { + return _type != null ? _type.hashCode() : 0; + } }