2023-02-08 20:55:54 +00:00
|
|
|
|
/**
|
|
|
|
|
* 用户数据文件
|
2023-02-19 17:08:05 +00:00
|
|
|
|
* 数据存储在/data/UserData/${uid}.json 下
|
2023-02-12 21:18:06 +00:00
|
|
|
|
* 兼容处理面板户数及Mys数据
|
|
|
|
|
*
|
2023-02-08 20:55:54 +00:00
|
|
|
|
*/
|
2023-02-09 16:57:25 +00:00
|
|
|
|
import lodash from 'lodash'
|
2023-02-08 20:55:54 +00:00
|
|
|
|
import Base from './Base.js'
|
2023-03-07 17:52:11 +00:00
|
|
|
|
import { Data } from '#miao'
|
2023-02-12 21:18:06 +00:00
|
|
|
|
import { AvatarData, ProfileRank, Character } from './index.js'
|
2023-02-08 20:55:54 +00:00
|
|
|
|
|
2023-02-13 19:47:22 +00:00
|
|
|
|
import MysAvatar from './player/MysAvatar.js'
|
|
|
|
|
import Profile from './player/Profile.js'
|
2023-02-08 20:55:54 +00:00
|
|
|
|
|
2023-02-19 17:08:05 +00:00
|
|
|
|
Data.createDir('/data/UserData', 'root')
|
2023-05-14 20:19:33 +00:00
|
|
|
|
Data.createDir('/data/PlayerData/gs', 'root')
|
|
|
|
|
Data.createDir('/data/PlayerData/sr', 'root')
|
|
|
|
|
|
2023-02-08 20:55:54 +00:00
|
|
|
|
export default class Player extends Base {
|
2023-05-14 20:19:33 +00:00
|
|
|
|
constructor (uid, game = 'gs') {
|
2023-02-08 20:55:54 +00:00
|
|
|
|
super()
|
2023-02-09 16:57:25 +00:00
|
|
|
|
uid = uid?._mys?.uid || uid?.uid || uid
|
2023-02-08 20:55:54 +00:00
|
|
|
|
if (!uid) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2023-05-19 18:50:39 +00:00
|
|
|
|
let cacheObj = this._getCache(`player:${game}:${uid}`)
|
2023-02-08 20:55:54 +00:00
|
|
|
|
if (cacheObj) {
|
|
|
|
|
return cacheObj
|
|
|
|
|
}
|
|
|
|
|
this.uid = uid
|
2023-05-14 20:19:33 +00:00
|
|
|
|
this.game = game
|
2023-02-08 20:55:54 +00:00
|
|
|
|
this.reload()
|
2023-02-22 17:48:44 +00:00
|
|
|
|
return this._cache(100)
|
2023-02-08 20:55:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 19:47:22 +00:00
|
|
|
|
get hasProfile () {
|
|
|
|
|
let ret = false
|
|
|
|
|
lodash.forEach(this._avatars, (avatar) => {
|
|
|
|
|
if (avatar.isProfile) {
|
|
|
|
|
ret = true
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-14 20:19:33 +00:00
|
|
|
|
get _file () {
|
|
|
|
|
if (this.isSr) {
|
|
|
|
|
return `/data/PlayerData/sr/${this.uid}.json`
|
|
|
|
|
} else {
|
|
|
|
|
return `/data/UserData/${this.uid}.json`
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static create (e, game = 'gs') {
|
2023-02-09 16:57:25 +00:00
|
|
|
|
if (e?._mys?.uid || e.uid) {
|
2023-02-08 20:55:54 +00:00
|
|
|
|
// 传入为e
|
2023-05-14 20:19:33 +00:00
|
|
|
|
let player = new Player(e?._mys?.uid || e.uid, (game === 'sr' || e.isSr) ? 'sr' : 'gs')
|
2023-02-08 20:55:54 +00:00
|
|
|
|
player.e = e
|
|
|
|
|
return player
|
|
|
|
|
} else {
|
2023-05-14 20:19:33 +00:00
|
|
|
|
return new Player(e, game)
|
2023-02-08 20:55:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-12 21:18:06 +00:00
|
|
|
|
// 获取面板更新服务名
|
2023-05-14 20:19:33 +00:00
|
|
|
|
static getProfileServName (uid, game = 'gs') {
|
|
|
|
|
let Serv = Profile.getServ(uid, game)
|
2023-02-12 21:18:06 +00:00
|
|
|
|
return Serv.name
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-14 20:19:33 +00:00
|
|
|
|
static delByUid (uid, game = 'gs') {
|
|
|
|
|
let player = Player.create(uid, game)
|
2023-02-17 20:42:05 +00:00
|
|
|
|
if (player) {
|
|
|
|
|
player.del()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-08 20:55:54 +00:00
|
|
|
|
/**
|
|
|
|
|
* 重新加载json文件
|
|
|
|
|
*/
|
|
|
|
|
reload () {
|
|
|
|
|
let data
|
2023-05-14 20:19:33 +00:00
|
|
|
|
data = Data.readJSON(this._file, 'root')
|
2023-02-08 20:55:54 +00:00
|
|
|
|
this.setBasicData(data)
|
|
|
|
|
if (data.chars) {
|
|
|
|
|
this.setAvatars(data.chars)
|
|
|
|
|
}
|
|
|
|
|
this.setAvatars(data.avatars || [])
|
2023-03-03 22:01:11 +00:00
|
|
|
|
if (data._ck) {
|
|
|
|
|
this._ck = data._ck
|
|
|
|
|
}
|
2023-02-19 04:05:42 +00:00
|
|
|
|
if (!data.avatars) {
|
|
|
|
|
this.save()
|
|
|
|
|
}
|
2023-02-08 20:55:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 保存json文件
|
2023-02-19 02:38:51 +00:00
|
|
|
|
* @param flag false时暂时禁用保存,true时启用保存,并保存数据
|
|
|
|
|
* @returns {boolean}
|
2023-02-08 20:55:54 +00:00
|
|
|
|
*/
|
2023-02-19 02:38:51 +00:00
|
|
|
|
save (flag = null) {
|
|
|
|
|
if (flag === true) {
|
|
|
|
|
this._save = true
|
|
|
|
|
} else if (flag === false || this._save === false) {
|
|
|
|
|
this._save = false
|
|
|
|
|
return false
|
|
|
|
|
}
|
2023-02-10 19:40:55 +00:00
|
|
|
|
let ret = Data.getData(this, 'uid,name,level,word,face,card,sign,info,_info,_mys,_profile')
|
2023-02-08 20:55:54 +00:00
|
|
|
|
ret.avatars = {}
|
2023-02-13 19:47:22 +00:00
|
|
|
|
this.forEachAvatar((avatar) => {
|
|
|
|
|
ret.avatars[avatar.id] = avatar.toJSON()
|
2023-02-08 20:55:54 +00:00
|
|
|
|
})
|
2023-03-03 22:01:11 +00:00
|
|
|
|
if (this._ck) {
|
|
|
|
|
ret._ck = this._ck
|
2023-02-08 20:55:54 +00:00
|
|
|
|
}
|
2023-05-14 20:19:33 +00:00
|
|
|
|
if (this.isSr) {
|
|
|
|
|
Data.writeJSON(`/data/PlayerData/sr/${this.uid}.json`, ret, 'root')
|
|
|
|
|
} else {
|
|
|
|
|
Data.writeJSON(`/data/UserData/${this.uid}.json`, ret, 'root')
|
|
|
|
|
}
|
2023-02-08 20:55:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-17 20:42:05 +00:00
|
|
|
|
del () {
|
|
|
|
|
try {
|
2023-05-14 20:19:33 +00:00
|
|
|
|
Data.delFile(this._file, 'root')
|
|
|
|
|
ProfileRank.delUidInfo(this.uid, this.game)
|
2023-02-17 20:42:05 +00:00
|
|
|
|
this._delCache()
|
|
|
|
|
Bot.logger.mark(`【面板数据删除】${this.uid}本地文件数据已删除...`)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log('del error', e)
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-08 20:55:54 +00:00
|
|
|
|
/**
|
|
|
|
|
* 设置玩家基础数据
|
|
|
|
|
* @param ds
|
|
|
|
|
*/
|
|
|
|
|
setBasicData (ds) {
|
|
|
|
|
this.name = ds.name || this.name || ''
|
2023-02-10 19:40:55 +00:00
|
|
|
|
this.level = ds.level || this.level || ''
|
|
|
|
|
this.word = ds.word || this.word || ''
|
2023-02-08 20:55:54 +00:00
|
|
|
|
this.face = ds.face || this.face || ''
|
|
|
|
|
this.card = ds.card || this.card || ''
|
|
|
|
|
this.sign = ds.sign || this.sign || ''
|
2023-02-10 19:40:55 +00:00
|
|
|
|
this.info = ds.info || this.info || false
|
2023-02-08 20:55:54 +00:00
|
|
|
|
this._avatars = this._avatars || {}
|
|
|
|
|
this._profile = ds._profile || this._profile
|
|
|
|
|
this._mys = ds._mys || this._mys
|
2023-02-10 19:40:55 +00:00
|
|
|
|
this._info = ds._info || this._info
|
2023-02-08 20:55:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 16:57:25 +00:00
|
|
|
|
// 设置角色列表
|
2023-02-08 20:55:54 +00:00
|
|
|
|
setAvatars (ds) {
|
|
|
|
|
lodash.forEach(ds, (avatar) => {
|
|
|
|
|
this.setAvatar(avatar)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 16:57:25 +00:00
|
|
|
|
// 设置角色数据
|
|
|
|
|
setAvatar (ds, source = '') {
|
2023-02-12 21:18:06 +00:00
|
|
|
|
let avatar = this.getAvatar(ds.id, true)
|
2023-02-08 20:55:54 +00:00
|
|
|
|
avatar.setAvatar(ds, source)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 16:57:25 +00:00
|
|
|
|
// 获取Avatar角色
|
2023-02-12 21:18:06 +00:00
|
|
|
|
getAvatar (id, create = false) {
|
|
|
|
|
let char = Character.get(id)
|
|
|
|
|
let avatars = this._avatars
|
2023-05-14 20:19:33 +00:00
|
|
|
|
if (this.isGs) {
|
|
|
|
|
// 兼容处理旅行者的情况
|
|
|
|
|
if (char.isTraveler && !create) {
|
|
|
|
|
id = avatars['10000005'] ? 10000005 : 10000007
|
|
|
|
|
}
|
2023-02-12 21:18:06 +00:00
|
|
|
|
}
|
|
|
|
|
if (!avatars[id] && create) {
|
2023-05-20 17:14:07 +00:00
|
|
|
|
avatars[id] = AvatarData.create({ id }, this.game)
|
2023-02-08 20:55:54 +00:00
|
|
|
|
}
|
2023-02-12 21:18:06 +00:00
|
|
|
|
return avatars[id] || false
|
2023-02-08 20:55:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 16:57:25 +00:00
|
|
|
|
// 异步循环角色
|
2023-02-08 20:55:54 +00:00
|
|
|
|
async forEachAvatarAsync (fn) {
|
|
|
|
|
for (let id in this._avatars) {
|
|
|
|
|
let ret = await fn(this._avatars[id], id)
|
|
|
|
|
if (ret === false) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 16:57:25 +00:00
|
|
|
|
// 循环Avatar
|
2023-02-08 20:55:54 +00:00
|
|
|
|
forEachAvatar (fn) {
|
|
|
|
|
for (let id in this._avatars) {
|
2023-02-13 19:47:22 +00:00
|
|
|
|
let avatar = this._avatars[id]
|
|
|
|
|
if (avatar && avatar.hasData) {
|
|
|
|
|
let ret = fn(this._avatars[id])
|
|
|
|
|
if (ret === false) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2023-02-08 20:55:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 16:57:25 +00:00
|
|
|
|
// 获取所有Avatar数据
|
2023-02-08 20:55:54 +00:00
|
|
|
|
getAvatarData (ids = '') {
|
|
|
|
|
let ret = {}
|
|
|
|
|
if (!ids) {
|
|
|
|
|
this.forEachAvatar((avatar) => {
|
|
|
|
|
ret[avatar.id] = avatar.getDetail()
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
lodash.forEach(ids, (id) => {
|
2023-02-12 21:18:06 +00:00
|
|
|
|
let avatar = this.getAvatar(id)
|
|
|
|
|
if (avatar) {
|
|
|
|
|
ret[id] = avatar.getDetail()
|
|
|
|
|
}
|
2023-02-08 20:55:54 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 16:57:25 +00:00
|
|
|
|
// 获取指定角色的面板数据
|
2023-02-08 20:55:54 +00:00
|
|
|
|
getProfile (id) {
|
|
|
|
|
let avatar = this.getAvatar(id)
|
2023-02-12 21:18:06 +00:00
|
|
|
|
return avatar ? avatar.getProfile() : false
|
2023-02-08 20:55:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 16:57:25 +00:00
|
|
|
|
// 获取所有面板数据
|
2023-02-08 20:55:54 +00:00
|
|
|
|
getProfiles () {
|
|
|
|
|
let ret = {}
|
|
|
|
|
lodash.forEach(this._avatars, (avatar) => {
|
|
|
|
|
let profile = avatar.getProfile()
|
|
|
|
|
if (profile) {
|
|
|
|
|
ret[profile.id] = profile
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 07:24:44 +00:00
|
|
|
|
getUpdateTime () {
|
|
|
|
|
let ret = {}
|
|
|
|
|
if (this._profile) {
|
|
|
|
|
ret.profile = MysAvatar.getDate(this._profile)
|
|
|
|
|
}
|
|
|
|
|
if (this._mys) {
|
|
|
|
|
ret.mys = MysAvatar.getDate(this._mys)
|
|
|
|
|
}
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-10 21:31:41 +00:00
|
|
|
|
getInfo () {
|
2023-02-11 13:22:09 +00:00
|
|
|
|
return MysAvatar.getInfo(this)
|
2023-02-10 21:31:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 16:57:25 +00:00
|
|
|
|
// 更新面板
|
2023-02-13 19:47:22 +00:00
|
|
|
|
async refreshProfile (force = 2) {
|
2023-02-09 16:57:25 +00:00
|
|
|
|
return await Profile.refreshProfile(this, force)
|
2023-02-08 20:55:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新米游社数据
|
2023-02-11 13:22:09 +00:00
|
|
|
|
/**
|
|
|
|
|
* 更新米游社数据
|
|
|
|
|
* @param force: 0:不强制,长超时时间 1:短超时时间 2:无视缓存,强制刷新
|
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
|
*/
|
|
|
|
|
async refreshMysDetail (force = 0) {
|
2023-02-11 07:24:44 +00:00
|
|
|
|
return MysAvatar.refreshMysDetail(this, force)
|
2023-02-10 19:40:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 13:22:09 +00:00
|
|
|
|
async refreshMysInfo (force = 0) {
|
2023-02-10 19:40:55 +00:00
|
|
|
|
return await MysAvatar.refreshMysInfo(this, force)
|
2023-02-08 20:55:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 通过已有的Mys CharData更新
|
|
|
|
|
setMysCharData (charData) {
|
|
|
|
|
MysAvatar.setMysCharData(this, charData)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 使用MysApi刷新指定角色的天赋信息
|
2023-02-11 13:22:09 +00:00
|
|
|
|
async refreshTalent (ids = '', force = 0) {
|
2023-02-08 20:55:54 +00:00
|
|
|
|
return await MysAvatar.refreshTalent(this, ids, force)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 19:47:22 +00:00
|
|
|
|
async refresh (cfg) {
|
2023-02-19 02:38:51 +00:00
|
|
|
|
this.save(false)
|
|
|
|
|
try {
|
|
|
|
|
if (cfg.index || cfg.index === 0) {
|
|
|
|
|
await this.refreshMysInfo(cfg.index)
|
|
|
|
|
}
|
|
|
|
|
if (cfg.detail || cfg.detail === 0) {
|
|
|
|
|
await this.refreshMysDetail(cfg.detail)
|
|
|
|
|
}
|
|
|
|
|
if (cfg.talent || cfg.talent === 0) {
|
|
|
|
|
await this.refreshTalent(cfg.ids, cfg.talent)
|
|
|
|
|
}
|
|
|
|
|
if (cfg.profile || cfg.profile === 0) {
|
|
|
|
|
await this.refreshProfile(cfg.profile)
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
Bot.logger.mark(`刷新uid${this.uid}数据遇到错误...`)
|
|
|
|
|
console.log(e)
|
2023-02-13 19:47:22 +00:00
|
|
|
|
}
|
2023-02-19 02:38:51 +00:00
|
|
|
|
this.save(true)
|
2023-02-13 19:47:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async refreshAndGetAvatarData (cfg) {
|
|
|
|
|
await this.refresh(cfg)
|
2023-02-10 14:01:18 +00:00
|
|
|
|
|
|
|
|
|
let rank = false
|
|
|
|
|
let e = this.e
|
|
|
|
|
if (cfg.rank === true && e && e.group_id) {
|
|
|
|
|
rank = await ProfileRank.create({ group: e.group_id, uid: this.uid, qq: e.user_id })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let avatarRet = {}
|
|
|
|
|
this.forEachAvatar((avatar) => {
|
|
|
|
|
let { talent } = avatar
|
|
|
|
|
let ds = avatar.getDetail()
|
|
|
|
|
ds.aeq = talent?.a?.original + talent?.e?.original + talent?.q?.original || 3
|
|
|
|
|
avatarRet[ds.id] = ds
|
|
|
|
|
|
|
|
|
|
let profile = avatar.getProfile()
|
|
|
|
|
if (profile) {
|
|
|
|
|
let mark = profile.getArtisMark(false)
|
|
|
|
|
ds.artisMark = Data.getData(mark, 'mark,markClass,names')
|
|
|
|
|
if (rank) {
|
|
|
|
|
rank.getRank(profile)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
if (cfg.retType !== 'array') {
|
|
|
|
|
return avatarRet
|
|
|
|
|
}
|
|
|
|
|
avatarRet = lodash.values(avatarRet)
|
|
|
|
|
if (cfg.sort) {
|
|
|
|
|
let sortKey = 'level,star,aeq,cons,weapon.level,weapon.star,weapon.affix,fetter'.split(',')
|
|
|
|
|
avatarRet = lodash.orderBy(avatarRet, sortKey)
|
|
|
|
|
avatarRet = avatarRet.reverse()
|
|
|
|
|
}
|
|
|
|
|
return avatarRet
|
|
|
|
|
}
|
2023-02-08 20:55:54 +00:00
|
|
|
|
}
|