miao-plugin/models/character/CharId.js

76 lines
1.7 KiB
JavaScript
Raw Normal View History

/**
* 角色别名及角色ID相关
* */
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
async function init () {
2023-10-20 08:59:06 +00:00
let { diyCfg } = await Data.importCfg('character')
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(',')
})
meta.addData(customChars)
meta.addAlias(customAlias)
}
await init()
const CharId = {
2023-10-20 08:59:06 +00:00
getId (ds = '', game = '', elem = '') {
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
}
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 }
}
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)
}
}
}
2023-10-20 08:59:06 +00:00
let match = Meta.matchGame(game, 'char', ds)
if (match) {
return ret(match.data, match.game)
}
// 无匹配结果
return false
},
2023-10-20 08:59:06 +00:00
isTraveler (id) {
if (id) {
return [10000007, 10000005, 20000000].includes(id * 1)
}
return false
}
2023-10-20 08:59:06 +00:00
}
export default CharId