From a4b25bc0c5f00c7ba87527cb5bc1618cc64bafa2 Mon Sep 17 00:00:00 2001 From: aynakeya Date: Fri, 10 Oct 2025 00:04:25 +0800 Subject: [PATCH] update medal struct --- go.mod | 4 ++++ go.sum | 1 + message/danmu.go | 34 +++++++++++++++++++++++++++------- message/gift.go | 2 +- message/superchat.go | 2 +- message/user.go | 22 ++++++++++++++++------ utils/strconv.go | 12 ++++++++++++ utils/strconv_test.go | 11 +++++++++++ 8 files changed, 73 insertions(+), 15 deletions(-) create mode 100644 utils/strconv_test.go diff --git a/go.mod b/go.mod index a637caf..b28d9bf 100644 --- a/go.mod +++ b/go.mod @@ -6,12 +6,16 @@ require ( github.com/andybalholm/brotli v1.1.0 github.com/gorilla/websocket v1.5.3 github.com/sirupsen/logrus v1.9.3 + github.com/stretchr/testify v1.7.0 github.com/tidwall/gjson v1.17.3 google.golang.org/protobuf v1.34.2 ) require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect golang.org/x/sys v0.25.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect ) diff --git a/go.sum b/go.sum index dfd278f..c61ba34 100644 --- a/go.sum +++ b/go.sum @@ -26,6 +26,7 @@ golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/message/danmu.go b/message/danmu.go index 0babd6a..5665921 100644 --- a/message/danmu.go +++ b/message/danmu.go @@ -102,23 +102,43 @@ func (d *Danmaku) Parse(data []byte) { i2 := info.Get("2") i3 := info.Get("3") d.Content = info.Get("1").String() //弹幕内容 + // medal extra + v2colors := []uint32{0, 0, 0, 0, 0} + medalJson := info.Get("0.15.user.medal") + v2colors[0] = utils.HexToUint32(medalJson.Get("v2_medal_color_level").String(), 1) + v2colors[1] = utils.HexToUint32(medalJson.Get("v2_medal_color_start").String(), 1) + v2colors[2] = utils.HexToUint32(medalJson.Get("v2_medal_color_end").String(), 1) + v2colors[3] = utils.HexToUint32(medalJson.Get("v2_medal_color_border").String(), 1) + v2colors[4] = utils.HexToUint32(medalJson.Get("v2_medal_color_text").String(), 1) + + // if no medal, i3 is 0 length array d.Sender = &User{ //用户信息 Uid: int(i2.Get("0").Int()), //用户uid Uname: i2.Get("1").String(), //用户昵称 - UserLevel: i2.Get("16.0").Int(), //用户等级 + UserLevel: info.Get("16.0").Int(), //用户等级 Admin: i2.Get("2").Bool(), //是否为管理者 Urank: int(i2.Get("5").Int()), MobileVerify: i2.Get("6").Bool(), //是否绑定手机 GuardLevel: int(info.Get("7").Int()), //舰队等级 + UserColor: utils.HexToUint32(i2.Get("7").String(), 1), //勋章信息 Medal: &Medal{ - Level: int(i3.Get("0").Int()), //勋章等级 - Name: i3.Get("1").String(), //勋章名称 - UpName: i3.Get("2").String(), //勋章上主播昵称 - UpRoomId: int(i3.Get("3").Int()), //上主播房间id - Color: int(i3.Get("4").Int()), //勋章颜色 - UpUid: int(i3.Get("12").Int()), //上主播uid + Level: int(i3.Get("0").Int()), //勋章等级 + Name: i3.Get("1").String(), //勋章名称 + UpName: i3.Get("2").String(), //勋章上主播昵称 + UpRoomId: int(i3.Get("3").Int()), //上主播房间id + Color: int(i3.Get("4").Int()), //勋章颜色 + ColorStart: int(i3.Get("7").Int()), //勋章开始颜色 + ColorEnd: int(i3.Get("8").Int()), //勋章结束颜色 + ColorBorder: int(i3.Get("9").Int()), //勋章边框颜色 + IsLight: i3.Get("11").Int() == 1, //勋章是否为点亮的 + UpUid: int(i3.Get("12").Int()), //上主播uid + V2ColorLevel: v2colors[0], + V2ColorStart: v2colors[1], + V2ColorEnd: v2colors[2], + V2ColorBorder: v2colors[3], + V2ColorText: v2colors[4], }, } d.Extra = ext diff --git a/message/gift.go b/message/gift.go index 29f5374..2f0c667 100644 --- a/message/gift.go +++ b/message/gift.go @@ -67,7 +67,7 @@ type Gift struct { SvgaBlock int `json:"svga_block"` // SVGA块 TagImage string `json:"tag_image"` // 标签图片URL Tid string `json:"tid"` // TID - Timestamp int `json:"timestamp"` // 时间戳 + Timestamp int64 `json:"timestamp"` // 时间戳 TopList interface{} `json:"top_list"` // 顶级列表 TotalCoin int `json:"total_coin"` // 总硬币数 Uid int `json:"uid"` // 用户ID diff --git a/message/superchat.go b/message/superchat.go index e5068ef..160b87d 100644 --- a/message/superchat.go +++ b/message/superchat.go @@ -52,7 +52,7 @@ type SuperChat struct { Time int `json:"time"` //剩余时间 Token string `json:"token"` TransMark int `json:"trans_mark"` - Ts int `json:"ts"` + Ts int64 `json:"ts"` Uid int `json:"uid"` //用户ID UserInfo struct { Face string `json:"face"` //用户头像URL diff --git a/message/user.go b/message/user.go index 550ef51..9a92986 100644 --- a/message/user.go +++ b/message/user.go @@ -9,13 +9,23 @@ type User struct { Medal *Medal GuardLevel int UserLevel int64 + UserColor uint32 // rgb } type Medal struct { - Name string - Level int - Color int - UpRoomId int - UpUid int - UpName string + Name string + Level int + Color int + ColorStart int + ColorEnd int + ColorBorder int + UpRoomId int + UpUid int + UpName string + IsLight bool + V2ColorLevel uint32 // rgba + V2ColorStart uint32 // rgba + V2ColorEnd uint32 // rgba + V2ColorBorder uint32 // rgba + V2ColorText uint32 // rgba } diff --git a/utils/strconv.go b/utils/strconv.go index 4a2f526..11dba4c 100644 --- a/utils/strconv.go +++ b/utils/strconv.go @@ -2,6 +2,7 @@ package utils import ( "encoding/base64" + "strconv" "unsafe" ) @@ -28,3 +29,14 @@ func B64Decode(s string) ([]byte, error) { dst = dst[:n] return dst, nil } + +func HexToUint32(s string, prefixLen int) uint32 { + if len(s) <= prefixLen { + return 0 + } + parseInt, err := strconv.ParseInt(s[prefixLen:], 16, 64) + if err != nil { + return 0 + } + return uint32(parseInt) +} diff --git a/utils/strconv_test.go b/utils/strconv_test.go new file mode 100644 index 0000000..fd51c7c --- /dev/null +++ b/utils/strconv_test.go @@ -0,0 +1,11 @@ +package utils + +import ( + "github.com/stretchr/testify/require" + "testing" +) + +func TestHexToUint32(t *testing.T) { + require.Equal(t, uint32(0x919298CC), HexToUint32("#919298CC", 1)) + require.Equal(t, uint32(0), HexToUint32("", 1)) +}