Deck class no longer contains metadata - please create new classes for extra data you want stored along with the deck, incapsulate deck there if needed.

Accelerated Quest startup by removing N reads of N files (n=number of decks) that happened before displaying list of battles
Data specific for quest battle moved to QuestEvent
This commit is contained in:
Maxmtg
2011-09-05 10:47:08 +00:00
parent 2f62f07126
commit 69152d07c3
7 changed files with 130 additions and 119 deletions

View File

@@ -1,6 +1,7 @@
package forge.deck;
import forge.Constant;
import forge.PlayerType;
import forge.card.CardDb;
import forge.card.CardPool;
import forge.card.CardPoolView;
@@ -23,21 +24,17 @@ public final class Deck implements Comparable<Deck>, Serializable {
//gameType is from Constant.GameType, like Constant.GameType.Regular
private Map<String, String> metadata = new HashMap<String, String>();
private String name;
private String deckType;
private String comment;
private PlayerType playerType;
private CardPool main;
private CardPool sideboard;
/** Constant <code>NAME="Name"</code> */
public static final String NAME = "Name";
/** Constant <code>DECK_TYPE="Deck Type"</code> */
public static final String DECK_TYPE = "Deck Type";
/** Constant <code>COMMENT="Comment"</code> */
public static final String COMMENT = "Comment";
/** Constant <code>DESCRIPTION="Description"</code> */
public static final String DESCRIPTION = "Description";
/** Constant <code>DIFFICULTY="Difficulty"</code> */
public static final String DIFFICULTY = "Difficulty";
//gameType is from Constant.GameType, like Constant.GameType.Regular
@@ -99,7 +96,7 @@ public final class Deck implements Comparable<Deck>, Serializable {
* @return a {@link java.lang.String} object.
*/
public String getDeckType() {
return metadata.get(DECK_TYPE);
return deckType;
}
//can only call this method ONCE
@@ -119,7 +116,7 @@ public final class Deck implements Comparable<Deck>, Serializable {
"Deck : setDeckType() error, invalid deck type - " + deckType);
}
metadata.put(DECK_TYPE, deckType);
this.deckType = deckType;
}
/**
@@ -128,7 +125,7 @@ public final class Deck implements Comparable<Deck>, Serializable {
* @param s a {@link java.lang.String} object.
*/
public void setName(String s) {
metadata.put(NAME, s);
name = s;
}
/**
@@ -137,7 +134,7 @@ public final class Deck implements Comparable<Deck>, Serializable {
* @return a {@link java.lang.String} object.
*/
public String getName() {
return metadata.get(NAME);
return name;
}
/**
@@ -146,7 +143,7 @@ public final class Deck implements Comparable<Deck>, Serializable {
* @param comment a {@link java.lang.String} object.
*/
public void setComment(String comment) {
metadata.put(COMMENT, comment);
this.comment = comment;
}
/**
@@ -155,7 +152,7 @@ public final class Deck implements Comparable<Deck>, Serializable {
* @return a {@link java.lang.String} object.
*/
public String getComment() {
return metadata.get(COMMENT);
return comment;
}
@@ -242,44 +239,6 @@ public final class Deck implements Comparable<Deck>, Serializable {
return getName();
}
// The setters and getters below are for Quest decks
/**
* <p>setDifficulty.</p>
*
* @param s a {@link java.lang.String} object.
*/
public void setDifficulty(String s) {
metadata.put(DIFFICULTY, s);
}
/**
* <p>getDifficulty.</p>
*
* @return a {@link java.lang.String} object.
*/
public String getDifficulty() {
return metadata.get(DIFFICULTY);
}
/**
* <p>setDescription.</p>
*
* @param s a {@link java.lang.String} object.
*/
public void setDescription(String s) {
metadata.put(DESCRIPTION, s);
}
/**
* <p>getDescription.</p>
*
* @return a {@link java.lang.String} object.
*/
public String getDescription() {
return metadata.get(DESCRIPTION);
}
/**
* <p>compareTo.</p>
*
@@ -299,41 +258,6 @@ public final class Deck implements Comparable<Deck>, Serializable {
return false;
}
/**
* <p>Getter for the field <code>metadata</code>.</p>
*
* @return a {@link java.util.Set} object.
*/
public Set<Map.Entry<String, String>> getMetadata() {
return metadata.entrySet();
}
/**
* <p>Getter for the field <code>metadata</code>.</p>
*
* @param key a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
* @since 1.0.15
*/
public String getMetadata(String key) {
if (metadata.containsKey(key))
return metadata.get(key);
System.err.println("In forge.deck/Deck.java, getMetadata() failed "+
"for property '"+key+"' in deck '"+getName()+"'.");
return "";
}
/**
* <p>addMetaData.</p>
*
* @param key a {@link java.lang.String} object.
* @param value a {@link java.lang.String} object.
*/
public void addMetaData(String key, String value) {
metadata.put(key, value);
}
public void clearSideboard() {
sideboard.clear();
}
@@ -343,4 +267,11 @@ public final class Deck implements Comparable<Deck>, Serializable {
}
public final PlayerType getPlayerType() {
return playerType;
}
public final void setPlayerType(PlayerType recommendedPlayer0) {
this.playerType = recommendedPlayer0;
}
}

View File

@@ -2,6 +2,7 @@ package forge.deck;
import forge.Constant;
import forge.PlayerType;
import forge.card.CardPrinted;
import forge.error.ErrorViewer;
@@ -42,6 +43,10 @@ public class DeckManager {
}
};
private static final String NAME = "Name";
private static final String DECK_TYPE = "Deck Type";
private static final String COMMENT = "Comment";
private static final String PLAYER = "Player";
private File deckDir;
Map<String, Deck> deckMap;
@@ -255,7 +260,6 @@ public class DeckManager {
* @return a {@link forge.deck.Deck} object.
*/
public static Deck readDeck(File deckFile) {
List<String> lines = new LinkedList<String>();
try {
@@ -286,7 +290,20 @@ public class DeckManager {
//read metadata
while (!(line = lineIterator.next()).equals("[main]")) {
String[] linedata = line.split("=", 2);
d.addMetaData(linedata[0], linedata[1]);
String field = linedata[0].toLowerCase();
if (NAME.equalsIgnoreCase(field)) {
d.setName(linedata[1]);
} else if (COMMENT.equalsIgnoreCase(field)) {
d.setComment(linedata[1]);
} else if (DECK_TYPE.equalsIgnoreCase(field)) {
d.setDeckType(linedata[1]);
} else if (PLAYER.equalsIgnoreCase(field)) {
if ("human".equalsIgnoreCase(linedata[1])) {
d.setPlayerType(PlayerType.HUMAN);
} else {
d.setPlayerType(PlayerType.COMPUTER);
}
}
}
addCardList(lineIterator, d);
@@ -301,7 +318,7 @@ public class DeckManager {
* @param iterator a {@link java.util.ListIterator} object.
* @return a {@link forge.deck.Deck} object.
*/
private static Deck readDeckOld(ListIterator<String> iterator) {
private static Deck readDeckOld(final ListIterator<String> iterator) {
String line;
//readDeck name
@@ -449,10 +466,11 @@ public class DeckManager {
private static void writeDeck(Deck d, BufferedWriter out) throws IOException {
out.write("[metadata]\n");
for (Entry<String, String> entry : d.getMetadata()) {
if (entry.getValue() != null)
out.write(format("%s=%s%n", entry.getKey(), entry.getValue().replaceAll("\n", "")));
}
out.write(format("%s=%s%n", NAME, d.getName().replaceAll("\n", "")));
out.write(format("%s=%s%n", DECK_TYPE, d.getDeckType().replaceAll("\n", "")));
out.write(format("%s=%s%n", PLAYER, d.getPlayerType()));
out.write(format("%s=%s%n", COMMENT, d.getComment().replaceAll("\n", "")));
out.write(format("%s%n", "[main]"));
for (Entry<CardPrinted, Integer> e : d.getMain()) {

View File

@@ -11,6 +11,8 @@ import forge.properties.NewConstants;
import java.io.File;
import java.util.*;
import org.apache.commons.lang3.StringUtils;
/**
* <p>QuestBattleManager class.</p>
*
@@ -116,6 +118,21 @@ public class QuestBattleManager {
return manager.getDeck(deckName);
}
/**
* <p>getQuestEventFromFile.</p>
* Returns QuestEvent data for the challenge stored in that file name.
*
* @param deckName a {@link java.lang.String} object.
* @return a {@link forge.deck.Deck} object.
*/
public static QuestEvent getQuestEventFromFile(String deckName) {
final File deckPath = ForgeProps.getFile(NewConstants.QUEST.DECKS);
File deckFile = new File(deckPath, deckName + ".dck");
QuestEvent result = readQuestBattleMetadataFromDeckFile(deckFile);
return result;
}
/**
* <p>readFile.</p>
* A reader util for accessing the AI deck list text files.
@@ -147,4 +164,31 @@ public class QuestBattleManager {
return list;
}
private static QuestEvent readQuestBattleMetadataFromDeckFile(final File f) {
QuestEvent out = new QuestEvent();
List<String> contents = FileUtil.readFile(f);
for (String s : contents) {
if ("[main]".equals(s)) { break; }
if (StringUtils.isBlank(s) || s.charAt(0) == '[') { continue; }
int eqPos = s.indexOf('=');
if (eqPos < 0) { continue; } // no equals sign here
String key = s.substring(0, eqPos);
String value = s.substring(eqPos + 1);
if ("DisplayName".equalsIgnoreCase(key)) {
out.displayName = value;
} else if ("Difficulty".equalsIgnoreCase(key)) {
out.difficulty = value;
} else if ("Description".equalsIgnoreCase(key)) {
out.description = value;
} else if ("Icon".equalsIgnoreCase(key)) {
out.icon = value;
}
}
return out;
}
}

View File

@@ -0,0 +1,19 @@
package forge.quest.data;
import forge.deck.Deck;
/**
* TODO: Write javadoc for this type.
*
*/
public class QuestEvent {
String displayName;
String difficulty;
String description;
String icon;
public final String getDisplayName() { return displayName; }
public final String getDifficulty() { return difficulty; }
public final String getDescription() { return description; }
public final String getIcon() { return icon; }
}

View File

@@ -4,6 +4,7 @@ package forge.quest.gui.main;
import forge.deck.Deck;
import forge.gui.GuiUtils;
import forge.quest.data.QuestBattleManager;
import forge.quest.data.QuestEvent;
import javax.swing.*;
@@ -23,12 +24,7 @@ public class QuestBattle extends QuestSelectablePanel {
private String deckName;
private static String oppName;
private static String oppDiff;
private static String oppDesc;
private static String oppIconAddress;
private static ImageIcon icon;
private static Deck oppDeck;
/**
* <p>Constructor for QuestBattle.</p>
@@ -59,19 +55,21 @@ public class QuestBattle extends QuestSelectablePanel {
String[] oppDecks = QuestBattleManager.generateBattles();
for (String oppDeckName : oppDecks) {
// Get deck object and properties for this opponent.
oppDeck = QuestBattleManager.getAIDeckFromFile(oppDeckName);
oppName = oppDeck.getMetadata("DisplayName");
oppDiff = oppDeck.getMetadata("Difficulty");
oppDesc = oppDeck.getMetadata("Description");
oppIconAddress = oppDeck.getMetadata("Icon");
icon = GuiUtils.getIconFromFile(oppName + ".jpg");
QuestEvent event1 = QuestBattleManager.getQuestEventFromFile(oppDeckName);
String oppName = event1.getDisplayName();
String oppDiff = event1.getDifficulty();
String oppDesc = event1.getDescription();
String oppIconAddress = event1.getIcon();
ImageIcon icon;
// If non-default icon defined, use it
if(!oppIconAddress.equals("")) {
icon = GuiUtils.getIconFromFile(oppIconAddress + ".jpg");
if(oppIconAddress.equals("")) {
icon = GuiUtils.getIconFromFile(oppName + ".jpg");
}
else
icon = GuiUtils.getIconFromFile(oppIconAddress + ".jpg");
// Add to list of current quest opponents.
opponentList.add(

View File

@@ -339,14 +339,14 @@ public class OldGuiNewGame extends JFrame implements NewConstants, NewConstants.
ForgeProps.getLocalized(NEW_GAME_TEXT.SAVE_SEALED_MSG),
ForgeProps.getLocalized(NEW_GAME_TEXT.SAVE_SEALED_TTL), JOptionPane.QUESTION_MESSAGE);
deck.setName(sDeckName);
deck.addMetaData("PlayerType", "Human");
deck.setPlayerType(PlayerType.HUMAN);
Constant.Runtime.HumanDeck[0] = deck;
Constant.Runtime.GameType[0] = Constant.GameType.Sealed;
Deck aiDeck = sd.buildAIDeck(sDeck.toForgeCardList());
aiDeck.setName("AI_" + sDeckName);
aiDeck.addMetaData("PlayerType", "AI");
aiDeck.setPlayerType(PlayerType.COMPUTER);
deckManager.addDeck(aiDeck);
deckManager.writeAllDecks();
deckManager.readAllDecks();
@@ -1128,9 +1128,9 @@ public class OldGuiNewGame extends JFrame implements NewConstants, NewConstants.
for (Deck allDeck : allDecks) {
if (allDeck.getDeckType().equals(Constant.GameType.Sealed)) {
if (allDeck.getMetadata("PlayerType").equals("Human")) {
if (allDeck.getPlayerType() == PlayerType.HUMAN) {
humanComboBox.addItem(allDeck.getName());
} else if (allDeck.getMetadata("PlayerType").equals("AI")) {
} else if (allDeck.getPlayerType() == PlayerType.COMPUTER) {
computerComboBox.addItem(allDeck.getName());
}
}