refactored and a bit improved DeckImport

This commit is contained in:
Maxmtg
2011-09-21 15:27:17 +00:00
parent acfd398777
commit bbab59e766
2 changed files with 36 additions and 31 deletions

View File

@@ -16,11 +16,11 @@ import forge.item.CardPrinted;
*/ */
public class DeckRecognizer { public class DeckRecognizer {
public enum TokenType { public enum TokenType {
KnownCardWithNumber, KnownCard,
UnknownCardWithNumber, UnknownCard,
SectionName, SectionName,
Comment, Comment,
Unknown UnknownText
} }
public static class Token { public static class Token {
@@ -29,30 +29,27 @@ public class DeckRecognizer {
private final int number; private final int number;
private final String text; private final String text;
public Token(CardPrinted knownCard, int count) { public static Token knownCard(CardPrinted theCard, int count) {
card = knownCard; return new Token(theCard, TokenType.KnownCard, count, null);
text = null; }
number = count; public static Token unknownCard(String cardNme, int count) {
type = TokenType.KnownCardWithNumber; return new Token(null, TokenType.UnknownCard, count, cardNme);
} }
public Token(String unknownCard, int count) { private Token(CardPrinted knownCard, TokenType type1, int count, String message)
card = null; {
text = unknownCard; card = knownCard;
number = count; number = count;
type = TokenType.UnknownCardWithNumber; type = type1;
text = message;
} }
public Token(TokenType type1, int count, String message) public Token(TokenType type1, int count, String message)
{ {
if (type1 == TokenType.KnownCardWithNumber || type1 == TokenType.UnknownCardWithNumber) { this(null, type1, count, message);
throw new IllegalArgumentException("Use specialized constructors for recognized card lines"); if (type1 == TokenType.KnownCard || type1 == TokenType.UnknownCard) {
throw new IllegalArgumentException("Use factory methods for recognized card lines");
} }
card = null;
number = count;
type = type1;
text = message;
} }
public String getText() { return text; } public String getText() { return text; }
@@ -82,19 +79,19 @@ public class DeckRecognizer {
return new Token(cardName, amount); return new Token(cardName, amount);
} */else { } */else {
if ( CardDb.instance().isCardSupported(line)) { if ( CardDb.instance().isCardSupported(line)) {
return new Token( CardDb.instance().getCard(line), 1); return Token.knownCard(CardDb.instance().getCard(line), 1);
} }
result = recognizeNonCard(line, 1); result = recognizeNonCard(line, 1);
} }
return result != null ? result : new Token(TokenType.Unknown, 0, line); return result != null ? result : new Token(TokenType.UnknownText, 0, line);
} }
private static Token recognizePossibleNameAndNumber(String name, int n) { private static Token recognizePossibleNameAndNumber(String name, int n) {
if ( CardDb.instance().isCardSupported(name)) if ( CardDb.instance().isCardSupported(name))
return new Token( CardDb.instance().getCard(name), n); return Token.knownCard(CardDb.instance().getCard(name), n);
Token known = recognizeNonCard(name, n); Token known = recognizeNonCard(name, n);
return null == known ? new Token(name, n) : known; return null == known ? Token.unknownCard(name, n) : known;
} }
private static Token recognizeNonCard(String text, int n) { private static Token recognizeNonCard(String text, int n) {
@@ -104,8 +101,14 @@ public class DeckRecognizer {
} }
private final static String[] knownComments = new String[] { private final static String[] knownComments = new String[] {
"lands", "creatures", "creature", "spells", "enchancements", "other spells", "artifacts", "cards" }; "land", "lands", "creatures", "creature", "spells", "enchancements", "other spells", "artifacts" };
private static boolean isDecoration(String line) { private final static String[] knownCommentParts = new String[] { "card" };
private static boolean isDecoration(String lineAsIs) {
String line = lineAsIs.toLowerCase();
for (String s : knownCommentParts) {
if (line.contains(s)) { return true; }
}
for (String s : knownComments) { for (String s : knownComments) {
if (line.equalsIgnoreCase(s)) { return true; } if (line.equalsIgnoreCase(s)) { return true; }
} }

View File

@@ -50,7 +50,7 @@ public class DeckImport extends JDialog {
"<div class='section'>Legend</div>" + "<div class='section'>Legend</div>" +
"<ul>" + "<ul>" +
"<li class='knowncard'>Recognized cards will be shown in green. These cards will be auto-imported into a new deck<BR></li>" + "<li class='knowncard'>Recognized cards will be shown in green. These cards will be auto-imported into a new deck<BR></li>" +
"<li class='unknowncard'>Lines which seem to see cards but could not be recognized, are shown in red<BR></li>" + "<li class='unknowncard'>Lines which seem to be cards but are either misspelled or unsupported by Forge, are shown in dark-red<BR></li>" +
"<li class='comment'>Lines that appear unsignificant will be shown in gray<BR><BR></li>" + "<li class='comment'>Lines that appear unsignificant will be shown in gray<BR><BR></li>" +
"</ul>" + "</ul>" +
"<div class='comment'>Submit feedback to Max mtg on slightlymagic.net forum</div>" + "<div class='comment'>Submit feedback to Max mtg on slightlymagic.net forum</div>" +
@@ -119,6 +119,7 @@ public class DeckImport extends JDialog {
processWindowEvent(new WindowEvent(DeckImport.this, WindowEvent.WINDOW_CLOSING)); } }); processWindowEvent(new WindowEvent(DeckImport.this, WindowEvent.WINDOW_CLOSING)); } });
txtInput.getDocument().addDocumentListener(new OnChangeTextUpdate()); txtInput.getDocument().addDocumentListener(new OnChangeTextUpdate());
cmdAccept.setEnabled(false);
} }
private void readInput() private void readInput()
@@ -155,12 +156,13 @@ public class DeckImport extends JDialog {
int[] cardsUnknown = new int[2]; int[] cardsUnknown = new int[2];
int idx = 0; int idx = 0;
for (DeckRecognizer.Token t : tokens) { for (DeckRecognizer.Token t : tokens) {
if (t.getType() == TokenType.KnownCardWithNumber) { cardsOk[idx] += t.getNumber(); } if (t.getType() == TokenType.KnownCard) { cardsOk[idx] += t.getNumber(); }
if (t.getType() == TokenType.UnknownCardWithNumber) { cardsUnknown[idx] += t.getNumber(); } if (t.getType() == TokenType.UnknownCard) { cardsUnknown[idx] += t.getNumber(); }
if (t.getType() == TokenType.SectionName && t.getText().toLowerCase().contains("side") ) { idx = 1; } if (t.getType() == TokenType.SectionName && t.getText().toLowerCase().contains("side") ) { idx = 1; }
} }
summaryMain.setText(String.format("Main: %d cards recognized, %d unknown cards", cardsOk[0], cardsUnknown[0])); summaryMain.setText(String.format("Main: %d cards recognized, %d unknown cards", cardsOk[0], cardsUnknown[0]));
summarySide.setText(String.format("Sideboard: %d cards recognized, %d unknown cards", cardsOk[1], cardsUnknown[1])); summarySide.setText(String.format("Sideboard: %d cards recognized, %d unknown cards", cardsOk[1], cardsUnknown[1]));
cmdAccept.setEnabled(cardsOk[0] > 0);
} }
private Deck buildDeck(){ private Deck buildDeck(){
@@ -177,13 +179,13 @@ public class DeckImport extends JDialog {
private String makeHtmlViewOfToken(DeckRecognizer.Token token) { private String makeHtmlViewOfToken(DeckRecognizer.Token token) {
switch(token.getType()) switch(token.getType())
{ {
case KnownCardWithNumber: case KnownCard:
return String.format("<div class='knowncard'>%s * %s [%s]</div>", token.getNumber(), token.getCard().getName(), token.getCard().getSet()); return String.format("<div class='knowncard'>%s * %s [%s]</div>", token.getNumber(), token.getCard().getName(), token.getCard().getSet());
case UnknownCardWithNumber: case UnknownCard:
return String.format("<div class='unknowncard'>%s * %s</div>", token.getNumber(), token.getText()); return String.format("<div class='unknowncard'>%s * %s</div>", token.getNumber(), token.getText());
case SectionName: case SectionName:
return String.format("<div class='section'>%s</div>", token.getText()); return String.format("<div class='section'>%s</div>", token.getText());
case Unknown: case UnknownText:
case Comment: case Comment:
return String.format("<div class='comment'>%s</div>", token.getText()); return String.format("<div class='comment'>%s</div>", token.getText());
} }