Added RecurringScheduledQueue.

This commit is contained in:
marcins78@gmail.com
2013-01-16 20:39:11 +00:00
parent feb07ddb91
commit 7686000c06
6 changed files with 149 additions and 100 deletions

View File

@@ -72,15 +72,15 @@ public class HallServer extends AbstractServer {
_pairingMechanismRegistry = pairingMechanismRegistry;
_hallChat = _chatServer.createChatRoom("Game Hall", true, 10);
_tournamentQueues.put("fotr_queue", new SingleEliminationRecurringQueue(635, "fotr_block",
_tournamentQueues.put("fotr_queue", new ImmediateRecurringQueue(635, "fotr_block",
CollectionType.ALL_CARDS, "fotrQueue-", "Fellowship Block", 8,
true, tournamentService, _tournamentPrizeSchemeRegistry.getTournamentPrizes("onDemand")));
_tournamentQueues.put("movie_queue", new SingleEliminationRecurringQueue(635, "movie",
true, tournamentService, _tournamentPrizeSchemeRegistry.getTournamentPrizes("onDemand"), _pairingMechanismRegistry.getPairingMechanism("singleElimination")));
_tournamentQueues.put("movie_queue", new ImmediateRecurringQueue(635, "movie",
CollectionType.ALL_CARDS, "movieQueue-", "Movie Block", 8,
true, tournamentService, _tournamentPrizeSchemeRegistry.getTournamentPrizes("onDemand")));
_tournamentQueues.put("expanded_queue", new SingleEliminationRecurringQueue(635, "expanded",
true, tournamentService, _tournamentPrizeSchemeRegistry.getTournamentPrizes("onDemand"), _pairingMechanismRegistry.getPairingMechanism("singleElimination")));
_tournamentQueues.put("expanded_queue", new ImmediateRecurringQueue(635, "expanded",
CollectionType.ALL_CARDS, "expandedQueue-", "Expanded", 8,
true, tournamentService, _tournamentPrizeSchemeRegistry.getTournamentPrizes("onDemand")));
true, tournamentService, _tournamentPrizeSchemeRegistry.getTournamentPrizes("onDemand"), _pairingMechanismRegistry.getPairingMechanism("singleElimination")));
}
@Override

View File

@@ -15,17 +15,41 @@ public abstract class AbstractTournamentQueue implements TournamentQueue {
protected Queue<String> _players = new LinkedList<String>();
protected Map<String, LotroDeck> _playerDecks = new HashMap<String, LotroDeck>();
protected boolean _requiresDeck;
private CollectionType _currencyCollection = CollectionType.MY_CARDS;
public AbstractTournamentQueue(int cost, boolean requiresDeck) {
protected final PairingMechanism _pairingMechanism;
protected final CollectionType _collectionType;
protected final TournamentPrizes _tournamentPrizes;
protected String _format;
public AbstractTournamentQueue(int cost, boolean requiresDeck, CollectionType collectionType, TournamentPrizes tournamentPrizes, PairingMechanism pairingMechanism, String format) {
_cost = cost;
_requiresDeck = requiresDeck;
_collectionType = collectionType;
_tournamentPrizes = tournamentPrizes;
_pairingMechanism = pairingMechanism;
_format = format;
}
@Override
public synchronized void joinPlayer(CollectionsManager collectionsManager, Player player, LotroDeck deck) {
if (!_players.contains(player.getName())) {
public String getPairingDescription() {
return _pairingMechanism.getPlayOffSystem();
}
@Override
public final CollectionType getCollectionType() {
return _collectionType;
}
@Override
public final String getPrizesDescription() {
return _tournamentPrizes.getPrizeDescription();
}
@Override
public final synchronized void joinPlayer(CollectionsManager collectionsManager, Player player, LotroDeck deck) {
if (!_players.contains(player.getName()) && isJoinable()) {
if (_cost <= 0 || collectionsManager.removeCurrencyFromPlayerCollection("Joined "+getTournamentQueueName()+" queue", player, _currencyCollection, _cost)) {
_players.add(player.getName());
if (_requiresDeck)
@@ -35,7 +59,7 @@ public abstract class AbstractTournamentQueue implements TournamentQueue {
}
@Override
public synchronized void leavePlayer(CollectionsManager collectionsManager, Player player) {
public final synchronized void leavePlayer(CollectionsManager collectionsManager, Player player) {
if (_players.contains(player.getName())) {
if (_cost > 0)
collectionsManager.addCurrencyToPlayerCollection(true, "Return for leaving "+getTournamentQueueName()+" queue", player, _currencyCollection, _cost);
@@ -45,7 +69,7 @@ public abstract class AbstractTournamentQueue implements TournamentQueue {
}
@Override
public synchronized void leaveAllPlayers(CollectionsManager collectionsManager) {
public final synchronized void leaveAllPlayers(CollectionsManager collectionsManager) {
if (_cost > 0) {
for (String player : _players)
collectionsManager.addCurrencyToPlayerCollection(false, "Return for leaving "+getTournamentQueueName()+" queue", player, _currencyCollection, _cost);
@@ -55,12 +79,27 @@ public abstract class AbstractTournamentQueue implements TournamentQueue {
}
@Override
public synchronized int getPlayerCount() {
public final synchronized int getPlayerCount() {
return _players.size();
}
@Override
public synchronized boolean isPlayerSignedUp(String player) {
public final synchronized boolean isPlayerSignedUp(String player) {
return _players.contains(player);
}
@Override
public final int getCost() {
return _cost;
}
@Override
public final boolean isRequiresDeck() {
return _requiresDeck;
}
@Override
public final String getFormat() {
return _format;
}
}

View File

@@ -6,43 +6,20 @@ import com.gempukku.lotro.db.vo.CollectionType;
import java.util.Date;
public class SingleEliminationRecurringQueue extends AbstractTournamentQueue implements TournamentQueue {
private String _format;
private CollectionType _collectionType;
public class ImmediateRecurringQueue extends AbstractTournamentQueue implements TournamentQueue {
private String _tournamentQueueName;
private int _playerCap;
private TournamentService _tournamentService;
private TournamentPrizes _tournamentPrizes;
private String _tournamentIdPrefix;
public SingleEliminationRecurringQueue(int cost, String format, CollectionType collectionType, String tournamentIdPrefix,
public ImmediateRecurringQueue(int cost, String format, CollectionType collectionType, String tournamentIdPrefix,
String tournamentQueueName, int playerCap, boolean requiresDeck,
TournamentService tournamentService, TournamentPrizes tournamentPrizes) {
super(cost, requiresDeck);
_format = format;
_collectionType = collectionType;
TournamentService tournamentService, TournamentPrizes tournamentPrizes, PairingMechanism pairingMechanism) {
super(cost, requiresDeck, collectionType, tournamentPrizes, pairingMechanism, format);
_tournamentQueueName = tournamentQueueName;
_playerCap = playerCap;
_tournamentIdPrefix = tournamentIdPrefix;
_tournamentService = tournamentService;
_tournamentPrizes = tournamentPrizes;
}
@Override
public int getCost() {
return _cost;
}
@Override
public String getFormat() {
return _format;
}
@Override
public CollectionType getCollectionType() {
return _collectionType;
}
@Override
@@ -50,26 +27,11 @@ public class SingleEliminationRecurringQueue extends AbstractTournamentQueue imp
return _tournamentQueueName;
}
@Override
public String getPrizesDescription() {
return _tournamentPrizes.getPrizeDescription();
}
@Override
public String getPairingDescription() {
return "Single elimination";
}
@Override
public String getStartCondition() {
return "When "+_playerCap+" players join";
}
@Override
public boolean isRequiresDeck() {
return _requiresDeck;
}
@Override
public synchronized boolean process(TournamentQueueCallback tournamentQueueCallback, CollectionsManager collectionsManager) {
if (_players.size() >= _playerCap) {

View File

@@ -0,0 +1,79 @@
package com.gempukku.lotro.tournament;
import com.gempukku.lotro.DateUtils;
import com.gempukku.lotro.collection.CollectionsManager;
import com.gempukku.lotro.db.vo.CollectionType;
import java.util.Date;
public class RecurringScheduledQueue extends AbstractTournamentQueue implements TournamentQueue {
private static final long _signupTimeBeforeStart = 1000 * 60 * 15; // 15 minutes before start
private long _repeatEvery;
private long _nextStart;
private String _nextStartText;
private String _tournamentIdPrefix;
private String _tournamentQueueName;
private int _minimumPlayers;
private TournamentService _tournamentService;
public RecurringScheduledQueue(long originalStart, long repeatEvery, String tournamentIdPrefix,
String tournamentQueueName, int cost, boolean requiresDeck,
CollectionType collectionType,
TournamentService tournamentService, TournamentPrizes tournamentPrizes, PairingMechanism pairingMechanism, String format, int minimumPlayers) {
super(cost, requiresDeck, collectionType, tournamentPrizes, pairingMechanism, format);
_repeatEvery = repeatEvery;
_tournamentIdPrefix = tournamentIdPrefix;
_tournamentQueueName = tournamentQueueName;
_tournamentService = tournamentService;
_minimumPlayers = minimumPlayers;
long number = (System.currentTimeMillis() - originalStart) / repeatEvery;
_nextStart = originalStart + (number + 1) * repeatEvery;
_nextStartText = DateUtils.formatDateWithHour(new Date(_nextStart));
}
@Override
public String getStartCondition() {
return _nextStartText;
}
@Override
public String getTournamentQueueName() {
return _tournamentQueueName;
}
@Override
public boolean isJoinable() {
return System.currentTimeMillis() >= _nextStart - _signupTimeBeforeStart;
}
@Override
public boolean process(TournamentQueueCallback tournamentQueueCallback, CollectionsManager collectionsManager) {
long now = System.currentTimeMillis();
if (now > _nextStart) {
if (_players.size() >= _minimumPlayers) {
String tournamentId = _tournamentIdPrefix+System.currentTimeMillis();
String tournamentName = _tournamentQueueName + " - " + DateUtils.getStringDateWithHour();
for (String player : _players)
_tournamentService.addPlayer(tournamentId, player, _playerDecks.get(player));
Tournament tournament = _tournamentService.addTournament(tournamentId, null, tournamentName, _format, _collectionType, Tournament.Stage.PLAYING_GAMES,
_pairingMechanism.getRegistryRepresentation(), _tournamentPrizes.getRegistryRepresentation(), new Date());
tournamentQueueCallback.createTournament(tournament);
_players.clear();
_playerDecks.clear();
} else {
leaveAllPlayers(collectionsManager);
}
_nextStart+=_repeatEvery;
_nextStartText = DateUtils.formatDateWithHour(new Date(_nextStart));
}
return false;
}
}

View File

@@ -13,43 +13,22 @@ public class ScheduledTournamentQueue extends AbstractTournamentQueue implements
private String _startCondition;
private TournamentService _tournamentService;
private String _tournamentName;
private String _format;
private CollectionType _collectionType;
private Tournament.Stage _stage;
private PairingMechanism _pairingMechanism;
private TournamentPrizes _prizeScheme;
private String _scheduledTournamentId;
public ScheduledTournamentQueue(String scheduledTournamentId, int cost, boolean requiresDeck, TournamentService tournamentService, long startTime,
String tournamentName, String format, CollectionType collectionType, Tournament.Stage stage,
PairingMechanism pairingMechanism, TournamentPrizes prizeScheme, int minimumPlayers) {
super(cost, requiresDeck);
PairingMechanism pairingMechanism, TournamentPrizes tournamentPrizes, int minimumPlayers) {
super(cost, requiresDeck, collectionType, tournamentPrizes, pairingMechanism, format);
_scheduledTournamentId = scheduledTournamentId;
_tournamentService = tournamentService;
_startTime = startTime;
_minimumPlayers = minimumPlayers;
_startCondition = DateUtils.formatDateWithHour(new Date(_startTime));
_tournamentName = tournamentName;
_format = format;
_collectionType = collectionType;
_stage = stage;
_pairingMechanism = pairingMechanism;
_prizeScheme = prizeScheme;
}
@Override
public int getCost() {
return _cost;
}
@Override
public String getFormat() {
return _format;
}
@Override
public CollectionType getCollectionType() {
return _collectionType;
}
@Override
@@ -57,11 +36,6 @@ public class ScheduledTournamentQueue extends AbstractTournamentQueue implements
return _tournamentName;
}
@Override
public String getPrizesDescription() {
return _prizeScheme.getPrizeDescription();
}
@Override
public String getPairingDescription() {
return _pairingMechanism.getPlayOffSystem() + ", minimum players: " + _minimumPlayers;
@@ -72,11 +46,6 @@ public class ScheduledTournamentQueue extends AbstractTournamentQueue implements
return _startCondition;
}
@Override
public boolean isRequiresDeck() {
return _requiresDeck;
}
@Override
public synchronized boolean process(TournamentQueueCallback tournamentQueueCallback, CollectionsManager collectionsManager) {
long now = System.currentTimeMillis();
@@ -87,7 +56,7 @@ public class ScheduledTournamentQueue extends AbstractTournamentQueue implements
_tournamentService.addPlayer(_scheduledTournamentId, player, _playerDecks.get(player));
Tournament tournament = _tournamentService.addTournament(_scheduledTournamentId, null, _tournamentName, _format, _collectionType, _stage,
_pairingMechanism.getRegistryRepresentation(), _prizeScheme.getRegistryRepresentation(), new Date());
_pairingMechanism.getRegistryRepresentation(), _tournamentPrizes.getRegistryRepresentation(), new Date());
tournamentQueueCallback.createTournament(tournament);
} else {
_tournamentService.updateScheduledTournamentStarted(_scheduledTournamentId);

View File

@@ -15,8 +15,8 @@ public class SingleEliminationRecurringQueueTest {
public void joiningQueue() {
TournamentService tournamentService = Mockito.mock(TournamentService.class);
SingleEliminationRecurringQueue queue = new SingleEliminationRecurringQueue(10, "format", CollectionType.MY_CARDS,
"id-", "name-", 2, false, tournamentService, new NoPrizes());
ImmediateRecurringQueue queue = new ImmediateRecurringQueue(10, "format", CollectionType.MY_CARDS,
"id-", "name-", 2, false, tournamentService, new NoPrizes(), new SingleEliminationPairing("singleElimination"));
Player player = new Player(1, "p1", "u", null);
@@ -37,8 +37,8 @@ public class SingleEliminationRecurringQueueTest {
public void leavingQueue() {
TournamentService tournamentService = Mockito.mock(TournamentService.class);
SingleEliminationRecurringQueue queue = new SingleEliminationRecurringQueue(10, "format", CollectionType.MY_CARDS,
"id-", "name-", 2, false, tournamentService, new NoPrizes());
ImmediateRecurringQueue queue = new ImmediateRecurringQueue(10, "format", CollectionType.MY_CARDS,
"id-", "name-", 2, false, tournamentService, new NoPrizes(), new SingleEliminationPairing("singleElimination"));
Player player = new Player(1, "p1", "u", null);
@@ -62,8 +62,8 @@ public class SingleEliminationRecurringQueueTest {
public void cancellingQueue() {
TournamentService tournamentService = Mockito.mock(TournamentService.class);
SingleEliminationRecurringQueue queue = new SingleEliminationRecurringQueue(10, "format", CollectionType.MY_CARDS,
"id-", "name-", 2, false, tournamentService, new NoPrizes());
ImmediateRecurringQueue queue = new ImmediateRecurringQueue(10, "format", CollectionType.MY_CARDS,
"id-", "name-", 2, false, tournamentService, new NoPrizes(), new SingleEliminationPairing("singleElimination"));
Player player = new Player(1, "p1", "u", null);
@@ -92,8 +92,8 @@ public class SingleEliminationRecurringQueueTest {
Mockito.eq(CollectionType.MY_CARDS), Mockito.eq(Tournament.Stage.PLAYING_GAMES), Mockito.eq("singleElimination"), Mockito.anyString(), Mockito.<Date>any()))
.thenReturn(tournament);
SingleEliminationRecurringQueue queue = new SingleEliminationRecurringQueue(10, "format", CollectionType.MY_CARDS,
"id-", "name-", 2, false, tournamentService, new NoPrizes());
ImmediateRecurringQueue queue = new ImmediateRecurringQueue(10, "format", CollectionType.MY_CARDS,
"id-", "name-", 2, false, tournamentService, new NoPrizes(), new SingleEliminationPairing("singleElimination"));
Player player1 = new Player(1, "p1", "u", null);
@@ -139,8 +139,8 @@ public class SingleEliminationRecurringQueueTest {
Mockito.eq(CollectionType.MY_CARDS), Mockito.eq(Tournament.Stage.PLAYING_GAMES), Mockito.eq("singleElimination"), Mockito.anyString(), Mockito.<Date>any()))
.thenReturn(tournament);
SingleEliminationRecurringQueue queue = new SingleEliminationRecurringQueue(10, "format", CollectionType.MY_CARDS,
"id-", "name-", 2, false, tournamentService, new NoPrizes());
ImmediateRecurringQueue queue = new ImmediateRecurringQueue(10, "format", CollectionType.MY_CARDS,
"id-", "name-", 2, false, tournamentService, new NoPrizes(), new SingleEliminationPairing("singleElimination"));
Player player1 = new Player(1, "p1", "u", null);
Player player2 = new Player(2, "p2", "u", null);