Browse Source

optimize card db loading

Mistivia 1 month ago
parent
commit
ec6b42fd00
5 changed files with 55 additions and 5 deletions
  1. 1 1
      .gitignore
  2. 22 0
      data/split.py
  3. 5 1
      src/control/deck.js
  4. 16 3
      src/control/loading.js
  5. 11 0
      src/data/cardDb.js

+ 1 - 1
.gitignore

@@ -24,5 +24,5 @@ dist-ssr
 *.sw?
 
 data/cards.json
-data/card_db.json
+data/card_db*
 deploy.sh

+ 22 - 0
data/split.py

@@ -0,0 +1,22 @@
+import json
+
+def split_file(input_filename):
+    chunk_size = 102400  # 100kB
+    part_number = 1
+    parts = []
+    with open(input_filename, 'rb') as input_file:
+        while True:
+            data = input_file.read(chunk_size)
+            if not data:
+                break
+            output_filename = f'card_db_parts/part{part_number}'
+            with open(output_filename, 'wb') as output_file:
+                output_file.write(data)
+            parts.append(f'part{part_number}')
+            part_number += 1
+        with open("card_db_parts/index.json", 'w') as fp:
+            fp.write(json.dumps(parts))
+    print(f"file splited to {part_number-1} parts")
+
+# 示例用法
+split_file('card_db.json')

+ 5 - 1
src/control/deck.js

@@ -1,14 +1,18 @@
 import { writable } from "svelte/store";
 import { parseYdke } from '../utils';
-import { getCardDb } from '../data/cardDb';
+import { getCardDb, getAltId } from '../data/cardDb';
 
 let deck = writable({main: [], extra: [], side: []});
 let deckState = {main: [], extra: [], side: []};
 
 function sanitizeDeck(deck) {
     let cardDb = getCardDb();
+    let altId = getAltId();
     let ret = [];
     for (let id of deck) {
+        if (altId[id] !== undefined) {
+            id = altId[id];
+        }
         if (cardDb[id] !== undefined) {
             ret.push(id);
         }

+ 16 - 3
src/control/loading.js

@@ -1,19 +1,32 @@
 import { writable } from 'svelte/store';
 import { initSearch } from './search';
 import { initDeck } from './deck';
-import { setCardDb } from '../data/cardDb';
+import { setCardDb, setAltId } 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");
+        // load card db
+        let response = await fetch("https://raye.mistivia.com/card_db_parts/index.json");
         if (!response.ok) {
             throw new Error('Network response was not ok');
         }
-        const data = await response.json();
+        let data = await response.json();
+        let tasks = data.map((i)=>fetch('https://raye.mistivia.com/card_db_parts/' + i));
+        let datas = await Promise.all(tasks);
+        datas = await Promise.all(datas.map((x) => x.text()));
+        data = JSON.parse(datas.join(''));
         setCardDb(data);
+        // load alt id
+        response = await fetch("https://ygocdb.com/api/v0/idChangelog.jsonp");
+        if (!response.ok) {
+            throw new Error('Network response was not ok');
+        }
+        data = await response.json();
+        setAltId(data);
     } catch (error) {
+        console.log(error);
         alert("加载失败!请刷新重试");
         return;
     }

+ 11 - 0
src/data/cardDb.js

@@ -1,4 +1,5 @@
 let cardDb = {};
+let altId = {};
 
 function setCardDb(d) {
     cardDb = d;
@@ -8,7 +9,17 @@ function getCardDb() {
     return cardDb;
 }
 
+function getAltId() {
+    return altId;
+}
+
+function setAltId(x) {
+    altId = x;
+}
+
 export {
     getCardDb,
     setCardDb,
+    getAltId,
+    setAltId,
 };