mirror of
https://github.com/correl/mage.git
synced 2024-11-24 19:19:56 +00: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
|
@ -1,11 +1,11 @@
|
||||||
package org.mage.plugins.card.dl.sources;
|
package org.mage.plugins.card.dl.sources;
|
||||||
|
|
||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonElement;
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
import mage.MageException;
|
import mage.MageException;
|
||||||
import mage.client.util.CardLanguage;
|
import mage.client.util.CardLanguage;
|
||||||
|
import mage.util.JsonUtil;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
import org.mage.plugins.card.dl.DownloadServiceInfo;
|
import org.mage.plugins.card.dl.DownloadServiceInfo;
|
||||||
import org.mage.plugins.card.images.CardDownloadData;
|
import org.mage.plugins.card.images.CardDownloadData;
|
||||||
|
@ -174,14 +174,18 @@ public enum ScryfallImageSource implements CardImageSource {
|
||||||
|
|
||||||
// OK, found card data, parse it
|
// OK, found card data, parse it
|
||||||
JsonObject jsonCard = JsonParser.parseReader(new InputStreamReader(jsonStream)).getAsJsonObject();
|
JsonObject jsonCard = JsonParser.parseReader(new InputStreamReader(jsonStream)).getAsJsonObject();
|
||||||
if (!jsonCard.has("card_faces")) {
|
JsonArray jsonFaces = JsonUtil.getAsArray(jsonCard, "card_faces");
|
||||||
|
if (jsonFaces == null) {
|
||||||
throw new MageException("Couldn't find card_faces in card's JSON data: " + jsonUrl);
|
throw new MageException("Couldn't find card_faces in card's JSON data: " + jsonUrl);
|
||||||
}
|
}
|
||||||
JsonArray jsonCardFaces = jsonCard.getAsJsonArray("card_faces");
|
|
||||||
JsonObject jsonCardFace = jsonCardFaces.get(card.isSecondSide() ? 1 : 0).getAsJsonObject();
|
|
||||||
JsonObject jsonImageUris = jsonCardFace.getAsJsonObject("image_uris");
|
|
||||||
|
|
||||||
return jsonImageUris.get("large").getAsString();
|
JsonObject jsonFace = jsonFaces.get(card.isSecondSide() ? 1 : 0).getAsJsonObject();
|
||||||
|
JsonObject jsonImages = JsonUtil.getAsObject(jsonFace, "image_uris");
|
||||||
|
if (jsonImages == null) {
|
||||||
|
throw new MageException("Couldn't find image_uris in card's JSON data: " + jsonUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
return JsonUtil.getAsString(jsonImages, "large");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -26,12 +26,6 @@
|
||||||
<artifactId>jspf-core</artifactId>
|
<artifactId>jspf-core</artifactId>
|
||||||
<version>0.9.1</version>
|
<version>0.9.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<!-- json support -->
|
|
||||||
<groupId>com.google.code.gson</groupId>
|
|
||||||
<artifactId>gson</artifactId>
|
|
||||||
<version>2.8.8</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jboss.remoting</groupId>
|
<groupId>org.jboss.remoting</groupId>
|
||||||
|
@ -61,6 +55,11 @@
|
||||||
<version>1.0.2</version>
|
<version>1.0.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>log4j</groupId>
|
||||||
|
<artifactId>log4j</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<artifactId>junit-jupiter</artifactId>
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package mage.cards.a;
|
package mage.cards.a;
|
||||||
|
|
||||||
import com.google.common.collect.Sets;
|
|
||||||
import mage.MageInt;
|
import mage.MageInt;
|
||||||
import mage.abilities.Abilities;
|
import mage.abilities.Abilities;
|
||||||
import mage.abilities.Ability;
|
import mage.abilities.Ability;
|
||||||
|
@ -17,6 +16,8 @@ import mage.game.Game;
|
||||||
import mage.game.permanent.Permanent;
|
import mage.game.permanent.Permanent;
|
||||||
import mage.target.targetpointer.FixedTarget;
|
import mage.target.targetpointer.FixedTarget;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@ -66,7 +67,7 @@ public final class AkromaVisionOfIxidor extends CardImpl {
|
||||||
|
|
||||||
class AkromaVisionOfIxidorEffect extends OneShotEffect {
|
class AkromaVisionOfIxidorEffect extends OneShotEffect {
|
||||||
|
|
||||||
private static final Set<Class<? extends Ability>> classes = Sets.newHashSet(
|
private static final Set<Class<? extends Ability>> classes = new HashSet<>(Arrays.asList(
|
||||||
FlyingAbility.class,
|
FlyingAbility.class,
|
||||||
FirstStrikeAbility.class,
|
FirstStrikeAbility.class,
|
||||||
DoubleStrikeAbility.class,
|
DoubleStrikeAbility.class,
|
||||||
|
@ -81,7 +82,7 @@ class AkromaVisionOfIxidorEffect extends OneShotEffect {
|
||||||
TrampleAbility.class,
|
TrampleAbility.class,
|
||||||
VigilanceAbility.class,
|
VigilanceAbility.class,
|
||||||
PartnerAbility.class
|
PartnerAbility.class
|
||||||
);
|
));
|
||||||
|
|
||||||
AkromaVisionOfIxidorEffect() {
|
AkromaVisionOfIxidorEffect() {
|
||||||
super(Outcome.Benefit);
|
super(Outcome.Benefit);
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package mage.cards.c;
|
package mage.cards.c;
|
||||||
|
|
||||||
import com.google.common.collect.Iterables;
|
|
||||||
import mage.abilities.Ability;
|
import mage.abilities.Ability;
|
||||||
import mage.abilities.Mode;
|
import mage.abilities.Mode;
|
||||||
import mage.abilities.effects.OneShotEffect;
|
import mage.abilities.effects.OneShotEffect;
|
||||||
|
@ -65,7 +64,8 @@ enum ConfrontThePastAdjuster implements TargetAdjuster {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void adjustTargets(Ability ability, Game game) {
|
public void adjustTargets(Ability ability, Game game) {
|
||||||
if (Iterables.getOnlyElement(ability.getEffects()) instanceof ReturnFromGraveyardToBattlefieldTargetEffect) {
|
if (ability.getEffects().size() == 1
|
||||||
|
&& ability.getEffects().get(0) instanceof ReturnFromGraveyardToBattlefieldTargetEffect) {
|
||||||
int xValue = ability.getManaCostsToPay().getX();
|
int xValue = ability.getManaCostsToPay().getX();
|
||||||
ability.getTargets().clear();
|
ability.getTargets().clear();
|
||||||
FilterPermanentCard filter = new FilterPermanentCard("planeswalker card with mana value X or less");
|
FilterPermanentCard filter = new FilterPermanentCard("planeswalker card with mana value X or less");
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package mage.cards.e;
|
package mage.cards.e;
|
||||||
|
|
||||||
import com.google.common.collect.Sets;
|
|
||||||
import mage.MageObject;
|
import mage.MageObject;
|
||||||
import mage.abilities.Ability;
|
import mage.abilities.Ability;
|
||||||
import mage.abilities.effects.OneShotEffect;
|
import mage.abilities.effects.OneShotEffect;
|
||||||
|
@ -22,6 +21,7 @@ import mage.target.common.TargetCardInYourGraveyard;
|
||||||
import mage.target.common.TargetOpponent;
|
import mage.target.common.TargetOpponent;
|
||||||
import mage.util.CardUtil;
|
import mage.util.CardUtil;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
@ -109,7 +109,10 @@ class EcologicalAppreciationEffect extends OneShotEffect {
|
||||||
Set<Card> disallowedCards = this.getTargets().stream()
|
Set<Card> disallowedCards = this.getTargets().stream()
|
||||||
.map(game::getCard)
|
.map(game::getCard)
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
return isValidTarget(card, Sets.union(disallowedCards, cards.getCards(game)));
|
Set<Card> checkList = new HashSet<>();
|
||||||
|
checkList.addAll(disallowedCards);
|
||||||
|
checkList.addAll(cards.getCards(game));
|
||||||
|
return isValidTarget(card, checkList);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
targetCardsInGY.setNotTarget(true);
|
targetCardsInGY.setNotTarget(true);
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package mage.cards.k;
|
package mage.cards.k;
|
||||||
|
|
||||||
import com.google.common.base.Objects;
|
|
||||||
import mage.MageInt;
|
import mage.MageInt;
|
||||||
import mage.MageObject;
|
import mage.MageObject;
|
||||||
import mage.abilities.Ability;
|
import mage.abilities.Ability;
|
||||||
|
@ -22,6 +21,7 @@ import mage.filter.predicate.mageobject.AnotherPredicate;
|
||||||
import mage.game.Game;
|
import mage.game.Game;
|
||||||
import mage.target.TargetPermanent;
|
import mage.target.TargetPermanent;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -74,7 +74,7 @@ enum KitesailSkirmisherPredicate implements ObjectSourcePlayerPredicate<MageObje
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean apply(ObjectSourcePlayer<MageObject> input, Game game) {
|
public boolean apply(ObjectSourcePlayer<MageObject> input, Game game) {
|
||||||
return Objects.equal(
|
return Objects.equals(
|
||||||
game.getCombat().getDefenderId(input.getSourceId()),
|
game.getCombat().getDefenderId(input.getSourceId()),
|
||||||
game.getCombat().getDefenderId(input.getObject().getId())
|
game.getCombat().getDefenderId(input.getObject().getId())
|
||||||
);
|
);
|
||||||
|
|
|
@ -35,8 +35,6 @@ import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import static com.google.common.collect.Iterables.getOnlyElement;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author htrajan
|
* @author htrajan
|
||||||
*/
|
*/
|
||||||
|
@ -97,8 +95,7 @@ class TayamLuminousEnigmaCost extends RemoveCounterCost {
|
||||||
Player controller = game.getPlayer(controllerId);
|
Player controller = game.getPlayer(controllerId);
|
||||||
for (int i = 0; i < countersToRemove; i++) {
|
for (int i = 0; i < countersToRemove; i++) {
|
||||||
if (target.choose(Outcome.UnboostCreature, controllerId, source.getSourceId(), game)) {
|
if (target.choose(Outcome.UnboostCreature, controllerId, source.getSourceId(), game)) {
|
||||||
UUID targetId = getOnlyElement(target.getTargets());
|
Permanent permanent = game.getPermanent(target.getFirstTarget());
|
||||||
Permanent permanent = game.getPermanent(targetId);
|
|
||||||
if (permanent != null) {
|
if (permanent != null) {
|
||||||
if (!permanent.getCounters(game).isEmpty()) {
|
if (!permanent.getCounters(game).isEmpty()) {
|
||||||
String counterName = null;
|
String counterName = null;
|
||||||
|
|
|
@ -3,6 +3,7 @@ package org.mage.test.utils;
|
||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
|
import mage.util.JsonUtil;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
@ -25,10 +26,17 @@ public class JsonGsonTest {
|
||||||
try {
|
try {
|
||||||
// low level parser for unknown data structure
|
// low level parser for unknown data structure
|
||||||
JsonObject json = JsonParser.parseReader(new FileReader(sampleFileName)).getAsJsonObject();
|
JsonObject json = JsonParser.parseReader(new FileReader(sampleFileName)).getAsJsonObject();
|
||||||
Assert.assertEquals("Unknown data", "card", json.get("object").getAsString());
|
|
||||||
JsonArray jsonFaces = json.getAsJsonArray("card_faces");
|
// data types
|
||||||
|
Assert.assertEquals("string", "card", JsonUtil.getAsString(json, "object"));
|
||||||
|
JsonArray jsonFaces = JsonUtil.getAsArray(json, "card_faces");
|
||||||
|
Assert.assertEquals("int", 60370, JsonUtil.getAsInt(json, "mtgo_id"));
|
||||||
|
Assert.assertTrue("boolean", JsonUtil.getAsBoolean(json, "highres_image"));
|
||||||
|
Assert.assertEquals("double", 4.0, JsonUtil.getAsDouble(json, "cmc"), 0.0);
|
||||||
|
Assert.assertNotNull("array", jsonFaces);
|
||||||
|
|
||||||
Assert.assertEquals("Card must have 2 faces", 2, jsonFaces.size());
|
Assert.assertEquals("Card must have 2 faces", 2, jsonFaces.size());
|
||||||
Assert.assertEquals("Unknown second side", "Infectious Curse", jsonFaces.get(1).getAsJsonObject().get("name").getAsString());
|
Assert.assertEquals("Unknown second side", "Infectious Curse", JsonUtil.getAsString(jsonFaces.get(1).getAsJsonObject(), "name"));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
Assert.fail("Can't load sample json file: " + sampleFileName);
|
Assert.fail("Can't load sample json file: " + sampleFileName);
|
||||||
|
|
|
@ -14,11 +14,6 @@
|
||||||
<name>Mage Framework</name>
|
<name>Mage Framework</name>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
|
||||||
<groupId>com.googlecode.json-simple</groupId>
|
|
||||||
<artifactId>json-simple</artifactId>
|
|
||||||
<version>1.1.1</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>log4j</groupId>
|
<groupId>log4j</groupId>
|
||||||
<artifactId>log4j</artifactId>
|
<artifactId>log4j</artifactId>
|
||||||
|
@ -34,6 +29,10 @@
|
||||||
<groupId>com.google.guava</groupId>
|
<groupId>com.google.guava</groupId>
|
||||||
<artifactId>guava</artifactId>
|
<artifactId>guava</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.j256.ormlite</groupId>
|
<groupId>com.j256.ormlite</groupId>
|
||||||
<artifactId>ormlite-jdbc</artifactId>
|
<artifactId>ormlite-jdbc</artifactId>
|
||||||
|
|
|
@ -22,9 +22,9 @@ public class CodDeckImporter extends XmlDeckImporter {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public DeckCardLists importDeck(String filename, StringBuilder errorMessages, boolean saveAutoFixedFile) {
|
public DeckCardLists importDeck(String fileName, StringBuilder errorMessages, boolean saveAutoFixedFile) {
|
||||||
try {
|
try {
|
||||||
Document doc = getXmlDocument(filename);
|
Document doc = getXmlDocument(fileName);
|
||||||
DeckCardLists decklist = new DeckCardLists();
|
DeckCardLists decklist = new DeckCardLists();
|
||||||
|
|
||||||
List<Node> mainCards = getNodes(doc, "/cockatrice_deck/zone[@name='main']/card");
|
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) {
|
public DeckCardLists importDeck(String fileName, boolean saveAutoFixedFile) {
|
||||||
return importDeck(file, new StringBuilder(), saveAutoFixedFile);
|
return importDeck(fileName, new StringBuilder(), saveAutoFixedFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
public CardLookup getCardLookup() {
|
public CardLookup getCardLookup() {
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
package mage.cards.decks.importer;
|
package mage.cards.decks.importer;
|
||||||
|
|
||||||
|
import com.google.gson.*;
|
||||||
import mage.cards.decks.DeckCardLists;
|
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.File;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
|
@ -16,26 +14,25 @@ public abstract class JsonDeckImporter extends DeckImporter {
|
||||||
protected StringBuilder sbMessage = new StringBuilder();
|
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 errorMessages you can setup output messages to showup to user
|
||||||
* @param saveAutoFixedFile do not supported for that format
|
* @param saveAutoFixedFile do not supported for that format
|
||||||
* @return decks list
|
* @return decks list
|
||||||
*/
|
*/
|
||||||
public DeckCardLists importDeck(String file, StringBuilder errorMessages, boolean saveAutoFixedFile) {
|
public DeckCardLists importDeck(String fileName, StringBuilder errorMessages, boolean saveAutoFixedFile) {
|
||||||
File f = new File(file);
|
File f = new File(fileName);
|
||||||
DeckCardLists deckList = new DeckCardLists();
|
DeckCardLists deckList = new DeckCardLists();
|
||||||
if (!f.exists()) {
|
if (!f.exists()) {
|
||||||
logger.warn("Deckfile " + file + " not found.");
|
logger.warn("Deckfile " + fileName + " not found.");
|
||||||
return deckList;
|
return deckList;
|
||||||
}
|
}
|
||||||
|
|
||||||
sbMessage.setLength(0);
|
sbMessage.setLength(0);
|
||||||
try {
|
try {
|
||||||
try (FileReader reader = new FileReader(f)) {
|
try (FileReader reader = new FileReader(f)) {
|
||||||
try { // Json parsing
|
try {
|
||||||
JSONParser parser = new JSONParser();
|
JsonObject json = JsonParser.parseReader(reader).getAsJsonObject();
|
||||||
JSONObject rootObj = (JSONObject) parser.parse(reader);
|
readJson(json, deckList);
|
||||||
readJson(rootObj, deckList);
|
|
||||||
|
|
||||||
if (sbMessage.length() > 0) {
|
if (sbMessage.length() > 0) {
|
||||||
if (errorMessages != null) {
|
if (errorMessages != null) {
|
||||||
|
@ -46,8 +43,8 @@ public abstract class JsonDeckImporter extends DeckImporter {
|
||||||
logger.fatal(sbMessage);
|
logger.fatal(sbMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (ParseException ex) {
|
} catch (JsonParseException ex) {
|
||||||
logger.fatal(null, ex);
|
logger.fatal("Can't parse json-deck: " + fileName, ex);
|
||||||
}
|
}
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
logger.fatal(null, ex);
|
logger.fatal(null, ex);
|
||||||
|
@ -59,9 +56,9 @@ public abstract class JsonDeckImporter extends DeckImporter {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DeckCardLists importDeck(String file, boolean saveAutoFixedFile) {
|
public DeckCardLists importDeck(String fileName, boolean saveAutoFixedFile) {
|
||||||
return importDeck(file, null, 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;
|
package mage.cards.decks.importer;
|
||||||
|
|
||||||
|
import com.google.gson.JsonArray;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
import mage.cards.decks.DeckCardInfo;
|
import mage.cards.decks.DeckCardInfo;
|
||||||
import mage.cards.decks.DeckCardLists;
|
import mage.cards.decks.DeckCardLists;
|
||||||
import mage.cards.repository.CardInfo;
|
import mage.cards.repository.CardInfo;
|
||||||
import org.json.simple.JSONArray;
|
import mage.util.JsonUtil;
|
||||||
import org.json.simple.JSONObject;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
@ -16,39 +17,45 @@ import java.util.Optional;
|
||||||
public class MtgjsonDeckImporter extends JsonDeckImporter {
|
public class MtgjsonDeckImporter extends JsonDeckImporter {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void readJson(JSONObject rootObj, DeckCardLists deckList) {
|
protected void readJson(JsonObject json, DeckCardLists deckList) {
|
||||||
JSONObject data = (JSONObject) rootObj.get("data");
|
JsonObject data = JsonUtil.getAsObject(json, "data");
|
||||||
if (data == null) {
|
if (data == null) {
|
||||||
sbMessage.append("Could not find data in json").append("'\n");
|
sbMessage.append("Could not find data in json").append("'\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// info
|
// info
|
||||||
String deckSet = (String) data.get("code");
|
String deckSet = JsonUtil.getAsString(data, "code");
|
||||||
String name = (String) data.get("name");
|
String name = JsonUtil.getAsString(data, "name");
|
||||||
if (name != null) {
|
if (!name.isEmpty()) {
|
||||||
deckList.setName(name);
|
deckList.setName(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
// mainboard
|
// mainboard
|
||||||
JSONArray mainBoard = (JSONArray) data.get("mainBoard");
|
JsonArray mainBoard = JsonUtil.getAsArray(data, "mainBoard");
|
||||||
List<mage.cards.decks.DeckCardInfo> mainDeckList = deckList.getCards();
|
List<mage.cards.decks.DeckCardInfo> mainDeckList = deckList.getCards();
|
||||||
addBoardToList(mainBoard, mainDeckList, deckSet);
|
addBoardToList(mainBoard, mainDeckList, deckSet);
|
||||||
|
|
||||||
// sideboard
|
// sideboard
|
||||||
JSONArray sideBoard = (JSONArray) data.get("sideBoard");
|
JsonArray sideBoard = JsonUtil.getAsArray(data, "sideBoard");
|
||||||
List<mage.cards.decks.DeckCardInfo> sideDeckList = deckList.getSideboard();
|
List<mage.cards.decks.DeckCardInfo> sideDeckList = deckList.getSideboard();
|
||||||
addBoardToList(sideBoard, sideDeckList, deckSet);
|
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 -> {
|
board.forEach(arrayCard -> {
|
||||||
JSONObject card = (JSONObject) arrayCard;
|
JsonObject card = (JsonObject) arrayCard;
|
||||||
String name = (String) card.get("name");
|
String name = JsonUtil.getAsString(card, "name");
|
||||||
String setCode = (String) card.get("setCode");
|
String setCode = JsonUtil.getAsString(card, "setCode");
|
||||||
if (setCode == null || setCode.isEmpty()) {
|
if (setCode.isEmpty()) {
|
||||||
setCode = deckSet;
|
setCode = deckSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
int num = ((Number) card.get("count")).intValue();
|
int num = JsonUtil.getAsInt(card, "count");
|
||||||
Optional<CardInfo> cardLookup = getCardLookup().lookupCardInfo(name, setCode);
|
Optional<CardInfo> cardLookup = getCardLookup().lookupCardInfo(name, setCode);
|
||||||
if (!cardLookup.isPresent()) {
|
if (!cardLookup.isPresent()) {
|
||||||
sbMessage.append("Could not find card: '").append(name).append("'\n");
|
sbMessage.append("Could not find card: '").append(name).append("'\n");
|
||||||
|
|
|
@ -22,9 +22,9 @@ public class O8dDeckImporter extends XmlDeckImporter {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public DeckCardLists importDeck(String filename, StringBuilder errorMessages, boolean saveAutoFixedFile) {
|
public DeckCardLists importDeck(String fileName, StringBuilder errorMessages, boolean saveAutoFixedFile) {
|
||||||
try {
|
try {
|
||||||
Document doc = getXmlDocument(filename);
|
Document doc = getXmlDocument(fileName);
|
||||||
DeckCardLists decklist = new DeckCardLists();
|
DeckCardLists decklist = new DeckCardLists();
|
||||||
|
|
||||||
List<Node> mainCards = getNodes(doc, "/deck/section[@name='Main']/card");
|
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)
|
* @param saveAutoFixedFile save fixed deck file (if any fixes applied)
|
||||||
* @return decks list
|
* @return decks list
|
||||||
*/
|
*/
|
||||||
public DeckCardLists importDeck(String file, StringBuilder errorMessages, boolean saveAutoFixedFile) {
|
public DeckCardLists importDeck(String fileName, StringBuilder errorMessages, boolean saveAutoFixedFile) {
|
||||||
File f = new File(file);
|
File f = new File(fileName);
|
||||||
List<String> originalFile = new ArrayList<>();
|
List<String> originalFile = new ArrayList<>();
|
||||||
List<String> fixedFile = new ArrayList<>();
|
List<String> fixedFile = new ArrayList<>();
|
||||||
DeckCardLists deckList = new DeckCardLists();
|
DeckCardLists deckList = new DeckCardLists();
|
||||||
if (!f.exists()) {
|
if (!f.exists()) {
|
||||||
logger.warn("Deckfile " + file + " not found.");
|
logger.warn("Deckfile " + fileName + " not found.");
|
||||||
return deckList;
|
return deckList;
|
||||||
}
|
}
|
||||||
lineCount = 0;
|
lineCount = 0;
|
||||||
|
@ -94,8 +94,8 @@ public abstract class PlainTextDeckImporter extends DeckImporter {
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DeckCardLists importDeck(String file, boolean saveAutoFixedFile) {
|
public DeckCardLists importDeck(String fileName, boolean saveAutoFixedFile) {
|
||||||
return importDeck(file, null, 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;
|
||||||
|
}
|
||||||
|
}
|
6
pom.xml
6
pom.xml
|
@ -174,6 +174,12 @@
|
||||||
<artifactId>slf4j-log4j12</artifactId>
|
<artifactId>slf4j-log4j12</artifactId>
|
||||||
<version>1.8.0-beta2</version>
|
<version>1.8.0-beta2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<!-- json support -->
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>2.8.8</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.guava</groupId>
|
<groupId>com.google.guava</groupId>
|
||||||
<artifactId>guava</artifactId>
|
<artifactId>guava</artifactId>
|
||||||
|
|
Loading…
Reference in a new issue