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:
Oleg Agafonov 2021-10-01 17:57:54 +04:00
parent fa70af6131
commit 1c6eb10bd1
17 changed files with 147 additions and 76 deletions

View file

@ -1,11 +1,11 @@
package org.mage.plugins.card.dl.sources;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import mage.MageException;
import mage.client.util.CardLanguage;
import mage.util.JsonUtil;
import org.apache.log4j.Logger;
import org.mage.plugins.card.dl.DownloadServiceInfo;
import org.mage.plugins.card.images.CardDownloadData;
@ -174,14 +174,18 @@ public enum ScryfallImageSource implements CardImageSource {
// OK, found card data, parse it
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);
}
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

View file

@ -26,12 +26,6 @@
<artifactId>jspf-core</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<!-- json support -->
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>org.jboss.remoting</groupId>
@ -61,6 +55,11 @@
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>

View file

@ -1,6 +1,5 @@
package mage.cards.a;
import com.google.common.collect.Sets;
import mage.MageInt;
import mage.abilities.Abilities;
import mage.abilities.Ability;
@ -17,6 +16,8 @@ import mage.game.Game;
import mage.game.permanent.Permanent;
import mage.target.targetpointer.FixedTarget;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
@ -66,7 +67,7 @@ public final class AkromaVisionOfIxidor extends CardImpl {
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,
FirstStrikeAbility.class,
DoubleStrikeAbility.class,
@ -81,7 +82,7 @@ class AkromaVisionOfIxidorEffect extends OneShotEffect {
TrampleAbility.class,
VigilanceAbility.class,
PartnerAbility.class
);
));
AkromaVisionOfIxidorEffect() {
super(Outcome.Benefit);

View file

@ -1,6 +1,5 @@
package mage.cards.c;
import com.google.common.collect.Iterables;
import mage.abilities.Ability;
import mage.abilities.Mode;
import mage.abilities.effects.OneShotEffect;
@ -65,7 +64,8 @@ enum ConfrontThePastAdjuster implements TargetAdjuster {
@Override
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();
ability.getTargets().clear();
FilterPermanentCard filter = new FilterPermanentCard("planeswalker card with mana value X or less");

View file

@ -1,6 +1,5 @@
package mage.cards.e;
import com.google.common.collect.Sets;
import mage.MageObject;
import mage.abilities.Ability;
import mage.abilities.effects.OneShotEffect;
@ -22,6 +21,7 @@ import mage.target.common.TargetCardInYourGraveyard;
import mage.target.common.TargetOpponent;
import mage.util.CardUtil;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
@ -109,7 +109,10 @@ class EcologicalAppreciationEffect extends OneShotEffect {
Set<Card> disallowedCards = this.getTargets().stream()
.map(game::getCard)
.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);

View file

@ -1,6 +1,5 @@
package mage.cards.k;
import com.google.common.base.Objects;
import mage.MageInt;
import mage.MageObject;
import mage.abilities.Ability;
@ -22,6 +21,7 @@ import mage.filter.predicate.mageobject.AnotherPredicate;
import mage.game.Game;
import mage.target.TargetPermanent;
import java.util.Objects;
import java.util.UUID;
/**
@ -74,7 +74,7 @@ enum KitesailSkirmisherPredicate implements ObjectSourcePlayerPredicate<MageObje
@Override
public boolean apply(ObjectSourcePlayer<MageObject> input, Game game) {
return Objects.equal(
return Objects.equals(
game.getCombat().getDefenderId(input.getSourceId()),
game.getCombat().getDefenderId(input.getObject().getId())
);

View file

@ -35,8 +35,6 @@ import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import static com.google.common.collect.Iterables.getOnlyElement;
/**
* @author htrajan
*/
@ -97,8 +95,7 @@ class TayamLuminousEnigmaCost extends RemoveCounterCost {
Player controller = game.getPlayer(controllerId);
for (int i = 0; i < countersToRemove; i++) {
if (target.choose(Outcome.UnboostCreature, controllerId, source.getSourceId(), game)) {
UUID targetId = getOnlyElement(target.getTargets());
Permanent permanent = game.getPermanent(targetId);
Permanent permanent = game.getPermanent(target.getFirstTarget());
if (permanent != null) {
if (!permanent.getCounters(game).isEmpty()) {
String counterName = null;

View file

@ -3,6 +3,7 @@ package org.mage.test.utils;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import mage.util.JsonUtil;
import org.junit.Assert;
import org.junit.Test;
@ -25,10 +26,17 @@ public class JsonGsonTest {
try {
// low level parser for unknown data structure
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("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) {
e.printStackTrace();
Assert.fail("Can't load sample json file: " + sampleFileName);

View file

@ -14,11 +14,6 @@
<name>Mage Framework</name>
<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
@ -34,6 +29,10 @@
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>com.j256.ormlite</groupId>
<artifactId>ormlite-jdbc</artifactId>

View file

@ -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");

View file

@ -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() {

View file

@ -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);
}

View file

@ -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");

View file

@ -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");

View file

@ -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);
}
/**

View 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;
}
}

View file

@ -174,6 +174,12 @@
<artifactId>slf4j-log4j12</artifactId>
<version>1.8.0-beta2</version>
</dependency>
<dependency>
<!-- json support -->
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>