2022-11-21 21:26:06 +00:00
|
|
|
|
import lodash from 'lodash'
|
|
|
|
|
import Base from './Base.js'
|
2023-05-19 18:50:39 +00:00
|
|
|
|
import { Format } from '#miao'
|
2022-11-21 21:26:06 +00:00
|
|
|
|
|
2023-05-16 19:15:00 +00:00
|
|
|
|
const baseAttr = {
|
|
|
|
|
gs: 'atk,def,hp,mastery,recharge,cpct,cdmg,dmg,phy,heal,shield'.split(','),
|
2023-09-07 03:23:58 +00:00
|
|
|
|
sr: 'atk,def,hp,speed,recharge,cpct,cdmg,dmg,heal,stance,effPct,effDef'.split(',')
|
2023-05-16 19:15:00 +00:00
|
|
|
|
}
|
|
|
|
|
let attrReg = {
|
|
|
|
|
gs: new RegExp(`^(${baseAttr.gs.join('|')})(Base|Plus|Pct|Inc)$`),
|
|
|
|
|
sr: new RegExp(`^(${baseAttr.sr.join('|')})(Base|Plus|Pct|Inc)$`)
|
|
|
|
|
}
|
2022-11-21 21:26:06 +00:00
|
|
|
|
|
|
|
|
|
class ProfileAttr extends Base {
|
2023-05-20 13:51:44 +00:00
|
|
|
|
constructor (char, data = null) {
|
2022-11-21 21:26:06 +00:00
|
|
|
|
super()
|
2023-05-20 13:51:44 +00:00
|
|
|
|
this.char = char
|
|
|
|
|
this.game = char.game
|
2023-05-19 18:50:39 +00:00
|
|
|
|
this.init(data, this.game)
|
2022-11-21 21:26:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-20 13:51:44 +00:00
|
|
|
|
static create (char, data = null) {
|
|
|
|
|
return new ProfileAttr(char, data)
|
2023-03-05 07:21:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-20 13:51:44 +00:00
|
|
|
|
init (data) {
|
2022-11-21 21:26:06 +00:00
|
|
|
|
// 基础属性
|
|
|
|
|
this._attr = {}
|
2023-03-05 07:21:44 +00:00
|
|
|
|
this._base = {}
|
2022-11-21 21:26:06 +00:00
|
|
|
|
let attr = this._attr
|
2023-03-05 07:21:44 +00:00
|
|
|
|
let base = this._base
|
2023-05-16 19:15:00 +00:00
|
|
|
|
lodash.forEach(baseAttr[this.game], (key) => {
|
2022-11-21 21:26:06 +00:00
|
|
|
|
attr[key] = {
|
|
|
|
|
base: 0,
|
|
|
|
|
plus: 0,
|
|
|
|
|
pct: 0
|
|
|
|
|
}
|
2023-03-05 07:21:44 +00:00
|
|
|
|
base[key] = 0
|
2022-11-21 21:26:06 +00:00
|
|
|
|
})
|
2023-05-20 13:51:44 +00:00
|
|
|
|
|
2022-11-21 21:26:06 +00:00
|
|
|
|
if (data) {
|
|
|
|
|
this.setAttr(data, true)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* getter
|
|
|
|
|
*
|
|
|
|
|
* @param key
|
|
|
|
|
* @returns {*|number}
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
_get (key) {
|
|
|
|
|
let attr = this._attr
|
2023-05-16 19:15:00 +00:00
|
|
|
|
if (baseAttr[this.game].includes(key)) {
|
2022-11-21 21:26:06 +00:00
|
|
|
|
let a = attr[key]
|
|
|
|
|
return a.base * (1 + a.pct / 100) + a.plus
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-16 19:15:00 +00:00
|
|
|
|
let testRet = attrReg[this.game].exec(key)
|
2022-11-21 21:26:06 +00:00
|
|
|
|
if (testRet && testRet[1] && testRet[2]) {
|
|
|
|
|
let key = testRet[1]
|
|
|
|
|
let key2 = testRet[2].toLowerCase()
|
|
|
|
|
return attr[key][key2] || 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加或追加Attr数据
|
|
|
|
|
* @param key
|
|
|
|
|
* @param val
|
2023-05-16 19:15:00 +00:00
|
|
|
|
* @param isBase
|
2022-11-21 21:26:06 +00:00
|
|
|
|
* @returns {boolean}
|
|
|
|
|
*/
|
2023-03-05 07:21:44 +00:00
|
|
|
|
addAttr (key, val, isBase = false) {
|
2022-11-21 21:26:06 +00:00
|
|
|
|
let attr = this._attr
|
2023-03-05 07:21:44 +00:00
|
|
|
|
let base = this._base
|
2023-05-20 13:51:44 +00:00
|
|
|
|
|
|
|
|
|
if (this.isSr && Format.isElem(key, this.game)) {
|
|
|
|
|
if (Format.sameElem(this.char.elem, key, this.game)) {
|
|
|
|
|
key = 'dmg'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-16 19:15:00 +00:00
|
|
|
|
if (baseAttr[this.game].includes(key)) {
|
2022-11-21 21:26:06 +00:00
|
|
|
|
attr[key].plus += val * 1
|
2023-03-05 07:21:44 +00:00
|
|
|
|
if (isBase) {
|
|
|
|
|
base[key] = (base[key] || 0) + val * 1
|
|
|
|
|
}
|
2022-11-21 21:26:06 +00:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-16 19:15:00 +00:00
|
|
|
|
let testRet = attrReg[this.game].exec(key)
|
2022-11-21 21:26:06 +00:00
|
|
|
|
if (testRet && testRet[1] && testRet[2]) {
|
|
|
|
|
let key = testRet[1]
|
|
|
|
|
let key2 = testRet[2].toLowerCase()
|
2022-11-24 16:31:36 +00:00
|
|
|
|
attr[key][key2] = attr[key][key2] || 0
|
2022-11-21 21:26:06 +00:00
|
|
|
|
attr[key][key2] += val * 1
|
2023-03-05 07:21:44 +00:00
|
|
|
|
if (key2 === 'base' || isBase) {
|
|
|
|
|
base[key] = (base[key] || 0) + val * 1
|
|
|
|
|
}
|
2022-11-21 21:26:06 +00:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置属性
|
|
|
|
|
* @param data
|
|
|
|
|
* @param withBase:带有base数据的初始化设置,会将atk/hp/def视作结果数据而非plus数据
|
|
|
|
|
*/
|
|
|
|
|
setAttr (data, withBase = false) {
|
|
|
|
|
if (withBase) {
|
|
|
|
|
lodash.forEach(['hp', 'def', 'atk'], (key) => {
|
|
|
|
|
let base = `${key}Base`
|
|
|
|
|
if (data[key] && data[base]) {
|
|
|
|
|
data[`${key}Plus`] = data[key] - data[base]
|
|
|
|
|
delete data[key]
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
lodash.forEach(data, (val, key) => {
|
2023-05-20 13:51:44 +00:00
|
|
|
|
if (this.isSr && Format.isElem(key, this.game)) {
|
|
|
|
|
if (this.char.elem === Format.elem(key, '', this.game)) {
|
|
|
|
|
this.addAttr('dmg', val)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.addAttr(key, val)
|
|
|
|
|
}
|
2022-11-21 21:26:06 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getAttr () {
|
|
|
|
|
let ret = {}
|
2023-05-16 19:15:00 +00:00
|
|
|
|
lodash.forEach(baseAttr[this.game], (key) => {
|
2022-11-21 21:26:06 +00:00
|
|
|
|
ret[key] = this[key]
|
2023-10-14 09:20:20 +00:00
|
|
|
|
if (['hp', 'atk', 'def', 'speed'].includes(key)) {
|
2022-11-21 21:26:06 +00:00
|
|
|
|
ret[`${key}Base`] = this[`${key}Base`]
|
|
|
|
|
}
|
|
|
|
|
})
|
2022-11-24 16:31:36 +00:00
|
|
|
|
ret._calc = true
|
2022-11-21 21:26:06 +00:00
|
|
|
|
return ret
|
|
|
|
|
}
|
2023-03-05 07:21:44 +00:00
|
|
|
|
|
|
|
|
|
getBase () {
|
|
|
|
|
return this._base
|
|
|
|
|
}
|
2022-11-21 21:26:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default ProfileAttr
|