Adding tournament cache.

This commit is contained in:
marcins78@gmail.com
2012-07-03 15:11:40 +00:00
parent b974099212
commit 9b9424df55
3 changed files with 35 additions and 20 deletions

View File

@@ -59,13 +59,12 @@ public class SingleEliminationRecurringQueue implements TournamentQueue {
String parameters = _lotroFormat.getName() + "," + _collectionType.getCode() + "," + _collectionType.getFullName() + "," + tournamentName; String parameters = _lotroFormat.getName() + "," + _collectionType.getCode() + "," + _collectionType.getFullName() + "," + tournamentName;
_tournamentService.addTournament(_cost, tournamentId, SingleEliminationTournament.class.getName(), parameters, new Date());
for (Map.Entry<String, LotroDeck> playerDeck : _playerDecks.entrySet()) for (Map.Entry<String, LotroDeck> playerDeck : _playerDecks.entrySet())
_tournamentService.addPlayer(tournamentId, playerDeck.getKey(), playerDeck.getValue()); _tournamentService.addPlayer(tournamentId, playerDeck.getKey(), playerDeck.getValue());
tournamentQueueCallback.createTournament( Tournament tournament = _tournamentService.addTournament(_cost, tournamentId, SingleEliminationTournament.class.getName(), parameters, new Date());
new SingleEliminationTournament(_tournamentService, tournamentId, parameters));
tournamentQueueCallback.createTournament(tournament);
_playerDecks.clear(); _playerDecks.clear();
} }

View File

@@ -10,12 +10,18 @@ public class TournamentService {
private TournamentPlayerDAO _tournamentPlayerDao; private TournamentPlayerDAO _tournamentPlayerDao;
private TournamentMatchDAO _tournamentMatchDao; private TournamentMatchDAO _tournamentMatchDao;
private Map<String, Tournament> _tournamentById = new HashMap<String, Tournament>();
public TournamentService(TournamentDAO tournamentDao, TournamentPlayerDAO tournamentPlayerDao, TournamentMatchDAO tournamentMatchDao) { public TournamentService(TournamentDAO tournamentDao, TournamentPlayerDAO tournamentPlayerDao, TournamentMatchDAO tournamentMatchDao) {
_tournamentDao = tournamentDao; _tournamentDao = tournamentDao;
_tournamentPlayerDao = tournamentPlayerDao; _tournamentPlayerDao = tournamentPlayerDao;
_tournamentMatchDao = tournamentMatchDao; _tournamentMatchDao = tournamentMatchDao;
} }
public void clearCache() {
_tournamentById.clear();
}
public void addPlayer(String tournamentId, String playerName, LotroDeck deck) { public void addPlayer(String tournamentId, String playerName, LotroDeck deck) {
_tournamentPlayerDao.addPlayer(tournamentId, playerName, deck); _tournamentPlayerDao.addPlayer(tournamentId, playerName, deck);
} }
@@ -48,8 +54,9 @@ public class TournamentService {
return _tournamentMatchDao.getMatches(tournamentId, round); 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); _tournamentDao.addTournament(cost, tournamentId, tournamentClass, parameters, start);
return createTournamentAndStoreInCache(tournamentId, new TournamentInfo(cost, tournamentId, tournamentClass, parameters, start));
} }
public void markTournamentFinished(String tournamentId) { public void markTournamentFinished(String tournamentId) {
@@ -59,33 +66,38 @@ public class TournamentService {
public List<Tournament> getLiveTournaments() { public List<Tournament> getLiveTournaments() {
List<Tournament> result = new ArrayList<Tournament>(); List<Tournament> result = new ArrayList<Tournament>();
for (TournamentInfo tournamentInfo : _tournamentDao.getUnfinishedTournaments()) { for (TournamentInfo tournamentInfo : _tournamentDao.getUnfinishedTournaments()) {
String clazz = tournamentInfo.getTournamentClass(); Tournament tournament = _tournamentById.get(tournamentInfo.getTournamentId());
try { if (tournament == null)
Class<?> aClass = Class.forName(clazz); tournament = createTournamentAndStoreInCache(tournament.getTournamentId(), tournamentInfo);
Constructor<?> constructor = aClass.getConstructor(TournamentService.class, String.class, String.class); result.add(tournament);
Tournament tournament = (Tournament) constructor.newInstance(this, tournamentInfo.getTournamentId(), tournamentInfo.getParameters());
result.add(tournament);
} catch (Exception exp) {
throw new RuntimeException("Unable to create Tournament", exp);
}
} }
return result; return result;
} }
public Tournament getTournamentById(String tournamentId) { public Tournament getTournamentById(String tournamentId) {
TournamentInfo tournamentInfo = _tournamentDao.getTournamentById(tournamentId); Tournament tournament = _tournamentById.get(tournamentId);
if (tournamentInfo == null) if (tournament == null) {
return 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(); String clazz = tournamentInfo.getTournamentClass();
try { try {
Class<?> aClass = Class.forName(clazz); Class<?> aClass = Class.forName(clazz);
Constructor<?> constructor = aClass.getConstructor(TournamentService.class, String.class, String.class); 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) { } catch (Exception exp) {
throw new RuntimeException("Unable to create Tournament", exp); throw new RuntimeException("Unable to create Tournament", exp);
} }
_tournamentById.put(tournamentId, tournament);
return tournament;
} }
} }

View File

@@ -13,6 +13,7 @@ import com.gempukku.lotro.game.Player;
import com.gempukku.lotro.game.formats.LotroFormatLibrary; import com.gempukku.lotro.game.formats.LotroFormatLibrary;
import com.gempukku.lotro.hall.HallServer; import com.gempukku.lotro.hall.HallServer;
import com.gempukku.lotro.league.*; import com.gempukku.lotro.league.*;
import com.gempukku.lotro.tournament.TournamentService;
import com.sun.jersey.spi.resource.Singleton; import com.sun.jersey.spi.resource.Singleton;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
@@ -36,6 +37,8 @@ public class AdminResource extends AbstractResource {
@Context @Context
private LeagueService _leagueService; private LeagueService _leagueService;
@Context @Context
private TournamentService _tournamentService;
@Context
private DeckDAO _deckDao; private DeckDAO _deckDao;
@Context @Context
private LeagueDAO _leagueDao; private LeagueDAO _leagueDao;
@@ -61,6 +64,7 @@ public class AdminResource extends AbstractResource {
_deckDao.clearCache(); _deckDao.clearCache();
_merchantDao.clearCache(); _merchantDao.clearCache();
_leagueService.clearCache(); _leagueService.clearCache();
_tournamentService.clearCache();
return "OK"; return "OK";
} }