Skip to content

Commit

Permalink
scryfall init other sets
Browse files Browse the repository at this point in the history
  • Loading branch information
nicol authored and nicol committed Dec 17, 2024
1 parent 6e030e3 commit 565ed72
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
69 changes: 60 additions & 9 deletions src/main/java/org/magic/api/providers/impl/ScryFallProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.magic.services.MTGConstants;
import org.magic.services.network.RequestBuilder;
import org.magic.services.network.URLTools;
import org.magic.services.threads.MTGRunnable;
import org.magic.services.threads.ThreadManager;
import org.magic.services.tools.BeanTools;
import org.magic.services.tools.FileTools;
Expand Down Expand Up @@ -78,10 +79,48 @@ public class ScryFallProvider extends AbstractCardsProvider {
private static final String BASE_URI = "https://api.scryfall.com";
private static final String BASE_SUBURI = "/cards/";
private Map<String, String> languages;
private HashMap<String, List<String>> mapOtherSet;



public enum BULKTYPE {ORACLE_CARDS,UNIQUE_ARTWORK,DEFAULT_CARDS, ALL_CARDS,RULINGS}


@Override
public void init() {


ThreadManager.getInstance().executeThread(new MTGRunnable() {

@Override
protected void auditedRun() {

try {
initMapOtherSet();
} catch (IOException e) {
logger.error("error init other sets {}",e);
}

}
}, "init scryfall Sets");



}

private void initMapOtherSet() throws IOException
{

var f = bulkData(BULKTYPE.DEFAULT_CARDS);
var arr = URLTools.toJson(FileTools.readFile(f)).getAsJsonArray();

for(var e : arr)
{
if(e.getAsJsonObject().get("oracle_id")!=null)
mapOtherSet.computeIfAbsent(e.getAsJsonObject().get("oracle_id").getAsString(),v->new ArrayList<>()).add(e.getAsJsonObject().get("set").getAsString());
}
}

private List<MTGRuling> generatesRulings(String oracleId)
{
File f;
Expand Down Expand Up @@ -110,6 +149,10 @@ private List<MTGRuling> generatesRulings(String oracleId)

public ScryFallProvider() {
languages = new HashMap<>();
mapOtherSet = new HashMap<String, List<String>>();



languages.put("es","Spanish");
languages.put("fr","French");
languages.put("de","German");
Expand All @@ -133,10 +176,10 @@ public File bulkData(BULKTYPE t) throws IOException
{
var f = new File(MTGConstants.DATA_DIR,t.name().toLowerCase()+".json");

if(f.exists()|| FileTools.daysBetween(f)<=1)
if(f.exists() && FileTools.daysBetween(f)<=1)
return f;


logger.info("{} will be update",f);
var arr = URLTools.extractAsJson(BASE_URI + "/bulk-data").getAsJsonObject().get("data").getAsJsonArray();

for(var obj : arr)
Expand All @@ -145,8 +188,6 @@ public File bulkData(BULKTYPE t) throws IOException

if(jo.get("type").getAsString().equalsIgnoreCase(t.name()))
{


URLTools.download(jo.get("download_uri").getAsString(), f);
return f;
}
Expand Down Expand Up @@ -426,6 +467,7 @@ private Boolean readAsBoolean(JsonObject obj , String k)
}
}



private MTGCard generateCard(JsonObject obj, boolean loadMeld) throws ExecutionException {

Expand Down Expand Up @@ -463,12 +505,15 @@ private MTGCard generateCard(JsonObject obj, boolean loadMeld) throws ExecutionE
mc.setHasContentWarning(readAsBoolean(obj,"content_warning"));
mc.setFlavorName(readAsString(obj,"flavor_name"));
mc.setOriginalReleaseDate(readAsString(obj,RELEASED_AT));
//mc.setRulings(generatesRulings(readAsString(obj,"oracle_id")))

mc.setRulings(generatesRulings(readAsString(obj,"oracle_id")));
generateTypes(mc, obj.get(TYPE_LINE));


mapOtherSet.get(readAsString(obj,"oracle_id")).forEach(s->mc.getEditions().add(getSetById(s.toUpperCase())));



generateTypes(mc, obj.get(TYPE_LINE));


if (obj.get(GAMES) != null) {
mc.setArenaCard(obj.get(GAMES).getAsJsonArray().contains(new JsonPrimitive("arena")));
Expand Down Expand Up @@ -537,9 +582,15 @@ private MTGCard generateCard(JsonObject obj, boolean loadMeld) throws ExecutionE
}
}






postTreatmentCard(mc);

notify(mc);
notify(mc);


return mc;

}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/magic/services/MTGControler.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private MTGControler() {
logger.info("Java {}. Vendor: {}",Runtime.version(),SystemUtils.JAVA_VENDOR);
logger.info("OS {}, Version: {}",SystemUtils.OS_NAME,SystemUtils.OS_VERSION);
logger.info("Local directory : {}",MTGConstants.CONF_DIR);
logger.info("Logger File : {}",MTGLogger.getContext().getConfiguration());
logger.debug("Logger File : {}",MTGLogger.getContext().getConfiguration());
logger.info(bottom);


Expand Down

0 comments on commit 565ed72

Please sign in to comment.