为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) this._artis = new Artis(this.game, true)
} }
get isAvatar () {
return true
}
// 是否是合法面板数据 // 是否是合法面板数据
get isProfile () { get isProfile () {
return ProfileAvatar.isProfile(this) return ProfileAvatar.isProfile(this)
@ -21,7 +25,7 @@ export default class Avatar extends AvatarBase {
// profile.hasData 别名 // profile.hasData 别名
get hasData () { get hasData () {
return ProfileAvatar.isProfile(this) return !!(this.level > 1 || this?.weapon?.name || this?.talent?.a)
} }
get imgs () { get imgs () {

View File

@ -35,7 +35,7 @@ export default class Player extends Base {
get hasProfile () { get hasProfile () {
let ret = false let ret = false
lodash.forEach(this._avatars, (avatar) => { this.forEachAvatar((avatar) => {
if (avatar.isProfile) { if (avatar.isProfile) {
ret = true ret = true
return false return false
@ -92,11 +92,13 @@ export default class Player extends Base {
/** /**
* 重新加载json文件 * 重新加载json文件
* 注意为了性能默认不初始化avatars数据按需初始化
* 如需获取avatar请使用 player.getAvatar() 来进行获取以确保初始化
*/ */
reload () { reload () {
let data = Data.readJSON(this._file, 'root') let data = Data.readJSON(this._file, 'root')
this.setBasicData(data) this.setBasicData(data)
this.setAvatars(data.avatars || []) this.setAvatars(data.avatars || [], true)
if (data._ck) { if (data._ck) {
this._ck = 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) => { lodash.forEach(ds, (avatar) => {
if (!avatar.id) {
return true
}
if (lazy) {
this._avatars[avatar.id] = avatar
} else {
this.setAvatar(avatar) this.setAvatar(avatar)
}
}) })
} }
@ -171,6 +181,21 @@ export default class Player extends Base {
avatar.setAvatar(ds, source) 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角色 // 获取Avatar角色
getAvatar (id, create = false) { getAvatar (id, create = false) {
let char = Character.get(id) let char = Character.get(id)
@ -181,28 +206,31 @@ export default class Player extends Base {
id = avatars['10000005'] ? 10000005 : 10000007 id = avatars['10000005'] ? 10000005 : 10000007
} }
} }
if (!avatars[id] && create) { if (!avatars[id]) {
if (create) {
avatars[id] = Avatar.create({ id }, this.game) avatars[id] = Avatar.create({ id }, this.game)
} } else {
return avatars[id] || false
}
// 异步循环角色
async forEachAvatarAsync (fn) {
for (let id in this._avatars) {
let ret = await fn(this._avatars[id], id)
if (ret === false) {
return false 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 // 循环Avatar
async forEachAvatar (fn) { async forEachAvatar (fn) {
for (let id in this._avatars) { for (let id in this._avatars) {
let avatar = this._avatars[id] let avatar = this.getAvatar(id)
if (avatar && avatar.hasData) { if (avatar && avatar.hasData) {
let ret = fn(this._avatars[id], id) let ret = fn(avatar, id)
ret = Data.isPromise(ret) ? await ret : ret ret = Data.isPromise(ret) ? await ret : ret
if (ret === false) { if (ret === false) {
return false return false

View File

@ -29,10 +29,6 @@ export default class AvatarBase extends Base {
return this.char?.name || '' return this.char?.name || ''
} }
get hasData () {
return !!(this.level > 1 || this?.weapon?.name || this?.talent?.a)
}
get costume () { get costume () {
let costume = this._costume let costume = this._costume
if (lodash.isArray(costume)) { if (lodash.isArray(costume)) {

View File

@ -4,7 +4,6 @@ import { Data } from '#miao'
import { chestInfo } from '../../resources/meta/info/index.js' import { chestInfo } from '../../resources/meta/info/index.js'
import AvatarUtil from './AvatarUtil.js' import AvatarUtil from './AvatarUtil.js'
const MysAvatar = { const MysAvatar = {
checkForce (player, force) { checkForce (player, force) {
let e = player?.e let e = player?.e
@ -18,8 +17,8 @@ const MysAvatar = {
} }
player._info = 0 player._info = 0
player._mys = 0 player._mys = 0
lodash.forEach(player._avatars, (ds) => { player.forEachAvatar((avatar) => {
ds._talent = 0 avatar._talent = 0
}) })
return 2 return 2
}, },
@ -105,12 +104,12 @@ const MysAvatar = {
if (lodash.keys(charIds).length > 8) { if (lodash.keys(charIds).length > 8) {
player.forEachAvatar((avatar) => { player.forEachAvatar((avatar) => {
if (!charIds[avatar.id] && !avatar.isProfile) { 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._mys = new Date() * 1
player.save() player.save()
} }
@ -170,7 +169,7 @@ const MysAvatar = {
getNeedRefreshIds (player, ids, force = 0) { getNeedRefreshIds (player, ids, force = 0) {
let ret = [] let ret = []
if (!ids) { if (!ids) {
ids = lodash.keys(player._avatars) ids = player.getAvatarIds()
} else if (!lodash.isArray(ids)) { } else if (!lodash.isArray(ids)) {
ids = [ids] ids = [ids]
} }