miao-plugin/components/Meta.js

173 lines
3.5 KiB
JavaScript
Raw Normal View History

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.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) {
let { data, alias, alias2 } = this
2023-10-20 08:59:06 +00:00
lodash.forEach(ds, (txt, id) => {
id = alias[id] || alias2[id] || id
alias[txt.toLowerCase()] = 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
// 添加别名
addAlias (ds, isPrivate = false) {
let { alias, alias2 } = this
2023-10-20 08:59:06 +00:00
lodash.forEach(ds, (txt, id) => {
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
}
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()
let { data, alias, alias2, aliasFn } = this
2023-10-20 08:59:06 +00:00
if (data[txt]) {
return txt
}
if (alias[txt] || alias2[txt]) {
return alias[txt] || alias2[txt]
2023-10-19 20:18:30 +00:00
}
if (aliasFn) {
let id = aliasFn(txt)
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
}
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)
}
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 = '') => {
if (!game) {
game = 'gs'
}
let meta = Meta.create(game, type)
2023-10-20 08:59:06 +00:00
return meta[fnKey](args)
}
}
const Meta = {
addAliasFn (game, type, fn) {
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
// 获取存储
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'),
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()
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