diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallServer.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallServer.java index 803085557..29e3e2b9c 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallServer.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallServer.java @@ -22,6 +22,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock; public class HallServer extends AbstractServer { private ChatServer _chatServer; private LeagueService _leagueService; + private TournamentService _tournamentService; private LotroCardBlueprintLibrary _library; private LotroFormatLibrary _formatLibrary; private CollectionsManager _collectionsManager; @@ -54,12 +55,15 @@ public class HallServer extends AbstractServer { _lotroServer = lotroServer; _chatServer = chatServer; _leagueService = leagueService; + _tournamentService = tournamentService; _library = library; _formatLibrary = formatLibrary; _collectionsManager = collectionsManager; _hallChat = _chatServer.createChatRoom("Game Hall", 10); - _tournamentQueues.put("fotr_queue", new SingleEliminationTournamentQueue(_formatLibrary.getFormat("fotr_block"), + _runningTournaments.addAll(tournamentService.getLiveTournaments()); + + _tournamentQueues.put("fotr_queue", new SingleEliminationTournamentQueue(0, _formatLibrary.getFormat("fotr_block"), new CollectionType("default", "All cards"), "fotrQueue-", 1, "Fellowship Block On-Demand", 4, tournamentService)); } @@ -483,8 +487,10 @@ public class HallServer extends AbstractServer { for (Tournament runningTournament : new ArrayList(_runningTournaments)) { runningTournament.advanceTournament(new HallTournamentCallback(runningTournament)); - if (runningTournament.isFinished()) + if (runningTournament.isFinished()) { + _tournamentService.markTournamentFinished(runningTournament.getTournamentId()); _runningTournaments.remove(runningTournament); + } } } finally { @@ -539,7 +545,7 @@ public class HallServer extends AbstractServer { public void gameCancelled() { createGameInternal(participants); } - }, _tournament.getLotroFormat(), _tournament.getTournamentName(), true); + }, _formatLibrary.getFormat(_tournament.getFormat()), _tournament.getTournamentName(), true); } finally { _hallDataAccessLock.writeLock().unlock(); } diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/SingleEliminationTournament.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/SingleEliminationTournament.java index b03bfffde..bce63130e 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/SingleEliminationTournament.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/SingleEliminationTournament.java @@ -3,7 +3,6 @@ package com.gempukku.lotro.tournament; import com.gempukku.lotro.competitive.PlayerStanding; import com.gempukku.lotro.competitive.StandingsProducer; import com.gempukku.lotro.db.vo.CollectionType; -import com.gempukku.lotro.game.LotroFormat; import com.gempukku.lotro.game.LotroGameParticipant; import com.gempukku.lotro.logic.vo.LotroDeck; @@ -12,8 +11,9 @@ import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; public class SingleEliminationTournament implements Tournament { + private int _cost; private CollectionType _collectionType; - private LotroFormat _lotroFormat; + private String _lotroFormat; private String _tournamentName; private Map _playerDecks; private TournamentService _tournamentService; @@ -36,9 +36,15 @@ public class SingleEliminationTournament implements Tournament { private List _tournamentMatches = new ArrayList(); - public SingleEliminationTournament(TournamentService tournamentService, String tournamentId, String tournamentName, CollectionType collectionType, LotroFormat lotroFormat) { + public SingleEliminationTournament(TournamentService tournamentService, String tournamentId, String parameters) { _tournamentService = tournamentService; + String[] params = parameters.split(","); + _cost = Integer.parseInt(params[0]); + _lotroFormat = params[1]; + _collectionType = new CollectionType(params[2], params[3]); + _tournamentName = params[4]; + _tournamentId = tournamentId; _playerDecks = new HashMap(_tournamentService.getPlayers(tournamentId)); @@ -47,10 +53,6 @@ public class SingleEliminationTournament implements Tournament { _playersInContention = new HashSet(_playerDecks.keySet()); _playersInContention.removeAll(_tournamentService.getDroppedPlayers(tournamentId)); - _tournamentName = tournamentName; - _collectionType = collectionType; - _lotroFormat = lotroFormat; - int round = 1; while (true) { List matches = _tournamentService.getMatches(tournamentId, round); @@ -80,13 +82,18 @@ public class SingleEliminationTournament implements Tournament { } } + @Override + public String getTournamentId() { + return _tournamentId; + } + @Override public CollectionType getCollectionType() { return _collectionType; } @Override - public LotroFormat getLotroFormat() { + public String getFormat() { return _lotroFormat; } diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/SingleEliminationTournamentQueue.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/SingleEliminationTournamentQueue.java index 967cd11f2..16184e432 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/SingleEliminationTournamentQueue.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/SingleEliminationTournamentQueue.java @@ -4,10 +4,12 @@ import com.gempukku.lotro.db.vo.CollectionType; import com.gempukku.lotro.game.LotroFormat; import com.gempukku.lotro.logic.vo.LotroDeck; +import java.util.Date; import java.util.HashMap; import java.util.Map; public class SingleEliminationTournamentQueue implements TournamentQueue { + private int _cost; private LotroFormat _lotroFormat; private CollectionType _collectionType; private String _tournamentQueueName; @@ -20,7 +22,8 @@ public class SingleEliminationTournamentQueue implements TournamentQueue { private String _tournamentIdPrefix; private int _tournamentIteration; - public SingleEliminationTournamentQueue(LotroFormat lotroFormat, CollectionType collectionType, String tournamentIdPrefix, int tournamentIteration, String tournamentQueueName, int playerCap, TournamentService tournamentService) { + public SingleEliminationTournamentQueue(int cost, LotroFormat lotroFormat, CollectionType collectionType, String tournamentIdPrefix, int tournamentIteration, String tournamentQueueName, int playerCap, TournamentService tournamentService) { + _cost = cost; _lotroFormat = lotroFormat; _collectionType = collectionType; _tournamentQueueName = tournamentQueueName; @@ -49,15 +52,19 @@ public class SingleEliminationTournamentQueue implements TournamentQueue { public synchronized boolean process(TournamentQueueCallback tournamentQueueCallback) { if (_playerDecks.size() == _playerCap) { String tournamentId = _tournamentIdPrefix + System.currentTimeMillis(); + String tournamentName = _tournamentQueueName + " - " + _tournamentIteration; + + String parameters = _cost + "," + _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, - _tournamentQueueName + " - " + _tournamentIteration, _collectionType, _lotroFormat)); + new SingleEliminationTournament(_tournamentService, tournamentId, parameters)); tournamentQueueCallback.createTournamentQueue( - new SingleEliminationTournamentQueue(_lotroFormat, _collectionType, _tournamentIdPrefix, _tournamentIteration + 1, + new SingleEliminationTournamentQueue(_cost, _lotroFormat, _collectionType, _tournamentIdPrefix, _tournamentIteration + 1, _tournamentQueueName, _playerCap, _tournamentService)); return true; } diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/Tournament.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/Tournament.java index b5c8a0180..4efde8e34 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/Tournament.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/Tournament.java @@ -2,14 +2,15 @@ package com.gempukku.lotro.tournament; import com.gempukku.lotro.competitive.PlayerStanding; import com.gempukku.lotro.db.vo.CollectionType; -import com.gempukku.lotro.game.LotroFormat; import java.util.List; public interface Tournament { + public String getTournamentId(); + public CollectionType getCollectionType(); - public LotroFormat getLotroFormat(); + public String getFormat(); public String getTournamentName(); diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentDAO.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentDAO.java new file mode 100644 index 000000000..f299abef7 --- /dev/null +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentDAO.java @@ -0,0 +1,12 @@ +package com.gempukku.lotro.tournament; + +import java.util.Date; +import java.util.List; + +public interface TournamentDAO { + public void addTournament(int cost, String tournamentId, String className, String type, Date start); + + public void markTournamentFinished(String tournamentId); + + public List getUnfinishedTournaments(); +} diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentInfo.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentInfo.java new file mode 100644 index 000000000..42c383141 --- /dev/null +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentInfo.java @@ -0,0 +1,39 @@ +package com.gempukku.lotro.tournament; + +import java.util.Date; + +public class TournamentInfo { + private int cost; + private String tournamentId; + private String tournamentClass; + private String parameters; + private Date start; + + public TournamentInfo(int cost, String tournamentId, String tournamentClass, String parameters, Date start) { + this.cost = cost; + this.tournamentId = tournamentId; + this.tournamentClass = tournamentClass; + this.parameters = parameters; + this.start = start; + } + + public int getCost() { + return cost; + } + + public String getTournamentId() { + return tournamentId; + } + + public String getTournamentClass() { + return tournamentClass; + } + + public String getParameters() { + return parameters; + } + + public Date getStart() { + return start; + } +} 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 40c9f448e..7ce15c477 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 @@ -2,15 +2,16 @@ package com.gempukku.lotro.tournament; import com.gempukku.lotro.logic.vo.LotroDeck; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.lang.reflect.Constructor; +import java.util.*; public class TournamentService { + private TournamentDAO _tournamentDao; private TournamentPlayerDAO _tournamentPlayerDao; private TournamentMatchDAO _tournamentMatchDao; - public TournamentService(TournamentPlayerDAO tournamentPlayerDao, TournamentMatchDAO tournamentMatchDao) { + public TournamentService(TournamentDAO tournamentDao, TournamentPlayerDAO tournamentPlayerDao, TournamentMatchDAO tournamentMatchDao) { + _tournamentDao = tournamentDao; _tournamentPlayerDao = tournamentPlayerDao; _tournamentMatchDao = tournamentMatchDao; } @@ -42,4 +43,29 @@ public class TournamentService { public List getMatches(String tournamentId, int round) { return _tournamentMatchDao.getMatches(tournamentId, round); } + + public void addTournament(int cost, String tournamentId, String tournamentClass, String parameters, Date start) { + _tournamentDao.addTournament(cost, tournamentId, tournamentClass, parameters, start); + } + + public void markTournamentFinished(String tournamentId) { + _tournamentDao.markTournamentFinished(tournamentId); + } + + 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); + } + } + return result; + } } diff --git a/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/provider/ServerProvider.java b/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/provider/ServerProvider.java index 082ded254..67ca2e238 100644 --- a/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/provider/ServerProvider.java +++ b/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/provider/ServerProvider.java @@ -11,6 +11,7 @@ import com.gempukku.lotro.game.formats.LotroFormatLibrary; import com.gempukku.lotro.hall.HallServer; import com.gempukku.lotro.league.LeagueService; import com.gempukku.lotro.merchant.MerchantService; +import com.gempukku.lotro.tournament.TournamentDAO; import com.gempukku.lotro.tournament.TournamentMatchDAO; import com.gempukku.lotro.tournament.TournamentPlayerDAO; import com.gempukku.lotro.tournament.TournamentService; @@ -54,6 +55,8 @@ public class ServerProvider implements InjectableProvider { @Context private GameHistoryDAO _gameHistoryDao; @Context + private TournamentDAO _tournamentDao; + @Context private TournamentPlayerDAO _tournamentPlayerDao; @Context private TournamentMatchDAO _tournamentMatchDao; @@ -147,7 +150,7 @@ public class ServerProvider implements InjectableProvider { private synchronized Injectable getTournamentServiceInjectable() { if (_tournamentServiceInjectable == null) { - final TournamentService tournamentService = new TournamentService(_tournamentPlayerDao, _tournamentMatchDao); + final TournamentService tournamentService = new TournamentService(_tournamentDao, _tournamentPlayerDao, _tournamentMatchDao); _tournamentServiceInjectable = new Injectable() { @Override public TournamentService getValue() {