miao-plugin/models/artis/Artis.js

290 lines
6.5 KiB
JavaScript
Raw Permalink Normal View History

2023-10-18 13:19:12 +00:00
/**
* 面板圣遗物
*/
import lodash from 'lodash'
2023-10-19 16:31:35 +00:00
import { Artifact, ArtifactSet } from '#miao.models'
2023-10-18 13:19:12 +00:00
import { Data, Format } from '#miao'
2023-10-19 16:31:35 +00:00
import Base from '../Base.js'
import ArtisAttr from './ArtisAttr.js'
2023-10-19 16:31:35 +00:00
import ArtisSet from './ArtisSet.js'
import ArtisMark from './ArtisMark.js'
2023-10-18 13:19:12 +00:00
2023-10-19 16:31:35 +00:00
export default class Artis extends Base {
2023-10-18 17:01:11 +00:00
constructor (game = 'gs', isProfile = false) {
2023-10-19 16:31:35 +00:00
super()
this.game = game
this.artis = {}
2023-10-18 13:19:12 +00:00
this.isProfile = !!isProfile
}
2023-10-19 16:31:35 +00:00
// 获取圣遗物套装数据
getSetData () {
return ArtisSet.getSetData(this)
}
get sets () {
return this.getSetData().sets || {}
}
get names () {
return this.getSetData().names || []
}
// 有圣遗物数据
get hasArtis () {
return !lodash.isEmpty(this.artis)
}
_get (key) {
let artis = this.artis
switch (key) {
case 'length':
return lodash.keys(artis).length
}
if (artis[key]) {
return artis[key]
}
}
forEach (fn) {
lodash.forEach(this.artis, (ds, idx) => {
if (ds.name) {
return fn(ds, idx)
}
})
}
eachIdx (fn) {
for (let idx = 1; idx <= (this.isGs ? 5 : 6); idx++) {
this.artis[idx] = this.artis[idx] || {}
let ret = fn(this.artis[idx], idx)
if (ret === false) {
break
}
}
}
setArtisData (ds = {}) {
this.eachIdx((arti, idx) => {
this.setArtis(idx, ds[idx] || ds[`arti${idx}`] || {})
})
}
getDetail () {
let ret = {}
for (let idx = 1; idx <= 5; idx++) {
let ds = this.artis[idx]
if (ds && (ds.name || ds.id)) {
let artis = Artifact.get(ds)
if (!artis) {
continue
}
let tmp = {
...artis?.getData('img,name,set'),
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
}
eachArtisSet (fn) {
ArtifactSet.eachSet(this.sets, fn, this.game)
}
2023-10-18 13:19:12 +00:00
// 有圣遗物词条
get hasAttr () {
return ArtisAttr.hasAttr(this)
2023-10-18 13:19:12 +00:00
}
2023-10-19 16:31:35 +00:00
setArtisBase (idx = 1, ds = {}) {
this.artis[idx] = this.artis[idx] || {}
let arti = this.artis[idx]
if (this.isSr) {
let 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
} 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
}
2023-10-18 13:19:12 +00:00
}
setArtis (idx = 1, ds = {}) {
2023-10-18 17:01:11 +00:00
idx = idx.toString().replace('arti', '') * 1 || 1
2023-10-19 16:31:35 +00:00
this.setArtisBase(idx, ds)
2023-10-18 17:01:11 +00:00
if (!this.isProfile) {
return
}
2023-10-18 13:19:12 +00:00
let arti = this.artis[idx]
if (!ds.attrIds || !ds.mainId) {
return false
}
arti.mainId = ds.mainId
arti.attrIds = ds.attrIds
2023-10-18 17:01:11 +00:00
let artiObj = Artifact.get(arti.id || arti.name, this.game)
2023-10-18 13:19:12 +00:00
if (!artiObj) {
return false
}
2023-10-18 17:01:11 +00:00
let attr = artiObj.getAttrData(arti, idx, this.game)
2023-10-18 13:19:12 +00:00
if (!attr) {
console.log('attr id error', ds.main, ds.mainId, idx, arti.level, arti.star)
return false
}
2023-10-18 17:01:11 +00:00
arti.main = attr.main
arti.attrs = attr.attrs
2023-10-18 13:19:12 +00:00
}
// 获取保存数据
toJSON () {
let ret = {}
this.eachIdx((ds, idx) => {
2023-10-19 16:31:35 +00:00
let key = this.isGs ? 'name' : 'id'
let tmp = {
2023-10-23 02:56:47 +00:00
level: ds.level || 1
2023-10-19 16:31:35 +00:00
}
if (!ds[key]) {
return true
}
tmp[key] = ds[key]
2023-10-23 02:56:47 +00:00
if (this.isGs) {
tmp.star = ds.star || 5
}
2023-10-18 13:19:12 +00:00
// 如果不为面板数据则不保存mainId和attrIds
if (!this.isProfile) {
2023-10-19 16:31:35 +00:00
ret[idx] = tmp
2023-10-18 13:19:12 +00:00
return true
}
2023-10-19 16:31:35 +00:00
if (!ds.mainId || !ds.attrIds) {
return true
}
ret[idx] = tmp
2023-10-18 17:01:11 +00:00
tmp.mainId = ds.mainId || ds.main?.id
2023-10-18 13:19:12 +00:00
if (this.isSr) {
tmp.attrIds = []
lodash.forEach(ds.attrs, (as) => {
tmp.attrIds.push([as?.id || '', as?.count || 1, as?.step || 0].join(','))
})
} else {
tmp.attrIds = ds.attrIds
}
})
return ret
}
// 获取指定idx的主词条
getMainAttr (idx = '') {
if (!idx) {
let ret = {}
this.eachIdx((arti, idx) => {
ret[idx] = this.getMainAttr(idx)
})
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 = '') {
let mainAttr = this.getMainAttr()
let check = true
2023-11-12 18:18:45 +00:00
pos = pos || (this.isGs ? '3,4,5' : '3,4,5,6')
2023-10-18 13:19:12 +00:00
let dmgIdx = this.isGs ? 4 : 5
2023-11-12 18:18:45 +00:00
let attrs = attr.split(',')
2023-10-18 13:19:12 +00:00
Data.eachStr(pos.toString(), (p) => {
2023-11-12 18:18:45 +00:00
let posAttr = mainAttr[p]
if (!attrs.includes(posAttr)) {
if (p === dmgIdx && attrs.includes('dmg') && Format.isElem(posAttr)) {
return true
}
/* if (/Plus$/.test(posAttr) && attrs.includes(posAttr.replace('Pct', ''))) {
return true
} */
return check = false
2023-10-18 13:19:12 +00:00
}
})
return check
}
isSameArtis (target) {
let k = (ds) => [ds?.name || '', ds?.level || '', ds?.star || ''].join('|')
let ret = true
this.eachIdx((ds, idx) => {
2023-10-19 16:31:35 +00:00
if (k(ds) !== k(target[idx])) {
return ret = false
}
})
return ret
}
getAllAttr () {
let ret = {}
let add = (ds) => {
if (!ds) {
return
}
let key = ds.key
if (!ret[key]) {
ret[key] = {
key,
value: 0,
upNum: 0,
eff: 0
}
}
let tmp = ret[key]
tmp.value += ds.value
if (ds.eff && ds.upNum) {
tmp.eff += ds.eff
tmp.upNum += ds.upNum
}
}
this.forEach((arti) => {
// add(arti.main)
lodash.forEach(arti.attrs, (attr) => {
add(attr)
})
})
ret = lodash.sortBy(lodash.values(ret), ['eff']).reverse()
return ArtisMark.formatArti(ret, false, false, this.game)
}
2023-10-18 13:19:12 +00:00
}