2023-02-08 20:55:54 +00:00
|
|
|
import lodash from 'lodash'
|
|
|
|
import Base from './Base.js'
|
|
|
|
import moment from 'moment'
|
|
|
|
import { Character, AvatarArtis, ProfileData, Weapon } from './index.js'
|
2023-05-19 18:50:39 +00:00
|
|
|
import { Data, Format } from '#miao'
|
2023-02-13 19:47:22 +00:00
|
|
|
import AttrCalc from './profile/AttrCalc.js'
|
|
|
|
import Profile from './player/Profile.js'
|
2023-02-08 20:55:54 +00:00
|
|
|
|
|
|
|
const charKey = 'name,abbr,sName,star,imgs,face,side,gacha,weaponTypeName'.split(',')
|
|
|
|
|
|
|
|
export default class AvatarData extends Base {
|
2023-05-20 17:14:07 +00:00
|
|
|
constructor (ds = {}, game = 'gs') {
|
2023-02-08 20:55:54 +00:00
|
|
|
super()
|
|
|
|
let char = Character.get({ id: ds.id, elem: ds.elem })
|
|
|
|
if (!char) {
|
2023-02-09 16:57:25 +00:00
|
|
|
return
|
2023-02-08 20:55:54 +00:00
|
|
|
}
|
|
|
|
this.id = char.id
|
|
|
|
this.char = char
|
2023-05-14 20:19:33 +00:00
|
|
|
this.game = char.game || game
|
2023-02-09 16:57:25 +00:00
|
|
|
this.initArtis()
|
2023-05-20 17:14:07 +00:00
|
|
|
this.setAvatar(ds)
|
2023-02-09 16:57:25 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 14:01:18 +00:00
|
|
|
get hasTalent () {
|
2023-02-10 13:53:57 +00:00
|
|
|
return this.talent && !lodash.isEmpty(this.talent) && !!this._talent
|
|
|
|
}
|
|
|
|
|
2023-02-10 14:01:18 +00:00
|
|
|
get name () {
|
2023-02-10 13:53:57 +00:00
|
|
|
return this.char?.name || ''
|
|
|
|
}
|
|
|
|
|
2023-02-13 19:47:22 +00:00
|
|
|
get hasData () {
|
|
|
|
return !!(this.level > 1 || this?.weapon?.name || this?.talent?.a)
|
|
|
|
}
|
|
|
|
|
2023-02-10 13:53:57 +00:00
|
|
|
// 是否是合法面板数据
|
2023-02-10 14:01:18 +00:00
|
|
|
get isProfile () {
|
2023-02-10 13:53:57 +00:00
|
|
|
return Profile.isProfile(this)
|
|
|
|
}
|
|
|
|
|
2023-02-10 14:01:18 +00:00
|
|
|
get costume () {
|
2023-02-10 13:53:57 +00:00
|
|
|
let costume = this._costume
|
|
|
|
if (lodash.isArray(costume)) {
|
|
|
|
costume = costume[0]
|
|
|
|
}
|
|
|
|
return costume
|
|
|
|
}
|
|
|
|
|
2023-02-10 14:01:18 +00:00
|
|
|
get originalTalent () {
|
2023-02-10 13:53:57 +00:00
|
|
|
return lodash.mapValues(this.talent, (ds) => ds.original)
|
2023-02-08 20:55:54 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 13:53:57 +00:00
|
|
|
/**
|
|
|
|
* 获取圣遗物套装属性
|
|
|
|
* @returns {boolean|*|{imgs: *[], names: *[], sets: {}, abbrs: *[], sName: string, name: (string|*)}|{}}
|
|
|
|
*/
|
2023-02-10 14:01:18 +00:00
|
|
|
get artisSet () {
|
2023-02-10 13:53:57 +00:00
|
|
|
return this.artis.getSetData()
|
|
|
|
}
|
|
|
|
|
2023-02-10 14:01:18 +00:00
|
|
|
get dataSource () {
|
2023-02-10 13:53:57 +00:00
|
|
|
return {
|
|
|
|
enka: 'Enka.Network',
|
|
|
|
miao: '喵喵Api',
|
2023-05-16 05:04:43 +00:00
|
|
|
mgg: 'MiniGG-Api',
|
2023-04-13 18:56:44 +00:00
|
|
|
hutao: 'Hutao-Enka',
|
2023-05-16 05:04:43 +00:00
|
|
|
mys: '米游社',
|
2023-05-19 18:50:39 +00:00
|
|
|
homo: 'Mihomo'
|
2023-02-10 13:53:57 +00:00
|
|
|
}[this._source] || this._source
|
|
|
|
}
|
|
|
|
|
2023-02-10 14:01:18 +00:00
|
|
|
get updateTime () {
|
2023-02-10 13:53:57 +00:00
|
|
|
let time = this._time
|
|
|
|
if (!time) {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
if (lodash.isString(time)) {
|
|
|
|
return moment(time).format('MM-DD HH:mm')
|
|
|
|
}
|
|
|
|
if (lodash.isNumber(time)) {
|
|
|
|
return moment(new Date(time)).format('MM-DD HH:mm')
|
|
|
|
}
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
2023-05-20 17:14:07 +00:00
|
|
|
static create (ds, game = 'gs') {
|
|
|
|
let avatar = new AvatarData(ds, game)
|
2023-02-08 20:55:54 +00:00
|
|
|
if (!avatar) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return avatar
|
|
|
|
}
|
|
|
|
|
2023-02-10 14:01:18 +00:00
|
|
|
initArtis () {
|
2023-05-16 05:04:43 +00:00
|
|
|
this.artis = new AvatarArtis(this.id, this.game)
|
2023-02-10 13:53:57 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 14:01:18 +00:00
|
|
|
_get (key) {
|
2023-02-08 20:55:54 +00:00
|
|
|
if (charKey.includes(key)) {
|
|
|
|
return this.char[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-10 14:01:18 +00:00
|
|
|
setAvatar (ds, source = '') {
|
2023-02-08 20:55:54 +00:00
|
|
|
this._now = new Date() * 1
|
|
|
|
this.setBasic(ds, source)
|
|
|
|
ds.weapon && this.setWeapon(ds.weapon)
|
2023-02-09 16:57:25 +00:00
|
|
|
ds.talent && this.setTalent(ds.talent, 'original', source)
|
2023-02-08 20:55:54 +00:00
|
|
|
ds.artis && this.setArtis(ds)
|
|
|
|
delete this._now
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 设置角色基础数据
|
|
|
|
* @param ds
|
|
|
|
* @param source
|
|
|
|
*/
|
2023-02-10 14:01:18 +00:00
|
|
|
setBasic (ds = {}, source = '') {
|
2023-02-08 20:55:54 +00:00
|
|
|
const now = this._now || (new Date()) * 1
|
|
|
|
this.level = ds.lv || ds.level || this.level || 1
|
|
|
|
this.cons = ds.cons || this.cons || 0
|
|
|
|
this.fetter = ds.fetter || this.fetter || 0
|
|
|
|
this._costume = ds.costume || this._costume || 0
|
|
|
|
this.elem = ds.elem || this.elem || this.char.elem || ''
|
|
|
|
this.promote = lodash.isUndefined(ds.promote) ? (this.promote || AttrCalc.calcPromote(this.level)) : (ds.promote || 0)
|
2023-05-16 05:04:43 +00:00
|
|
|
this.trees = ds.trees || this.trees || []
|
2023-05-20 17:14:07 +00:00
|
|
|
this._source = ds._source || this._source || ''
|
2023-02-08 20:55:54 +00:00
|
|
|
this._time = ds._time || this._time || now
|
|
|
|
this._update = ds._update || this._update || ds._time || now
|
|
|
|
this._talent = ds._talent || this._talent || ds._time || now
|
2023-05-16 05:04:43 +00:00
|
|
|
|
2023-02-08 20:55:54 +00:00
|
|
|
// 存在数据源时更新时间
|
|
|
|
if (source) {
|
|
|
|
this._update = now
|
|
|
|
if (source !== 'mys') {
|
|
|
|
this._source = source
|
|
|
|
this._time = now
|
|
|
|
} else {
|
|
|
|
this._source = this._source || source
|
|
|
|
this._time = this._source !== 'mys' ? (this._time || now) : now
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-10 14:01:18 +00:00
|
|
|
setWeapon (ds = {}) {
|
2023-05-16 05:04:43 +00:00
|
|
|
let w = Weapon.get(ds.name || ds.id, this.game)
|
2023-02-10 13:53:57 +00:00
|
|
|
if (!w) {
|
|
|
|
return false
|
|
|
|
}
|
2023-02-08 20:55:54 +00:00
|
|
|
this.weapon = {
|
2023-05-16 05:04:43 +00:00
|
|
|
id: ds.id || w.id,
|
|
|
|
name: ds.name || w.name,
|
2023-02-08 20:55:54 +00:00
|
|
|
level: ds.level || ds.lv || 1,
|
|
|
|
promote: lodash.isUndefined(ds.promote) ? AttrCalc.calcPromote(ds.level || ds.lv || 1) : (ds.promote || 0),
|
|
|
|
affix: ds.affix,
|
2023-02-10 13:53:57 +00:00
|
|
|
...w.getData('star,abbr,type,img')
|
2023-02-08 20:55:54 +00:00
|
|
|
}
|
|
|
|
if (this.weapon.level < 20) {
|
|
|
|
this.weapon.promote = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-19 18:50:39 +00:00
|
|
|
getWeaponDetail () {
|
|
|
|
if (this.isGs) {
|
|
|
|
return this.weapon
|
|
|
|
}
|
|
|
|
let ret = {
|
|
|
|
...this.weapon
|
|
|
|
}
|
|
|
|
let wData = Weapon.get(ret.id, this.game)
|
|
|
|
ret.splash = wData.imgs.gacha
|
|
|
|
let attrs = wData.calcAttr(ret.level, ret.promote)
|
|
|
|
lodash.forEach(attrs, (val, key) => {
|
|
|
|
attrs[key] = Format.comma(val, 1)
|
|
|
|
})
|
|
|
|
ret.attrs = attrs
|
|
|
|
ret.desc = wData.getAffixDesc(ret.affix)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2023-02-18 10:29:09 +00:00
|
|
|
setTalent (ds = false, mode = 'original', updateTime = '') {
|
2023-02-08 20:55:54 +00:00
|
|
|
const now = this._now || (new Date()) * 1
|
2023-02-18 10:29:09 +00:00
|
|
|
if (ds) {
|
|
|
|
let ret = this.char.getAvatarTalent(ds, this.cons, mode)
|
|
|
|
if (ret) {
|
|
|
|
this.talent = ret || this.talent
|
|
|
|
// 设置天赋更新时间
|
|
|
|
this._talent = ds._talent || this._talent || ds._time || now
|
|
|
|
}
|
2023-02-10 14:01:18 +00:00
|
|
|
}
|
2023-02-18 10:29:09 +00:00
|
|
|
if (updateTime) {
|
2023-02-08 20:55:54 +00:00
|
|
|
this._talent = now
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-10 14:01:18 +00:00
|
|
|
setArtis (ds, source) {
|
2023-02-08 20:55:54 +00:00
|
|
|
this.artis.setArtisData(ds.artis, source)
|
|
|
|
}
|
|
|
|
|
2023-02-10 14:01:18 +00:00
|
|
|
getProfile () {
|
2023-02-08 20:55:54 +00:00
|
|
|
if (!this.isProfile) {
|
|
|
|
return false
|
|
|
|
}
|
2023-05-16 05:04:43 +00:00
|
|
|
return ProfileData.create(this, this.game)
|
2023-02-08 20:55:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 判断当前profileData是否具备有效圣遗物信息
|
2023-02-10 14:01:18 +00:00
|
|
|
hasArtis () {
|
2023-02-09 16:57:25 +00:00
|
|
|
return this.isProfile && this.artis.length > 0
|
2023-02-08 20:55:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// toJSON 供保存使用
|
2023-02-10 14:01:18 +00:00
|
|
|
toJSON () {
|
2023-05-16 05:04:43 +00:00
|
|
|
let keys = this.isGs ?
|
|
|
|
'name,id,elem,level,promote,fetter,costume,cons,talent:originalTalent' :
|
|
|
|
'name,id,elem,level,promote,cons,talent:originalTalent,trees'
|
2023-02-08 20:55:54 +00:00
|
|
|
return {
|
2023-05-16 05:04:43 +00:00
|
|
|
...this.getData(keys),
|
|
|
|
weapon: Data.getData(this.weapon, this.isGs ? 'name,level,promote,affix' : 'id,level,promote,affix'),
|
2023-02-08 20:55:54 +00:00
|
|
|
...this.getData('artis,_source,_time,_update,_talent')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-10 14:01:18 +00:00
|
|
|
getDetail (keys = '') {
|
2023-02-19 04:05:42 +00:00
|
|
|
let imgs = this.char.getImgs(this.costume)
|
2023-05-14 20:19:33 +00:00
|
|
|
if (this.isGs) {
|
|
|
|
return {
|
|
|
|
...(this.getData(keys || 'id,name,level,star,cons,fetter,elem,abbr,weapon,talent,artisSet') || {}),
|
|
|
|
...Data.getData(imgs, 'face,qFace,side,gacha')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return {
|
2023-05-16 05:04:43 +00:00
|
|
|
...(this.getData(keys || 'id,name,level,star,cons,elem,abbr,weapon,talent,artisSet,trees') || {}),
|
2023-05-14 20:19:33 +00:00
|
|
|
...Data.getData(imgs, 'face,qFace,gacha,preview')
|
|
|
|
}
|
2023-02-19 04:05:42 +00:00
|
|
|
}
|
2023-02-08 20:55:54 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 14:01:18 +00:00
|
|
|
getArtisDetail () {
|
2023-02-09 16:57:25 +00:00
|
|
|
return this.artis.getDetail()
|
|
|
|
}
|
2023-02-08 20:55:54 +00:00
|
|
|
}
|