mkmrare/cmd/convert.ts

62 lines
1.9 KiB
TypeScript
Raw Permalink Normal View History

2019-11-21 15:56:34 +00:00
import { existsSync } from "fs";
2019-11-22 14:58:55 +00:00
import { asyncLoadJSON, asyncSaveJSON, MCMCard, MCMDB, ScryfallCard, ScryfallDB } from "../lib";
2019-11-21 14:16:04 +00:00
interface JSONSet {
2019-11-22 14:58:55 +00:00
cards: MCMCard[];
2019-11-21 14:16:04 +00:00
}
type JSONDB = Record<string, JSONSet>;
2019-11-22 14:58:55 +00:00
const mtgjsonFile = "AllPrintings.json";
const scryfallJSONFile = "scryfall-default-cards.json";
2019-11-21 14:16:04 +00:00
async function run() {
2019-11-22 14:58:55 +00:00
if (!existsSync(mtgjsonFile)) {
console.error(
mtgjsonFile +
" not found. Download from: https://www.mtgjson.com/files/AllPrintings.json"
);
process.exit(1);
}
if (!existsSync(scryfallJSONFile)) {
2019-11-21 15:56:34 +00:00
console.error(
2019-11-22 14:58:55 +00:00
scryfallJSONFile +
" not found. Download from: https://archive.scryfall.com/json/scryfall-default-cards.json"
2019-11-21 15:56:34 +00:00
);
process.exit(1);
}
2019-11-22 14:58:55 +00:00
console.info("Loading " + mtgjsonFile);
const mtgdb = await asyncLoadJSON<JSONDB>(mtgjsonFile);
console.info("Loading " + scryfallJSONFile);
const scrydb = await asyncLoadJSON<ScryfallDB>(scryfallJSONFile);
let scrycards: Record<string, ScryfallCard> = {};
scrydb.forEach(c => (scrycards[c.id] = c));
let acc: MCMDB = {};
for (const set in mtgdb) {
mtgdb[set].cards.forEach(c => {
2019-11-21 15:56:34 +00:00
c.set = set;
2019-11-22 14:58:55 +00:00
if (c.scryfallId in scrycards) {
c.scryfallUrl = scrycards[c.scryfallId].scryfall_uri;
if ("image_uris" in scrycards[c.scryfallId]) {
c.scryfallImageUrl = scrycards[c.scryfallId].image_uris.normal;
} else if (
"card_faces" in scrycards[c.scryfallId] &&
"image_uris" in scrycards[c.scryfallId].card_faces[0]
) {
c.scryfallImageUrl =
scrycards[c.scryfallId].card_faces[0].image_uris.normal;
} else {
console.log(scrycards[c.scryfallId]);
}
}
// Search for scryfall card
2019-11-21 15:56:34 +00:00
acc[c.mcmId] = c;
});
2019-11-21 14:16:04 +00:00
}
asyncSaveJSON("mcmCards.json", acc);
2019-11-21 15:56:34 +00:00
console.info("Saved converted info to mcmCards.json. You're ready to go!");
2019-11-21 14:16:04 +00:00
}
run();