mirror of
https://github.com/AndrioCelos/TableturfBattleApp.git
synced 2026-07-19 16:51:27 -05:00
32 lines
962 B
TypeScript
32 lines
962 B
TypeScript
const cardDatabase = {
|
|
cards: null as Card[] | null,
|
|
loadAsync() {
|
|
return new Promise<Card[]>((resolve, reject) => {
|
|
if (cardDatabase.cards != null) {
|
|
resolve(cardDatabase.cards);
|
|
return;
|
|
}
|
|
const cardListRequest = new XMLHttpRequest();
|
|
cardListRequest.open('GET', `${config.apiBaseUrl}/cards`);
|
|
cardListRequest.addEventListener('load', e => {
|
|
const cards = [ ];
|
|
if (cardListRequest.status == 200) {
|
|
const s = cardListRequest.responseText;
|
|
const response = JSON.parse(s) as object[];
|
|
for (const o of response) {
|
|
cards.push(Card.fromJson(o));
|
|
}
|
|
cardDatabase.cards = cards;
|
|
resolve(cards);
|
|
} else {
|
|
reject(new Error(`Error downloading card database: response was ${cardListRequest.status}`));
|
|
}
|
|
});
|
|
cardListRequest.addEventListener('error', e => {
|
|
reject(new Error('Error downloading card database: no further information.'))
|
|
});
|
|
cardListRequest.send();
|
|
});
|
|
}
|
|
}
|