mlpcardgame/src/mlpccg/set.ts

51 lines
1.2 KiB
TypeScript

import { SetFile } from "./types";
import { Database } from "./database";
import axios from "axios";
const baseURL = "https://mcg.zyg.ovh/setdata/";
export const allSets = [
"PR",
"CN",
"RR",
"CS",
"CG",
"AD",
"EO",
"HM",
"MT",
"DE",
"SB",
"FF",
"Promo"
];
export async function loadSets() {
if (Database == null) {
throw new Error("Database was not initialized, init with 'initDB()'");
}
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 => {
if (Database == null) {
throw new Error("Database was not initialized, init with 'initDB()'");
}
console.log(`Processing cards from ${set.Name}`);
return await Database.cards.bulkPut(set.Cards);
})
);
}
async function downloadSet(setid: string): Promise<SetFile> {
const setdata = await axios(`${baseURL}${setid.toLowerCase()}.json`);
setdata.data.Cards = (setdata.data as SetFile).Cards.map(c => {
c.Set = setid;
return c;
});
return setdata.data;
}