miao-plugin/components/profile-data/miao.js

223 lines
5.4 KiB
JavaScript
Raw Normal View History

2022-07-27 18:36:49 +00:00
import fetch from 'node-fetch'
import lodash from 'lodash'
import Character from '../models/Character.js'
import moment from 'moment'
import { artiIdx, artiSetMap, attrMap } from './miao-meta.js'
import cmeta from './enka-char.js'
2022-06-14 22:07:24 +00:00
2022-06-14 22:14:58 +00:00
let Miao = {
2022-07-27 18:36:49 +00:00
key: 'miao',
2022-06-14 22:07:24 +00:00
cd: 1,
2022-07-27 18:36:49 +00:00
async request ({ e, uid, avatar = '', diyCfg, sysCfg }) {
let url = diyCfg?.miaoApi?.url || sysCfg.miaoApi.url
let token = diyCfg?.miaoApi?.token || sysCfg.miaoApi.token
let profileApi = diyCfg?.miaoApi?.listApi || sysCfg.miaoApi.listApi
let api = profileApi({ url, uid, avatar, token })
let data
let req = await fetch(api)
data = await req.json()
2022-06-14 22:07:24 +00:00
if (data.status !== 0) {
2022-07-27 18:36:49 +00:00
e.reply(data.msg || '请求失败')
return false
2022-06-14 22:07:24 +00:00
}
if (!data.uidListData || data.uidListData.length === 0) {
2022-07-27 18:36:49 +00:00
e.reply('请打开游戏内角色展柜的“显示详情”后等待5分钟重新获取面板')
return false
2022-06-14 22:07:24 +00:00
}
2022-07-27 18:36:49 +00:00
return Miao.getData(uid, data)
2022-06-14 22:07:24 +00:00
},
2022-07-27 18:36:49 +00:00
getData (uid, data) {
2022-06-14 22:07:24 +00:00
let ret = {
uid,
chars: {}
}
lodash.forEach({
2022-07-27 18:36:49 +00:00
name: 'nickname',
// avatar: "profilePicture.avatarId",
lv: 'level'
2022-06-14 22:07:24 +00:00
}, (src, key) => {
2022-07-27 18:36:49 +00:00
ret[key] = lodash.get(data, src, '')
2022-06-14 22:07:24 +00:00
})
lodash.forEach(data.uidListData, (ds) => {
2022-07-27 18:36:49 +00:00
let char = Miao.getAvatar(ds)
ret.chars[char.id] = char
2022-06-14 22:07:24 +00:00
})
2022-07-27 18:36:49 +00:00
return ret
2022-06-14 22:07:24 +00:00
},
2022-07-27 18:36:49 +00:00
getAvatar (ds) {
let char = Character.get(ds.usernameid)
let now = moment()
2022-06-14 22:07:24 +00:00
return {
id: ds.usernameid,
2022-07-27 18:36:49 +00:00
name: char ? char.name : '',
dataSource: 'miao-pre',
updateTime: now.format('YYYY-MM-DD HH:mm:ss'),
2022-06-14 22:07:24 +00:00
lv: ds.level
2022-07-27 18:36:49 +00:00
}
2022-06-14 22:07:24 +00:00
},
2022-07-27 18:36:49 +00:00
async getCharData (uid, ds, saveCharData, { diyCfg = {}, sysCfg = {} }) {
if (ds.dataSource !== 'miao-pre' || !ds.id) {
return ds
2022-06-14 22:07:24 +00:00
}
try {
2022-07-27 18:36:49 +00:00
let url = diyCfg?.miaoApi?.url || sysCfg.miaoApi.url
let token = diyCfg?.miaoApi?.token || sysCfg.miaoApi.token
let profileApi = diyCfg?.miaoApi?.detailApi || sysCfg.miaoApi.detailApi
let api = profileApi({ url, token, uid, avatar: ds.id })
let req = await fetch(api)
let data = await req.json()
2022-06-14 22:07:24 +00:00
if (data.status === 0 && data.uidData) {
2022-07-27 18:36:49 +00:00
data = Miao.getAvatarDetail(data)
2022-06-14 22:07:24 +00:00
if (data) {
2022-07-27 18:36:49 +00:00
saveCharData(uid, data)
return data
2022-06-14 22:07:24 +00:00
}
}
2022-07-27 18:36:49 +00:00
return ds
2022-06-14 22:07:24 +00:00
} catch (err) {
2022-07-27 18:36:49 +00:00
console.log(err)
return ds
2022-06-14 22:07:24 +00:00
}
},
2022-07-27 18:36:49 +00:00
getAvatarDetail (data) {
let ds = data.uidData
let char = Character.get(ds.id)
let now = moment()
2022-06-14 22:07:24 +00:00
return {
id: ds.id,
2022-07-27 18:36:49 +00:00
name: char ? char.name : '',
dataSource: 'miao',
updateTime: now.format('YYYY-MM-DD HH:mm:ss'),
2022-06-14 22:07:24 +00:00
lv: ds.level,
fetter: ds.fetterLevel,
2022-06-14 22:14:58 +00:00
attr: Miao.getAttr(data.uidDataCombatValue),
weapon: Miao.getWeapon(ds.weapon),
artis: Miao.getArtifact(data.uidDataByReliquary),
2022-06-14 22:07:24 +00:00
cons: ds.constellationNum,
2022-06-14 22:14:58 +00:00
talent: Miao.getTalent(char.id, ds.skill),
2022-06-14 22:07:24 +00:00
_priority: 10
2022-07-27 18:36:49 +00:00
}
2022-06-14 22:07:24 +00:00
},
2022-07-27 18:36:49 +00:00
getAttr (data) {
let ret = {}
2022-06-14 22:07:24 +00:00
lodash.forEach({
2022-07-27 18:36:49 +00:00
atk: 'attack',
atkBase: 'baseATK',
hp: 'health',
hpBase: 'baseHP',
def: 'defense',
defBase: 'baseDEF',
mastery: 'elementMastery',
2022-06-14 22:07:24 +00:00
cRate: {
2022-07-27 18:36:49 +00:00
src: 'critRate',
2022-06-14 22:07:24 +00:00
pct: true
},
cDmg: {
2022-07-27 18:36:49 +00:00
src: 'critDamage',
2022-06-14 22:07:24 +00:00
pct: true
},
hInc: {
2022-07-27 18:36:49 +00:00
src: 'heal',
2022-06-14 22:07:24 +00:00
pct: true
},
recharge: {
2022-07-27 18:36:49 +00:00
src: 'recharge',
2022-06-14 22:07:24 +00:00
pct: true
}
}, (cfg, key) => {
if (!lodash.isObject(cfg)) {
2022-07-27 18:36:49 +00:00
cfg = { src: cfg }
2022-06-14 22:07:24 +00:00
}
2022-07-27 18:36:49 +00:00
let val = data[cfg.src] || 0
2022-06-14 22:07:24 +00:00
if (cfg.pct) {
val = val * 100
}
2022-07-27 18:36:49 +00:00
ret[key] = val
})
let maxDmg = 0;
let hurt = data.addHurt || {}
lodash.forEach('fire,elec,water,grass,wind,rock,ice'.split(','), (key) => {
maxDmg = Math.max(hurt[key] * 100, maxDmg)
2022-06-14 22:07:24 +00:00
})
2022-07-27 18:36:49 +00:00
ret.dmgBonus = maxDmg
ret.phyBonus = hurt.physical * 100
return ret
2022-06-14 22:07:24 +00:00
},
2022-07-27 18:36:49 +00:00
getWeapon (weapon) {
2022-06-14 22:07:24 +00:00
return {
name: weapon.name,
star: weapon.rank,
level: weapon.level,
promote: weapon.promoteLevel,
affix: (weapon.affixLevel || 0) + 1
}
},
2022-07-27 18:36:49 +00:00
getArtifact (data) {
let ret = {}
2022-06-14 22:07:24 +00:00
let get = function (d) {
if (!d) {
2022-07-27 18:36:49 +00:00
return []
2022-06-14 22:07:24 +00:00
}
2022-07-27 18:36:49 +00:00
let name = d.name
name = name.replace('FIGHT_PROP_', '')
2022-06-14 22:07:24 +00:00
if (!attrMap[name]) {
2022-07-27 18:36:49 +00:00
return []
2022-06-14 22:07:24 +00:00
}
2022-07-27 18:36:49 +00:00
let value = d.value
2022-06-14 22:07:24 +00:00
if (value && value < 1) {
2022-07-27 18:36:49 +00:00
value = value * 100
2022-06-14 22:07:24 +00:00
}
2022-07-27 18:36:49 +00:00
return [attrMap[name], value]
2022-06-14 22:07:24 +00:00
}
lodash.forEach(data, (ds) => {
2022-07-27 18:36:49 +00:00
let sub = ds.appendAffix || []
let idx = artiIdx[ds.type]
2022-06-14 22:07:24 +00:00
if (!idx) {
2022-07-27 18:36:49 +00:00
return
2022-06-14 22:07:24 +00:00
}
ret[`arti${idx}`] = {
name: ds.name,
2022-07-27 18:36:49 +00:00
set: artiSetMap[ds.name] || '',
2022-06-14 22:07:24 +00:00
level: ds.level,
main: get(ds.mainAffix),
attrs: [
get(sub[0]),
get(sub[1]),
get(sub[2]),
get(sub[3])
]
}
})
2022-07-27 18:36:49 +00:00
return ret
2022-06-14 22:07:24 +00:00
},
2022-07-27 18:36:49 +00:00
getTalent (charid, data = {}) {
let cm = cmeta[charid] || {}
let cn = cm.Skills || {}
let idx = 1
let idxMap = { 0: 'a', 1: 'e', 2: 'q', a: 'a', s: 'e', e: 'q' }
2022-06-14 22:07:24 +00:00
lodash.forEach(cn, (n, id) => {
2022-07-27 18:36:49 +00:00
let nRet = /skill_(\w)/.exec(n.toLowerCase())
idxMap[id] = nRet && nRet[1] ? idxMap[nRet[1]] : idxMap[idx]
idx++
})
2022-06-14 22:07:24 +00:00
2022-07-27 18:36:49 +00:00
let ret = {}
2022-06-14 22:07:24 +00:00
lodash.forEach(data, (ds) => {
2022-07-27 18:36:49 +00:00
let key = idxMap[ds.id]
2022-06-14 22:07:24 +00:00
ret[key] = {
level_original: ds.level,
level_current: ds.level
}
})
2022-07-27 18:36:49 +00:00
return ret
}
2022-06-14 22:07:24 +00:00
}
2022-07-27 18:36:49 +00:00
export default Miao