import fs from 'fs'; import path from 'path'; import { getLightconeInfoApi } from '../api'; import { LightConeDetail } from '@/types'; const DATA_DIR = path.join(process.cwd(), 'data'); const lightconeFileCache: Record> = {}; export let lightconeMap: Record = {}; export let lightconeBonusMap: Record> = {}; function getJsonFilePath(locale: string): string { return path.join(DATA_DIR, `lightcones.${locale}.json`); } function loadLightconeFromFileIfExists(locale: string): Record | null { if (lightconeFileCache[locale]) return lightconeFileCache[locale]; const filePath = getJsonFilePath(locale); const fileBonusPath = path.join(DATA_DIR, `lightcone_bonus.json`); if (fs.existsSync(fileBonusPath)) { const data = JSON.parse(fs.readFileSync(fileBonusPath, 'utf-8')) as Record>; lightconeBonusMap = data; } if (fs.existsSync(filePath)) { const data = JSON.parse(fs.readFileSync(filePath, 'utf-8')) as Record; Object.keys(data).forEach((key) => { data[key].Bonus = lightconeBonusMap[key] || {}; }); lightconeFileCache[locale] = data; return data; } return null; } export async function loadLightcones(charIds: string[], locale: string): Promise> { const fileData = loadLightconeFromFileIfExists(locale); const fileIds = fileData ? Object.keys(fileData) : []; if (fileData && charIds.every(id => fileIds.includes(id))) { lightconeMap = fileData; return lightconeMap; } const result: Record = {}; await Promise.all( charIds.map(async id => { const info = await getLightconeInfoApi(Number(id), locale); if (info) { info.Bonus = lightconeBonusMap[id] || {}; result[id] = info; } }) ); fs.mkdirSync(DATA_DIR, { recursive: true }); const filePath = getJsonFilePath(locale); fs.writeFileSync(filePath, JSON.stringify(result, null, 2), 'utf-8'); lightconeFileCache[locale] = result; lightconeMap = result; return result; }