Commit 72a57075 authored by Chunchi Che's avatar Chunchi Che

move forbiddens from store to api

parent 3845eb27
Pipeline #23048 passed with stages
in 14 minutes and 25 seconds
//! 禁限卡表
import { clear, createStore, get, set } from "idb-keyval";
import { useConfig } from "@/config";
const { lflistUrl } = useConfig();
type Forbiddens = Map<number, number>;
const IDB_NAME = "forbiddens";
// 禁限卡表的时间,比如 [2023.4] - 2023年4月表
export let forbiddenTime = "?";
const idb = createStore(IDB_NAME, IDB_NAME);
export async function initForbiddens(): Promise<void> {
const text = await (await fetch(lflistUrl)).text();
const { time, forbiddens } = extractForbiddensFromText(text);
forbiddenTime = time;
// 先清掉之前的记录
clear(idb);
// 设置新记录
await Promise.all(
Array.from(forbiddens).map(([key, value]) => set(key, value, idb))
);
}
// 获取禁限信息
export async function getForbiddenInfo(
id: number
): Promise<number | undefined> {
return await get(id, idb);
}
// 解析函数,提取卡片编号和限制张数
function parseCardInfo(
input: string
): { cardId: number; limitCount: number } | null {
const match = input.match(/^(\d+)\s+(\d+)\s+--/);
if (match) {
const cardId = parseInt(match[1]);
const limitCount = parseInt(match[2]);
return { cardId, limitCount };
}
return null;
}
// 分割文本为行,并提取每行的限制信息
function extractForbiddensFromText(text: string): {
time: string;
forbiddens: Forbiddens;
} {
const lines = text.split("\n");
const forbiddens = new Map<number, number>([]);
// remove first line
lines.shift();
let time = "?";
for (const line of lines) {
if (line.startsWith("#")) {
// do nothing
} else if (line.startsWith("!")) {
if (time !== "?") {
// 已经读取完第一个禁限表的信息了,退出循环
break;
} else {
time = line.substring(1).trim();
}
} else {
const cardInfo = parseCardInfo(line);
if (cardInfo) {
forbiddens.set(cardInfo.cardId, cardInfo.limitCount);
}
}
}
return { time, forbiddens };
}
export * from "./cards"; export * from "./cards";
export * from "./cookies"; export * from "./cookies";
export * from "./forbiddens";
export * from "./mycard"; export * from "./mycard";
export * from "./ocgcore/idl/ocgcore"; export * from "./ocgcore/idl/ocgcore";
export * from "./ocgcore/ocgHelper"; export * from "./ocgcore/ocgHelper";
......
//! 禁卡表Store //! 禁限卡表
import { proxy } from "valtio"; import { clear, createStore, get, set } from "idb-keyval";
import { useConfig } from "@/config"; import { useConfig } from "@/config";
...@@ -8,20 +8,31 @@ const { lflistUrl } = useConfig(); ...@@ -8,20 +8,31 @@ const { lflistUrl } = useConfig();
type Forbiddens = Map<number, number>; type Forbiddens = Map<number, number>;
class ForbiddenStore { const IDB_NAME = "forbiddens";
time: string = "?";
inner: Forbiddens = new Map([]);
async init() {
const text = await (await fetch(lflistUrl)).text();
const { time, forbiddens } = extractForbiddensFromText(text);
this.time = time;
this.inner = forbiddens;
}
// 获取禁限信息 // 禁限卡表的时间,比如 [2023.4] - 2023年4月表
get(id: number) { export let forbiddenTime = "?";
return this.inner.get(id);
} const idb = createStore(IDB_NAME, IDB_NAME);
export async function init(): Promise<void> {
const text = await (await fetch(lflistUrl)).text();
const { time, forbiddens } = extractForbiddensFromText(text);
forbiddenTime = time;
// 先清掉之前的记录
clear(idb);
// 设置新记录
await Promise.all(
Array.from(forbiddens).map(([key, value]) => set(key, value, idb))
);
}
// 获取禁限信息
export async function getForbiddenInfo(
id: number
): Promise<number | undefined> {
return await get(id, idb);
} }
// 解析函数,提取卡片编号和限制张数 // 解析函数,提取卡片编号和限制张数
...@@ -70,5 +81,3 @@ function extractForbiddensFromText(text: string): { ...@@ -70,5 +81,3 @@ function extractForbiddensFromText(text: string): {
return { time, forbiddens }; return { time, forbiddens };
} }
export const forbiddenStore = proxy(new ForbiddenStore());
...@@ -2,7 +2,6 @@ export * from "./accountStore"; ...@@ -2,7 +2,6 @@ export * from "./accountStore";
export * from "./cardStore"; export * from "./cardStore";
export * from "./chatStore"; export * from "./chatStore";
export * from "./deckStore"; export * from "./deckStore";
export * from "./forbiddenStore";
export * from "./initStore"; export * from "./initStore";
export * from "./matStore"; export * from "./matStore";
export * from "./placeStore"; export * from "./placeStore";
...@@ -17,7 +16,6 @@ import { accountStore } from "./accountStore"; ...@@ -17,7 +16,6 @@ import { accountStore } from "./accountStore";
import { cardStore } from "./cardStore"; import { cardStore } from "./cardStore";
import { chatStore } from "./chatStore"; import { chatStore } from "./chatStore";
import { deckStore } from "./deckStore"; import { deckStore } from "./deckStore";
import { forbiddenStore } from "./forbiddenStore";
import { initStore } from "./initStore"; import { initStore } from "./initStore";
import { matStore } from "./matStore"; import { matStore } from "./matStore";
import { placeStore } from "./placeStore"; import { placeStore } from "./placeStore";
...@@ -35,7 +33,6 @@ devtools(accountStore, { name: "account", enabled: DEV }); ...@@ -35,7 +33,6 @@ devtools(accountStore, { name: "account", enabled: DEV });
devtools(roomStore, { name: "room", enabled: DEV }); devtools(roomStore, { name: "room", enabled: DEV });
devtools(deckStore, { name: "deck", enabled: DEV }); devtools(deckStore, { name: "deck", enabled: DEV });
devtools(initStore, { name: "init", enabled: DEV }); devtools(initStore, { name: "init", enabled: DEV });
devtools(forbiddenStore, { name: "forbidden", enabled: DEV });
// 重置`Store` // 重置`Store`
export const resetUniverse = () => { export const resetUniverse = () => {
......
...@@ -26,10 +26,10 @@ import { LoaderFunction } from "react-router-dom"; ...@@ -26,10 +26,10 @@ import { LoaderFunction } from "react-router-dom";
import { useSnapshot } from "valtio"; import { useSnapshot } from "valtio";
import { subscribeKey } from "valtio/utils"; import { subscribeKey } from "valtio/utils";
import { type CardMeta, searchCards } from "@/api"; import { type CardMeta, initForbiddens, searchCards } from "@/api";
import { isExtraDeckCard, isToken } from "@/common"; import { isExtraDeckCard, isToken } from "@/common";
import { FtsConditions } from "@/middleware/sqlite/fts"; import { FtsConditions } from "@/middleware/sqlite/fts";
import { deckStore, forbiddenStore, type IDeck, initStore } from "@/stores"; import { deckStore, type IDeck, initStore } from "@/stores";
import { import {
Background, Background,
IconFont, IconFont,
...@@ -40,9 +40,9 @@ import { ...@@ -40,9 +40,9 @@ import {
import { CardDetail } from "./CardDetail"; import { CardDetail } from "./CardDetail";
import { DeckSelect } from "./DeckSelect"; import { DeckSelect } from "./DeckSelect";
import { editDeckStore } from "./store";
import { Filter } from "./Filter"; import { Filter } from "./Filter";
import styles from "./index.module.scss"; import styles from "./index.module.scss";
import { editDeckStore } from "./store";
import { editingDeckToIDeck, iDeckToEditingDeck, type Type } from "./utils"; import { editingDeckToIDeck, iDeckToEditingDeck, type Type } from "./utils";
export const loader: LoaderFunction = async () => { export const loader: LoaderFunction = async () => {
...@@ -53,8 +53,8 @@ export const loader: LoaderFunction = async () => { ...@@ -53,8 +53,8 @@ export const loader: LoaderFunction = async () => {
}); });
} }
// 加载禁限卡表 // 更新禁限卡表
await forbiddenStore.init(); await initForbiddens();
return null; return null;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment