Added tournament recovery.

This commit is contained in:
marcins78@gmail.com
2012-07-03 11:49:02 +00:00
parent 421848f07d
commit 7bde45627b
8 changed files with 123 additions and 22 deletions

View File

@@ -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<Tournament>(_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();
}

View File

@@ -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<String, LotroDeck> _playerDecks;
private TournamentService _tournamentService;
@@ -36,9 +36,15 @@ public class SingleEliminationTournament implements Tournament {
private List<TournamentMatch> _tournamentMatches = new ArrayList<TournamentMatch>();
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<String, LotroDeck>(_tournamentService.getPlayers(tournamentId));
@@ -47,10 +53,6 @@ public class SingleEliminationTournament implements Tournament {
_playersInContention = new HashSet<String>(_playerDecks.keySet());
_playersInContention.removeAll(_tournamentService.getDroppedPlayers(tournamentId));
_tournamentName = tournamentName;
_collectionType = collectionType;
_lotroFormat = lotroFormat;
int round = 1;
while (true) {
List<TournamentMatch> 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;
}

View File

@@ -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<String, LotroDeck> 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;
}

View File

@@ -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();

View File

@@ -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<TournamentInfo> getUnfinishedTournaments();
}

View File

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

View File

@@ -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<TournamentMatch> 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<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);
}
}
return result;
}
}

View File

@@ -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, Type> {
@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<Context, Type> {
private synchronized Injectable<TournamentService> getTournamentServiceInjectable() {
if (_tournamentServiceInjectable == null) {
final TournamentService tournamentService = new TournamentService(_tournamentPlayerDao, _tournamentMatchDao);
final TournamentService tournamentService = new TournamentService(_tournamentDao, _tournamentPlayerDao, _tournamentMatchDao);
_tournamentServiceInjectable = new Injectable<TournamentService>() {
@Override
public TournamentService getValue() {