miao-plugin/apps/stat.js

502 lines
12 KiB
JavaScript
Raw Normal View History

/*
* 胡桃数据库的统计
*
* */
2022-07-23 20:32:10 +00:00
import { HutaoApi, Character } from '../components/models.js'
import { Cfg } from '../components/index.js'
import lodash from 'lodash'
import fs from 'fs'
import Common from '../components/Common.js'
import Abyss from '../components/models/Abyss.js'
import Avatars from '../components/models/Avatars.js'
2022-07-23 20:32:10 +00:00
export async function consStat (e, { render }) {
if (Cfg.isDisable(e, 'wiki.stat')) {
2022-07-23 20:32:10 +00:00
return
2022-04-09 21:33:21 +00:00
}
2022-07-23 20:32:10 +00:00
let consData = await HutaoApi.getCons()
2022-08-06 22:36:05 +00:00
let overview = await HutaoApi.getOverview()
if (!consData) {
2022-07-23 20:32:10 +00:00
e.reply('角色持有数据获取失败,请稍后重试~')
return true
}
2022-07-23 20:32:10 +00:00
let msg = e.msg
2022-07-23 20:32:10 +00:00
let mode = /持有/.test(msg) ? 'char' : 'cons'
2022-07-23 20:32:10 +00:00
let conNum = -1
if (mode === 'cons') {
lodash.forEach([/0|零/, /1|一/, /2|二/, /3|三/, /4|四/, /5|五/, /6|六|满/], (reg, idx) => {
if (reg.test(msg)) {
2022-07-23 20:32:10 +00:00
conNum = idx
return false
}
})
}
if (!consData && !consData.data) {
2022-07-23 20:32:10 +00:00
return true
}
2022-07-23 20:32:10 +00:00
let data = consData.data
2022-07-23 20:32:10 +00:00
let Lumine = lodash.filter(data, (ds) => ds.avatar === 10000007)[0] || {}
let Aether = lodash.filter(data, (ds) => ds.avatar === 10000005)[0] || {}
2022-07-23 20:32:10 +00:00
Lumine.holdingRate = (1 - Aether.holdingRate) || Lumine.holdingRate
2022-07-23 20:32:10 +00:00
let ret = []
lodash.forEach(data, (ds) => {
2022-07-23 20:32:10 +00:00
let char = Character.get(ds.avatar)
let data = {
name: char.name || ds.avatar,
star: char.star || 3,
hold: ds.holdingRate
2022-07-23 20:32:10 +00:00
}
2022-07-23 20:32:10 +00:00
if (mode === 'char') {
data.cons = lodash.map(ds.rate, (c) => {
2022-07-23 20:32:10 +00:00
c.value = c.value * ds.holdingRate
return c
})
} else {
data.cons = ds.rate
}
data.cons = lodash.sortBy(data.cons, ['id'])
2022-07-23 20:32:10 +00:00
ret.push(data)
})
if (conNum > -1) {
2022-07-23 20:32:10 +00:00
ret = lodash.sortBy(ret, [`cons[${conNum}].value`])
ret.reverse()
} else {
2022-07-23 20:32:10 +00:00
ret = lodash.sortBy(ret, ['hold'])
}
2022-07-23 20:32:10 +00:00
// 渲染图像
return await Common.render('stat/character', {
chars: ret,
abbr: Character.getAbbr(),
2022-07-23 20:32:10 +00:00
mode,
conNum,
2022-08-06 22:36:05 +00:00
totalCount: overview?.data?.totalPlayerCount || 0,
lastUpdate: consData.lastUpdate,
pct: function (num) {
2022-07-23 20:32:10 +00:00
return (num * 100).toFixed(2)
}
}, { e, render, scale: 1.5 })
}
2022-07-23 20:32:10 +00:00
export async function abyssPct (e, { render }) {
if (Cfg.isDisable(e, 'wiki.stat')) {
2022-07-23 20:32:10 +00:00
return
2022-04-09 21:33:21 +00:00
}
2022-07-23 20:32:10 +00:00
let mode = /使用/.test(e.msg) ? 'use' : 'pct'
let modeName
let abyssData
let modeMulti = 1
2022-07-23 20:32:10 +00:00
if (mode === 'use') {
modeName = '使用率'
abyssData = await HutaoApi.getAbyssUse()
} else {
2022-07-23 20:32:10 +00:00
modeName = '出场率'
abyssData = await HutaoApi.getAbyssPct()
modeMulti = 8
}
2022-08-06 22:36:05 +00:00
let overview = await HutaoApi.getOverview()
if (!abyssData) {
2022-07-23 20:32:10 +00:00
e.reply(`深渊${modeName}数据获取失败,请稍后重试~`)
return true
}
2022-07-23 20:32:10 +00:00
let ret = []
let chooseFloor = -1
let msg = e.msg
const floorName = {
2022-07-23 20:32:10 +00:00
12: '十二层',
11: '十一层',
10: '十层',
9: '九层'
}
// 匹配深渊楼层信息
lodash.forEach(floorName, (cn, num) => {
2022-07-23 20:32:10 +00:00
let reg = new RegExp(`${cn}|${num}`)
if (reg.test(msg)) {
2022-07-23 20:32:10 +00:00
chooseFloor = num
return false
}
2022-07-23 20:32:10 +00:00
})
2022-07-23 20:32:10 +00:00
let data = abyssData.data
data = lodash.sortBy(data, 'floor')
data = data.reverse()
lodash.forEach(data, (floorData) => {
2022-07-23 20:32:10 +00:00
let avatars = []
lodash.forEach(floorData.avatarUsage, (ds) => {
2022-07-23 20:32:10 +00:00
let char = Character.get(ds.id)
if (char) {
avatars.push({
name: char.name,
star: char.star,
value: ds.value * modeMulti
})
}
})
2022-07-23 20:32:10 +00:00
avatars = lodash.sortBy(avatars, 'value', ['asc'])
avatars.reverse()
if (chooseFloor === -1) {
2022-07-23 20:32:10 +00:00
avatars = avatars.slice(0, 14)
}
ret.push({
floor: floorData.floor,
avatars
2022-07-23 20:32:10 +00:00
})
})
2022-07-23 20:32:10 +00:00
return await Common.render('stat/abyss-pct', {
abyss: ret,
floorName,
chooseFloor,
mode,
modeName,
2022-08-06 22:36:05 +00:00
totalCount: overview?.data?.collectedPlayerCount || 0,
lastUpdate: abyssData.lastUpdate
2022-07-23 20:32:10 +00:00
}, { e, render, scale: 1.5 })
}
2022-07-23 20:32:10 +00:00
async function getTalentData (e, isUpdate = false) {
// 技能查询缓存
2022-07-23 20:32:10 +00:00
let cachePath = './data/cache/'
if (!fs.existsSync(cachePath)) {
2022-07-23 20:32:10 +00:00
fs.mkdirSync(cachePath)
}
2022-07-23 20:32:10 +00:00
cachePath += 'talentList/'
if (!fs.existsSync(cachePath)) {
2022-07-23 20:32:10 +00:00
fs.mkdirSync(cachePath)
}
2022-07-23 20:32:10 +00:00
let avatarRet = []
let uid = e.selfUser.uid
2022-07-23 20:32:10 +00:00
let hasCache = await redis.get(`cache:uid-talent-new:${uid}`)
if (hasCache) {
// 有缓存优先使用缓存
2022-07-23 20:32:10 +00:00
let jsonRet = fs.readFileSync(cachePath + `${uid}.json`, 'utf8')
avatarRet = JSON.parse(jsonRet)
return avatarRet
} else if (!isUpdate) {
2022-07-23 20:32:10 +00:00
e.noReplyTalentList = true
await global.YunzaiApps.mysInfo.talentList(e)
return await getTalentData(e, true)
}
2022-07-23 20:32:10 +00:00
return false
}
2022-07-23 20:32:10 +00:00
export async function abyssTeam (e, { render }) {
if (Common.todoV3(e)) {
return true
}
let MysApi = await e.getMysApi({
2022-07-23 20:32:10 +00:00
auth: 'cookie', // 所有用户均可查询
targetType: 'self', // 被查询用户可以是任意用户
cookieType: 'self' // cookie可以是任意可用cookie
})
if (!MysApi || !MysApi.selfUser || !MysApi.selfUser.uid) {
2022-07-23 20:32:10 +00:00
return true
}
2022-07-23 20:32:10 +00:00
let abyssData = await HutaoApi.getAbyssTeam()
if (!abyssData || !abyssData.data) {
2022-07-23 20:32:10 +00:00
e.reply('深渊组队数据获取失败,请稍后重试~')
return true
}
2022-07-23 20:32:10 +00:00
abyssData = abyssData.data
let talentData = await getTalentData(e)
if (!talentData || talentData.length === 0) {
2022-07-23 20:32:10 +00:00
e.reply('暂时未能获得角色的练度信息,请使用【#练度统计】命令尝试手工获取...')
return true
}
2022-07-23 20:32:10 +00:00
let avatarRet = {}
let data = {}
2022-07-23 20:32:10 +00:00
let noAvatar = {}
lodash.forEach(talentData, (avatar) => {
avatarRet[avatar.id] = Math.min(avatar.level, avatar.weapon_level) * 100 + Math.max(avatar.a_original, avatar.e_original, avatar.q_original) * 1000
2022-07-23 20:32:10 +00:00
})
let getTeamCfg = (str) => {
2022-07-23 20:32:10 +00:00
let teams = str.split(',')
teams.sort()
let teamMark = 0
lodash.forEach(teams, (a) => {
if (!avatarRet[a]) {
2022-07-23 20:32:10 +00:00
teamMark = -1
noAvatar[a] = true
}
if (teamMark !== -1) {
2022-07-23 20:32:10 +00:00
teamMark += avatarRet[a] * 1
}
})
if (teamMark === -1) {
2022-07-23 20:32:10 +00:00
teamMark = 1
}
return {
2022-07-23 20:32:10 +00:00
key: teams.join(','),
mark: teamMark
2022-07-23 20:32:10 +00:00
}
}
let hasSame = function (team1, team2) {
for (let idx = 0; idx < team1.length; idx++) {
if (team2.includes(team1[idx])) {
2022-07-23 20:32:10 +00:00
return true
}
}
2022-07-23 20:32:10 +00:00
return false
}
lodash.forEach(abyssData, (ds) => {
2022-07-23 20:32:10 +00:00
let floor = ds.level.floor
if (!data[floor]) {
data[floor] = {
up: {},
down: {},
teams: []
2022-07-23 20:32:10 +00:00
}
}
lodash.forEach(ds.teams, (ds) => {
lodash.forEach(['up', 'down'], (halfKey) => {
2022-07-23 20:32:10 +00:00
let teamCfg = getTeamCfg(ds.id[`${halfKey}Half`])
if (teamCfg) {
if (!data[floor][halfKey][teamCfg.key]) {
data[floor][halfKey][teamCfg.key] = {
count: 0,
mark: 0,
hasTeam: teamCfg.mark > 1
2022-07-23 20:32:10 +00:00
}
}
2022-07-23 20:32:10 +00:00
data[floor][halfKey][teamCfg.key].count += ds.value
data[floor][halfKey][teamCfg.key].mark += ds.value * teamCfg.mark
}
})
2022-07-23 20:32:10 +00:00
})
2022-07-23 20:32:10 +00:00
let temp = []
lodash.forEach(['up', 'down'], (halfKey) => {
lodash.forEach(data[floor][halfKey], (ds, team) => {
temp.push({
team,
2022-07-23 20:32:10 +00:00
teamArr: team.split(','),
half: halfKey,
count: ds.count,
mark: ds.mark,
mark2: 1,
hasTeam: ds.hasTeam
})
})
2022-07-23 20:32:10 +00:00
temp = lodash.sortBy(temp, 'mark')
data[floor].teams = temp.reverse()
})
})
2022-07-23 20:32:10 +00:00
let ret = {}
lodash.forEach(data, (floorData, floor) => {
ret[floor] = {}
2022-07-23 20:32:10 +00:00
let ds = ret[floor]
lodash.forEach(floorData.teams, (t1) => {
if (t1.mark2 <= 0) {
2022-07-23 20:32:10 +00:00
return
}
lodash.forEach(floorData.teams, (t2) => {
if (t1.mark2 <= 0) {
2022-07-23 20:32:10 +00:00
return false
}
if (t1.half === t2.half || t2.mark2 <= 0) {
2022-07-23 20:32:10 +00:00
return
}
2022-07-23 20:32:10 +00:00
let teamKey = t1.half === 'up' ? (t1.team + '+' + t2.team) : (t2.team + '+' + t1.team)
if (ds[teamKey]) {
2022-07-23 20:32:10 +00:00
return
}
if (hasSame(t1.teamArr, t2.teamArr)) {
2022-07-23 20:32:10 +00:00
return
}
ds[teamKey] = {
2022-07-23 20:32:10 +00:00
up: t1.half === 'up' ? t1 : t2,
down: t1.half === 'up' ? t2 : t1,
count: Math.min(t1.count, t2.count),
mark: t1.hasTeam && t2.hasTeam ? t1.mark + t2.mark : t1.count + t2.count // 如果不存在组队则进行评分惩罚
}
2022-07-23 20:32:10 +00:00
t1.mark2--
t2.mark2--
return false
})
if (lodash.keys(ds).length >= 20) {
2022-07-23 20:32:10 +00:00
return false
}
})
2022-07-23 20:32:10 +00:00
})
lodash.forEach(ret, (ds, floor) => {
2022-07-23 20:32:10 +00:00
ds = lodash.sortBy(lodash.values(ds), 'mark')
ds = ds.reverse()
ds = ds.slice(0, 4)
lodash.forEach(ds, (team) => {
2022-07-23 20:32:10 +00:00
team.up.teamArr = Character.sortIds(team.up.teamArr)
team.down.teamArr = Character.sortIds(team.down.teamArr)
})
2022-07-23 20:32:10 +00:00
ret[floor] = ds
})
2022-07-23 20:32:10 +00:00
let avatarMap = {}
lodash.forEach(talentData, (ds) => {
avatarMap[ds.id] = {
id: ds.id,
name: ds.name,
star: ds.rarity,
level: ds.level,
cons: ds.cons
}
})
lodash.forEach(noAvatar, (d, id) => {
2022-07-23 20:32:10 +00:00
let char = Character.get(id)
avatarMap[id] = {
id,
name: char.name,
star: char.star,
level: 0,
2022-07-23 20:32:10 +00:00
cons: 0
}
})
2022-07-23 20:32:10 +00:00
return await Common.render('stat/abyss-team', {
teams: ret,
2022-07-23 20:32:10 +00:00
avatars: avatarMap
}, { e, render, scale: 1.5 })
}
export async function uploadData (e, { render }) {
let isMatch = /^#(喵喵|上传)深渊(数据)?$/.test(e.original_msg || e.msg || '')
if (!Cfg.get('wiki.abyss', false) && !isMatch) {
return false
}
let MysApi = await e.getMysApi({
auth: 'all',
2022-07-23 20:32:10 +00:00
targetType: 'self',
cookieType: 'all',
action: '获取深渊信息'
2022-07-23 20:32:10 +00:00
})
2022-08-06 22:42:14 +00:00
if (!MysApi || !MysApi.isSelfCookie) {
if (isMatch) {
e.reply(`请绑定ck后再使用${e.original_msg}`)
}
return false
}
2022-07-23 20:32:10 +00:00
let ret = {}
let uid = e.selfUser.uid
2022-08-06 22:36:05 +00:00
let resDetail, resAbyss, overview
try {
resAbyss = await MysApi.getSpiralAbyss(1)
2022-08-06 22:36:05 +00:00
overview = await HutaoApi.getOverview()
if (resAbyss.floors.length > 0 && !await Avatars.hasTalentCache(uid)) {
e.reply('正在获取用户信息,请稍候...')
}
resDetail = await MysApi.getCharacter()
if (!resDetail || !resAbyss || !resDetail.avatars || resDetail.avatars.length <= 3) {
2022-07-23 20:32:10 +00:00
e.reply('角色信息获取失败')
return true
}
2022-07-23 20:32:10 +00:00
delete resDetail._res
delete resAbyss._res
ret = await HutaoApi.uploadData({
uid,
resDetail,
resAbyss
2022-07-23 20:32:10 +00:00
})
} catch (err) {
// console.log(err);
}
if (ret && ret.retcode === 0) {
let stat = []
if (ret.data) {
if (resAbyss.floors.length === 0) {
e.reply('暂未获得本期深渊挑战数据...')
return true
}
let abyss = new Abyss(resAbyss)
let avatars = new Avatars(uid, resDetail.avatars)
let avatarIds = abyss.getAvatars()
let addMsg = function (title, ds) {
let tmp = {}
if (!ds && !ds.avatarId && !ds.percent) {
2022-07-23 20:32:10 +00:00
return
}
2022-07-23 20:32:10 +00:00
let char = Character.get(ds.avatarId)
tmp.title = title
tmp.id = char.id
tmp.value = `${(ds.value / 10000).toFixed(1)} W`
let msg = []
tmp.msg = msg
let pct = (percent, name) => {
if (percent < 0.2) {
msg.push({
title: '少于',
value: (Math.max(0.1, 100 - percent * 100)).toFixed(1),
name: name
})
} else {
msg.push({
title: '超过',
value: (Math.min(99.9, percent * 100)).toFixed(1),
name: name
})
}
}
2022-07-23 20:32:10 +00:00
pct(ds.percent, char.name)
pct(ds.percentTotal, '总记录')
stat.push(tmp)
}
addMsg('最强一击', ret.data.damage || {})
addMsg('最高承伤', ret.data.takeDamage || {})
let avatarData = await avatars.getTalentData(avatarIds, MysApi)
return await Common.render('stat/abyss-summary', {
abyss: abyss.getData(),
avatars: avatarData,
stat,
save_id: uid,
2022-08-06 22:36:05 +00:00
totalCount: overview?.data?.collectedPlayerCount || 0,
uid
}, { e, render, scale: 1.8 })
} else {
e.reply('暂未获得本期深渊挑战数据...')
return true
}
} else {
2022-07-23 20:32:10 +00:00
e.reply(`${ret.message || '上传失败'},请稍后重试...`)
}
2022-07-23 20:32:10 +00:00
return true
}