magarena/src/magic/firemind/JsonOrgParser.java

87 lines
3.1 KiB
Java
Raw Normal View History

2014-10-10 17:39:10 -07:00
package magic.firemind;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
2020-01-15 12:02:42 -08:00
import org.json.JSONArray;
import org.json.JSONObject;
2014-11-25 08:42:47 -08:00
import magic.data.json.DownloadableJsonFile;
2014-10-10 17:39:10 -07:00
import magic.model.MagicCardDefinition;
import magic.model.MagicDeck;
2020-01-15 12:02:42 -08:00
import magic.utility.DeckUtils;
2014-10-10 17:39:10 -07:00
public final class JsonOrgParser {
private JsonOrgParser() {}
public static List<MagicDeck> parse(final File jsonFile) throws IOException {
final List<MagicDeck> decks = new ArrayList<>();
2014-11-25 08:42:47 -08:00
final JSONObject jsonRoot = new JSONObject(DownloadableJsonFile.getJsonString(jsonFile));
2014-10-10 17:39:10 -07:00
final List<String> formats = new ArrayList<>(Arrays.asList(JSONObject.getNames(jsonRoot)));
for (String format : formats) {
final JSONObject jsonFormat = jsonRoot.getJSONObject(format);
final String[] deckNamesArray = JSONObject.getNames(jsonFormat);
if (deckNamesArray == null)
continue; // no decks specified for given format.
2015-12-31 02:54:52 -08:00
final List<String> deckNames = new ArrayList<>(Arrays.asList(deckNamesArray));
2014-10-10 17:39:10 -07:00
for (String deckName : deckNames) {
final JSONObject jsonDeck = jsonFormat.getJSONObject(deckName);
final MagicDeck deck = new MagicDeck();
decks.add(deck);
deck.setFilename(format + "." + deckName);
deck.setDescription(getDeckDescription(jsonDeck));
addCardsToDeck(deck, jsonDeck.getJSONArray("cards"));
}
}
return decks;
}
2015-12-31 02:54:52 -08:00
2014-10-10 17:39:10 -07:00
private static void addCardsToDeck(final MagicDeck deck, final JSONArray jsonCards) {
for (int i = 0; i < jsonCards.length(); i++) {
final JSONObject jsonCard = jsonCards.getJSONObject(i);
final String cardName = jsonCard.getString("name");
final int cardQuantity = jsonCard.getInt("quantity");
final MagicCardDefinition cardDef = DeckUtils.getCard(cardName);
for (int j = 0; j < cardQuantity; j++) {
deck.add(cardDef);
}
}
}
private static String getJsonString(JSONObject jsonDeck, String key) {
return !jsonDeck.isNull(key) ? jsonDeck.getString(key) : "";
}
2014-10-10 17:39:10 -07:00
private static String getDeckDescription(final JSONObject jsonDeck) {
final StringBuilder sb = new StringBuilder();
sb.append("Author: ").append(getJsonString(jsonDeck, "author"));
2014-10-10 17:39:10 -07:00
sb.append("\nRating: ").append(jsonDeck.getString("rating"));
sb.append("\nReleased: ").append(getFormattedReleaseDate(getJsonString(jsonDeck, "releaseDate")));
if (!getJsonString(jsonDeck, "description").trim().isEmpty()) {
2014-10-10 17:39:10 -07:00
sb.append("\n\n").append(jsonDeck.getString("description"));
}
return sb.toString();
}
private static String getFormattedReleaseDate(final String jsonValue) {
if (jsonValue.length() != 8) {
return jsonValue;
} else {
return jsonValue.substring(0, 4) + "-"
+ jsonValue.substring(4, 6) + "-"
+ jsonValue.substring(6, 8);
}
}
2014-11-25 08:42:47 -08:00
2014-10-10 17:39:10 -07:00
}