mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-19 20:28:00 +00:00
Refactor deck import logic to be reusable by mobile game
Don't prompt about replacing deck contents if deck is empty
This commit is contained in:
93
forge-gui/src/main/java/forge/deck/DeckImportController.java
Normal file
93
forge-gui/src/main/java/forge/deck/DeckImportController.java
Normal file
@@ -0,0 +1,93 @@
|
||||
package forge.deck;
|
||||
|
||||
import java.text.DateFormatSymbols;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import forge.interfaces.ICheckBox;
|
||||
import forge.interfaces.IComboBox;
|
||||
import forge.item.PaperCard;
|
||||
import forge.model.FModel;
|
||||
import forge.util.gui.SOptionPane;
|
||||
|
||||
public class DeckImportController {
|
||||
private final boolean replacingDeck;
|
||||
private final ICheckBox newEditionCheck, dateTimeCheck, onlyCoreExpCheck;
|
||||
private final IComboBox<String> monthDropdown;
|
||||
private final IComboBox<Integer> yearDropdown;
|
||||
private final List<DeckRecognizer.Token> tokens = new ArrayList<DeckRecognizer.Token>();
|
||||
|
||||
public DeckImportController(boolean replacingDeck0, ICheckBox newEditionCheck0, ICheckBox dateTimeCheck0, ICheckBox onlyCoreExpCheck0, IComboBox<String> monthDropdown0, IComboBox<Integer> yearDropdown0) {
|
||||
replacingDeck = replacingDeck0;
|
||||
newEditionCheck = newEditionCheck0;
|
||||
dateTimeCheck = dateTimeCheck0;
|
||||
onlyCoreExpCheck = onlyCoreExpCheck0;
|
||||
monthDropdown = monthDropdown0;
|
||||
yearDropdown = yearDropdown0;
|
||||
|
||||
fillDateDropdowns();
|
||||
}
|
||||
|
||||
private void fillDateDropdowns() {
|
||||
DateFormatSymbols dfs = new DateFormatSymbols();
|
||||
monthDropdown.removeAllItems();
|
||||
String[] months = dfs.getMonths();
|
||||
for (String monthName : months) {
|
||||
if (!StringUtils.isBlank(monthName)) {
|
||||
monthDropdown.addItem(monthName);
|
||||
}
|
||||
}
|
||||
int yearNow = Calendar.getInstance().get(Calendar.YEAR);
|
||||
for (int i = yearNow; i >= 1993; i--) {
|
||||
yearDropdown.addItem(Integer.valueOf(i));
|
||||
}
|
||||
}
|
||||
|
||||
public List<DeckRecognizer.Token> parseInput(String input) {
|
||||
tokens.clear();
|
||||
|
||||
DeckRecognizer recognizer = new DeckRecognizer(newEditionCheck.isSelected(), onlyCoreExpCheck.isSelected(), FModel.getMagicDb().getCommonCards());
|
||||
if (dateTimeCheck.isSelected()) {
|
||||
recognizer.setDateConstraint(monthDropdown.getSelectedIndex(), yearDropdown.getSelectedItem());
|
||||
}
|
||||
String[] lines = input.split("\n");
|
||||
for (String line : lines) {
|
||||
tokens.add(recognizer.recognizeLine(line));
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
|
||||
public Deck accept() {
|
||||
if (tokens.isEmpty()) { return null; }
|
||||
|
||||
if (replacingDeck) {
|
||||
final String warning = "This will replace contents of the current deck with whatever you are importing.\n\nProceed?";
|
||||
if (!SOptionPane.showConfirmDialog(warning, "Replace Current Deck", "Replace", "Cancel")) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
final Deck result = new Deck();
|
||||
boolean isMain = true;
|
||||
for (final DeckRecognizer.Token t : tokens) {
|
||||
final DeckRecognizer.TokenType type = t.getType();
|
||||
if ((type == DeckRecognizer.TokenType.SectionName) && t.getText().toLowerCase().contains("side")) {
|
||||
isMain = false;
|
||||
}
|
||||
if (type != DeckRecognizer.TokenType.KnownCard) {
|
||||
continue;
|
||||
}
|
||||
final PaperCard crd = t.getCard();
|
||||
if (isMain) {
|
||||
result.getMain().add(crd, t.getNumber());
|
||||
}
|
||||
else {
|
||||
result.getOrCreate(DeckSection.Sideboard).add(crd, t.getNumber());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -9,4 +9,6 @@ public interface IComboBox<E> {
|
||||
void setSelectedIndex(int index);
|
||||
void addItem(E item);
|
||||
void removeAllItems();
|
||||
int getSelectedIndex();
|
||||
E getSelectedItem();
|
||||
}
|
||||
|
||||
@@ -93,7 +93,6 @@ public class CustomLimited extends DeckBase {
|
||||
* @return the custom limited
|
||||
*/
|
||||
public static CustomLimited parse(final List<String> dfData, final IStorage<Deck> cubes) {
|
||||
|
||||
final FileSection data = FileSection.parse(dfData, ":");
|
||||
|
||||
List<Pair<String, Integer>> slots = new ArrayList<Pair<String,Integer>>();
|
||||
@@ -117,8 +116,6 @@ public class CustomLimited extends DeckBase {
|
||||
return cd;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Gets the num packs.
|
||||
*
|
||||
@@ -177,4 +174,9 @@ public class CustomLimited extends DeckBase {
|
||||
public boolean isSingleton() {
|
||||
return singleton;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return cardPool.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user