mlpcardgame/src/mlpccg/set.ts

48 lines
971 B
TypeScript

import { SetFile } from "./types";
import { openCardDB } from "./database";
const baseURL = "https://mcg.zyg.ovh/setdata/";
const allSets = [
"PR",
"CN",
"RR",
"CS",
"CG",
"AD",
"EO",
"HM",
"MT",
"DE",
"SB",
"FF",
"Promo"
];
export async function loadSets() {
const db = await openCardDB();
const itemcount = await db.count("card");
if (itemcount > 100) {
// DB already filled, exit early
return;
}
const sets = await Promise.all(allSets.map(set => downloadSet(set)));
await Promise.all(
sets.map(
async set =>
await Promise.all(
set.Cards.map(async card => await db.put("card", card))
)
)
);
}
async function downloadSet(setid: string): Promise<SetFile> {
const setfile = await fetch(`${baseURL}${setid.toLowerCase()}.json`);
const setdata: SetFile = await setfile.json();
setdata.Cards = setdata.Cards.map(c => {
c.Set = setid;
return c;
});
return setdata;
}