feat: 实现 blPlus 函数框架
This commit is contained in:
parent
e89e69a094
commit
ec6d65c9b8
@ -1,8 +1,10 @@
|
|||||||
package beatleader
|
package beatleader
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"git.lxtend.com/qqbot/constants"
|
"git.lxtend.com/qqbot/constants"
|
||||||
@ -10,6 +12,7 @@ import (
|
|||||||
"git.lxtend.com/qqbot/message"
|
"git.lxtend.com/qqbot/message"
|
||||||
"git.lxtend.com/qqbot/model"
|
"git.lxtend.com/qqbot/model"
|
||||||
"git.lxtend.com/qqbot/service/beatleader"
|
"git.lxtend.com/qqbot/service/beatleader"
|
||||||
|
"git.lxtend.com/qqbot/service/scoresaber"
|
||||||
"git.lxtend.com/qqbot/util"
|
"git.lxtend.com/qqbot/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -29,66 +32,66 @@ func init() {
|
|||||||
handler.RegisterHandler("jbl", screenShotBL, constants.LEVEL_USER)
|
handler.RegisterHandler("jbl", screenShotBL, constants.LEVEL_USER)
|
||||||
}
|
}
|
||||||
|
|
||||||
// func blPlus(msg model.Message) (reply model.Reply) {
|
func blPlus(msg model.Message) (reply model.Reply) {
|
||||||
// var (
|
var (
|
||||||
// resultStr strings.Builder
|
resultStr strings.Builder
|
||||||
// err error
|
err error
|
||||||
// maxRetries = 5 // 最大重试次数
|
maxRetries = 5 // 最大重试次数
|
||||||
// attempts = 0
|
attempts = 0
|
||||||
// )
|
)
|
||||||
// var N int
|
var N int
|
||||||
// if len(msg.RawMsg) > len("bl+") {
|
if len(msg.RawMsg) > len("bl+") {
|
||||||
// N, err = strconv.Atoi(msg.RawMsg[len("bl+"):])
|
N, err = strconv.Atoi(msg.RawMsg[len("bl+"):])
|
||||||
// if err != nil || N <= 0 {
|
if err != nil || N <= 0 {
|
||||||
// return model.Reply{
|
return model.Reply{
|
||||||
// ReplyMsg: "请输入一个正整数",
|
ReplyMsg: "请输入一个正整数",
|
||||||
// ReferOriginMsg: true,
|
ReferOriginMsg: true,
|
||||||
// FromMsg: msg,
|
FromMsg: msg,
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// userIdStr := strconv.Itoa(int(msg.UserId))
|
userIdStr := strconv.Itoa(int(msg.UserId))
|
||||||
// userBLID, err := scoresaber.GetSSID(userIdStr)
|
userBLID, err := scoresaber.GetSSID(userIdStr)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// return model.Reply{
|
return model.Reply{
|
||||||
// ReplyMsg: err.Error(),
|
ReplyMsg: err.Error(),
|
||||||
// ReferOriginMsg: true,
|
ReferOriginMsg: true,
|
||||||
// FromMsg: msg,
|
FromMsg: msg,
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// var userInfo beatleader.PlayerData
|
var userInfo *beatleader.PlayerData
|
||||||
// for attempts < maxRetries {
|
for attempts < maxRetries {
|
||||||
// err = nil
|
err = nil
|
||||||
// userInfo, err = beatleader.FetchPlayerData(userBLID)
|
userInfo, err = beatleader.FetchPlayerData(userBLID)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// break
|
break
|
||||||
// }
|
}
|
||||||
// attempts++
|
attempts++
|
||||||
// }
|
}
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// return model.Reply{
|
return model.Reply{
|
||||||
// ReplyMsg: "获取您的分数时出现问题,请稍后重试。" + err.Error(),
|
ReplyMsg: "获取您的分数时出现问题,请稍后重试。" + err.Error(),
|
||||||
// ReferOriginMsg: true,
|
ReferOriginMsg: true,
|
||||||
// FromMsg: msg,
|
FromMsg: msg,
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// resultStr.WriteString(fmt.Sprintf("您当前的全区排名为:%d\n", userInfo.CountryRank))
|
resultStr.WriteString(fmt.Sprintf("您当前的全区排名为:%d\n", userInfo.CountryRank))
|
||||||
// // 获取当前用户所在区对应+N位的玩家列表
|
// 获取当前用户所在区对应+N位的玩家列表
|
||||||
// leaderboard, err := beatleader.FetchCountryLeaderboard(userInfo.Country, userInfo.CountryRank-N, userInfo.ID)
|
// leaderboard, err := beatleader.FetchCountryLeaderboard(userInfo.Country, userInfo.CountryRank-N, userInfo.ID)
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
// return model.Reply{
|
// return model.Reply{
|
||||||
// ReplyMsg: "获取当前用户所在区对应+N位的玩家列表时出现问题,请稍后重试。" + err.Error(),
|
// ReplyMsg: "获取当前用户所在区对应+N位的玩家列表时出现问题,请稍后重试。" + err.Error(),
|
||||||
// ReferOriginMsg: true,
|
// ReferOriginMsg: true,
|
||||||
// FromMsg: msg,
|
// FromMsg: msg,
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// resultStr.WriteString(fmt.Sprintf("您只需要再打出%.2fpp就能达到%s区第%d名。", targetPlayer.PP-userInfo.PP, userInfo.Country, targetPlayer.CountryRank))
|
// resultStr.WriteString(fmt.Sprintf("您只需要再打出%.2fpp就能达到%s区第%d名。", targetPlayer.PP-userInfo.PP, userInfo.Country, targetPlayer.CountryRank))
|
||||||
// return model.Reply{
|
return model.Reply{
|
||||||
// ReplyMsg: resultStr.String(),
|
ReplyMsg: resultStr.String(),
|
||||||
// ReferOriginMsg: true,
|
ReferOriginMsg: true,
|
||||||
// FromMsg: msg,
|
FromMsg: msg,
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
func getMyBL(msg model.Message) (reply *model.Reply) {
|
func getMyBL(msg model.Message) (reply *model.Reply) {
|
||||||
var (
|
var (
|
||||||
|
@ -67,3 +67,58 @@ func FetchPlayerData(blID string) (*PlayerData, error) {
|
|||||||
|
|
||||||
return &playerData, nil
|
return &playerData, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FetchCountryLeaderboard(country string, aimRank int, userId string) (*PlayerDataLite, error) {
|
||||||
|
url := fmt.Sprintf("https://api.beatleader.com/players?leaderboardContext=general&page=1&count=50&sortBy=pp&mapsType=ranked&ppType=general&order=desc")
|
||||||
|
|
||||||
|
// 创建请求
|
||||||
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
|
|
||||||
|
// 设置请求头
|
||||||
|
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0")
|
||||||
|
req.Header.Set("Accept", "application/json, text/plain, */*")
|
||||||
|
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2")
|
||||||
|
req.Header.Set("Accept-Encoding", "gzip")
|
||||||
|
req.Header.Set("DNT", "1")
|
||||||
|
req.Header.Set("Connection", "keep-alive")
|
||||||
|
req.Header.Set("Referer", fmt.Sprintf("https://beatleader.xyz/u/%s/scores/date/desc/1", userId))
|
||||||
|
req.Header.Set("Sec-Fetch-Dest", "empty")
|
||||||
|
req.Header.Set("Sec-Fetch-Mode", "cors")
|
||||||
|
req.Header.Set("Sec-Fetch-Site", "same-origin")
|
||||||
|
req.Header.Set("Sec-GPC", "1")
|
||||||
|
req.Header.Set("Pragma", "no-cache")
|
||||||
|
req.Header.Set("Cache-Control", "no-cache")
|
||||||
|
req.Header.Set("TE", "trailers")
|
||||||
|
|
||||||
|
// 发送请求,失败则重试至多3次
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
for i := 0; i < 3 && err != nil; i++ {
|
||||||
|
resp, err = client.Do(req)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// 处理压缩响应
|
||||||
|
var reader io.Reader
|
||||||
|
if resp.Header.Get("Content-Encoding") == "gzip" {
|
||||||
|
reader, err = gzip.NewReader(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer reader.(*gzip.Reader).Close()
|
||||||
|
} else {
|
||||||
|
reader = resp.Body
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析响应体
|
||||||
|
var playerDataLite PlayerDataLite
|
||||||
|
err = json.NewDecoder(reader).Decode(&playerDataLite)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &playerDataLite, nil
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user