Commit ee7feabe authored by timel's avatar timel

fix: remove idb

parent e679bb9c
//! 禁限卡表
import { clear, createStore, get, setMany } from "idb-keyval";
import { useConfig } from "@/config"; import { useConfig } from "@/config";
const { lflistUrl } = useConfig(); const { lflistUrl } = useConfig();
type Forbiddens = Map<number, number>; type Forbiddens = Map<number, number>;
const IDB_NAME = "forbiddens";
// 禁限卡表的时间,比如 [2023.4] - 2023年4月表
export let forbiddenTime = "?"; export let forbiddenTime = "?";
const idb = createStore(IDB_NAME, IDB_NAME);
export async function initForbiddens(): Promise<void> { export async function initForbiddens(): Promise<void> {
const text = await (await fetch(lflistUrl)).text(); const text = await (await fetch(lflistUrl)).text();
const { time, forbiddens } = extractForbiddensFromText(text); const { time, forbiddens } = extractForbiddensFromText(text);
forbiddenTime = time; forbiddenTime = time;
setForbiddens(forbiddens);
// 先清掉之前的记录
clear(idb);
// 设置新记录
await setMany(Array.from(forbiddens));
} }
// 获取禁限信息 const forbiddensMap: Forbiddens = new Map<number, number>();
export async function getForbiddenInfo(
id: number,
): Promise<number | undefined> {
return await get(id, idb);
}
// 解析函数,提取卡片编号和限制张数 function setForbiddens(forbiddens: Forbiddens): void {
function parseCardInfo( forbiddensMap.clear();
input: string, for (const [cardId, limitCount] of forbiddens) {
): { cardId: number; limitCount: number } | null { forbiddensMap.set(cardId, limitCount);
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;
} }
// 分割文本为行,并提取每行的限制信息 export function getForbiddenInfo(id: number): number | undefined {
return forbiddensMap.get(id);
}
function extractForbiddensFromText(text: string): { function extractForbiddensFromText(text: string): {
time: string; time: string;
forbiddens: Forbiddens; forbiddens: Forbiddens;
} { } {
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;
}
const lines = text.split("\n"); const lines = text.split("\n");
const forbiddens = new Map<number, number>([]); const forbiddens = new Map<number, number>();
// remove first line lines.shift(); // remove first line
lines.shift();
let time = "?"; let time = "?";
......
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