Skip to content

Commit

Permalink
都改了什么东西啊
Browse files Browse the repository at this point in the history
  • Loading branch information
IJNKAWAKAZE committed Apr 3, 2024
1 parent 5c26d4f commit 6503f7f
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 316 deletions.
11 changes: 3 additions & 8 deletions src/plugins/sign/auto_sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,8 @@ func sign(user UserSign) {
skAccount.Skland.Token = userAccount.SklandToken
skAccount.Skland.Cred = userAccount.SklandCred

var skPlayer skland.Player
skPlayer.NickName = player.PlayerName
skPlayer.ChannelName = player.ServerName
skPlayer.Uid = player.Uid

// 执行签到
record, err := skland.SignGamePlayer(&skPlayer, skAccount)
award, hasSigned, err := skland.SignGamePlayer(player.Uid, skAccount)
if err != nil {
// 签到失败
sendMessage := tgbotapi.NewMessage(user.UserNumber, fmt.Sprintf("角色 %s 签到失败!\nmsg:%s", player.PlayerName, err.Error()))
Expand All @@ -63,13 +58,13 @@ func sign(user UserSign) {
return
}
// 今日已完成签到
if record.HasSigned {
if hasSigned {
sendMessage := tgbotapi.NewMessage(user.UserNumber, fmt.Sprintf("角色 %s 今天已经签到过了", player.PlayerName))
bot.Arknights.Send(sendMessage)
return
}
// 签到成功
sendMessage := tgbotapi.NewMessage(user.UserNumber, fmt.Sprintf("角色 %s 签到成功!\n今日奖励:%s", player.PlayerName, record.Award))
sendMessage := tgbotapi.NewMessage(user.UserNumber, fmt.Sprintf("角色 %s 签到成功!\n今日奖励:%s", player.PlayerName, award))
bot.Arknights.Send(sendMessage)
}
}
Expand Down
10 changes: 3 additions & 7 deletions src/plugins/sign/sign_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,16 @@ func SignHandle(update tgbotapi.Update) error {
}

func Sign(player account.UserPlayer, account account.UserAccount, chatId int64) error {
var skPlayer skland.Player
var skAccount skland.Account
playerName := player.PlayerName
skPlayer.NickName = playerName
skPlayer.ChannelName = player.ServerName
skPlayer.Uid = player.Uid
skAccount.Hypergryph.Token = account.HypergryphToken
skAccount.Skland.Token = account.SklandToken
skAccount.Skland.Cred = account.SklandCred

sendAction := tgbotapi.NewChatAction(chatId, "typing")
bot.Arknights.Send(sendAction)

record, err := skland.SignGamePlayer(&skPlayer, skAccount)
award, hasSigned, err := skland.SignGamePlayer(player.Uid, skAccount)
if err != nil {
sendMessage := tgbotapi.NewMessage(chatId, fmt.Sprintf("角色 %s 签到失败!\nmsg:%s", playerName, err.Error()))
msg, _ := bot.Arknights.Send(sendMessage)
Expand All @@ -102,13 +98,13 @@ func Sign(player account.UserPlayer, account account.UserAccount, chatId int64)
return err
}
// 今日已完成签到
if record.HasSigned {
if hasSigned {
sendMessage := tgbotapi.NewMessage(chatId, fmt.Sprintf("角色 %s 今天已经签到过了", playerName))
bot.Arknights.Send(sendMessage)
return nil
}
// 签到成功
sendMessage := tgbotapi.NewMessage(chatId, fmt.Sprintf("角色 %s 签到成功!\n今日奖励:%s", playerName, record.Award))
sendMessage := tgbotapi.NewMessage(chatId, fmt.Sprintf("角色 %s 签到成功!\n今日奖励:%s", playerName, award))
bot.Arknights.Send(sendMessage)
return nil
}
Expand Down
12 changes: 3 additions & 9 deletions src/plugins/skland/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func Login(token string) (Account, error) {
}
account.Hypergryph.Token = token

res, err := grantApp(token, AppCodeSKLAND)
res, err := grantApp(token, "4ca99fa6b56cc2ba")
if err != nil {
return account, fmt.Errorf("grant app error: %w", err)
}
Expand Down Expand Up @@ -127,12 +127,12 @@ func RefreshToken(account Account) (Account, error) {
return account, nil
}

// 获取用户信息
// GetUser 获取用户信息
func GetUser(skland AccountSkland) (*User, error) {
return SklandRequest[*User](SKR(), "GET", "/api/v1/user", skland)
}

// 检查token有效性
// CheckToken 检查token有效性
func CheckToken(token string) error {
req := SKR().SetQueryParam("token", token)
_, err := HypergryphRequest[any](req, "GET", "/user/info/v1/basic")
Expand Down Expand Up @@ -177,9 +177,3 @@ func ArknightsPlayers(skland AccountSkland) ([]*Player, error) {
}
return players, nil
}

// GenTokenByUid 根据Oauth token和uid生成应用token
func GenTokenByUid(uid string, token string) (*GenTokenByUidData, error) {
req := HR().SetBody(gh.M{"uid": uid, "token": token})
return HypergryphBindingAPIRequest[*GenTokenByUidData](req, "POST", "/account/binding/v1/token_by_uid")
}
22 changes: 0 additions & 22 deletions src/plugins/skland/config.go

This file was deleted.

67 changes: 67 additions & 0 deletions src/plugins/skland/game_sign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package skland

import (
"fmt"
"github.com/starudream/go-lib/core/v2/gh"
"github.com/starudream/go-lib/resty/v2"
"strconv"
"strings"
)

type SignGameData struct {
Ts string `json:"ts"`
Awards SignGameAwards `json:"awards"`
}

type SignGameAward struct {
Type string `json:"type"`
Count int `json:"count"`
Resource *SignGameRes `json:"resource"`
}

type SignGameRes struct {
Id string `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
Rarity int `json:"rarity"`
}

type SignGameAwards []*SignGameAward

func SignGamePlayer(uid string, account Account) (award string, hasSigned bool, err error) {
account, err = RefreshToken(account)
if err != nil {
return
}
signGameData, err := signGame("1", uid, account.Skland)
if err != nil {
e, ok1 := resty.AsRespErr(err)
if ok1 {
t, ok2 := e.Response.Error().(*SKBaseResp[interface{}])
if ok2 && t.Message == "请勿重复签到!" {
err = nil
hasSigned = true
}
} else {
err = fmt.Errorf("sign game error: %w", err)
return
}
} else {
award = signGameData.Awards.shortString()
}
return
}

// 签到
func signGame(gid, uid string, skland AccountSkland) (*SignGameData, error) {
req := SKR().SetBody(gh.M{"gameId": gid, "uid": uid})
return SklandRequest[*SignGameData](req, "POST", "/api/v1/game/attendance", skland)
}

func (t SignGameAwards) shortString() string {
v := make([]string, len(t))
for i, a := range t {
v[i] = a.Resource.Name + "*" + strconv.Itoa(a.Count)
}
return strings.Join(v, ", ")
}
59 changes: 59 additions & 0 deletions src/plugins/skland/generate_sign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package skland

import (
"crypto/hmac"
"crypto/md5"
"crypto/sha256"
"encoding/hex"
"github.com/starudream/go-lib/core/v2/codec/json"
"github.com/starudream/go-lib/core/v2/utils/structutil"
"github.com/starudream/go-lib/resty/v2"
"net/url"
"strconv"
"time"
)

func addSign(r *resty.Request, method, path string, skland AccountSkland) {
ts := strconv.FormatInt(time.Now().Unix(), 10)

headers := signHeaders{Platform: "1", Timestamp: ts, DId: "743a446c83032899", VName: "1.5.1"}

r.SetHeaders(tom(headers))

_, signature := sign(headers, method, path, skland.Token, r.QueryParam, r.Body)

r.SetHeader("cred", skland.Cred)
r.SetHeader("sign", signature)
}

func tom(s any) map[string]string {
t := structutil.New(s)
t.TagName = "json"
m := map[string]string{}
for k, v := range t.Map() {
m[k] = v.(string)
}
return m
}

func sign(headers signHeaders, method, path, token string, query url.Values, body any) (string, string) {
str := query.Encode()
if method != "GET" {
str = json.MustMarshalString(body)
}

content := path + str + headers.Timestamp + json.MustMarshalString(headers)

b1 := hmac256(token, content)
s1 := hex.EncodeToString(b1)
b2 := md5.Sum([]byte(s1))
s2 := hex.EncodeToString(b2[:])

return content, s2
}

func hmac256(key, content string) []byte {
h := hmac.New(sha256.New, []byte(key))
h.Write([]byte(content))
return h.Sum(nil)
}
Loading

0 comments on commit 6503f7f

Please sign in to comment.