From 9b9424df55802b558ac52fb01e6a741d8d3dc4c8 Mon Sep 17 00:00:00 2001 From: "marcins78@gmail.com" Date: Tue, 3 Jul 2012 15:11:40 +0000 Subject: [PATCH] Adding tournament cache. --- .../SingleEliminationRecurringQueue.java | 7 ++- .../lotro/tournament/TournamentService.java | 44 ++++++++++++------- .../gempukku/lotro/server/AdminResource.java | 4 ++ 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/SingleEliminationRecurringQueue.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/SingleEliminationRecurringQueue.java index 4b9d1379b..e9450e768 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/SingleEliminationRecurringQueue.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/SingleEliminationRecurringQueue.java @@ -59,13 +59,12 @@ public class SingleEliminationRecurringQueue implements TournamentQueue { String parameters = _lotroFormat.getName() + "," + _collectionType.getCode() + "," + _collectionType.getFullName() + "," + tournamentName; - _tournamentService.addTournament(_cost, tournamentId, SingleEliminationTournament.class.getName(), parameters, new Date()); - for (Map.Entry playerDeck : _playerDecks.entrySet()) _tournamentService.addPlayer(tournamentId, playerDeck.getKey(), playerDeck.getValue()); - tournamentQueueCallback.createTournament( - new SingleEliminationTournament(_tournamentService, tournamentId, parameters)); + Tournament tournament = _tournamentService.addTournament(_cost, tournamentId, SingleEliminationTournament.class.getName(), parameters, new Date()); + + tournamentQueueCallback.createTournament(tournament); _playerDecks.clear(); } diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentService.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentService.java index 029abf8b8..b5f8ffb82 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentService.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentService.java @@ -10,12 +10,18 @@ public class TournamentService { private TournamentPlayerDAO _tournamentPlayerDao; private TournamentMatchDAO _tournamentMatchDao; + private Map _tournamentById = new HashMap(); + public TournamentService(TournamentDAO tournamentDao, TournamentPlayerDAO tournamentPlayerDao, TournamentMatchDAO tournamentMatchDao) { _tournamentDao = tournamentDao; _tournamentPlayerDao = tournamentPlayerDao; _tournamentMatchDao = tournamentMatchDao; } + public void clearCache() { + _tournamentById.clear(); + } + public void addPlayer(String tournamentId, String playerName, LotroDeck deck) { _tournamentPlayerDao.addPlayer(tournamentId, playerName, deck); } @@ -48,8 +54,9 @@ public class TournamentService { return _tournamentMatchDao.getMatches(tournamentId, round); } - public void addTournament(int cost, String tournamentId, String tournamentClass, String parameters, Date start) { + public Tournament addTournament(int cost, String tournamentId, String tournamentClass, String parameters, Date start) { _tournamentDao.addTournament(cost, tournamentId, tournamentClass, parameters, start); + return createTournamentAndStoreInCache(tournamentId, new TournamentInfo(cost, tournamentId, tournamentClass, parameters, start)); } public void markTournamentFinished(String tournamentId) { @@ -59,33 +66,38 @@ public class TournamentService { public List getLiveTournaments() { List result = new ArrayList(); for (TournamentInfo tournamentInfo : _tournamentDao.getUnfinishedTournaments()) { - String clazz = tournamentInfo.getTournamentClass(); - try { - Class aClass = Class.forName(clazz); - Constructor constructor = aClass.getConstructor(TournamentService.class, String.class, String.class); - Tournament tournament = (Tournament) constructor.newInstance(this, tournamentInfo.getTournamentId(), tournamentInfo.getParameters()); - - result.add(tournament); - } catch (Exception exp) { - throw new RuntimeException("Unable to create Tournament", exp); - } + Tournament tournament = _tournamentById.get(tournamentInfo.getTournamentId()); + if (tournament == null) + tournament = createTournamentAndStoreInCache(tournament.getTournamentId(), tournamentInfo); + result.add(tournament); } return result; } public Tournament getTournamentById(String tournamentId) { - TournamentInfo tournamentInfo = _tournamentDao.getTournamentById(tournamentId); - if (tournamentInfo == null) - return null; + Tournament tournament = _tournamentById.get(tournamentId); + if (tournament == null) { + TournamentInfo tournamentInfo = _tournamentDao.getTournamentById(tournamentId); + if (tournamentInfo == null) + return null; + + tournament = createTournamentAndStoreInCache(tournamentId, tournamentInfo); + } + return tournament; + } + + private Tournament createTournamentAndStoreInCache(String tournamentId, TournamentInfo tournamentInfo) { + Tournament tournament; String clazz = tournamentInfo.getTournamentClass(); try { Class aClass = Class.forName(clazz); Constructor constructor = aClass.getConstructor(TournamentService.class, String.class, String.class); - Tournament tournament = (Tournament) constructor.newInstance(this, tournamentInfo.getTournamentId(), tournamentInfo.getParameters()); + tournament = (Tournament) constructor.newInstance(this, tournamentInfo.getTournamentId(), tournamentInfo.getParameters()); - return tournament; } catch (Exception exp) { throw new RuntimeException("Unable to create Tournament", exp); } + _tournamentById.put(tournamentId, tournament); + return tournament; } } diff --git a/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/AdminResource.java b/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/AdminResource.java index 2abe90240..35c75d278 100644 --- a/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/AdminResource.java +++ b/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/AdminResource.java @@ -13,6 +13,7 @@ import com.gempukku.lotro.game.Player; import com.gempukku.lotro.game.formats.LotroFormatLibrary; import com.gempukku.lotro.hall.HallServer; import com.gempukku.lotro.league.*; +import com.gempukku.lotro.tournament.TournamentService; import com.sun.jersey.spi.resource.Singleton; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -36,6 +37,8 @@ public class AdminResource extends AbstractResource { @Context private LeagueService _leagueService; @Context + private TournamentService _tournamentService; + @Context private DeckDAO _deckDao; @Context private LeagueDAO _leagueDao; @@ -61,6 +64,7 @@ public class AdminResource extends AbstractResource { _deckDao.clearCache(); _merchantDao.clearCache(); _leagueService.clearCache(); + _tournamentService.clearCache(); return "OK"; }