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 enum TokenType {
KnownCardWithNumber,
UnknownCardWithNumber,
KnownCard,
UnknownCard,
SectionName,
Comment,
Unknown
UnknownText
}
public static class Token {
@@ -29,30 +29,27 @@ public class DeckRecognizer {
private final int number;
private final String text;
public Token(CardPrinted knownCard, int count) {
card = knownCard;
text = null;
number = count;
type = TokenType.KnownCardWithNumber;
public static Token knownCard(CardPrinted theCard, int count) {
return new Token(theCard, TokenType.KnownCard, count, null);
}
public static Token unknownCard(String cardNme, int count) {
return new Token(null, TokenType.UnknownCard, count, cardNme);
}
public Token(String unknownCard, int count) {
card = null;
text = unknownCard;
private Token(CardPrinted knownCard, TokenType type1, int count, String message)
{
card = knownCard;
number = count;
type = TokenType.UnknownCardWithNumber;
type = type1;
text = message;
}
public Token(TokenType type1, int count, String message)
{
if (type1 == TokenType.KnownCardWithNumber || type1 == TokenType.UnknownCardWithNumber) {
throw new IllegalArgumentException("Use specialized constructors for recognized card lines");
this(null, type1, count, message);
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; }
@@ -82,19 +79,19 @@ public class DeckRecognizer {
return new Token(cardName, amount);
} */else {
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);
}
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) {
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);
return null == known ? new Token(name, n) : known;
return null == known ? Token.unknownCard(name, n) : known;
}
private static Token recognizeNonCard(String text, int n) {
@@ -104,8 +101,14 @@ public class DeckRecognizer {
}
private final static String[] knownComments = new String[] {
"lands", "creatures", "creature", "spells", "enchancements", "other spells", "artifacts", "cards" };
private static boolean isDecoration(String line) {
"land", "lands", "creatures", "creature", "spells", "enchancements", "other spells", "artifacts" };
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) {
if (line.equalsIgnoreCase(s)) { return true; }
}

View File

@@ -50,7 +50,7 @@ public class DeckImport extends JDialog {
"<div class='section'>Legend</div>" +
"<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='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>" +
"</ul>" +
"<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)); } });
txtInput.getDocument().addDocumentListener(new OnChangeTextUpdate());
cmdAccept.setEnabled(false);
}
private void readInput()
@@ -155,12 +156,13 @@ public class DeckImport extends JDialog {
int[] cardsUnknown = new int[2];
int idx = 0;
for (DeckRecognizer.Token t : tokens) {
if (t.getType() == TokenType.KnownCardWithNumber) { cardsOk[idx] += t.getNumber(); }
if (t.getType() == TokenType.UnknownCardWithNumber) { cardsUnknown[idx] += t.getNumber(); }
if (t.getType() == TokenType.KnownCard) { cardsOk[idx] += t.getNumber(); }
if (t.getType() == TokenType.UnknownCard) { cardsUnknown[idx] += t.getNumber(); }
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]));
summarySide.setText(String.format("Sideboard: %d cards recognized, %d unknown cards", cardsOk[1], cardsUnknown[1]));
cmdAccept.setEnabled(cardsOk[0] > 0);
}
private Deck buildDeck(){
@@ -177,13 +179,13 @@ public class DeckImport extends JDialog {
private String makeHtmlViewOfToken(DeckRecognizer.Token token) {
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());
case UnknownCardWithNumber:
case UnknownCard:
return String.format("<div class='unknowncard'>%s * %s</div>", token.getNumber(), token.getText());
case SectionName:
return String.format("<div class='section'>%s</div>", token.getText());
case Unknown:
case UnknownText:
case Comment:
return String.format("<div class='comment'>%s</div>", token.getText());
}