为player.avatar增加懒初始化

This commit is contained in:
Kokomi 2023-10-19 19:24:42 +08:00
parent 119eff6f27
commit 2993021be9
4 changed files with 55 additions and 28 deletions

View File

@ -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 () {

View File

@ -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) => {
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) {
if (!avatars[id]) {
if (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) {
} 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

View File

@ -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)) {

View File

@ -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]
}