Fix decks being unselectable immediately after import

This commit is contained in:
Andrio Celos 2023-12-11 10:20:57 +11:00
parent 29ae032914
commit 08a539b67d
2 changed files with 8 additions and 5 deletions

View File

@ -13,6 +13,10 @@ class SavedDeck {
this.isReadOnly = isReadOnly;
}
static fromJson(obj: any) {
return new SavedDeck(obj.name, obj.sleeves ?? 0, obj.cards, obj.upgrades ?? new Array(15).fill(1), false);
}
get isValid() {
if (!cardDatabase.cards) throw new Error('Card database must be loaded to validate decks.');
if (this.cards.length != 15) return false;

View File

@ -103,7 +103,7 @@ function saveDecks() {
const decksString = localStorage.getItem('decks');
if (decksString) {
for (const deck of JSON.parse(decksString)) {
decks.push(new SavedDeck(deck.name, deck.sleeves ?? 0, deck.cards, deck.upgrades ?? new Array(15).fill(1), false));
decks.push(SavedDeck.fromJson(deck));
}
} else {
const lastDeckString = localStorage.getItem('lastDeck');
@ -271,7 +271,7 @@ deckImportForm.addEventListener('submit', e => {
}
});
function parseDecksForImport(s: string) {
function parseDecksForImport(s: string) : (SavedDeck | number[])[] {
let isKoishiShareUrl = false;
const pos = s.indexOf('deck=');
if (pos >= 0) {
@ -292,15 +292,14 @@ function parseDecksForImport(s: string) {
if (typeof(deck) != 'object' || !Array.isArray(deck.cards) || deck.cards.length > 15 || (deck.cards as any[]).find((i => typeof(i) != 'number' || !cardDatabase.isValidCardNumber(i))))
throw new SyntaxError('Invalid JSON deck');
}
return data; // Our export data
return data.map(SavedDeck.fromJson); // Our export data
}
} else if (typeof(data) == 'object') {
if (!Array.isArray(data.cards) || data.cards.length > 15 || (data.cards as any[]).find((i => typeof(i) != 'number' || !cardDatabase.isValidCardNumber(i))))
throw new SyntaxError('Invalid JSON deck');
return [ data ]; // Our old export data
return [ SavedDeck.fromJson(data) ]; // Our old export data
} else
throw new SyntaxError('Invalid JSON deck');
// TODO: add support for tblturf.ink
}
deckViewMenuButton.addEventListener('click', () => {