2022-09-20 11:50:27 +00:00
|
|
|
|
import { Character, Avatar, MysApi } from '../../models/index.js'
|
2022-09-19 05:45:42 +00:00
|
|
|
|
import { Cfg, Common, Profile } from '../../components/index.js'
|
2022-07-23 20:32:10 +00:00
|
|
|
|
import lodash from 'lodash'
|
|
|
|
|
import { segment } from 'oicq'
|
2022-06-25 23:45:43 +00:00
|
|
|
|
|
2022-08-24 01:07:06 +00:00
|
|
|
|
export async function renderAvatar (e, avatar, renderType = 'card') {
|
2022-06-25 23:45:43 +00:00
|
|
|
|
// 如果传递的是名字,则获取
|
2022-09-19 19:16:00 +00:00
|
|
|
|
let uid = e.uid
|
2022-07-23 20:32:10 +00:00
|
|
|
|
if (typeof (avatar) === 'string') {
|
|
|
|
|
let char = Character.get(avatar)
|
2022-06-25 23:45:43 +00:00
|
|
|
|
if (!char) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
return false
|
2022-06-25 23:45:43 +00:00
|
|
|
|
}
|
2022-09-20 11:50:27 +00:00
|
|
|
|
let mys = await MysApi.init(e)
|
|
|
|
|
if (!mys) return true
|
|
|
|
|
uid = mys.uid
|
2022-06-29 23:05:31 +00:00
|
|
|
|
if (char.isCustom) {
|
|
|
|
|
avatar = { id: char.id, name: char.name, detail: false }
|
2022-06-29 20:40:06 +00:00
|
|
|
|
} else {
|
2022-08-18 10:13:42 +00:00
|
|
|
|
let profile = Profile.get(uid, char.id, true)
|
2022-09-18 21:12:03 +00:00
|
|
|
|
if (profile && profile.hasData) {
|
2022-06-29 23:05:31 +00:00
|
|
|
|
// 优先使用Profile数据
|
2022-07-23 20:32:10 +00:00
|
|
|
|
avatar = profile
|
2022-06-29 23:05:31 +00:00
|
|
|
|
} else {
|
|
|
|
|
// 使用Mys数据兜底
|
2022-09-20 11:50:27 +00:00
|
|
|
|
let charData = await mys.getCharacter()
|
2022-07-23 20:32:10 +00:00
|
|
|
|
if (!charData) return true
|
|
|
|
|
|
|
|
|
|
let avatars = charData.avatars
|
2022-09-04 06:15:00 +00:00
|
|
|
|
if (char.isTraveler) {
|
|
|
|
|
char = await char.checkAvatars(avatars, uid)
|
|
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
|
avatars = lodash.keyBy(avatars, 'id')
|
|
|
|
|
avatar = avatars[char.id] || { id: char.id, name: char.name, detail: false }
|
2022-06-29 23:05:31 +00:00
|
|
|
|
}
|
2022-06-25 23:45:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-24 01:07:06 +00:00
|
|
|
|
return await renderCard(e, avatar, renderType)
|
2022-06-25 23:45:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 渲染角色卡片
|
2022-09-18 20:54:01 +00:00
|
|
|
|
async function renderCard (e, ds, renderType = 'card') {
|
|
|
|
|
let char = Character.get(ds)
|
2022-06-25 23:45:43 +00:00
|
|
|
|
if (!char) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
return false
|
2022-06-25 23:45:43 +00:00
|
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
|
let bg = char.getCardImg(Cfg.get('char.se', false))
|
|
|
|
|
if (renderType === 'photo') {
|
|
|
|
|
e.reply(segment.image(process.cwd() + '/plugins/miao-plugin/resources/' + bg.img))
|
|
|
|
|
return true
|
2022-06-25 23:45:43 +00:00
|
|
|
|
}
|
2022-09-18 20:54:01 +00:00
|
|
|
|
let uid = e.uid || (e.targetUser && e.targetUser.uid)
|
|
|
|
|
let data = {}
|
|
|
|
|
let custom = char.isCustom
|
|
|
|
|
if (!custom) {
|
2022-09-20 11:50:27 +00:00
|
|
|
|
let mys = await MysApi.init(e)
|
2022-09-20 19:15:22 +00:00
|
|
|
|
let avatar = new Avatar(ds, uid, mys.isSelfCookie)
|
2022-09-19 19:16:00 +00:00
|
|
|
|
data = avatar.getData('id,name,sName,level,fetter,cons,weapon,elem,artis,artisSet,imgs,dataSourceName,updateTime')
|
2022-09-20 19:15:22 +00:00
|
|
|
|
data.talent = await avatar.getTalent(mys)
|
|
|
|
|
if (data.talent) {
|
2022-09-18 20:54:01 +00:00
|
|
|
|
data.talentMap = ['a', 'e', 'q']
|
|
|
|
|
// 计算皇冠个数
|
|
|
|
|
data.crownNum = lodash.filter(lodash.map(data.talent, (d) => d.original), (d) => d >= 10).length
|
|
|
|
|
}
|
2022-09-23 22:57:42 +00:00
|
|
|
|
} else {
|
|
|
|
|
data = char.getData('id,name,sName')
|
2022-09-18 20:54:01 +00:00
|
|
|
|
}
|
|
|
|
|
let width = 600
|
2022-09-23 22:57:42 +00:00
|
|
|
|
let imgCss = ''
|
|
|
|
|
let scale = 1.2
|
2022-09-18 20:54:01 +00:00
|
|
|
|
if (bg.mode === 'left') {
|
2022-09-23 22:57:42 +00:00
|
|
|
|
const height = 480
|
|
|
|
|
width = height * bg.width / bg.height
|
|
|
|
|
imgCss = `img.bg{width:auto;height:${height}px;}`
|
|
|
|
|
scale = 1.45
|
2022-09-18 20:54:01 +00:00
|
|
|
|
}
|
|
|
|
|
// 渲染图像
|
|
|
|
|
let msgRes = await Common.render('character/character-card', {
|
|
|
|
|
saveId: uid,
|
|
|
|
|
uid,
|
|
|
|
|
bg,
|
2022-09-23 22:57:42 +00:00
|
|
|
|
widthStyle: `<style>html,body,#container{width:${width}px} ${imgCss}</style>`,
|
2022-09-18 20:54:01 +00:00
|
|
|
|
mode: bg.mode,
|
|
|
|
|
custom,
|
|
|
|
|
data
|
2022-09-23 22:57:42 +00:00
|
|
|
|
}, { e, scale, retMsgId: true })
|
2022-09-18 20:54:01 +00:00
|
|
|
|
if (msgRes && msgRes.message_id) {
|
2022-09-24 18:28:25 +00:00
|
|
|
|
// 如果消息发送成功,就将message_id和图片路径存起来,3小时过期
|
|
|
|
|
await redis.set(`miao:original-picture:${msgRes.message_id}`, bg.img, { EX: 3600 * 3 })
|
2022-06-25 23:45:43 +00:00
|
|
|
|
}
|
2022-09-18 20:54:01 +00:00
|
|
|
|
return true
|
2022-06-25 23:45:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 11:50:27 +00:00
|
|
|
|
export async function getAvatarList (e, type, mys) {
|
|
|
|
|
let data = await mys.getCharacter()
|
2022-07-23 20:32:10 +00:00
|
|
|
|
if (!data) return false
|
2022-06-25 23:45:43 +00:00
|
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
|
let avatars = data.avatars
|
2022-06-25 23:45:43 +00:00
|
|
|
|
|
|
|
|
|
if (avatars.length <= 0) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
return false
|
2022-06-25 23:45:43 +00:00
|
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
|
let list = []
|
2022-06-25 23:45:43 +00:00
|
|
|
|
for (let val of avatars) {
|
|
|
|
|
if (type !== false) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
if (!Character.checkWifeType(val.id, type)) {
|
|
|
|
|
continue
|
2022-06-25 23:45:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (val.rarity > 5) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
val.rarity = 5
|
2022-06-25 23:45:43 +00:00
|
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
|
list.push(val)
|
2022-06-25 23:45:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (list.length <= 0) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
return false
|
2022-06-25 23:45:43 +00:00
|
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
|
let sortKey = 'level,fetter,weapon_level,rarity,weapon_rarity,cons,weapon_affix_level'
|
|
|
|
|
list = lodash.orderBy(list, sortKey, lodash.repeat('desc,', sortKey.length).split(','))
|
|
|
|
|
return list
|
2022-09-17 22:19:26 +00:00
|
|
|
|
}
|