2022-11-23 20:26:07 +00:00
|
|
|
/**
|
|
|
|
* 角色别名及角色ID相关
|
|
|
|
* */
|
2022-09-03 21:08:57 +00:00
|
|
|
import lodash from 'lodash'
|
2023-10-20 08:59:06 +00:00
|
|
|
import { Data, Format, Meta } from '#miao'
|
2023-05-14 20:19:33 +00:00
|
|
|
|
2022-09-03 21:08:57 +00:00
|
|
|
async function init () {
|
2023-10-20 08:59:06 +00:00
|
|
|
let { diyCfg } = await Data.importCfg('character')
|
2023-10-21 18:54:45 +00:00
|
|
|
let meta = Meta.create('gs', 'char')
|
|
|
|
let customChars = {}
|
|
|
|
let customAlias = {}
|
|
|
|
lodash.forEach(diyCfg.customCharacters, (alias, id) => {
|
|
|
|
let charId = meta.getId(id)
|
|
|
|
if (!charId) {
|
|
|
|
customChars[id] = {
|
|
|
|
id,
|
|
|
|
name: alias[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
customAlias[id] = alias.join(',')
|
2022-09-03 21:08:57 +00:00
|
|
|
})
|
2023-10-21 18:54:45 +00:00
|
|
|
meta.addData(customChars)
|
|
|
|
meta.addAlias(customAlias)
|
2022-09-03 21:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await init()
|
|
|
|
|
|
|
|
const CharId = {
|
2023-10-20 08:59:06 +00:00
|
|
|
|
|
|
|
getId (ds = '', game = '', elem = '') {
|
2022-09-03 21:08:57 +00:00
|
|
|
if (!ds) {
|
|
|
|
return false
|
|
|
|
}
|
2023-10-20 08:59:06 +00:00
|
|
|
if (lodash.isObject(ds)) {
|
|
|
|
let em = Format.elem(ds.elem || ds.element)
|
|
|
|
for (let key of ['id', 'name']) {
|
|
|
|
if (!ds[key]) continue
|
|
|
|
let ret = CharId.getId(ds[key], game, em || '')
|
|
|
|
if (ret) return ret
|
2023-05-14 20:19:33 +00:00
|
|
|
}
|
2023-10-20 08:59:06 +00:00
|
|
|
return false
|
2022-09-03 21:08:57 +00:00
|
|
|
}
|
2023-10-20 08:59:06 +00:00
|
|
|
|
|
|
|
const ret = (data, game = 'gs', em = '') => {
|
|
|
|
let { id, name } = data
|
|
|
|
return { id, data, name, game, elem: em || elem }
|
|
|
|
}
|
|
|
|
|
2023-10-23 02:56:47 +00:00
|
|
|
let match = Meta.matchGame(game, 'char', ds)
|
|
|
|
if (match) {
|
|
|
|
return ret(match.data, match.game)
|
|
|
|
}
|
2023-10-20 08:59:06 +00:00
|
|
|
if (game !== 'sr') {
|
|
|
|
// 尝试使用元素起始匹配
|
|
|
|
let em = Format.matchElem(ds, '', true)
|
|
|
|
if (em) {
|
|
|
|
let match = Meta.getData('gs', 'char', em.name)
|
|
|
|
if (match && CharId.isTraveler(match.id)) {
|
|
|
|
return ret(match.data, 'gs', em.elem)
|
2022-10-02 20:02:18 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-03 21:08:57 +00:00
|
|
|
}
|
|
|
|
// 无匹配结果
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
|
2023-11-27 20:18:06 +00:00
|
|
|
getRandomId (game = 'gs') {
|
|
|
|
let meta = Meta.create(game, 'char')
|
|
|
|
let ids = meta.getIds()
|
|
|
|
if (game === 'gs') {
|
|
|
|
ids = lodash.filter(ids, (id) => /^\d+$/.test(id))
|
|
|
|
}
|
|
|
|
return lodash.sample(ids)
|
|
|
|
},
|
|
|
|
|
2023-10-20 08:59:06 +00:00
|
|
|
isTraveler (id) {
|
2022-09-03 21:08:57 +00:00
|
|
|
if (id) {
|
|
|
|
return [10000007, 10000005, 20000000].includes(id * 1)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2023-10-20 08:59:06 +00:00
|
|
|
|
2022-09-03 21:08:57 +00:00
|
|
|
}
|
|
|
|
export default CharId
|