2023-10-20 08:59:06 +00:00
|
|
|
|
import { Data } from '#miao'
|
2023-10-19 20:18:30 +00:00
|
|
|
|
import lodash from 'lodash'
|
|
|
|
|
|
2023-10-20 08:59:06 +00:00
|
|
|
|
const MetaStore = {}
|
2023-10-19 20:18:30 +00:00
|
|
|
|
|
2023-10-20 08:59:06 +00:00
|
|
|
|
class MetaData {
|
|
|
|
|
constructor (game = 'gs', type = '') {
|
|
|
|
|
this.game = game
|
|
|
|
|
this.type = type
|
|
|
|
|
this.data = {}
|
|
|
|
|
this.alias = {}
|
|
|
|
|
this.abbr = {}
|
|
|
|
|
this.cfg = {}
|
|
|
|
|
}
|
2023-10-19 20:18:30 +00:00
|
|
|
|
|
|
|
|
|
// 添加数据
|
2023-10-20 08:59:06 +00:00
|
|
|
|
addData (datas, pk = 'id') {
|
|
|
|
|
let { data, alias } = this
|
|
|
|
|
lodash.forEach(datas, (ds, id) => {
|
2023-10-19 20:18:30 +00:00
|
|
|
|
id = ds[pk] || id
|
2023-10-20 08:59:06 +00:00
|
|
|
|
data[id] = ds
|
|
|
|
|
if (ds.name && ds.name !== id) {
|
2023-10-19 20:18:30 +00:00
|
|
|
|
alias[ds.name] = id
|
|
|
|
|
}
|
|
|
|
|
})
|
2023-10-20 08:59:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addDataItem (id, ds) {
|
|
|
|
|
let { data, alias } = this
|
|
|
|
|
data[id] = ds
|
|
|
|
|
alias[id] = id
|
|
|
|
|
if (ds.name) {
|
|
|
|
|
alias[ds.name] = id
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-19 20:18:30 +00:00
|
|
|
|
|
|
|
|
|
// 添加简写
|
2023-10-20 08:59:06 +00:00
|
|
|
|
addAbbr (ds) {
|
|
|
|
|
let { abbr, alias } = this
|
|
|
|
|
lodash.forEach(ds, (txt, id) => {
|
|
|
|
|
abbr[id] = lodash.trim(txt + '')
|
2023-10-19 20:18:30 +00:00
|
|
|
|
alias[abbr] = alias[id] || id
|
|
|
|
|
})
|
2023-10-20 08:59:06 +00:00
|
|
|
|
}
|
2023-10-19 20:18:30 +00:00
|
|
|
|
|
|
|
|
|
// 添加别名
|
2023-10-20 08:59:06 +00:00
|
|
|
|
addAlias (ds) {
|
|
|
|
|
let { alias } = this
|
|
|
|
|
lodash.forEach(ds, (txt, id) => {
|
2023-10-19 20:18:30 +00:00
|
|
|
|
lodash.forEach(txt.split(','), (t) => {
|
2023-10-20 08:59:06 +00:00
|
|
|
|
alias[lodash.trim(t + '').toLowerCase()] = alias[id] || id
|
2023-10-19 20:18:30 +00:00
|
|
|
|
})
|
|
|
|
|
})
|
2023-10-20 08:59:06 +00:00
|
|
|
|
}
|
2023-10-19 20:18:30 +00:00
|
|
|
|
|
|
|
|
|
// 注册别名Fn
|
2023-10-20 08:59:06 +00:00
|
|
|
|
addAliasFn (fn) {
|
2023-10-19 20:18:30 +00:00
|
|
|
|
if (fn) {
|
2023-10-20 08:59:06 +00:00
|
|
|
|
this.aliasFn = fn
|
2023-10-19 20:18:30 +00:00
|
|
|
|
}
|
2023-10-20 08:59:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addCfg (cfgMap) {
|
|
|
|
|
let { cfg } = this
|
|
|
|
|
lodash.forEach(cfgMap, (v, k) => {
|
|
|
|
|
cfg[k] = v
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-10-19 20:18:30 +00:00
|
|
|
|
|
2023-10-20 08:59:06 +00:00
|
|
|
|
getId (txt) {
|
|
|
|
|
txt = lodash.trim(txt + '').toLowerCase()
|
|
|
|
|
let { data, alias, aliasFn } = this
|
|
|
|
|
if (data[txt]) {
|
|
|
|
|
return txt
|
|
|
|
|
}
|
2023-10-19 20:18:30 +00:00
|
|
|
|
if (alias[txt]) {
|
|
|
|
|
return alias[txt]
|
|
|
|
|
}
|
|
|
|
|
if (aliasFn) {
|
|
|
|
|
let id = aliasFn(txt)
|
|
|
|
|
if (alias[id]) {
|
|
|
|
|
return alias
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
2023-10-20 08:59:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getData (txt) {
|
|
|
|
|
let id = this.getId(txt)
|
|
|
|
|
let { data } = this
|
|
|
|
|
return data[id] || null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getCfg (key = '') {
|
|
|
|
|
if (!key) {
|
|
|
|
|
return this.cfg
|
|
|
|
|
}
|
|
|
|
|
return this.cfg[key]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getIds () {
|
|
|
|
|
return lodash.keys(this.data)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const MetaFn = (fnKey) => {
|
|
|
|
|
return (game, type, args = '') => {
|
|
|
|
|
let meta = Meta.getMeta(game, type)
|
|
|
|
|
return meta[fnKey](args)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Meta = {
|
|
|
|
|
addAliasFn (game, type, fn) {
|
|
|
|
|
let meta = Meta.getMeta(game, type)
|
|
|
|
|
meta.addAliasFn(fn)
|
2023-10-19 20:18:30 +00:00
|
|
|
|
},
|
|
|
|
|
|
2023-10-20 08:59:06 +00:00
|
|
|
|
// 获取存储
|
|
|
|
|
getMeta (game, type) {
|
|
|
|
|
let key = `${game}.${type}`
|
|
|
|
|
if (!MetaStore[key]) {
|
|
|
|
|
MetaStore[key] = new MetaData(game, type)
|
|
|
|
|
}
|
|
|
|
|
return MetaStore[key]
|
|
|
|
|
},
|
|
|
|
|
getId: MetaFn('getId'),
|
|
|
|
|
getIds: MetaFn('getIds'),
|
|
|
|
|
getData: MetaFn('getData'),
|
|
|
|
|
getCfg: MetaFn('getCfg'),
|
2023-10-19 20:18:30 +00:00
|
|
|
|
// 在各个游戏内匹配,以传入的game优先
|
2023-10-20 08:59:06 +00:00
|
|
|
|
matchGame (game = 'gs', type, txt) {
|
|
|
|
|
txt = lodash.trim(txt + '').toLowerCase()
|
2023-10-19 20:18:30 +00:00
|
|
|
|
let games = game === 'gs' ? ['gs', 'sr'] : ['sr', 'gs']
|
|
|
|
|
for (let currGame of games) {
|
2023-10-20 08:59:06 +00:00
|
|
|
|
let id = Meta.getId(currGame, type, txt)
|
2023-10-19 20:18:30 +00:00
|
|
|
|
if (id) {
|
2023-10-20 08:59:06 +00:00
|
|
|
|
let data = Meta.getData(currGame, type, id)
|
|
|
|
|
return { game, id, data }
|
2023-10-19 20:18:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export default Meta
|