From ec6b42fd005a5416371a1cace0c35366aa9f1d58 Mon Sep 17 00:00:00 2001
From: Mistivia <i@mistivia.com>
Date: Tue, 11 Feb 2025 22:14:02 +0800
Subject: optimize card db loading

---
 .gitignore             |  2 +-
 data/split.py          | 22 ++++++++++++++++++++++
 src/control/deck.js    |  6 +++++-
 src/control/loading.js | 19 ++++++++++++++++---
 src/data/cardDb.js     | 11 +++++++++++
 5 files changed, 55 insertions(+), 5 deletions(-)
 create mode 100644 data/split.py

diff --git a/.gitignore b/.gitignore
index d0e783c..f29ae9c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -24,5 +24,5 @@ dist-ssr
 *.sw?
 
 data/cards.json
-data/card_db.json
+data/card_db*
 deploy.sh
diff --git a/data/split.py b/data/split.py
new file mode 100644
index 0000000..1f93fca
--- /dev/null
+++ b/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')
diff --git a/src/control/deck.js b/src/control/deck.js
index 564443a..43c784f 100644
--- a/src/control/deck.js
+++ b/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);
         }
diff --git a/src/control/loading.js b/src/control/loading.js
index 93114d7..70dd99c 100644
--- a/src/control/loading.js
+++ b/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;
     }
diff --git a/src/data/cardDb.js b/src/data/cardDb.js
index f3f5635..b9b991f 100644
--- a/src/data/cardDb.js
+++ b/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,
 };
-- 
cgit v1.0

