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;
_tournamentService.addTournament(_cost, tournamentId, SingleEliminationTournament.class.getName(), parameters, new Date());
for (Map.Entry<String, LotroDeck> 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();
}

View File

@@ -10,12 +10,18 @@ public class TournamentService {
private TournamentPlayerDAO _tournamentPlayerDao;
private TournamentMatchDAO _tournamentMatchDao;
private Map<String, Tournament> _tournamentById = new HashMap<String, Tournament>();
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<Tournament> getLiveTournaments() {
List<Tournament> result = new ArrayList<Tournament>();
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;
}
}

View File

@@ -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";
}