2022-07-31 21:42:16 +00:00
|
|
|
import Base from './Base.js'
|
|
|
|
import lodash from 'lodash'
|
|
|
|
import Data from '../Data.js'
|
2022-08-01 21:15:37 +00:00
|
|
|
import Artifact from './Artifact.js'
|
|
|
|
import Character from './Character.js'
|
|
|
|
import Common from '../Common.js'
|
2022-07-31 21:42:16 +00:00
|
|
|
|
|
|
|
export default class Avatars extends Base {
|
2022-08-01 21:15:37 +00:00
|
|
|
constructor (uid, datas = []) {
|
2022-07-31 21:42:16 +00:00
|
|
|
super()
|
2022-08-01 21:15:37 +00:00
|
|
|
if (!uid) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
this.uid = uid
|
2022-07-31 21:42:16 +00:00
|
|
|
let avatars = {}
|
2022-08-06 22:36:05 +00:00
|
|
|
let abbr = Character.getAbbr()
|
2022-07-31 21:42:16 +00:00
|
|
|
lodash.forEach(datas, (avatar) => {
|
|
|
|
let data = Data.getData(avatar, 'id,name,level,star:rarity,cons:actived_constellation_num,fetter')
|
2022-08-06 22:36:05 +00:00
|
|
|
data.abbr = abbr[data.name] || data.name
|
2022-08-01 21:15:37 +00:00
|
|
|
data.elem = (avatar.element || '').toLowerCase() || 'hydro'
|
2022-07-31 21:42:16 +00:00
|
|
|
data.weapon = Data.getData(avatar.weapon, 'name,affix:affix_level,level,star:rarity')
|
2022-08-06 22:36:05 +00:00
|
|
|
data.weapon.abbr = abbr[data?.weapon?.name || ''] || data?.weapon?.name
|
|
|
|
if (data.star > 5) {
|
2022-08-07 22:14:24 +00:00
|
|
|
data.star = 5
|
2022-08-06 22:36:05 +00:00
|
|
|
}
|
2022-07-31 21:42:16 +00:00
|
|
|
let artis = {}
|
|
|
|
let sets = {}
|
|
|
|
lodash.forEach(avatar.reliquaries, (arti) => {
|
|
|
|
artis[arti.pos] = Data.getData(arti, 'name,level,set:set.name')
|
|
|
|
sets[arti.set.name] = (sets[arti.set.name] || 0) + 1
|
|
|
|
})
|
|
|
|
data.artis = artis
|
|
|
|
data.set = {}
|
|
|
|
for (let set in sets) {
|
|
|
|
if (sets[set] >= 4) {
|
|
|
|
data.set[set] = 4
|
|
|
|
} else if (sets[set] >= 2) {
|
|
|
|
data.set[set] = 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data.sets = []
|
|
|
|
lodash.forEach(data.set, (v, k) => {
|
|
|
|
let name = Artifact.getArtiBySet(k)
|
|
|
|
if (name) {
|
|
|
|
data.sets.push(name)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
avatars[data.id] = data
|
|
|
|
})
|
|
|
|
this.avatars = avatars
|
|
|
|
}
|
|
|
|
|
2022-08-01 21:15:37 +00:00
|
|
|
getData (ids) {
|
2022-07-31 21:42:16 +00:00
|
|
|
let rets = {}
|
|
|
|
let avatars = this.avatars
|
|
|
|
lodash.forEach(ids, (id) => {
|
|
|
|
rets[id] = avatars[id] || {}
|
|
|
|
})
|
|
|
|
return rets
|
|
|
|
}
|
2022-08-01 21:15:37 +00:00
|
|
|
|
2022-08-06 22:36:05 +00:00
|
|
|
getIds () {
|
|
|
|
let rets = []
|
|
|
|
lodash.forEach(this.avatars, (ds) => {
|
|
|
|
rets.push(ds.id)
|
|
|
|
})
|
|
|
|
return rets
|
|
|
|
}
|
|
|
|
|
2022-08-01 21:15:37 +00:00
|
|
|
async getTalentData (ids, MysApi = false) {
|
|
|
|
let avatarTalent = {}
|
|
|
|
let talentCache = await redis.get(`genshin:avatar-talent:${this.uid}`)
|
|
|
|
if (talentCache) {
|
|
|
|
avatarTalent = JSON.parse(talentCache)
|
|
|
|
}
|
|
|
|
let needReq = {}
|
|
|
|
lodash.forEach(ids, (id) => {
|
|
|
|
if (!avatarTalent[id]) {
|
|
|
|
needReq[id] = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
let needReqIds = lodash.keys(needReq)
|
|
|
|
if (needReqIds.length > 0 && MysApi && MysApi.isSelfCookie) {
|
|
|
|
let num = 10
|
|
|
|
let ms = 100
|
|
|
|
let skillRet = []
|
|
|
|
let avatarArr = lodash.chunk(needReqIds, num)
|
|
|
|
for (let val of avatarArr) {
|
|
|
|
for (let id of val) {
|
|
|
|
skillRet.push(this.getAvatarTalent(id, MysApi))
|
|
|
|
}
|
|
|
|
skillRet = await Promise.all(skillRet)
|
|
|
|
skillRet = skillRet.filter(item => item.id)
|
|
|
|
await Common.sleep(ms)
|
|
|
|
}
|
|
|
|
lodash.forEach(skillRet, (talent) => {
|
|
|
|
avatarTalent[talent.id] = talent
|
|
|
|
})
|
2022-08-15 20:54:58 +00:00
|
|
|
await redis.set(`genshin:avatar-talent:${this.uid}`, JSON.stringify(avatarTalent), { EX: 3600 * 2 })
|
2022-08-01 21:15:37 +00:00
|
|
|
}
|
|
|
|
let ret = this.getData(ids)
|
|
|
|
lodash.forEach(ret, (avatar, id) => {
|
|
|
|
avatar.talent = avatarTalent[id] || { a: {}, e: {}, q: {} }
|
|
|
|
})
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAvatarTalent (id, MysApi) {
|
|
|
|
let talent = { id, a: {}, e: {}, q: {} }
|
|
|
|
let talentRes = await MysApi.getDetail(id)
|
|
|
|
let char = Character.get(id)
|
|
|
|
let avatar = this.avatars[id]
|
|
|
|
if (!char || !avatar) {
|
|
|
|
return talent
|
|
|
|
}
|
|
|
|
let consTalent = char.getConsTalent()
|
|
|
|
if (talentRes && talentRes.skill_list) {
|
|
|
|
talent.id = id
|
|
|
|
let talentList = lodash.orderBy(talentRes.skill_list, ['id'], ['asc'])
|
|
|
|
for (let val of talentList) {
|
|
|
|
let { max_level: maxLv, level_current: lv } = val
|
|
|
|
let ds = {
|
|
|
|
current: lv,
|
|
|
|
original: lv,
|
|
|
|
crown: lv === maxLv
|
|
|
|
}
|
|
|
|
if (val.name.includes('普通攻击')) {
|
|
|
|
talent.a = ds
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (maxLv >= 10 && !talent.e?.current) {
|
|
|
|
talent.e = ds
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (maxLv >= 10 && !talent.q?.current) {
|
|
|
|
talent.q = ds
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lodash.forEach([3, 5], (c) => {
|
|
|
|
if (avatar.cons >= c) {
|
|
|
|
if (consTalent.e === c) {
|
|
|
|
talent.e.current += 3
|
|
|
|
talent.e.plus = true
|
|
|
|
} else if (consTalent.q === c) {
|
|
|
|
talent.q.current += 3
|
|
|
|
talent.q.plus = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return talent
|
|
|
|
}
|
2022-07-31 21:42:16 +00:00
|
|
|
}
|
2022-08-03 17:51:48 +00:00
|
|
|
|
|
|
|
Avatars.hasTalentCache = async function (uid) {
|
|
|
|
return !!await redis.get(`genshin:avatar-talent:${uid}`)
|
|
|
|
}
|