aboutsummaryrefslogtreecommitdiff
path: root/src/control
diff options
context:
space:
mode:
Diffstat (limited to 'src/control')
-rw-r--r--src/control/deck.js9
-rw-r--r--src/control/left_panel.js2
-rw-r--r--src/control/loading.js29
-rw-r--r--src/control/search.js8
4 files changed, 42 insertions, 6 deletions
diff --git a/src/control/deck.js b/src/control/deck.js
index ba6a1ff..564443a 100644
--- a/src/control/deck.js
+++ b/src/control/deck.js
@@ -1,11 +1,12 @@
import { writable } from "svelte/store";
import { parseYdke } from '../utils';
-import { cardDb } from '../data/cardDb';
+import { getCardDb } from '../data/cardDb';
let deck = writable({main: [], extra: [], side: []});
let deckState = {main: [], extra: [], side: []};
function sanitizeDeck(deck) {
+ let cardDb = getCardDb();
let ret = [];
for (let id of deck) {
if (cardDb[id] !== undefined) {
@@ -53,6 +54,7 @@ let deckOps = {
}
},
"move": (from, to, fromIdx, toIdx) => {
+ let cardDb = getCardDb();
let id = deckState[from][fromIdx];
if (cardDb[id].isExtra && to === 'main') return;
if (!cardDb[id].isExtra && to === 'extra') return;
@@ -62,6 +64,7 @@ let deckOps = {
setDeck(deckState);
},
"add2extra": (id, targetIdx) => {
+ let cardDb = getCardDb();
if (!cardDb[id].isExtra) return;
let d = deckState;
if (canAdd(d, id)) {
@@ -74,6 +77,7 @@ let deckOps = {
}
},
"add2main": (id, targetIdx) => {
+ let cardDb = getCardDb();
console.log(targetIdx);
if (cardDb[id].isExtra) return;
let d = deckState;
@@ -117,11 +121,10 @@ function initDeck() {
}
}
-initDeck();
-
export {
deck,
setDeck,
deckOps,
+ initDeck,
};
diff --git a/src/control/left_panel.js b/src/control/left_panel.js
index 898d8af..87d8a4a 100644
--- a/src/control/left_panel.js
+++ b/src/control/left_panel.js
@@ -9,7 +9,7 @@ let descCache = new Map();
function setLeftPanelCard(id) {
leftPanelCardId.set(id);
curVersion += 1;
- leftPanelCardDesc.set('...');
+ leftPanelCardDesc.set('加载中...');
let ver = curVersion;
setDesc(ver, id);
}
diff --git a/src/control/loading.js b/src/control/loading.js
new file mode 100644
index 0000000..93114d7
--- /dev/null
+++ b/src/control/loading.js
@@ -0,0 +1,29 @@
+import { writable } from 'svelte/store';
+import { initSearch } from './search';
+import { initDeck } from './deck';
+import { setCardDb } from '../data/cardDb';
+
+let isLoading = writable(true);
+
+async function fetchCardDb() {
+ try {
+ const response = await fetch("https://121.40.137.206/ygo-deck-builder/card_db.json");
+ if (!response.ok) {
+ throw new Error('Network response was not ok');
+ }
+ const data = await response.json();
+ setCardDb(data);
+ } catch (error) {
+ alert("加载失败!请刷新重试");
+ return;
+ }
+ isLoading.set(false);
+ initDeck();
+ initSearch();
+}
+
+fetchCardDb();
+
+export {
+ isLoading,
+};
diff --git a/src/control/search.js b/src/control/search.js
index 4ebe9bf..5927950 100644
--- a/src/control/search.js
+++ b/src/control/search.js
@@ -1,5 +1,5 @@
import { writable } from 'svelte/store';
-import { cardDb } from '../data/cardDb';
+import { getCardDb} from '../data/cardDb';
let showingCards = writable([]);
let resultCards = [];
@@ -18,6 +18,7 @@ function changeInput(query) {
}
function doSearch(ver, query) {
+ let cardDb = getCardDb();
let result = [];
for (let key in cardDb) {
if (ver !== curVer) {
@@ -50,7 +51,9 @@ function doSearch(ver, query) {
showCards();
}
-doSearch(curVer, "");
+function initSearch() {
+ doSearch(curVer, "");
+}
function onPrevPage() {
if (curPage > 0) {
@@ -72,4 +75,5 @@ export {
onPrevPage,
onNextPage,
showingCards,
+ initSearch,
};