From 915651335a2d4b8020fea8318100892bdf9f1b02 Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Tue, 29 Apr 2014 17:32:24 +0200 Subject: [PATCH] Fixed that in tournament a automatically generated deck will be submitted if player doesn't submit a deck in time (fixes #421). --- .../mage/client/dialog/AddLandDialog.java | 4 +- .../client/dialog/NewTournamentDialog.java | 3 +- .../client/tournament/TournamentPanel.form | 73 ++++++++------- .../client/tournament/TournamentPanel.java | 66 +++++++------ Mage.Common/src/mage/view/TournamentView.java | 9 +- .../java/mage/player/ai/ComputerPlayer.java | 2 +- .../tournament/TournamentController.java | 1 - Mage/src/mage/game/Table.java | 34 ++++--- Mage/src/mage/game/tournament/Tournament.java | 5 +- .../mage/game/tournament/TournamentImpl.java | 23 ++++- .../game/tournament/TournamentPlayer.java | 35 +++++-- Mage/src/mage/util/TournamentUtil.java | 93 +++++++++++++++++++ 12 files changed, 261 insertions(+), 87 deletions(-) create mode 100644 Mage/src/mage/util/TournamentUtil.java diff --git a/Mage.Client/src/main/java/mage/client/dialog/AddLandDialog.java b/Mage.Client/src/main/java/mage/client/dialog/AddLandDialog.java index 2b2ee28d31..24802e4ecc 100644 --- a/Mage.Client/src/main/java/mage/client/dialog/AddLandDialog.java +++ b/Mage.Client/src/main/java/mage/client/dialog/AddLandDialog.java @@ -50,9 +50,9 @@ import mage.constants.Rarity; public class AddLandDialog extends MageDialog { private Deck deck; - private Set setCodesland = new HashSet(); + private final Set setCodesland = new HashSet<>(); - private static int DEFAULT_SEALED_DECK_CARD_NUMBER = 40; + private static final int DEFAULT_SEALED_DECK_CARD_NUMBER = 40; /** Creates new form AddLandDialog */ public AddLandDialog() { diff --git a/Mage.Client/src/main/java/mage/client/dialog/NewTournamentDialog.java b/Mage.Client/src/main/java/mage/client/dialog/NewTournamentDialog.java index 0ac337d9ae..5a54960e65 100644 --- a/Mage.Client/src/main/java/mage/client/dialog/NewTournamentDialog.java +++ b/Mage.Client/src/main/java/mage/client/dialog/NewTournamentDialog.java @@ -428,7 +428,8 @@ public class NewTournamentDialog extends MageDialog { if (tOptions.getLimitedOptions() == null) { tOptions.setLimitedOptions(new LimitedOptions()); } - tOptions.getLimitedOptions().setConstructionTime((Integer)this.spnConstructTime.getValue() * 60); + tOptions.getLimitedOptions().setConstructionTime(60); + // tOptions.getLimitedOptions().setConstructionTime((Integer)this.spnConstructTime.getValue() * 60); if (tournamentType.isCubeBooster()) { tOptions.getLimitedOptions().setDraftCubeName(this.cbDraftCube.getSelectedItem().toString()); } else { diff --git a/Mage.Client/src/main/java/mage/client/tournament/TournamentPanel.form b/Mage.Client/src/main/java/mage/client/tournament/TournamentPanel.form index c31f8b1d97..f3da192341 100644 --- a/Mage.Client/src/main/java/mage/client/tournament/TournamentPanel.form +++ b/Mage.Client/src/main/java/mage/client/tournament/TournamentPanel.form @@ -22,7 +22,7 @@ - + @@ -30,7 +30,7 @@ - + @@ -49,26 +49,30 @@ - + - - - - - - - - - - - + + + + + + + + - - - + + + + + + + + + + @@ -84,16 +88,17 @@ + - + - - + + - + @@ -109,7 +114,6 @@ - @@ -126,36 +130,43 @@ - - + - - - - - - - + + + + + + + + + + + + + + + diff --git a/Mage.Client/src/main/java/mage/client/tournament/TournamentPanel.java b/Mage.Client/src/main/java/mage/client/tournament/TournamentPanel.java index f3669cdad5..0f5f29b01b 100644 --- a/Mage.Client/src/main/java/mage/client/tournament/TournamentPanel.java +++ b/Mage.Client/src/main/java/mage/client/tournament/TournamentPanel.java @@ -209,6 +209,8 @@ public class TournamentPanel extends javax.swing.JPanel { firstInitDone = true; } + txtTournamentState.setText(tournament.getTournamentState()); + if (txtEndTime == null) { return; } @@ -267,8 +269,9 @@ public class TournamentPanel extends javax.swing.JPanel { txtType = new javax.swing.JTextField(); lblStartTime = new javax.swing.JLabel(); txtStartTime = new javax.swing.JTextField(); - lablEndTime = new javax.swing.JLabel(); txtEndTime = new javax.swing.JTextField(); + lblTournamentState = new javax.swing.JLabel(); + txtTournamentState = new javax.swing.JTextField(); btnQuitTournament = new javax.swing.JButton(); btnCloseWindow = new javax.swing.JButton(); jSplitPane2 = new javax.swing.JSplitPane(); @@ -286,7 +289,6 @@ public class TournamentPanel extends javax.swing.JPanel { txtName.setEditable(false); txtName.setHorizontalAlignment(javax.swing.JTextField.LEFT); - txtName.setText("tournament name"); txtName.setFocusable(false); txtName.setMaximumSize(new java.awt.Dimension(50, 22)); @@ -295,23 +297,24 @@ public class TournamentPanel extends javax.swing.JPanel { txtType.setEditable(false); txtType.setHorizontalAlignment(javax.swing.JTextField.LEFT); - txtType.setText("tournament type"); txtType.setFocusable(false); - lblStartTime.setText("Start time:"); + lblStartTime.setText("Start / end time:"); txtStartTime.setEditable(false); txtStartTime.setHorizontalAlignment(javax.swing.JTextField.CENTER); - txtStartTime.setText("jTextField1"); txtStartTime.setFocusable(false); - lablEndTime.setText("End time:"); - txtEndTime.setEditable(false); txtEndTime.setHorizontalAlignment(javax.swing.JTextField.CENTER); - txtEndTime.setText("jTextField2"); txtEndTime.setFocusable(false); + lblTournamentState.setText("State:"); + + txtTournamentState.setEditable(false); + txtTournamentState.setHorizontalAlignment(javax.swing.JTextField.CENTER); + txtTournamentState.setFocusable(false); + btnQuitTournament.setText("Quit Tournament"); btnQuitTournament.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); btnQuitTournament.addActionListener(new java.awt.event.ActionListener() { @@ -339,22 +342,25 @@ public class TournamentPanel extends javax.swing.JPanel { .addComponent(lblType)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(actionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) - .addComponent(txtName, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE) + .addComponent(txtName, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE) .addComponent(txtType)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(actionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) + .addComponent(lblStartTime) + .addComponent(lblTournamentState)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(actionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) - .addGroup(actionPanelLayout.createSequentialGroup() - .addComponent(lablEndTime) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(txtEndTime)) - .addGroup(actionPanelLayout.createSequentialGroup() - .addComponent(lblStartTime) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(txtStartTime, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE))) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(txtTournamentState) + .addComponent(txtStartTime, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)) .addGroup(actionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(btnCloseWindow, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(btnQuitTournament, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGroup(actionPanelLayout.createSequentialGroup() + .addGap(6, 6, 6) + .addComponent(txtEndTime, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, 47, Short.MAX_VALUE) + .addComponent(btnQuitTournament, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, actionPanelLayout.createSequentialGroup() + .addGap(154, 154, 154) + .addComponent(btnCloseWindow, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap()) ); actionPanelLayout.setVerticalGroup( @@ -366,15 +372,16 @@ public class TournamentPanel extends javax.swing.JPanel { .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lblStartTime) .addComponent(txtStartTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(btnQuitTournament)) - .addGap(9, 9, 9) + .addComponent(btnQuitTournament) + .addComponent(txtEndTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(actionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblType) .addComponent(txtType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(lablEndTime) - .addComponent(txtEndTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(btnCloseWindow)) - .addGap(0, 14, Short.MAX_VALUE)) + .addComponent(btnCloseWindow) + .addComponent(lblTournamentState) + .addComponent(txtTournamentState, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGap(0, 0, Short.MAX_VALUE)) ); jSplitPane2.setResizeWeight(1.0); @@ -407,14 +414,14 @@ public class TournamentPanel extends javax.swing.JPanel { layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(actionPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jSplitPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 908, Short.MAX_VALUE) + .addComponent(jSplitPane2, javax.swing.GroupLayout.Alignment.TRAILING) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(actionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jSplitPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 494, Short.MAX_VALUE)) + .addComponent(jSplitPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 511, Short.MAX_VALUE)) ); }// //GEN-END:initComponents @@ -439,15 +446,16 @@ public class TournamentPanel extends javax.swing.JPanel { private javax.swing.JScrollPane jScrollPane2; private javax.swing.JSplitPane jSplitPane1; private javax.swing.JSplitPane jSplitPane2; - private javax.swing.JLabel lablEndTime; private javax.swing.JLabel lblName; private javax.swing.JLabel lblStartTime; + private javax.swing.JLabel lblTournamentState; private javax.swing.JLabel lblType; private javax.swing.JTable tableMatches; private javax.swing.JTable tablePlayers; private javax.swing.JTextField txtEndTime; private javax.swing.JTextField txtName; private javax.swing.JTextField txtStartTime; + private javax.swing.JTextField txtTournamentState; private javax.swing.JTextField txtType; // End of variables declaration//GEN-END:variables diff --git a/Mage.Common/src/mage/view/TournamentView.java b/Mage.Common/src/mage/view/TournamentView.java index 5dba991e2a..27d471ca21 100644 --- a/Mage.Common/src/mage/view/TournamentView.java +++ b/Mage.Common/src/mage/view/TournamentView.java @@ -46,7 +46,8 @@ public class TournamentView implements Serializable { private final String tournamentName; private final String tournamentType; - + private final String tournamentState; + private final Date startTime; private final Date endTime; @@ -63,6 +64,7 @@ public class TournamentView implements Serializable { startTime = tournament.getStartTime(); endTime = tournament.getEndTime(); watchingAllowed = tournament.getOptions().isWatchingAllowed(); + tournamentState = tournament.getTournamentState(); for (TournamentPlayer player: tournament.getPlayers()) { players.add(new TournamentPlayerView(player)); @@ -103,4 +105,9 @@ public class TournamentView implements Serializable { public List getRounds() { return rounds; } + + public String getTournamentState() { + return tournamentState; + } + } diff --git a/Mage.Server.Plugins/Mage.Player.AI/src/main/java/mage/player/ai/ComputerPlayer.java b/Mage.Server.Plugins/Mage.Player.AI/src/main/java/mage/player/ai/ComputerPlayer.java index f50e64ae59..41c445b310 100644 --- a/Mage.Server.Plugins/Mage.Player.AI/src/main/java/mage/player/ai/ComputerPlayer.java +++ b/Mage.Server.Plugins/Mage.Player.AI/src/main/java/mage/player/ai/ComputerPlayer.java @@ -1512,7 +1512,7 @@ public class ComputerPlayer> extends PlayerImpl i } chosenColors = chooseDeckColorsIfPossible(); } - deck = buildDeck(new ArrayList(deck.getSideboard()), chosenColors); + deck = buildDeck(new ArrayList<>(deck.getSideboard()), chosenColors); } tournament.submitDeck(playerId, deck); } diff --git a/Mage.Server/src/main/java/mage/server/tournament/TournamentController.java b/Mage.Server/src/main/java/mage/server/tournament/TournamentController.java index 1b00fa6356..4cf5ea2a37 100644 --- a/Mage.Server/src/main/java/mage/server/tournament/TournamentController.java +++ b/Mage.Server/src/main/java/mage/server/tournament/TournamentController.java @@ -53,7 +53,6 @@ import mage.server.UserManager; import mage.server.draft.DraftController; import mage.server.draft.DraftManager; import mage.server.draft.DraftSession; -import mage.server.game.GameManager; import mage.server.game.GamesRoomManager; import mage.server.util.ThreadExecutor; import mage.view.ChatMessage.MessageColor; diff --git a/Mage/src/mage/game/Table.java b/Mage/src/mage/game/Table.java index 0b3679a0a4..e6dabdbb89 100644 --- a/Mage/src/mage/game/Table.java +++ b/Mage/src/mage/game/Table.java @@ -58,7 +58,7 @@ public class Table implements Serializable { private boolean isTournament; private boolean tournamentSubTable; private DeckValidator validator; - private TableState state = TableState.WAITING; + private TableState state; private Match match; private Tournament tournament; @@ -68,12 +68,14 @@ public class Table implements Serializable { this(roomId, gameType, name, controllerName, validator, playerTypes); this.tournament = tournament; this.isTournament = true; + setState(TableState.WAITING); } public Table(UUID roomId, String gameType, String name, String controllerName, DeckValidator validator, List playerTypes, Match match) { this(roomId, gameType, name, controllerName, validator, playerTypes); this.match = match; - this.isTournament = false; + this.isTournament = false; + setState(TableState.WAITING); } protected Table(UUID roomId, String gameType, String name, String controllerName, DeckValidator validator, List playerTypes) { @@ -106,23 +108,24 @@ public class Table implements Serializable { } public void initGame() { - state = TableState.DUELING; + setState(TableState.DUELING); } public void initTournament() { - state = TableState.DUELING; + setState(TableState.DUELING); + } public void endTournament() { - state = TableState.FINISHED; + setState(TableState.FINISHED); } public void initDraft() { - state = TableState.DRAFTING; + setState(TableState.DRAFTING); } public void construct() { - state = TableState.CONSTRUCTING; + setState(TableState.CONSTRUCTING); } /** @@ -131,7 +134,7 @@ public class Table implements Serializable { * */ public void closeTable() { - state = TableState.FINISHED; + setState(TableState.FINISHED); this.validator = null; } @@ -167,7 +170,7 @@ public class Table implements Serializable { } seat.setPlayer(player); if (isReady()) { - state = TableState.STARTING; + setState(TableState.STARTING); } return seat.getPlayer().getId(); } @@ -203,14 +206,21 @@ public class Table implements Serializable { Player player = seats[i].getPlayer(); if (player != null && player.getId().equals(playerId)) { seats[i].setPlayer(null); - if (state == TableState.STARTING) { - state = TableState.WAITING; + if (getState().equals(TableState.STARTING)) { + setState(TableState.WAITING); } break; } } } + final public void setState(TableState state) { + this.state = state; + if (isTournament()) { + getTournament().setTournamentState(state.toString()); + } + } + public TableState getState() { return state; } @@ -220,7 +230,7 @@ public class Table implements Serializable { } public void sideboard() { - state = TableState.SIDEBOARDING; + setState(TableState.SIDEBOARDING); } public String getName() { diff --git a/Mage/src/mage/game/tournament/Tournament.java b/Mage/src/mage/game/tournament/Tournament.java index 8487c68b78..84b8438a49 100644 --- a/Mage/src/mage/game/tournament/Tournament.java +++ b/Mage/src/mage/game/tournament/Tournament.java @@ -81,7 +81,10 @@ public interface Tournament { // tournament type TournamentType getTournamentType(); void setTournamentType(TournamentType tournamentType); - + // tournamentState + String getTournamentState(); + void setTournamentState(String tournamentState); + int getNumberRounds(); void cleanUpOnTournamentEnd(); boolean isAbort(); diff --git a/Mage/src/mage/game/tournament/TournamentImpl.java b/Mage/src/mage/game/tournament/TournamentImpl.java index e190dca675..e49d61880b 100644 --- a/Mage/src/mage/game/tournament/TournamentImpl.java +++ b/Mage/src/mage/game/tournament/TournamentImpl.java @@ -75,7 +75,8 @@ public abstract class TournamentImpl implements Tournament { protected Date startTime; protected Date endTime; protected boolean abort; - + protected String tournamentState; + public TournamentImpl(TournamentOptions options) { this.options = options; startTime = new Date(); @@ -107,6 +108,9 @@ public abstract class TournamentImpl implements Tournament { if (players.containsKey(playerId)) { players.get(playerId).submitDeck(deck); } + synchronized (this) { + this.notifyAll(); + } } @Override @@ -358,11 +362,16 @@ public abstract class TournamentImpl implements Tournament { } ).start(); } + // add autosubmit trigger + + synchronized(this) { while (!isDoneConstructing()) { try { this.wait(); - } catch (InterruptedException ex) { } + } catch (InterruptedException ex) { + + } } } } @@ -460,4 +469,14 @@ public abstract class TournamentImpl implements Tournament { this.abort = abort; } + @Override + public String getTournamentState() { + return tournamentState; + } + + @Override + public void setTournamentState(String tournamentState) { + this.tournamentState = tournamentState; + } + } diff --git a/Mage/src/mage/game/tournament/TournamentPlayer.java b/Mage/src/mage/game/tournament/TournamentPlayer.java index a052e6a589..2183852135 100644 --- a/Mage/src/mage/game/tournament/TournamentPlayer.java +++ b/Mage/src/mage/game/tournament/TournamentPlayer.java @@ -28,10 +28,18 @@ package mage.game.tournament; +import java.util.List; +import java.util.Random; +import java.util.Set; import mage.cards.Card; import mage.cards.decks.Deck; +import mage.cards.repository.CardCriteria; +import mage.cards.repository.CardInfo; +import mage.cards.repository.CardRepository; +import mage.constants.Rarity; import mage.constants.TournamentPlayerState; import mage.players.Player; +import mage.util.TournamentUtil; /** * @@ -105,7 +113,7 @@ public class TournamentPlayer { } public void submitDeck(Deck deck) { - this.deck = deck; + this.deck = deck; // Check if player manipulated available cards??? this.doneConstructing = true; this.setState(TournamentPlayerState.WAITING); } @@ -115,15 +123,30 @@ public class TournamentPlayer { } public Deck generateDeck() { - //TODO: improve this - while (deck.getCards().size() < 40 && deck.getSideboard().size() > 0) { - Card card = deck.getSideboard().iterator().next(); - deck.getCards().add(card); - deck.getSideboard().remove(card); + // user passed to submit deck in time + // all all cards to deck + deck.getCards().addAll(deck.getSideboard()); + deck.getSideboard().clear(); + // add lands to deck + int landsPerType = 7; + if (deck.getCards().size() >= 90) { + landsPerType = 14; } + Set landSets = TournamentUtil.getLandSetCodeForDeckSets(deck.getExpansionSetCodes()); + deck.getCards().addAll(TournamentUtil.getLands("Mountain", landsPerType, landSets)); + deck.getCards().addAll(TournamentUtil.getLands("Plains", landsPerType, landSets)); + deck.getCards().addAll(TournamentUtil.getLands("Swamp", landsPerType, landSets)); + deck.getCards().addAll(TournamentUtil.getLands("Forest", landsPerType, landSets)); + deck.getCards().addAll(TournamentUtil.getLands("Island", landsPerType, landSets)); + + this.doneConstructing = true; + this.setState(TournamentPlayerState.WAITING); + return deck; } + + public boolean isDoneConstructing() { return this.doneConstructing; } diff --git a/Mage/src/mage/util/TournamentUtil.java b/Mage/src/mage/util/TournamentUtil.java new file mode 100644 index 0000000000..b29b6a6df9 --- /dev/null +++ b/Mage/src/mage/util/TournamentUtil.java @@ -0,0 +1,93 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package mage.util; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; +import mage.cards.Card; +import mage.cards.repository.CardCriteria; +import mage.cards.repository.CardInfo; +import mage.cards.repository.CardRepository; +import mage.cards.repository.ExpansionInfo; +import mage.cards.repository.ExpansionRepository; +import mage.constants.Rarity; + +/** + * + * @author LevelX2 + */ +public class TournamentUtil { + + /** + * Tries to calculate the most appropiate sets to add basic lands for cards of a deck + * + * @param setCodesDeck + * @return setCode for lands + */ + + public static Set getLandSetCodeForDeckSets(Set setCodesDeck) { + + Set setCodesland = new HashSet<>(); + // decide from which sets basic lands are taken from + for (String setCode :setCodesDeck) { + ExpansionInfo expansionInfo = ExpansionRepository.instance.getSetByCode(setCode); + if (expansionInfo.hasBasicLands()) { + setCodesland.add(expansionInfo.getCode()); + } + } + + // if sets have no basic land, take land from block + if (setCodesland.isEmpty()) { + for (String setCode :setCodesDeck) { + ExpansionInfo expansionInfo = ExpansionRepository.instance.getSetByCode(setCode); + ExpansionInfo [] blockSets = ExpansionRepository.instance.getSetsFromBlock(expansionInfo.getBlockName()); + for (ExpansionInfo blockSet: blockSets) { + if (blockSet.hasBasicLands()) { + setCodesland.add(blockSet.getCode()); + } + } + } + } + // if still no set with lands found, take one by random + if (setCodesland.isEmpty()) { + // if sets have no basic lands and also it has no parent or parent has no lands get last set with lands + // select a set with basic lands by random + Random generator = new Random(); + List basicLandSets = ExpansionRepository.instance.getSetsWithBasicLandsByReleaseDate(); + if (basicLandSets.size() > 0) { + setCodesland.add(basicLandSets.get(generator.nextInt(basicLandSets.size())).getCode()); + } + } + + if (setCodesland.isEmpty()) { + throw new IllegalArgumentException("No set with basic land was found"); + } + return setCodesland; + } + + public static List getLands(String landName, int number, Set landSets) { + Random random = new Random(); + CardCriteria criteria = new CardCriteria(); + if (!landSets.isEmpty()) { + criteria.setCodes(landSets.toArray(new String[landSets.size()])); + } + criteria.rarities(Rarity.LAND).name(landName); + List lands = CardRepository.instance.findCards(criteria); + List cards = new ArrayList<>(); + if (!lands.isEmpty()) { + for (int i = 0; i < number; i++) { + Card land = lands.get(random.nextInt(lands.size())).getCard(); + cards.add(land); + } + } + return cards; + } + +}