2022-07-23 20:32:10 +00:00
|
|
|
import lodash from 'lodash'
|
2022-08-18 10:13:42 +00:00
|
|
|
import Base from './Base.js'
|
|
|
|
import { Data } from '../components/index.js'
|
2022-09-03 21:08:57 +00:00
|
|
|
import CharImg from './character-lib/CharImg.js'
|
|
|
|
import CharTalent from './character-lib/CharTalent.js'
|
|
|
|
import CharId from './character-lib/CharId.js'
|
2022-07-23 20:32:10 +00:00
|
|
|
|
|
|
|
const _path = process.cwd()
|
2022-09-03 21:08:57 +00:00
|
|
|
let { abbrMap, wifeMap, idSort } = CharId
|
2022-06-29 23:05:31 +00:00
|
|
|
|
2022-03-26 08:21:44 +00:00
|
|
|
class Character extends Base {
|
2022-09-03 21:08:57 +00:00
|
|
|
constructor ({ id, name = '', elem = '' }) {
|
2022-07-23 20:32:10 +00:00
|
|
|
super()
|
2022-09-03 21:08:57 +00:00
|
|
|
let uuid = CharId.isTraveler(id) ? `character:${id}:${elem || 'anemo'}` : `character:${id}`
|
|
|
|
// 检查缓存
|
|
|
|
let cacheObj = Base.get(uuid)
|
|
|
|
if (cacheObj) {
|
|
|
|
return cacheObj
|
2022-04-10 05:36:31 +00:00
|
|
|
}
|
2022-09-03 21:08:57 +00:00
|
|
|
// 设置数据
|
|
|
|
this.id = id
|
2022-07-23 20:32:10 +00:00
|
|
|
this.name = name
|
2022-09-03 21:08:57 +00:00
|
|
|
this._uuid = uuid
|
|
|
|
if (!this.isCustom) {
|
|
|
|
let meta = getMeta(name)
|
|
|
|
this.meta = meta
|
|
|
|
for (let key of this._dataKey.split(',')) {
|
|
|
|
if (key === 'elem') {
|
|
|
|
this.elem = elem || meta.elem
|
|
|
|
} else {
|
|
|
|
this[key] = meta[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.meta = {}
|
2022-04-10 05:36:31 +00:00
|
|
|
}
|
2022-09-03 21:08:57 +00:00
|
|
|
return this._expire()
|
2022-03-27 20:58:02 +00:00
|
|
|
}
|
2022-04-08 21:52:05 +00:00
|
|
|
|
2022-09-03 21:08:57 +00:00
|
|
|
_dataKey = 'id,name,abbr,title,star,elem,allegiance,weapon,birthday,astro,cncv,jpcv,ver,desc,talentCons'
|
|
|
|
|
|
|
|
// 是否为自定义角色
|
|
|
|
get isCustom () {
|
|
|
|
return !/[12]0\d{6}/.test(this.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 是否是旅行者
|
|
|
|
get isTraveler () {
|
|
|
|
return CharId.isTraveler(this.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取武器类型
|
2022-08-31 18:26:36 +00:00
|
|
|
get weaponType () {
|
2022-08-24 07:05:47 +00:00
|
|
|
const map = {
|
|
|
|
sword: '单手剑',
|
|
|
|
catalyst: '法器',
|
|
|
|
bow: '弓',
|
|
|
|
claymore: '双手剑',
|
|
|
|
polearm: '长柄武器'
|
|
|
|
}
|
|
|
|
let weaponType = this.weapon || ''
|
|
|
|
return map[weaponType.toLowerCase()] || ''
|
|
|
|
}
|
|
|
|
|
2022-09-03 21:08:57 +00:00
|
|
|
// 获取元素名称
|
|
|
|
get elemName () {
|
|
|
|
return CharId.getElemName(this.elem)
|
2022-08-24 07:05:47 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 21:08:57 +00:00
|
|
|
// 获取头像
|
|
|
|
get face () {
|
|
|
|
return this.getImgs().face
|
2022-08-24 07:05:47 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 21:08:57 +00:00
|
|
|
// 获取侧脸图像
|
|
|
|
get side () {
|
|
|
|
return this.getImgs().side
|
|
|
|
}
|
2022-03-27 20:58:02 +00:00
|
|
|
|
2022-09-03 21:08:57 +00:00
|
|
|
// 获取详情数据
|
|
|
|
get detail () {
|
|
|
|
return this.getDetail()
|
|
|
|
}
|
2022-04-08 21:52:05 +00:00
|
|
|
|
2022-09-03 21:08:57 +00:00
|
|
|
// 获取基础数据
|
|
|
|
get baseAttr () {
|
|
|
|
return this.meta.baseAttr
|
|
|
|
}
|
2022-04-08 21:52:05 +00:00
|
|
|
|
2022-09-03 21:08:57 +00:00
|
|
|
// 获取成长数据
|
|
|
|
get growAttr () {
|
|
|
|
return this.meta.growAttr
|
|
|
|
}
|
2022-04-08 21:52:05 +00:00
|
|
|
|
2022-09-03 21:08:57 +00:00
|
|
|
// 获取角色character-img图片
|
|
|
|
getCardImg (se = false, def = true) {
|
|
|
|
return CharImg.getCardImg(this.name, se, def)
|
2022-04-08 21:52:05 +00:00
|
|
|
}
|
2022-04-10 05:36:31 +00:00
|
|
|
|
2022-08-31 18:26:36 +00:00
|
|
|
checkAvatars (avatars) {
|
2022-04-10 05:36:31 +00:00
|
|
|
if (!lodash.includes([20000000, 10000005, 10000007], this.id * 1)) {
|
2022-07-23 20:32:10 +00:00
|
|
|
return
|
2022-04-10 05:36:31 +00:00
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
let avatarIds = []
|
2022-04-10 05:36:31 +00:00
|
|
|
if (lodash.isArray(avatars)) {
|
2022-07-23 20:32:10 +00:00
|
|
|
avatarIds = lodash.map(avatars, (a) => a.id * 1)
|
2022-04-10 05:36:31 +00:00
|
|
|
} else {
|
2022-07-23 20:32:10 +00:00
|
|
|
avatarIds = [avatars.id]
|
2022-04-10 05:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lodash.includes(avatarIds, 10000005)) {
|
|
|
|
// 空
|
2022-07-23 20:32:10 +00:00
|
|
|
lodash.extend(this, getMeta('空'))
|
2022-04-10 05:36:31 +00:00
|
|
|
} else if (lodash.includes(avatarIds, 10000007)) {
|
|
|
|
// 荧
|
2022-07-23 20:32:10 +00:00
|
|
|
lodash.extend(this, getMeta('荧'))
|
2022-04-10 05:36:31 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-05 07:53:13 +00:00
|
|
|
|
2022-08-31 18:26:36 +00:00
|
|
|
getAvatarTalent (talent = {}, cons = 0, mode = 'level') {
|
2022-09-03 21:08:57 +00:00
|
|
|
return CharTalent.getAvatarTalent(this.id, talent, cons, mode, this.talentCons)
|
2022-06-29 20:40:06 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 18:26:36 +00:00
|
|
|
checkWifeType (type) {
|
2022-07-23 20:32:10 +00:00
|
|
|
return !!wifeMap[type][this.id]
|
2022-06-29 23:05:31 +00:00
|
|
|
}
|
2022-08-31 18:26:36 +00:00
|
|
|
|
|
|
|
checkCostume (id) {
|
2022-09-03 21:08:57 +00:00
|
|
|
let costume = this.meta?.costume || []
|
|
|
|
return costume.includes(id * 1)
|
2022-08-31 18:26:36 +00:00
|
|
|
}
|
2022-09-01 20:09:59 +00:00
|
|
|
|
2022-09-03 21:08:57 +00:00
|
|
|
// 获取Character图像资源
|
|
|
|
getImgs (costume = '', elem = '') {
|
|
|
|
let cId = this.checkCostume(costume) ? '2' : ''
|
|
|
|
if (this._imgs) {
|
|
|
|
return this._imgs
|
2022-09-01 20:09:59 +00:00
|
|
|
}
|
2022-09-03 21:08:57 +00:00
|
|
|
this._imgs = CharImg.getImgs(this.name, cId, this.isTraveler ? this.elem : '')
|
|
|
|
return this._imgs
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取详情数据
|
|
|
|
getDetail (elem = '') {
|
|
|
|
if (this._detail) {
|
|
|
|
return this._detail
|
2022-09-01 20:09:59 +00:00
|
|
|
}
|
2022-09-03 21:08:57 +00:00
|
|
|
if (this.isCustom) {
|
|
|
|
return {}
|
2022-09-01 20:09:59 +00:00
|
|
|
}
|
2022-09-03 21:08:57 +00:00
|
|
|
const path = `${_path}/plugins/miao-plugin/resources/meta/character/${this.name}`
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (this.isTraveler) {
|
|
|
|
this._detail = Data.readJSON(`${path}/${this.elem}`, 'detail.json') || {}
|
|
|
|
} else {
|
|
|
|
this._detail = Data.readJSON(`${path}`, 'detail.json') || {}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e)
|
2022-09-01 20:09:59 +00:00
|
|
|
}
|
2022-09-03 21:08:57 +00:00
|
|
|
return this._detail
|
|
|
|
}
|
|
|
|
|
|
|
|
getTalentKey (id) {
|
|
|
|
let meta = this.meta
|
|
|
|
return meta.talentKey[meta.talentId[id]] || false
|
|
|
|
}
|
|
|
|
|
|
|
|
getTalentElem () {
|
|
|
|
|
2022-09-01 20:09:59 +00:00
|
|
|
}
|
2022-03-26 08:21:44 +00:00
|
|
|
}
|
2022-03-24 13:14:22 +00:00
|
|
|
|
2022-04-10 05:36:31 +00:00
|
|
|
let getMeta = function (name) {
|
2022-07-23 20:32:10 +00:00
|
|
|
return Data.readJSON(`${_path}/plugins/miao-plugin/resources/meta/character/${name}/`, 'data.json') || {}
|
2022-04-10 05:36:31 +00:00
|
|
|
}
|
2022-03-24 14:40:59 +00:00
|
|
|
|
|
|
|
Character.get = function (val) {
|
2022-09-03 21:08:57 +00:00
|
|
|
let id = CharId.getId(val)
|
|
|
|
if (!id) {
|
2022-07-23 20:32:10 +00:00
|
|
|
return false
|
2022-06-26 00:55:14 +00:00
|
|
|
}
|
2022-09-03 21:08:57 +00:00
|
|
|
return new Character(id)
|
2022-07-23 20:32:10 +00:00
|
|
|
}
|
2022-04-10 05:36:31 +00:00
|
|
|
|
2022-04-04 21:36:44 +00:00
|
|
|
Character.getAbbr = function () {
|
2022-07-23 20:32:10 +00:00
|
|
|
return abbrMap
|
|
|
|
}
|
|
|
|
|
|
|
|
Character.checkWifeType = function (charid, type) {
|
2022-07-30 20:20:10 +00:00
|
|
|
return !!wifeMap[type][charid]
|
2022-04-04 21:36:44 +00:00
|
|
|
}
|
|
|
|
|
2022-05-04 20:53:20 +00:00
|
|
|
Character.sortIds = function (arr) {
|
2022-07-23 20:32:10 +00:00
|
|
|
return arr.sort((a, b) => (idSort[a] || 300) - (idSort[b] || 300))
|
2022-05-04 20:53:20 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
export default Character
|