mirror of
https://github.com/yoimiya-kokomi/miao-plugin.git
synced 2024-11-25 17:35:34 +00:00
269 lines
7.0 KiB
JavaScript
269 lines
7.0 KiB
JavaScript
/**
|
||
* 面板圣遗物
|
||
*/
|
||
import lodash from 'lodash'
|
||
import Base from './Base.js'
|
||
import { Artifact, ArtifactSet } from './index.js'
|
||
import { Format, Data } from '../components/index.js'
|
||
import ArtisMark from './profile/ArtisMark.js'
|
||
|
||
export default class AvatarArtis extends Base {
|
||
constructor (charid = 0) {
|
||
super()
|
||
this.charid = charid
|
||
this.artis = {}
|
||
}
|
||
|
||
get sets () {
|
||
return this.getSetData().sets || {}
|
||
}
|
||
|
||
get names () {
|
||
return this.getSetData().names || []
|
||
}
|
||
|
||
get hasArtis () {
|
||
return !lodash.isEmpty(this.artis)
|
||
}
|
||
|
||
get hasAttr () {
|
||
return ArtisMark.hasAttr(this.artis)
|
||
}
|
||
|
||
static _eachArtisSet (sets, fn) {
|
||
lodash.forEach(sets || [], (v, k) => {
|
||
let artisSet = ArtifactSet.get(k)
|
||
if (artisSet) {
|
||
if (v >= 4) {
|
||
fn(artisSet, 2)
|
||
}
|
||
fn(artisSet, v)
|
||
}
|
||
})
|
||
}
|
||
|
||
static getArtisKeyTitle () {
|
||
return ArtisMark.getKeyTitleMap()
|
||
}
|
||
|
||
setArtisData (ds = {}, profile = false) {
|
||
// let force = !this.hasArtis || ArtisMark.hasAttr(ds) || !ArtisMark.hasAttr(this.artis)
|
||
if (!profile || (profile && ArtisMark.hasAttr(ds))) {
|
||
for (let idx = 1; idx <= 5; idx++) {
|
||
if (ds[idx] || ds[`arti${idx}`]) {
|
||
this.setArtis(idx, ds[idx] || ds[`arti${idx}`], profile)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
setArtis (idx = 1, ds = {}, profile = false) {
|
||
idx = idx.toString().replace('arti', '')
|
||
this.artis[idx] = this.artis[idx] || {}
|
||
let arti = this.artis[idx]
|
||
if (profile) {
|
||
arti.name = ds._name || ds.name || arti.name || ''
|
||
arti.set = ds._set || Artifact.getSetNameByArti(arti._name) || ds.set || ''
|
||
arti.level = ds._level || ds.level || 1
|
||
arti.star = ds._star || ds.star || 5
|
||
arti.main = ds.main
|
||
arti.attrs = ds.attrs
|
||
return true
|
||
}
|
||
arti.name = ds.name || arti.name || ''
|
||
arti.set = ds.set || Artifact.getSetNameByArti(arti.name) || ''
|
||
arti.level = ds.level || 1
|
||
arti.star = ds.star || 5
|
||
|
||
if (ds.mainId || ds.main) {
|
||
arti._name = ds._name || ds.name || arti._name || arti.name
|
||
arti._set = ds._set || Artifact.getSetNameByArti(arti._name) || arti._set || ''
|
||
arti._level = ds._level || ds.level || arti._level || arti.level
|
||
arti._star = ds._star || ds.star || arti._star || arti.star || 5
|
||
}
|
||
|
||
// 存在面板数据,更新面板数据
|
||
if (ds.mainId && ds.attrIds) {
|
||
arti.mainId = ds.mainId
|
||
arti.attrIds = ds.attrIds
|
||
arti.main = Artifact.getMainById(ds.mainId, arti._level, arti._star)
|
||
arti.attrs = Artifact.getAttrsByIds(ds.attrIds, arti._star)
|
||
} else if (ds.main && ds.attrs) {
|
||
arti.main = ArtisMark.formatAttr(ds.main || {})
|
||
arti.attrs = []
|
||
for (let attrIdx in ds.attrs || []) {
|
||
if (ds.attrs[attrIdx]) {
|
||
arti.attrs.push(ArtisMark.formatAttr(ds.attrs[attrIdx]))
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
forEach (fn) {
|
||
lodash.forEach(this.artis, (ds, idx) => {
|
||
if (ds.name) {
|
||
fn(ds, idx)
|
||
}
|
||
})
|
||
}
|
||
|
||
_get (key) {
|
||
let artis = this.artis
|
||
switch (key) {
|
||
case 'length':
|
||
return lodash.keys(artis).length
|
||
}
|
||
if (artis[key]) {
|
||
return artis[key]
|
||
}
|
||
}
|
||
|
||
toJSON () {
|
||
let ret = {}
|
||
for (let idx = 1; idx <= 5; idx++) {
|
||
let ds = this.artis[idx]
|
||
if (ds) {
|
||
let tmp = {
|
||
name: ds.name || '',
|
||
level: ds.level || 1,
|
||
star: ds.star || 5
|
||
}
|
||
if ((ds.mainId && ds.attrIds) || (ds.main && ds.attrs)) {
|
||
if ((ds._name && ds._name !== ds.name) || (ds._level && ds._level !== ds.level) || (ds._star && ds._star !== ds.star)) {
|
||
tmp._name = ds._name || null
|
||
tmp._level = ds._level || null
|
||
tmp._star = ds._star || null
|
||
}
|
||
}
|
||
if (ds.mainId && ds.attrIds) {
|
||
tmp.mainId = ds.mainId || null
|
||
tmp.attrIds = ds.attrIds
|
||
} else if (ds.main && ds.attrs) {
|
||
tmp.main = ds.main || null
|
||
tmp.attrs = []
|
||
for (let attrIdx in ds.attrs || []) {
|
||
if (ds.attrs[attrIdx]) {
|
||
tmp.attrs.push(ArtisMark.formatAttr(ds.attrs[attrIdx]))
|
||
}
|
||
}
|
||
}
|
||
ret[idx] = tmp
|
||
}
|
||
}
|
||
return ret
|
||
}
|
||
|
||
getDetail (profile = false) {
|
||
let ret = {}
|
||
for (let idx = 1; idx <= 5; idx++) {
|
||
let ds = this.artis[idx]
|
||
if (ds) {
|
||
let artis = Artifact.get(profile ? ds._name : ds.name)
|
||
let tmp = {
|
||
...artis?.getData('img,name,set'),
|
||
level: (profile ? ds._level : ds.level) || 1
|
||
}
|
||
if (ds.main && ds.attrs) {
|
||
tmp.main = ds.main || null
|
||
tmp.attrs = []
|
||
for (let attrIdx in ds.attrs || []) {
|
||
if (ds.attrs[attrIdx]) {
|
||
tmp.attrs.push(ArtisMark.formatAttr(ds.attrs[attrIdx]))
|
||
}
|
||
}
|
||
}
|
||
ret[idx] = tmp
|
||
}
|
||
}
|
||
return ret
|
||
}
|
||
|
||
mainAttr (idx = '') {
|
||
if (!idx) {
|
||
let ret = {}
|
||
for (let i = 1; i <= 5; i++) {
|
||
ret[i] = this.mainAttr(i)
|
||
}
|
||
return ret
|
||
}
|
||
let main = this.artis[idx]?.main
|
||
if (!main) {
|
||
return ''
|
||
}
|
||
return main.key || ''
|
||
}
|
||
|
||
is (check, pos = '') {
|
||
if (pos) {
|
||
return this.isAttr(check, pos)
|
||
}
|
||
let sets = this.getSetData()?.abbrs || []
|
||
let ret = false
|
||
Data.eachStr(check, (s) => {
|
||
if (sets.includes(s)) {
|
||
ret = true
|
||
return false
|
||
}
|
||
})
|
||
return ret
|
||
}
|
||
|
||
isAttr (attr, pos = '3,4,5') {
|
||
let mainAttr = this.mainAttr()
|
||
let check = true
|
||
Data.eachStr(pos.toString(), (p) => {
|
||
let attrs = attr.split(',')
|
||
if (!attrs.includes(mainAttr[p]) && (p === '4' && !attrs.includes('dmg') && Format.isElem(mainAttr[p]))) {
|
||
check = false
|
||
return false
|
||
}
|
||
})
|
||
return check
|
||
}
|
||
|
||
/**
|
||
* 获取圣遗物套装数据
|
||
* @returns {*|{imgs: *[], names: *[], sets: {}, abbrs: *[], sName: string, name: (string|*)}}
|
||
* sets: 套装名:2/4
|
||
* names: [套装名]
|
||
* imgs: [img]
|
||
* abbrs:[别名]
|
||
* name: '组合名字', 若为4件套会使用套装完整名
|
||
* sName: '简写名字',若为4件套也会使用简写
|
||
*/
|
||
getSetData (profile = false) {
|
||
let setCount = {}
|
||
this.forEach((arti, idx) => {
|
||
setCount[profile ? arti._set : arti.set] = (setCount[profile ? arti._set : arti.set] || 0) + 1
|
||
})
|
||
let sets = {}
|
||
let names = []
|
||
let imgs = []
|
||
let abbrs = []
|
||
let abbrs2 = []
|
||
for (let set in setCount) {
|
||
if (setCount[set] >= 2) {
|
||
let count = setCount[set] >= 4 ? 4 : 2
|
||
sets[set] = count
|
||
let artiSet = ArtifactSet.get(set)
|
||
names.push(artiSet.name)
|
||
imgs.push(artiSet.img)
|
||
abbrs.push(artiSet.abbr + count)
|
||
abbrs2.push(artiSet.name + count)
|
||
}
|
||
}
|
||
return {
|
||
sets,
|
||
names,
|
||
imgs,
|
||
abbrs: [...abbrs, ...abbrs2],
|
||
name: (abbrs.length > 1 || abbrs2[0]?.length > 7) ? abbrs.join('+') : abbrs2[0],
|
||
sName: abbrs.join('+')
|
||
}
|
||
}
|
||
|
||
eachArtisSet (fn) {
|
||
AvatarArtis._eachArtisSet(this.sets, fn)
|
||
}
|
||
}
|