mirror of
https://github.com/magefree/mage.git
synced 2025-12-26 05:22:02 -08:00
Dev: migrated to single json lib (removed json-simple from deck import code), clean up guava lib usage in cards;
This commit is contained in:
parent
fa70af6131
commit
1c6eb10bd1
17 changed files with 147 additions and 76 deletions
|
|
@ -22,9 +22,9 @@ public class CodDeckImporter extends XmlDeckImporter {
|
|||
* @return
|
||||
*/
|
||||
@Override
|
||||
public DeckCardLists importDeck(String filename, StringBuilder errorMessages, boolean saveAutoFixedFile) {
|
||||
public DeckCardLists importDeck(String fileName, StringBuilder errorMessages, boolean saveAutoFixedFile) {
|
||||
try {
|
||||
Document doc = getXmlDocument(filename);
|
||||
Document doc = getXmlDocument(fileName);
|
||||
DeckCardLists decklist = new DeckCardLists();
|
||||
|
||||
List<Node> mainCards = getNodes(doc, "/cockatrice_deck/zone[@name='main']/card");
|
||||
|
|
|
|||
|
|
@ -86,10 +86,10 @@ public abstract class DeckImporter {
|
|||
}
|
||||
}
|
||||
|
||||
public abstract DeckCardLists importDeck(String file, StringBuilder errorMessages, boolean saveAutoFixedFile);
|
||||
public abstract DeckCardLists importDeck(String fileName, StringBuilder errorMessages, boolean saveAutoFixedFile);
|
||||
|
||||
public DeckCardLists importDeck(String file, boolean saveAutoFixedFile) {
|
||||
return importDeck(file, new StringBuilder(), saveAutoFixedFile);
|
||||
public DeckCardLists importDeck(String fileName, boolean saveAutoFixedFile) {
|
||||
return importDeck(fileName, new StringBuilder(), saveAutoFixedFile);
|
||||
}
|
||||
|
||||
public CardLookup getCardLookup() {
|
||||
|
|
|
|||
|
|
@ -1,9 +1,7 @@
|
|||
package mage.cards.decks.importer;
|
||||
|
||||
import com.google.gson.*;
|
||||
import mage.cards.decks.DeckCardLists;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
|
|
@ -16,26 +14,25 @@ public abstract class JsonDeckImporter extends DeckImporter {
|
|||
protected StringBuilder sbMessage = new StringBuilder();
|
||||
|
||||
/**
|
||||
* @param file file to import
|
||||
* @param fileName file to import
|
||||
* @param errorMessages you can setup output messages to showup to user
|
||||
* @param saveAutoFixedFile do not supported for that format
|
||||
* @return decks list
|
||||
*/
|
||||
public DeckCardLists importDeck(String file, StringBuilder errorMessages, boolean saveAutoFixedFile) {
|
||||
File f = new File(file);
|
||||
public DeckCardLists importDeck(String fileName, StringBuilder errorMessages, boolean saveAutoFixedFile) {
|
||||
File f = new File(fileName);
|
||||
DeckCardLists deckList = new DeckCardLists();
|
||||
if (!f.exists()) {
|
||||
logger.warn("Deckfile " + file + " not found.");
|
||||
logger.warn("Deckfile " + fileName + " not found.");
|
||||
return deckList;
|
||||
}
|
||||
|
||||
sbMessage.setLength(0);
|
||||
try {
|
||||
try (FileReader reader = new FileReader(f)) {
|
||||
try { // Json parsing
|
||||
JSONParser parser = new JSONParser();
|
||||
JSONObject rootObj = (JSONObject) parser.parse(reader);
|
||||
readJson(rootObj, deckList);
|
||||
try {
|
||||
JsonObject json = JsonParser.parseReader(reader).getAsJsonObject();
|
||||
readJson(json, deckList);
|
||||
|
||||
if (sbMessage.length() > 0) {
|
||||
if (errorMessages != null) {
|
||||
|
|
@ -46,8 +43,8 @@ public abstract class JsonDeckImporter extends DeckImporter {
|
|||
logger.fatal(sbMessage);
|
||||
}
|
||||
}
|
||||
} catch (ParseException ex) {
|
||||
logger.fatal(null, ex);
|
||||
} catch (JsonParseException ex) {
|
||||
logger.fatal("Can't parse json-deck: " + fileName, ex);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
logger.fatal(null, ex);
|
||||
|
|
@ -59,9 +56,9 @@ public abstract class JsonDeckImporter extends DeckImporter {
|
|||
}
|
||||
|
||||
@Override
|
||||
public DeckCardLists importDeck(String file, boolean saveAutoFixedFile) {
|
||||
return importDeck(file, null, saveAutoFixedFile);
|
||||
public DeckCardLists importDeck(String fileName, boolean saveAutoFixedFile) {
|
||||
return importDeck(fileName, null, saveAutoFixedFile);
|
||||
}
|
||||
|
||||
protected abstract void readJson(JSONObject line, DeckCardLists decklist);
|
||||
protected abstract void readJson(JsonObject json, DeckCardLists decklist);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
package mage.cards.decks.importer;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import mage.cards.decks.DeckCardInfo;
|
||||
import mage.cards.decks.DeckCardLists;
|
||||
import mage.cards.repository.CardInfo;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import mage.util.JsonUtil;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
|
@ -16,39 +17,45 @@ import java.util.Optional;
|
|||
public class MtgjsonDeckImporter extends JsonDeckImporter {
|
||||
|
||||
@Override
|
||||
protected void readJson(JSONObject rootObj, DeckCardLists deckList) {
|
||||
JSONObject data = (JSONObject) rootObj.get("data");
|
||||
protected void readJson(JsonObject json, DeckCardLists deckList) {
|
||||
JsonObject data = JsonUtil.getAsObject(json, "data");
|
||||
if (data == null) {
|
||||
sbMessage.append("Could not find data in json").append("'\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// info
|
||||
String deckSet = (String) data.get("code");
|
||||
String name = (String) data.get("name");
|
||||
if (name != null) {
|
||||
String deckSet = JsonUtil.getAsString(data, "code");
|
||||
String name = JsonUtil.getAsString(data, "name");
|
||||
if (!name.isEmpty()) {
|
||||
deckList.setName(name);
|
||||
}
|
||||
|
||||
// mainboard
|
||||
JSONArray mainBoard = (JSONArray) data.get("mainBoard");
|
||||
JsonArray mainBoard = JsonUtil.getAsArray(data, "mainBoard");
|
||||
List<mage.cards.decks.DeckCardInfo> mainDeckList = deckList.getCards();
|
||||
addBoardToList(mainBoard, mainDeckList, deckSet);
|
||||
|
||||
// sideboard
|
||||
JSONArray sideBoard = (JSONArray) data.get("sideBoard");
|
||||
JsonArray sideBoard = JsonUtil.getAsArray(data, "sideBoard");
|
||||
List<mage.cards.decks.DeckCardInfo> sideDeckList = deckList.getSideboard();
|
||||
addBoardToList(sideBoard, sideDeckList, deckSet);
|
||||
}
|
||||
|
||||
private void addBoardToList(JSONArray board, List<mage.cards.decks.DeckCardInfo> list, String deckSet) {
|
||||
private void addBoardToList(JsonArray board, List<mage.cards.decks.DeckCardInfo> list, String deckSet) {
|
||||
if (board == null || board.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
board.forEach(arrayCard -> {
|
||||
JSONObject card = (JSONObject) arrayCard;
|
||||
String name = (String) card.get("name");
|
||||
String setCode = (String) card.get("setCode");
|
||||
if (setCode == null || setCode.isEmpty()) {
|
||||
JsonObject card = (JsonObject) arrayCard;
|
||||
String name = JsonUtil.getAsString(card, "name");
|
||||
String setCode = JsonUtil.getAsString(card, "setCode");
|
||||
if (setCode.isEmpty()) {
|
||||
setCode = deckSet;
|
||||
}
|
||||
|
||||
int num = ((Number) card.get("count")).intValue();
|
||||
int num = JsonUtil.getAsInt(card, "count");
|
||||
Optional<CardInfo> cardLookup = getCardLookup().lookupCardInfo(name, setCode);
|
||||
if (!cardLookup.isPresent()) {
|
||||
sbMessage.append("Could not find card: '").append(name).append("'\n");
|
||||
|
|
|
|||
|
|
@ -22,9 +22,9 @@ public class O8dDeckImporter extends XmlDeckImporter {
|
|||
* @return
|
||||
*/
|
||||
@Override
|
||||
public DeckCardLists importDeck(String filename, StringBuilder errorMessages, boolean saveAutoFixedFile) {
|
||||
public DeckCardLists importDeck(String fileName, StringBuilder errorMessages, boolean saveAutoFixedFile) {
|
||||
try {
|
||||
Document doc = getXmlDocument(filename);
|
||||
Document doc = getXmlDocument(fileName);
|
||||
DeckCardLists decklist = new DeckCardLists();
|
||||
|
||||
List<Node> mainCards = getNodes(doc, "/deck/section[@name='Main']/card");
|
||||
|
|
|
|||
|
|
@ -26,13 +26,13 @@ public abstract class PlainTextDeckImporter extends DeckImporter {
|
|||
* @param saveAutoFixedFile save fixed deck file (if any fixes applied)
|
||||
* @return decks list
|
||||
*/
|
||||
public DeckCardLists importDeck(String file, StringBuilder errorMessages, boolean saveAutoFixedFile) {
|
||||
File f = new File(file);
|
||||
public DeckCardLists importDeck(String fileName, StringBuilder errorMessages, boolean saveAutoFixedFile) {
|
||||
File f = new File(fileName);
|
||||
List<String> originalFile = new ArrayList<>();
|
||||
List<String> fixedFile = new ArrayList<>();
|
||||
DeckCardLists deckList = new DeckCardLists();
|
||||
if (!f.exists()) {
|
||||
logger.warn("Deckfile " + file + " not found.");
|
||||
logger.warn("Deckfile " + fileName + " not found.");
|
||||
return deckList;
|
||||
}
|
||||
lineCount = 0;
|
||||
|
|
@ -94,8 +94,8 @@ public abstract class PlainTextDeckImporter extends DeckImporter {
|
|||
|
||||
|
||||
@Override
|
||||
public DeckCardLists importDeck(String file, boolean saveAutoFixedFile) {
|
||||
return importDeck(file, null, saveAutoFixedFile);
|
||||
public DeckCardLists importDeck(String fileName, boolean saveAutoFixedFile) {
|
||||
return importDeck(fileName, null, saveAutoFixedFile);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
50
Mage/src/main/java/mage/util/JsonUtil.java
Normal file
50
Mage/src/main/java/mage/util/JsonUtil.java
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
package mage.util;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
/**
|
||||
* @author JayDi85
|
||||
*/
|
||||
public class JsonUtil {
|
||||
|
||||
public static JsonObject getAsObject(JsonObject json, String field) {
|
||||
return json.has(field) ? json.get(field).getAsJsonObject() : null;
|
||||
}
|
||||
|
||||
public static JsonArray getAsArray(JsonObject json, String field) {
|
||||
return json.has(field) ? json.get(field).getAsJsonArray() : null;
|
||||
}
|
||||
|
||||
public static String getAsString(JsonObject json, String field) {
|
||||
return getAsString(json, field, "");
|
||||
}
|
||||
|
||||
public static String getAsString(JsonObject json, String field, String nullValue) {
|
||||
return json.has(field) ? json.get(field).getAsString() : nullValue;
|
||||
}
|
||||
|
||||
public static int getAsInt(JsonObject json, String field) {
|
||||
return getAsInt(json, field, 0);
|
||||
}
|
||||
|
||||
public static int getAsInt(JsonObject json, String field, int nullValue) {
|
||||
return json.has(field) ? json.get(field).getAsInt() : nullValue;
|
||||
}
|
||||
|
||||
public static double getAsDouble(JsonObject json, String field) {
|
||||
return getAsDouble(json, field, 0.0);
|
||||
}
|
||||
|
||||
public static double getAsDouble(JsonObject json, String field, double nullValue) {
|
||||
return json.has(field) ? json.get(field).getAsDouble() : nullValue;
|
||||
}
|
||||
|
||||
public static boolean getAsBoolean(JsonObject json, String field) {
|
||||
return getAsBoolean(json, field, false);
|
||||
}
|
||||
|
||||
public static boolean getAsBoolean(JsonObject json, String field, boolean nullValue) {
|
||||
return json.has(field) ? json.get(field).getAsBoolean() : nullValue;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue