miao-plugin/models/AvatarArtis.js

314 lines
8.3 KiB
JavaScript
Raw Normal View History

2023-02-08 20:55:54 +00:00
/**
* 面板圣遗物
*/
import lodash from 'lodash'
import Base from './Base.js'
import { Artifact, ArtifactSet } from './index.js'
2023-03-07 17:52:11 +00:00
import { Format, Data } from '#miao'
import ArtisMark from './profile/ArtisMark.js'
2023-02-08 20:55:54 +00:00
export default class AvatarArtis extends Base {
constructor (charid = 0, game = 'gs') {
2023-02-08 20:55:54 +00:00
super()
this.game = game
2023-02-08 20:55:54 +00:00
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 () {
2023-05-14 20:19:33 +00:00
if (this.isSr) {
return true
}
return ArtisMark.hasAttr(this.artis)
}
static _eachArtisSet (sets, fn, game = 'gs') {
lodash.forEach(sets || [], (v, k) => {
let artisSet = ArtifactSet.get(k, game)
if (artisSet) {
if (v >= 4) {
fn(artisSet, 2)
}
fn(artisSet, v)
}
})
}
2023-05-18 20:23:19 +00:00
static getArtisKeyTitle (game = 'gs') {
return ArtisMark.getKeyTitleMap(game)
}
setArtisData (ds = {}, isProfile = false) {
if (!isProfile || (isProfile && ArtisMark.hasAttr(ds))) {
for (let idx = 1; idx <= (this.isGs ? 5 : 6); idx++) {
if (ds[idx] || ds[`arti${idx}`]) {
this.setArtis(idx, ds[idx] || ds[`arti${idx}`], isProfile)
2023-02-08 20:55:54 +00:00
}
}
}
}
setArtis (idx = 1, ds = {}, isProfile = false) {
2023-02-08 20:55:54 +00:00
idx = idx.toString().replace('arti', '')
this.artis[idx] = this.artis[idx] || {}
let arti = this.artis[idx]
let artiObj
if (this.isSr) {
artiObj = Artifact.get(ds.id, this.game)
if (!artiObj) {
return false
}
arti.id = artiObj.id || ds.id || arti.id || ''
arti.name = artiObj.name || arti.name || ''
arti.set = artiObj.setName || arti.set || ''
arti.level = ds.level || arti.level || 1
arti.star = artiObj.getStarById(ds.id) || arti.star || 5
let attrIds = ds.attrIds || ds.attrs
if (ds.mainId && attrIds) {
let attr = artiObj.getAttrData(ds.mainId, attrIds, arti.level, arti.star, idx)
if (attr) {
arti.mainId = ds.mainId
arti.attrIds = attrIds
arti.main = attr.main || arti.main || {}
arti.attrs = attr.attrs || arti.attrs || {}
} else {
console.log('attr id error', ds.main, ds.mainId, idx, arti.level, arti.star)
}
} else {
arti.attrs = []
}
return
} else {
if (isProfile) {
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
} else {
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 = []
2023-02-08 20:55:54 +00:00
for (let attrIdx in ds.attrs || []) {
if (ds.attrs[attrIdx]) {
arti.attrs.push(ArtisMark.formatAttr(ds.attrs[attrIdx]))
2023-02-08 20:55:54 +00:00
}
}
}
}
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 <= (this.isGs ? 5 : 6); idx++) {
2023-02-08 20:55:54 +00:00
let ds = this.artis[idx]
if (!ds) {
continue
}
let tmp = {
level: ds.level || 1,
star: ds.star || 5
}
if (this.isSr) {
tmp.id = ds.id
tmp.mainId = ds.main?.id
tmp.attrIds = []
lodash.forEach(ds.attrs, (as) => {
tmp.attrIds.push([
as?.id || '',
as?.count || 1,
as?.step || 0
].join(','))
})
} else {
tmp.name = ds.name || ''
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) {
2023-02-08 20:55:54 +00:00
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
2023-02-08 20:55:54 +00:00
}
return ret
}
getDetail (profile = false) {
2023-02-08 20:55:54 +00:00
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)
2023-02-08 20:55:54 +00:00
let tmp = {
...artis?.getData('img,name,set'),
level: (profile ? ds._level : ds.level) || 1
2023-02-08 20:55:54 +00:00
}
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) {
2023-02-08 20:55:54 +00:00
let setCount = {}
this.forEach((arti, idx) => {
setCount[profile ? arti._set : arti.set] = (setCount[profile ? arti._set : arti.set] || 0) + 1
2023-02-08 20:55:54 +00:00
})
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, this.game)
2023-02-08 20:55:54 +00:00
}
}