refactor(server,hero): 新增HeroList自动同步逻辑,格式化英雄配置

1.  新增syncHeroList工具函数,实现英雄列表自动去重、排序并格式化输出
2.  在apply接口中自动同步HeroList,保持图鉴与抽卡池数据源一致
3.  格式化heroSet.ts中的英雄配置代码,统一缩进与对象格式
4.  更新HeroList注释与内容,新增5010号英雄
This commit is contained in:
pan
2026-09-07 10:15:09 +08:00
parent b1de2c13f2
commit 2d82528abf
2 changed files with 104 additions and 60 deletions

View File

@@ -370,6 +370,28 @@ app.get('/api/export-csv', (_req, res) => {
res.send(fs.readFileSync(CSV_PATH, 'utf8'))
})
/** HeroList 定义匹配(用于导出时同步图鉴/卡池数据源) */
const HERO_LIST_RE = /export const HeroList: number\[\] = \[[\s\S]*?\n\];/
/** 按导出英雄 uuid 升序重建 HeroList(每行 10 个,含去重),避免编辑器增删英雄后列表不同步 */
function syncHeroList(src, heroes) {
const uuids = [...new Set(heroes.map((h) => h.uuid))].sort((a, b) => a - b)
if (!uuids.length) return src
const lines = []
for (let i = 0; i < uuids.length; i += 10) {
lines.push(' ' + uuids.slice(i, i + 10).join(', ') + ',')
}
const block =
`export const HeroList: number[] = [\n` +
` // 编辑器导出自动同步(共 ${uuids.length} 个)\n` +
`${lines.join('\n')}\n` +
`];`
if (!HERO_LIST_RE.test(src)) {
throw new Error('heroSet.ts 中未找到 HeroList 定义,无法同步')
}
return src.replace(HERO_LIST_RE, block)
}
app.post('/api/apply', (req, res) => {
try {
const heroes = req.body?.heroes
@@ -383,7 +405,9 @@ app.post('/api/apply', (req, res) => {
return res.status(500).json({ error: 'heroSet.ts 中未找到英雄区域标记(@hero-setup:heroes-begin/end)' })
}
const body = heroes.map(renderSingleHero).join('')
const next = src.slice(0, bi) + HEROES_BEGIN + '\n' + body + HEROES_END + src.slice(ei + HEROES_END.length)
let next = src.slice(0, bi) + HEROES_BEGIN + '\n' + body + HEROES_END + src.slice(ei + HEROES_END.length)
// 同步 HeroList:图鉴展示与抽卡池(HeroCardPoolList)均由该列表驱动
next = syncHeroList(next, heroes)
fs.writeFileSync(HEROSET_PATH, next, 'utf8')
res.json({ ok: true, path: HEROSET_PATH, count: heroes.length, bytes: Buffer.byteLength(next) })
} catch (e) {