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 = {}
|
2023-10-21 18:54:45 +00:00
|
|
|
|
this.alias2 = {}
|
2023-10-20 08:59:06 +00:00
|
|
|
|
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) {
|
2023-10-21 18:54:45 +00:00
|
|
|
|
let { data, alias, alias2 } = this
|
2023-10-20 08:59:06 +00:00
|
|
|
|
lodash.forEach(ds, (txt, id) => {
|
2023-10-21 18:54:45 +00:00
|
|
|
|
id = alias[id] || alias2[id] || id
|
|
|
|
|
if (data[id]) {
|
|
|
|
|
data[id].abbr = txt
|
|
|
|
|
}
|
2023-10-19 20:18:30 +00:00
|
|
|
|
})
|
2023-10-20 08:59:06 +00:00
|
|
|
|
}
|
2023-10-19 20:18:30 +00:00
|
|
|
|
|
|
|
|
|
// 添加别名
|
2023-10-21 18:54:45 +00:00
|
|
|
|
addAlias (ds, isPrivate = false) {
|
|
|
|
|
let { alias, alias2 } = this
|
2023-10-20 08:59:06 +00:00
|
|
|
|
lodash.forEach(ds, (txt, id) => {
|
2023-10-21 18:54:45 +00:00
|
|
|
|
lodash.forEach((txt + '').split(','), (t) => {
|
|
|
|
|
(isPrivate ? alias2 : alias)[lodash.trim(t + '').toLowerCase()] = alias[id] || alias2[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
|
|
|
|
}
|
|
|
|
|
|
2023-10-21 18:54:45 +00:00
|
|
|
|
addMeta (cfgMap) {
|
2023-10-20 08:59:06 +00:00
|
|
|
|
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()
|
2023-10-21 18:54:45 +00:00
|
|
|
|
let { data, alias, alias2, aliasFn } = this
|
2023-10-20 08:59:06 +00:00
|
|
|
|
if (data[txt]) {
|
|
|
|
|
return txt
|
|
|
|
|
}
|
2023-10-21 18:54:45 +00:00
|
|
|
|
if (alias[txt] || alias2[txt]) {
|
|
|
|
|
return alias[txt] || alias2[txt]
|
2023-10-19 20:18:30 +00:00
|
|
|
|
}
|
|
|
|
|
if (aliasFn) {
|
|
|
|
|
let id = aliasFn(txt)
|
2023-10-21 18:54:45 +00:00
|
|
|
|
if (alias[id] || alias2[id]) {
|
|
|
|
|
return alias[id] || alias2[id]
|
2023-10-19 20:18:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
2023-10-20 08:59:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getData (txt) {
|
|
|
|
|
let id = this.getId(txt)
|
|
|
|
|
let { data } = this
|
|
|
|
|
return data[id] || null
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-21 18:54:45 +00:00
|
|
|
|
getMeta (key = '') {
|
2023-10-20 08:59:06 +00:00
|
|
|
|
if (!key) {
|
|
|
|
|
return this.cfg
|
|
|
|
|
}
|
|
|
|
|
return this.cfg[key]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getIds () {
|
|
|
|
|
return lodash.keys(this.data)
|
|
|
|
|
}
|
2023-10-21 18:54:45 +00:00
|
|
|
|
|
|
|
|
|
getAlias () {
|
|
|
|
|
return lodash.keys(this.alias)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async forEach (fn) {
|
|
|
|
|
for (let id in this.data) {
|
|
|
|
|
let ds = this.data[id]
|
|
|
|
|
let ret = fn(ds, id)
|
|
|
|
|
ret = Data.isPromise(ret) ? await ret : ret
|
|
|
|
|
if (ret === false) {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-20 08:59:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const MetaFn = (fnKey) => {
|
|
|
|
|
return (game, type, args = '') => {
|
2023-11-07 19:07:56 +00:00
|
|
|
|
if (!game) {
|
|
|
|
|
game = 'gs'
|
|
|
|
|
}
|
2023-10-21 18:54:45 +00:00
|
|
|
|
let meta = Meta.create(game, type)
|
2023-10-20 08:59:06 +00:00
|
|
|
|
return meta[fnKey](args)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Meta = {
|
|
|
|
|
addAliasFn (game, type, fn) {
|
2023-10-21 18:54:45 +00:00
|
|
|
|
let meta = Meta.create(game, type)
|
2023-10-20 08:59:06 +00:00
|
|
|
|
meta.addAliasFn(fn)
|
2023-10-19 20:18:30 +00:00
|
|
|
|
},
|
|
|
|
|
|
2023-10-20 08:59:06 +00:00
|
|
|
|
// 获取存储
|
2023-10-21 18:54:45 +00:00
|
|
|
|
create (game, type) {
|
2023-10-20 08:59:06 +00:00
|
|
|
|
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'),
|
2023-10-21 18:54:45 +00:00
|
|
|
|
getMeta: MetaFn('getMeta'),
|
|
|
|
|
getAlias: MetaFn('getAlias'),
|
|
|
|
|
async forEach (game, type, fn) {
|
|
|
|
|
let meta = Meta.create(game, type)
|
|
|
|
|
meta.forEach(fn)
|
|
|
|
|
},
|
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-11-07 19:07:56 +00:00
|
|
|
|
let games = (!game || game === 'gs') ? ['gs', 'sr'] : ['sr', 'gs']
|
2023-10-19 20:18:30 +00:00
|
|
|
|
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)
|
2023-10-23 02:56:47 +00:00
|
|
|
|
return { game: currGame, id, data }
|
2023-10-19 20:18:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
export default Meta
|