From 817ae8fb46dd837316984bf4dcc0b5b45e540014 Mon Sep 17 00:00:00 2001 From: Kokomi <102026640+yoimiya-kokomi@users.noreply.github.com> Date: Mon, 13 Feb 2023 05:18:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9player.getAvatar()=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=97=85=E8=A1=8C=E8=80=85=E9=80=BB=E8=BE=91=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/character/AvatarWife.js | 2 +- apps/profile/ProfileDetail.js | 3 +- apps/profile/ProfileRank.js | 3 ++ models/AvatarData.js | 13 ------- models/Character.js | 64 +--------------------------------- models/Player.js | 40 +++++++++++++-------- models/ProfileReq.js | 8 +---- models/player-lib/EnkaData.js | 2 +- models/player-lib/MiaoData.js | 4 +-- models/player-lib/MysAvatar.js | 17 ++++++++- 10 files changed, 52 insertions(+), 104 deletions(-) diff --git a/apps/character/AvatarWife.js b/apps/character/AvatarWife.js index d5f6b865..8bbbc691 100644 --- a/apps/character/AvatarWife.js +++ b/apps/character/AvatarWife.js @@ -168,7 +168,7 @@ async function getAvatarList (player, type, mys) { let list = [] player.forEachAvatar((avatar) => { if (type !== false) { - if (!Character.checkWifeType(avatar.id, type)) { + if (!avatar.char.checkWifeType(avatar.id, type)) { return true } } diff --git a/apps/profile/ProfileDetail.js b/apps/profile/ProfileDetail.js index 72c9a1dc..e7fc1c14 100644 --- a/apps/profile/ProfileDetail.js +++ b/apps/profile/ProfileDetail.js @@ -117,8 +117,7 @@ export async function renderProfile (e, char, mode = 'profile', params = {}) { } let player = Player.create(uid) - let avatar = player.getAvatar(char.id) - let profile = e._profile || avatar.getProfile(char.id) + let profile = e._profile || player.getProfile(char.id) let refresh = async () => { let refreshRet = await autoRefresh(e) diff --git a/apps/profile/ProfileRank.js b/apps/profile/ProfileRank.js index d1131f3e..ef68338f 100644 --- a/apps/profile/ProfileRank.js +++ b/apps/profile/ProfileRank.js @@ -159,6 +159,9 @@ async function renderCharRankList ({ e, uids, char, mode, groupId }) { let uid = ds.uid || ds.value let player = Player.create(uid) let avatar = player.getAvatar(ds.charId || char.id) + if (!avatar) { + continue + } let profile = avatar.getProfile() if (profile) { diff --git a/models/AvatarData.js b/models/AvatarData.js index d9a25734..72c12a1f 100644 --- a/models/AvatarData.js +++ b/models/AvatarData.js @@ -21,19 +21,6 @@ export default class AvatarData extends Base { this.setAvatar(ds, source) } - /** - * 当前数据是否需要更新天赋 - * @returns {boolean} - */ - get needRefreshTalent () { - // 不存在天赋数据 - if (!this.hasTalent) { - return true - } - // 超过2个小时的天赋数据进行请求 - return (new Date() * 1) - this._talent > 3600 * 2 * 1000 - } - get hasTalent () { return this.talent && !lodash.isEmpty(this.talent) && !!this._talent } diff --git a/models/Character.js b/models/Character.js index 3774c905..a87eafb5 100644 --- a/models/Character.js +++ b/models/Character.js @@ -13,7 +13,7 @@ import CharId from './character-lib/CharId.js' import CharMeta from './character-lib/CharMeta.js' import CharCfg from './character-lib/CharCfg.js' -let { abbrMap, wifeMap, idSort, idMap } = CharId +let { wifeMap, idSort, idMap } = CharId let getMeta = function (name) { return Data.readJSON(`resources/meta/character/${name}/data.json`) @@ -172,22 +172,6 @@ class Character extends Base { }) } - // 当获取角色为旅行者时,会考虑当前uid的账号情况返回对应旅行者 - static async getAvatar (name, uid) { - let char = Character.get(name) - return await char.getTraveler(uid) - } - - // TODO:待废弃 - static getAbbr () { - return abbrMap - } - - // TODO:待废弃 - static checkWifeType (charid, type) { - return !!wifeMap[type][charid] - } - // 获取排序ID static sortIds (arr) { return arr.sort((a, b) => (idSort[a] || 300) - (idSort[b] || 300)) @@ -275,52 +259,6 @@ class Character extends Base { return this._detail } - // 获取别名数据 - - // TODO:迁移至Avatar - setTraveler (uid = '') { - if (this.isTraveler && uid && uid.toString().length === 9) { - Data.setCacheJSON(`miao:uid-traveler:${uid}`, { - id: CharId.getTravelerId(this.id), - elem: this.elem - }, 3600 * 24 * 120) - } - } - - // 检查wife类型 - - // 获取旅行者数据 - async getTraveler (uid) { - if (this.isTraveler) { - let tData = await Data.getCacheJSON(`miao:uid-traveler:${uid}`) - return Character.get({ - id: CharId.getTravelerId(tData.id || this.id), - elem: tData.elem || (this.elem !== 'multi' ? this.elem : 'anemo') - }) - } - return this - } - - async checkAvatars (avatars, uid = '') { - if (!this.isTraveler) { - return this - } - if (lodash.isObject(avatars) && avatars.id) { - avatars = [avatars] - } - for (let avatar of avatars) { - if (CharId.isTraveler(avatar.id)) { - let char = Character.get({ - id: avatar.id, - elem: (avatar.elem || avatar.element || 'anemo').toLowerCase() - }) - char.setTraveler(uid) - return char - } - } - return await this.getTraveler(uid) - } - // 获取伤害计算配置 getCalcRule () { if (!this._calcRule && this._calcRule !== false) { diff --git a/models/Player.js b/models/Player.js index 48182fdb..3c005080 100644 --- a/models/Player.js +++ b/models/Player.js @@ -1,10 +1,13 @@ /** * 用户数据文件 + * 数据存储在/data/userData/${uid}.json 下 + * 兼容处理面板户数及Mys数据 + * */ import lodash from 'lodash' import Base from './Base.js' import { Data } from '../components/index.js' -import { AvatarData, ProfileRank } from './index.js' +import { AvatarData, ProfileRank, Character } from './index.js' import MysAvatar from './player-lib/MysAvatar.js' import Profile from './player-lib/Profile.js' @@ -38,6 +41,12 @@ export default class Player extends Base { } } + // 获取面板更新服务名 + static getProfileServName (uid) { + let Serv = Profile.getServ(uid) + return Serv.name + } + /** * 重新加载json文件 */ @@ -96,16 +105,22 @@ export default class Player extends Base { // 设置角色数据 setAvatar (ds, source = '') { - let avatar = this.getAvatar(ds.id) + let avatar = this.getAvatar(ds.id, true) avatar.setAvatar(ds, source) } // 获取Avatar角色 - getAvatar (id) { - if (!this._avatars[id]) { - this._avatars[id] = AvatarData.create({ id }) + getAvatar (id, create = false) { + let char = Character.get(id) + let avatars = this._avatars + // 兼容处理旅行者的情况 + if (char.isTraveler && !create) { + id = avatars['10000005'] ? 10000005 : 10000007 } - return this._avatars[id] + if (!avatars[id] && create) { + avatars[id] = AvatarData.create({ id }) + } + return avatars[id] || false } // 异步循环角色 @@ -137,7 +152,10 @@ export default class Player extends Base { }) } else { lodash.forEach(ids, (id) => { - ret[id] = this.getAvatar(id) + let avatar = this.getAvatar(id) + if (avatar) { + ret[id] = avatar.getDetail() + } }) } return ret @@ -146,7 +164,7 @@ export default class Player extends Base { // 获取指定角色的面板数据 getProfile (id) { let avatar = this.getAvatar(id) - return avatar.getProfile() + return avatar ? avatar.getProfile() : false } // 获取所有面板数据 @@ -205,12 +223,6 @@ export default class Player extends Base { return await MysAvatar.refreshTalent(this, ids, force) } - // 获取面板更新服务名 - static getProfileServName (uid) { - let Serv = Profile.getServ(uid) - return Serv.name - } - async refreshAndGetAvatarData (cfg) { // 更新角色信息 await this.refreshMysDetail(cfg.force || 0) diff --git a/models/ProfileReq.js b/models/ProfileReq.js index e4964ede..8770a18c 100644 --- a/models/ProfileReq.js +++ b/models/ProfileReq.js @@ -1,10 +1,6 @@ import Base from './Base.js' import fetch from 'node-fetch' -function sleep (ms) { - return new Promise((resolve) => setTimeout(resolve, ms)) -} - export default class ProfileReq extends Base { constructor (e) { super() @@ -40,7 +36,7 @@ export default class ProfileReq extends Base { let serv = this.serv let extra = serv.name ? `当前面板服务${serv.name},` : '' const msgs = { - error: `UID${this.uid}更新面板失败,${extra}可能是面板服务维护中,请稍后重试...`, + error: `UID${this.uid}更新面板失败,${extra}\n可能是面板服务维护中,请稍后重试...`, empty: '请将角色放置在【游戏内】角色展柜,并打开【显示详情】,等待5分钟重新获取面板' } msg = msgs[msg] || msg @@ -66,8 +62,6 @@ export default class ProfileReq extends Base { return this.err(`请求过快,请${cdTime}秒后重试..`) } await this.setCd(20) - // this.msg(`开始获取uid:${this.uid}的数据,可能会需要一定时间~`) - // await sleep(100) // 发起请求 logger.mark(`面板请求UID:${this.uid},面板服务:${serv.name}...`) let data = {} diff --git a/models/player-lib/EnkaData.js b/models/player-lib/EnkaData.js index 9abac60e..f88c072c 100644 --- a/models/player-lib/EnkaData.js +++ b/models/player-lib/EnkaData.js @@ -8,7 +8,7 @@ let EnkaData = { if (!char) { return } - let avatar = player.getAvatar(char.id) + let avatar = player.getAvatar(char.id, true) let talentRet = EnkaData.getTalent(char.id, data.skillLevelMap) avatar.setAvatar({ level: data.propMap['4001'].val * 1, diff --git a/models/player-lib/MiaoData.js b/models/player-lib/MiaoData.js index 7e2667cf..bcca2512 100644 --- a/models/player-lib/MiaoData.js +++ b/models/player-lib/MiaoData.js @@ -1,4 +1,4 @@ -import { Character, Artifact } from '../index.js' +import { Character } from '../index.js' import lodash from 'lodash' import { attrMap, artisIdxMap } from './ProfileMeta.js' @@ -6,7 +6,7 @@ import { attrMap, artisIdxMap } from './ProfileMeta.js' let MiaoData = { setAvatar (player, ds) { let char = Character.get(ds.id) - let avatar = player.getAvatar(ds.id) + let avatar = player.getAvatar(ds.id, true) let talentRet = MiaoData.getTalent(char.id, ds.skill) avatar.setAvatar({ level: ds.level, diff --git a/models/player-lib/MysAvatar.js b/models/player-lib/MysAvatar.js index 0c970eb4..89706bce 100644 --- a/models/player-lib/MysAvatar.js +++ b/models/player-lib/MysAvatar.js @@ -68,6 +68,7 @@ const MysAvatar = { level: role.level, name: role.nickname }) + let charIds = {} lodash.forEach(charData.avatars, (ds) => { let avatar = Data.getData(ds, 'id,level,cons:actived_constellation_num,fetter') avatar.elem = ds.element.toLowerCase() @@ -90,7 +91,16 @@ const MysAvatar = { }) avatar.artis = artis player.setAvatar(avatar, 'mys') + charIds[avatar.id] = true }) + // 若角色数据>8,检查缓存,删除错误缓存的数据 + if (lodash.keys(charIds).length > 8) { + player.forEachAvatar((avatar) => { + if (!charIds[avatar.id] && !avatar.isProfile) { + delete player._avatars[avatar.id] + } + }) + } player._mys = new Date() * 1 player.save() }, @@ -152,6 +162,9 @@ const MysAvatar = { } lodash.forEach(ids, (id) => { let avatar = player.getAvatar(id) + if (!avatar) { + return true + } if (!avatar.hasTalent || MysAvatar.needRefresh(avatar._talent, force, { 0: 60 * 24, 1: 60, 2: 0 })) { ret.push(avatar.id) } @@ -184,7 +197,9 @@ const MysAvatar = { for (let val of avatarArr) { for (let id of val) { let avatar = player.getAvatar(id) - skillRet.push(await MysAvatar.refreshAvatarTalent(avatar, mys)) + if (avatar) { + skillRet.push(await MysAvatar.refreshAvatarTalent(avatar, mys)) + } } skillRet = await Promise.all(skillRet) skillRet = skillRet.filter(item => item.id)