67 lines
1.5 KiB
TypeScript
67 lines
1.5 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 const setNames = {
|
|
PR: "Premiere",
|
|
CN: "Canterlot Nights",
|
|
RR: "Rock and Rave",
|
|
CS: "Celestial Solstice",
|
|
CG: "Crystal Games",
|
|
AD: "Absolute Discord",
|
|
EO: "Equestrian Odysseys",
|
|
HM: "High Magic",
|
|
MT: "Marks In Time",
|
|
DE: "Defenders of Equestria",
|
|
SB: "Seaquestria and Beyond",
|
|
FF: "Friends Forever",
|
|
GF: "Promotional",
|
|
ST: "Sands of Time"
|
|
};
|
|
|
|
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()'");
|
|
}
|
|
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;
|
|
}
|