2022-09-25 13:56:10 +00:00
|
|
|
/*
|
|
|
|
* 用户角色列表
|
|
|
|
*
|
|
|
|
* 批量管理及天赋等数据获取
|
|
|
|
* 使用 Avatar Model实现兼容处理面板数据及Mys角色数据
|
|
|
|
* */
|
2022-07-31 21:42:16 +00:00
|
|
|
import Base from './Base.js'
|
|
|
|
import lodash from 'lodash'
|
2022-09-20 19:15:22 +00:00
|
|
|
import { Data, Common, Profile } from '../components/index.js'
|
|
|
|
import { Avatar, MysApi } from './index.js'
|
2022-07-31 21:42:16 +00:00
|
|
|
|
2022-09-18 20:54:01 +00:00
|
|
|
export default class AvatarList extends Base {
|
2022-09-20 19:15:22 +00:00
|
|
|
constructor (uid, datas = [], withProfile = false) {
|
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-09-20 19:15:22 +00:00
|
|
|
let profiles = {}
|
|
|
|
if (withProfile) {
|
|
|
|
profiles = Profile.getAll(uid)
|
|
|
|
}
|
|
|
|
lodash.forEach(datas, (ds) => {
|
|
|
|
let avatar = new Avatar(ds, profiles[ds.id] || false)
|
|
|
|
if (avatar) {
|
|
|
|
avatars[avatar.id] = avatar
|
2022-08-06 22:36:05 +00:00
|
|
|
}
|
2022-09-20 19:15:22 +00:00
|
|
|
})
|
|
|
|
// 使用面板数据补全
|
|
|
|
lodash.forEach(profiles, (profile) => {
|
|
|
|
if (!avatars[profile.id]) {
|
|
|
|
let avatar = new Avatar(profile)
|
|
|
|
if (avatar) {
|
|
|
|
avatars[avatar.id] = avatar
|
2022-07-31 21:42:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.avatars = avatars
|
|
|
|
}
|
|
|
|
|
2022-09-20 19:15:22 +00:00
|
|
|
getData (ids, keys = '') {
|
2022-07-31 21:42:16 +00:00
|
|
|
let rets = {}
|
2022-09-24 21:05:22 +00:00
|
|
|
keys = keys || 'id,name,level,star,cons,fetter,elem,face,side,gacha,abbr,weapon,artisSet'
|
2022-07-31 21:42:16 +00:00
|
|
|
let avatars = this.avatars
|
|
|
|
lodash.forEach(ids, (id) => {
|
2022-09-20 19:15:22 +00:00
|
|
|
rets[id] = avatars[id].getData(keys) || {}
|
2022-07-31 21:42:16 +00:00
|
|
|
})
|
|
|
|
return rets
|
|
|
|
}
|
2022-08-01 21:15:37 +00:00
|
|
|
|
2022-09-20 19:15:22 +00:00
|
|
|
getAvatar (id) {
|
|
|
|
return this.avatars[id]
|
|
|
|
}
|
|
|
|
|
|
|
|
getProfile (id) {
|
|
|
|
return this.avatars[id]?.profile
|
|
|
|
}
|
|
|
|
|
2022-08-06 22:36:05 +00:00
|
|
|
getIds () {
|
|
|
|
let rets = []
|
|
|
|
lodash.forEach(this.avatars, (ds) => {
|
|
|
|
rets.push(ds.id)
|
|
|
|
})
|
|
|
|
return rets
|
|
|
|
}
|
|
|
|
|
2022-09-20 19:15:22 +00:00
|
|
|
async getTalentData (ids = '', mys = false, keys = '') {
|
|
|
|
if (!ids) {
|
|
|
|
ids = this.getIds()
|
|
|
|
}
|
|
|
|
mys = mys || this._mys
|
2022-09-18 20:54:01 +00:00
|
|
|
let avatarTalent = await Data.getCacheJSON(`miao:avatar-talent:${this.uid}`)
|
2022-08-01 21:15:37 +00:00
|
|
|
let needReq = {}
|
|
|
|
lodash.forEach(ids, (id) => {
|
|
|
|
if (!avatarTalent[id]) {
|
|
|
|
needReq[id] = true
|
|
|
|
}
|
|
|
|
})
|
2022-09-20 19:15:22 +00:00
|
|
|
let avatars = this.avatars
|
2022-08-01 21:15:37 +00:00
|
|
|
let needReqIds = lodash.keys(needReq)
|
2022-09-20 19:15:22 +00:00
|
|
|
if (needReqIds.length > 0) {
|
|
|
|
if (needReqIds.length > 8) {
|
|
|
|
this.e && this.e.reply('正在获取角色信息,请稍候...')
|
|
|
|
}
|
2022-08-01 21:15:37 +00:00
|
|
|
let num = 10
|
|
|
|
let ms = 100
|
|
|
|
let skillRet = []
|
|
|
|
let avatarArr = lodash.chunk(needReqIds, num)
|
|
|
|
for (let val of avatarArr) {
|
|
|
|
for (let id of val) {
|
2022-09-20 19:15:22 +00:00
|
|
|
let avatar = avatars[id]
|
|
|
|
skillRet.push(await avatar.getTalent(mys))
|
2022-08-01 21:15:37 +00:00
|
|
|
}
|
|
|
|
skillRet = await Promise.all(skillRet)
|
|
|
|
skillRet = skillRet.filter(item => item.id)
|
|
|
|
await Common.sleep(ms)
|
|
|
|
}
|
|
|
|
lodash.forEach(skillRet, (talent) => {
|
|
|
|
avatarTalent[talent.id] = talent
|
|
|
|
})
|
2022-09-18 20:54:01 +00:00
|
|
|
await Data.setCacheJSON(`miao:avatar-talent:${this.uid}`, avatarTalent, 3600 * 2)
|
2022-08-01 21:15:37 +00:00
|
|
|
}
|
2022-09-20 19:15:22 +00:00
|
|
|
let ret = this.getData(ids, keys)
|
2022-08-01 21:15:37 +00:00
|
|
|
lodash.forEach(ret, (avatar, id) => {
|
2022-09-20 11:50:27 +00:00
|
|
|
avatar.talent = avatarTalent[id] || {}
|
2022-08-01 21:15:37 +00:00
|
|
|
})
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2022-09-20 11:50:27 +00:00
|
|
|
async getAvatarTalent (id, mys) {
|
2022-08-01 21:15:37 +00:00
|
|
|
let avatar = this.avatars[id]
|
2022-09-20 19:15:22 +00:00
|
|
|
return await avatar.getTalent(mys)
|
|
|
|
}
|
|
|
|
|
|
|
|
get isSelfCookie () {
|
|
|
|
return !!this._mys?.isSelfCookie
|
2022-08-01 21:15:37 +00:00
|
|
|
}
|
2022-07-31 21:42:16 +00:00
|
|
|
}
|
2022-08-03 17:51:48 +00:00
|
|
|
|
2022-09-18 20:54:01 +00:00
|
|
|
AvatarList.hasTalentCache = async function (uid) {
|
|
|
|
return !!await redis.get(`miao:avatar-talent:${uid}`)
|
2022-08-03 17:51:48 +00:00
|
|
|
}
|
2022-09-20 19:15:22 +00:00
|
|
|
|
|
|
|
AvatarList.getAll = async function (e, mys = false) {
|
|
|
|
if (!mys) {
|
|
|
|
mys = await MysApi.init(e)
|
|
|
|
}
|
|
|
|
if (!mys || !mys.uid) return false
|
|
|
|
let uid = mys.uid
|
|
|
|
let data = await mys.getCharacter()
|
|
|
|
if (!data) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
let ret = new AvatarList(uid, data.avatars, true)
|
|
|
|
ret.e = e
|
|
|
|
ret._mys = mys
|
|
|
|
return ret
|
|
|
|
}
|