mirror of
https://github.com/yoimiya-kokomi/miao-plugin.git
synced 2024-11-25 09:27:34 +00:00
129 lines
3.4 KiB
JavaScript
129 lines
3.4 KiB
JavaScript
import Base from '../Base.js'
|
||
import fetch from 'node-fetch'
|
||
|
||
export default class ProfileReq extends Base {
|
||
constructor (e, game = 'gs') {
|
||
super()
|
||
this.e = e
|
||
this.game = e.game || game
|
||
this.uid = e.uid
|
||
}
|
||
|
||
static create (e, game = 'gs') {
|
||
if (!e || !e.uid) {
|
||
return false
|
||
}
|
||
// 预设面板不更新数据
|
||
if (e.uid * 1 < 100000006) {
|
||
return false
|
||
}
|
||
return new ProfileReq(e, e.game || game)
|
||
}
|
||
|
||
async setCd (seconds = 60) {
|
||
let ext = new Date() * 1 + seconds * 1000
|
||
await redis.set(`miao:profile-cd:${this.uid}`, ext + '', { EX: seconds })
|
||
}
|
||
|
||
async inCd () {
|
||
let ext = await redis.get(`miao:profile-cd:${this.uid}`)
|
||
if (!ext || isNaN(ext)) {
|
||
return false
|
||
}
|
||
let cd = (new Date() * 1) - ext
|
||
if (cd < 0 && Math.abs(cd) < 100 * 60 * 1000) {
|
||
return Math.ceil(0 - cd / 1000)
|
||
}
|
||
return false
|
||
}
|
||
|
||
err (msg = '', cd = 0) {
|
||
let serv = this.serv
|
||
let extra = serv.name ? `当前面板服务${serv.name},` : ''
|
||
const msgs = {
|
||
error: `UID${this.uid}更新面板失败,${extra}\n可能是面板服务维护中,请稍后重试...`,
|
||
empty: '请将角色放置在【游戏内】角色展柜,并打开【显示详情】,等待5分钟重新获取面板'
|
||
}
|
||
msg = msgs[msg] || msg
|
||
this.msg(msg)
|
||
// 设置CD
|
||
if (cd) {
|
||
this.setCd(cd)
|
||
}
|
||
return false
|
||
}
|
||
|
||
msg (msg) {
|
||
let e = this.e
|
||
if (msg && !e._isReplyed) {
|
||
e.reply(msg)
|
||
e._isReplyed = true
|
||
}
|
||
}
|
||
|
||
log (msg) {
|
||
logger.mark(`【面板】${this.uid} :${msg}`)
|
||
}
|
||
|
||
async requestProfile (player, serv) {
|
||
let self = this
|
||
this.serv = serv
|
||
let uid = this.uid
|
||
let reqParam = await serv.getReqParam(uid, player.game)
|
||
let cdTime = await this.inCd()
|
||
if (cdTime && !process.argv.includes('web-debug')) {
|
||
// return this.err(`请求过快,请${cdTime}秒后重试..`)
|
||
}
|
||
await this.setCd(20)
|
||
// 若3秒后还未响应则返回提示
|
||
setTimeout(() => {
|
||
if (self._isReq) {
|
||
this.e.reply(`开始获取uid:${uid}的数据,可能会需要一定时间~`)
|
||
}
|
||
}, 2000)
|
||
// 发起请求
|
||
this.log(`${logger.yellow('开始请求数据')},面板服务:${serv.name}...`)
|
||
const startTime = new Date() * 1
|
||
let data = {}
|
||
try {
|
||
let params = reqParam.params || {}
|
||
params.timeout = params.timeout || 1000 * 20
|
||
self._isReq = true
|
||
let req = await fetch(reqParam.url, params)
|
||
data = await req.text()
|
||
self._isReq = false
|
||
const reqTime = new Date() * 1 - startTime
|
||
this.log(`${logger.green(`请求结束,请求用时${reqTime}ms`)},面板服务:${serv.name}...`)
|
||
if (data[0] === '<') {
|
||
let titleRet = /<title>(.+)<\/title>/.exec(data)
|
||
if (titleRet && titleRet[1]) {
|
||
data = { error: titleRet[1] }
|
||
} else {
|
||
return this.err('error', 60)
|
||
}
|
||
} else {
|
||
data = JSON.parse(data)
|
||
}
|
||
} catch (e) {
|
||
console.log('面板请求错误', e)
|
||
self._isReq = false
|
||
data = {}
|
||
}
|
||
data = await serv.response(data, this, player.game)
|
||
// 设置CD
|
||
cdTime = serv.getCdTime(data)
|
||
if (cdTime) {
|
||
await this.setCd(cdTime)
|
||
}
|
||
if (data === false) {
|
||
return false
|
||
}
|
||
serv.updatePlayer(player, data)
|
||
cdTime = serv.getCdTime(data)
|
||
if (cdTime) {
|
||
await this.setCd(cdTime)
|
||
}
|
||
return player
|
||
}
|
||
}
|