45 lines
945 B
TypeScript
45 lines
945 B
TypeScript
import { SetFile } from "./types";
|
|
import { Database } 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 itemcount = await Database.cards.count();
|
|
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 => {
|
|
console.log(`Processing cards from ${set.Name}`);
|
|
return await Database.cards.bulkPut(set.Cards);
|
|
})
|
|
);
|
|
}
|
|
|
|
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;
|
|
}
|