diff --git a/models/Avatar.js b/models/Avatar.js index 37f3caec..3eb93b31 100644 --- a/models/Avatar.js +++ b/models/Avatar.js @@ -14,6 +14,10 @@ export default class Avatar extends AvatarBase { this._artis = new Artis(this.game, true) } + get isAvatar () { + return true + } + // 是否是合法面板数据 get isProfile () { return ProfileAvatar.isProfile(this) @@ -21,7 +25,7 @@ export default class Avatar extends AvatarBase { // profile.hasData 别名 get hasData () { - return ProfileAvatar.isProfile(this) + return !!(this.level > 1 || this?.weapon?.name || this?.talent?.a) } get imgs () { diff --git a/models/Player.js b/models/Player.js index 2852ee4f..7fee6bfa 100644 --- a/models/Player.js +++ b/models/Player.js @@ -35,7 +35,7 @@ export default class Player extends Base { get hasProfile () { let ret = false - lodash.forEach(this._avatars, (avatar) => { + this.forEachAvatar((avatar) => { if (avatar.isProfile) { ret = true return false @@ -92,11 +92,13 @@ export default class Player extends Base { /** * 重新加载json文件 + * 注意:为了性能,默认不初始化avatars数据,按需初始化 + * 如需获取avatar请使用 player.getAvatar() 来进行获取以确保初始化 */ reload () { let data = Data.readJSON(this._file, 'root') this.setBasicData(data) - this.setAvatars(data.avatars || []) + this.setAvatars(data.avatars || [], true) if (data._ck) { this._ck = data._ck } @@ -159,9 +161,17 @@ export default class Player extends Base { } // 设置角色列表 - setAvatars (ds) { + // lazy:是否懒初始化avatar + setAvatars (ds, lazy = false) { lodash.forEach(ds, (avatar) => { - this.setAvatar(avatar) + if (!avatar.id) { + return true + } + if (lazy) { + this._avatars[avatar.id] = avatar + } else { + this.setAvatar(avatar) + } }) } @@ -171,6 +181,21 @@ export default class Player extends Base { avatar.setAvatar(ds, source) } + delAvatar (id) { + delete this._avatars[id] + } + + hasAvatar (id = '') { + if (!id) { + return !lodash.isEmpty(this._avatars) + } + return !!this._avatars[id] + } + + getAvatarIds () { + return lodash.keys(this._avatars) + } + // 获取Avatar角色 getAvatar (id, create = false) { let char = Character.get(id) @@ -181,28 +206,31 @@ export default class Player extends Base { id = avatars['10000005'] ? 10000005 : 10000007 } } - if (!avatars[id] && create) { - avatars[id] = Avatar.create({ id }, this.game) - } - return avatars[id] || false - } - - // 异步循环角色 - async forEachAvatarAsync (fn) { - for (let id in this._avatars) { - let ret = await fn(this._avatars[id], id) - if (ret === false) { + if (!avatars[id]) { + if (create) { + avatars[id] = Avatar.create({ id }, this.game) + } else { return false } } + let avatar = avatars[id] + if (!avatar.isAvatar) { + let data = avatars[id] + avatar = avatars[id] = Avatar.create(avatars[id], this.game) + avatar.setAvatar(data) + } + if (avatar.hasData) { + return avatar + } + return false } // 循环Avatar async forEachAvatar (fn) { for (let id in this._avatars) { - let avatar = this._avatars[id] + let avatar = this.getAvatar(id) if (avatar && avatar.hasData) { - let ret = fn(this._avatars[id], id) + let ret = fn(avatar, id) ret = Data.isPromise(ret) ? await ret : ret if (ret === false) { return false diff --git a/models/avatar/AvatarBase.js b/models/avatar/AvatarBase.js index 29ef2cb3..a1051012 100644 --- a/models/avatar/AvatarBase.js +++ b/models/avatar/AvatarBase.js @@ -29,10 +29,6 @@ export default class AvatarBase extends Base { return this.char?.name || '' } - get hasData () { - return !!(this.level > 1 || this?.weapon?.name || this?.talent?.a) - } - get costume () { let costume = this._costume if (lodash.isArray(costume)) { diff --git a/models/avatar/MysAvatar.js b/models/avatar/MysAvatar.js index 9ff014c2..754c76a1 100644 --- a/models/avatar/MysAvatar.js +++ b/models/avatar/MysAvatar.js @@ -4,7 +4,6 @@ import { Data } from '#miao' import { chestInfo } from '../../resources/meta/info/index.js' import AvatarUtil from './AvatarUtil.js' - const MysAvatar = { checkForce (player, force) { let e = player?.e @@ -18,8 +17,8 @@ const MysAvatar = { } player._info = 0 player._mys = 0 - lodash.forEach(player._avatars, (ds) => { - ds._talent = 0 + player.forEachAvatar((avatar) => { + avatar._talent = 0 }) return 2 }, @@ -105,12 +104,12 @@ const MysAvatar = { if (lodash.keys(charIds).length > 8) { player.forEachAvatar((avatar) => { if (!charIds[avatar.id] && !avatar.isProfile) { - delete player._avatars[avatar.id] + player.delAvatar(avatar.id) } }) } } - if (player._avatars && !lodash.isEmpty(player._avatars)) { + if (player.hasAvatar()) { player._mys = new Date() * 1 player.save() } @@ -170,7 +169,7 @@ const MysAvatar = { getNeedRefreshIds (player, ids, force = 0) { let ret = [] if (!ids) { - ids = lodash.keys(player._avatars) + ids = player.getAvatarIds() } else if (!lodash.isArray(ids)) { ids = [ids] }