优化部分旅行者相关命令的处理逻辑

This commit is contained in:
yoimiya-kokomi 2022-09-04 14:15:00 +08:00
parent f958455a5b
commit 73c69f784d
5 changed files with 81 additions and 52 deletions

View File

@ -39,7 +39,9 @@ export async function renderAvatar (e, avatar, renderType = 'card') {
if (!charData) return true
let avatars = charData.avatars
char.checkAvatars(avatars)
if (char.isTraveler) {
char = await char.checkAvatars(avatars, uid)
}
avatars = lodash.keyBy(avatars, 'id')
avatar = avatars[char.id] || { id: char.id, name: char.name, detail: false }
}
@ -77,7 +79,7 @@ async function renderCard (e, avatar, renderType = 'card') {
talentMap: { a: '普攻', e: '战技', q: '爆发' },
bg,
custom: char.isCustom,
...getCharacterData(avatar),
...getCharacterData(avatar, char),
ds: char.getData('name,id,title,desc')
}, { e, scale: 1.6, retMsgId: true })
if (msgRes && msgRes.message_id) {
@ -137,7 +139,7 @@ async function getTalent (e, avatars) {
/*
* 获取角色数据
* */
function getCharacterData (avatars) {
function getCharacterData (avatars, char) {
let list = []
let set = {}
let artiEffect = []
@ -177,16 +179,10 @@ function getCharacterData (avatars) {
}
}
if (avatars.id == '10000005') {
avatars.name = '空'
} else if (avatars.id == '10000007') {
avatars.name = '荧'
}
let reliquaries = list[0]
return {
name: avatars.name,
showName: abbr[avatars.name] ? abbr[avatars.name] : avatars.name,
name: char.name,
showName: char.abbr || char.name,
level: Data.def(avatars.lv, avatars.level),
fetter: avatars.fetter,
cons: Data.def(avatars.cons, avatars.actived_constellation_num),

View File

@ -14,11 +14,14 @@ export default class Avatars extends Base {
let abbr = Character.getAbbr()
lodash.forEach(datas, (avatar) => {
let data = Data.getData(avatar, 'id,name,level,star:rarity,cons:actived_constellation_num,fetter')
let char = Character.get(data.id)
data.elem = (avatar.element || '').toLowerCase() || 'anemo'
let char = Character.get({ id: data.id, elem: data.elem })
if (char.isTraveler) {
char.setTraveler(uid)
}
data.face = char.face
data.side = char.side
data.abbr = char.abbr
data.elem = (avatar.element || '').toLowerCase() || 'hydro'
data.weapon = Data.getData(avatar.weapon, 'name,affix:affix_level,level,star:rarity')
data.weapon.abbr = abbr[data?.weapon?.name || ''] || data?.weapon?.name
if (data.star > 5) {

View File

@ -94,29 +94,12 @@ class Character extends Base {
// 获取角色character-img图片
getCardImg (se = false, def = true) {
if (this.name === '旅行者') {
return CharImg.getCardImg(['空', '荧'], se, def)
}
return CharImg.getCardImg(this.name, se, def)
}
checkAvatars (avatars) {
if (!lodash.includes([20000000, 10000005, 10000007], this.id * 1)) {
return
}
let avatarIds = []
if (lodash.isArray(avatars)) {
avatarIds = lodash.map(avatars, (a) => a.id * 1)
} else {
avatarIds = [avatars.id]
}
if (lodash.includes(avatarIds, 10000005)) {
// 空
lodash.extend(this, getMeta('空'))
} else if (lodash.includes(avatarIds, 10000007)) {
// 荧
lodash.extend(this, getMeta('荧'))
}
}
getAvatarTalent (talent = {}, cons = 0, mode = 'level') {
return CharTalent.getAvatarTalent(this.id, talent, cons, mode, this.talentCons)
}
@ -162,13 +145,51 @@ class Character extends Base {
return this._detail
}
getTalentKey (id) {
let meta = this.meta
return meta.talentKey[meta.talentId[id]] || false
setTraveler (uid = '') {
if (this.isTraveler && uid && uid.toString().length === 9) {
redis.set(`genshin:uid-traveler:${uid}`, JSON.stringify({
id: CharId.getTravelerId(this.id),
elem: this.elem
}), { EX: 3600 * 24 * 120 })
}
}
getTalentElem () {
async getTraveler (uid) {
if (this.isTraveler) {
let tData = {}
let uidData = await redis.get(`genshin:uid-traveler:${uid}`)
if (uidData) {
try {
tData = JSON.parse(uidData) || tData
} catch (e) {
}
}
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).toLowerCase() || 'anemo'
})
char.setTraveler(uid)
return char
}
}
return await this.getTraveler(uid)
}
}
@ -183,6 +204,10 @@ Character.get = function (val) {
}
return new Character(id)
}
Character.getAvatar = async function (name, uid) {
let char = Character.get(name)
return await char.getTraveler(uid)
}
Character.getAbbr = function () {
return abbrMap

View File

@ -98,7 +98,8 @@ const CharId = {
return false
}
// 获取字段进行匹配
let { id = '', name = '', elem = '' } = ds
let { id = '', name = '' } = ds
let elem = CharId.getElem(ds.elem || ds.element) || ''
// 直接匹配
if (aliasMap[id || name]) {
return ret(aliasMap[id || name], elem)
@ -106,7 +107,7 @@ const CharId = {
// 尝试解析名字
let nId = CharId.getId(ds.name)
if (nId) {
return ret(ret.id, elem || ret.elem || '')
return ret(nId.id, elem || nId.elem || '')
}
// 无匹配结果
return false
@ -146,6 +147,10 @@ const CharId = {
}
}
return false
},
getTravelerId (id) {
return id * 1 === 10000005 ? 10000005 : 10000007
}
}
export default CharId

View File

@ -3,7 +3,7 @@ import lodash from 'lodash'
import sizeOf from 'image-size'
const CharImg = {
getCardImg (name, se = false, def = true) {
getCardImg (names, se = false, def = true) {
let list = []
let addImg = function (charImgPath, disable = false) {
let dirPath = `./plugins/miao-plugin/resources/${charImgPath}`
@ -21,19 +21,20 @@ const CharImg = {
list.push(`${charImgPath}/${img}`)
})
}
addImg(`character-img/${name}`)
addImg(`character-img/${name}/upload`)
addImg(`character-img/${name}/se`, !se)
const plusPath = './plugins/miao-plugin/resources/miao-res-plus/'
if (fs.existsSync(plusPath)) {
addImg(`miao-res-plus/character-img/${name}`)
addImg(`miao-res-plus/character-img/${name}/se`, !se)
if (!lodash.isArray(names)) {
names = [names]
}
for (let name of names) {
addImg(`character-img/${name}`)
addImg(`character-img/${name}/upload`)
addImg(`character-img/${name}/se`, !se)
const plusPath = './plugins/miao-plugin/resources/miao-res-plus/'
if (fs.existsSync(plusPath)) {
addImg(`miao-res-plus/character-img/${name}`)
addImg(`miao-res-plus/character-img/${name}/se`, !se)
}
}
let img = lodash.sample(list)
if (!img) {
if (def) {
img = '/character-img/default/01.jpg'
@ -41,7 +42,6 @@ const CharImg = {
return false
}
}
let ret = sizeOf(`./plugins/miao-plugin/resources/${img}`)
ret.img = img
ret.mode = ret.width > ret.height ? 'left' : 'bottom'