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 37eab4490..e6e8aa37c 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 @@ -115,5 +115,10 @@ public class LeaguePointsDAO { public int getPoints() { return _points; } + + public void addPointsForMatch(int points) { + _points += points; + _gamesPlayed++; + } } } diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/LeagueService.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/LeagueService.java index 83501fdc1..474288209 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/LeagueService.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/LeagueService.java @@ -31,6 +31,7 @@ public class LeagueService { private LeagueDAO _leagueDao; private LeaguePointsDAO _leaguePointsDao; + // Cached on this layer private LeagueMatchDAO _leagueMatchDao; private LeagueParticipationDAO _leagueParticipationDAO; private CollectionsManager _collectionsManager; @@ -44,6 +45,9 @@ public class LeagueService { private ExpireObjectCache> _cachedLeagueMatches = new ExpireObjectCache>(); private ExpireObjectCache> _cachedSerieMatches = new ExpireObjectCache>(); + private ExpireObjectCache> _cachedLeaguePoints = new ExpireObjectCache>(); + private ExpireObjectCache> _cachedSeriePoints = new ExpireObjectCache>(); + private int _activeLeaguesLoadedDate; private List _activeLeagues; @@ -99,6 +103,26 @@ public class LeagueService { }); } + private Map getLeaguePoints(final League league) { + return _cachedLeaguePoints.getCachedObject(league, + new Producable>() { + @Override + public Map produce(League key) { + return new ConcurrentHashMap(_leaguePointsDao.getLeaguePoints(league)); + } + }); + } + + private Map getLeagueSeriePoints(final League league, final LeagueSerieData serie) { + return _cachedSeriePoints.getCachedObject(serie, + new Producable>() { + @Override + public Map produce(LeagueSerieData key) { + return new ConcurrentHashMap(_leaguePointsDao.getLeagueSeriePoints(league, serie)); + } + }); + } + public List getActiveLeagues() { if (DateUtils.getCurrentDate() == _activeLeaguesLoadedDate) return Collections.unmodifiableList(_activeLeagues); @@ -204,8 +228,9 @@ public class LeagueService { public void reportLeagueGameResult(League league, LeagueSerieData serie, String winner, String loser) { addMatch(league, serie, winner, loser); - _leaguePointsDao.addPoints(league, serie, winner, 2); - _leaguePointsDao.addPoints(league, serie, loser, 1); + addPoints(league, serie, winner, 2); + addPoints(league, serie, loser, 1); + _leagueStandings.remove(league); _leagueSerieStandings.remove(serie); @@ -213,6 +238,34 @@ public class LeagueService { awardPrizesToPlayer(league, serie, loser, false); } + private void addPoints(League league, LeagueSerieData serie, String winner, int points) { + _leaguePointsDao.addPoints(league, serie, winner, points); + storeNewLeaguePoints(league, winner, points); + storeNewLeagueSeriePoints(league, serie, winner, points); + } + + private void storeNewLeagueSeriePoints(League league, LeagueSerieData serie, String winner, int points) { + final Map leagueSeriePoints = getLeagueSeriePoints(league, serie); + final LeaguePointsDAO.Points playerPoints = leagueSeriePoints.get(winner); + if (playerPoints != null) + playerPoints.addPointsForMatch(points); + else { + LeaguePointsDAO.Points newPoints = new LeaguePointsDAO.Points(points, 1); + leagueSeriePoints.put(winner, newPoints); + } + } + + private void storeNewLeaguePoints(League league, String winner, int points) { + final Map leaguePoints = getLeaguePoints(league); + final LeaguePointsDAO.Points playerPoints = leaguePoints.get(winner); + if (playerPoints != null) + playerPoints.addPointsForMatch(points); + else { + LeaguePointsDAO.Points newPoints = new LeaguePointsDAO.Points(points, 1); + leaguePoints.put(winner, newPoints); + } + } + private void addMatch(League league, LeagueSerieData serie, String winner, String loser) { LeagueMatch match = new LeagueMatch(winner, loser); _leagueMatchDao.addPlayedMatch(league, serie, match); @@ -270,14 +323,14 @@ public class LeagueService { } private List createLeagueSerieStandings(League league, LeagueSerieData leagueSerie) { - final Map points = _leaguePointsDao.getLeagueSeriePoints(league, leagueSerie); + final Map points = getLeagueSeriePoints(league, leagueSerie); final Collection matches = getLeagueSerieMatches(league, leagueSerie); return createStandingsForMatchesAndPoints(points, matches); } private List createLeagueStandings(League league) { - final Map points = _leaguePointsDao.getLeaguePoints(league); + final Map points = getLeaguePoints(league); final Collection matches = getLeagueMatches(league); return createStandingsForMatchesAndPoints(points, matches);