From e22e3f244b817ebbb7b222ec41f357c11a54c96a Mon Sep 17 00:00:00 2001 From: Akiba Date: Thu, 20 Jan 2022 12:40:23 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=94=AF=E6=8C=81=E8=A1=A8?= =?UTF-8?q?=E6=83=85=E5=BC=B9=E5=B9=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- message/danmu.go | 85 +++++++++++++++++++++++++++++++----------------- message/user.go | 2 +- 2 files changed, 56 insertions(+), 31 deletions(-) diff --git a/message/danmu.go b/message/danmu.go index e452bad..d83caf9 100644 --- a/message/danmu.go +++ b/message/danmu.go @@ -7,48 +7,73 @@ import ( "github.com/tidwall/gjson" ) -type Danmaku struct { - Sender User - Content string - Extra Extra - Timestamp int64 -} +const ( + TextDanmaku = iota + EmoticonDanmaku +) -type Extra struct { - SendFromMe bool `json:"send_from_me"` - Mode int `json:"mode"` - Color int `json:"color"` - DmType int `json:"dm_type"` - FontSize int `json:"font_size"` - PlayerMode int `json:"player_mode"` - ShowPlayerType int `json:"show_player_type"` - Content string `json:"content"` - UserHash string `json:"user_hash"` - EmoticonUnique string `json:"emoticon_unique"` - Direction int `json:"direction"` - PkDirection int `json:"pk_direction"` - SpaceType string `json:"space_type"` - SpaceUrl string `json:"space_url"` -} +type ( + Danmaku struct { + Sender *User + Content string + Extra *Extra + Emoticon *Emoticon + Type int + Timestamp int64 + } + + Extra struct { + SendFromMe bool `json:"send_from_me"` + Mode int `json:"mode"` + Color int `json:"color"` + DmType int `json:"dm_type"` + FontSize int `json:"font_size"` + PlayerMode int `json:"player_mode"` + ShowPlayerType int `json:"show_player_type"` + Content string `json:"content"` + UserHash string `json:"user_hash"` + EmoticonUnique string `json:"emoticon_unique"` + Direction int `json:"direction"` + PkDirection int `json:"pk_direction"` + SpaceType string `json:"space_type"` + SpaceUrl string `json:"space_url"` + } + Emoticon struct { + BulgeDisplay int `json:"bulge_display"` + EmoticonUnique string `json:"emoticon_unique"` + Height int `json:"height"` + InPlayerArea int `json:"in_player_area"` + IsDynamic int `json:"is_dynamic"` + Url string `json:"url"` + Width int `json:"width"` + } +) func (d *Danmaku) Parse(data []byte) { sb := bytes.NewBuffer(data).String() info := gjson.Get(sb, "info") + ext := new(Extra) + emo := new(Emoticon) + err := json.Unmarshal([]byte(info.Get("0.15.extra").String()), ext) + if err != nil { + log.Error("parse danmuku extra failed") + } + err = json.Unmarshal([]byte(info.Get("0.13").String()), emo) + if err != nil { + log.Error("parse danmuku emoticon failed") + } d.Content = info.Get("1").String() - d.Sender = User{ + d.Sender = &User{ Uid: int(info.Get("2.0").Int()), Uname: info.Get("2.1").String(), - Medal: Medal{ + Medal: &Medal{ Name: info.Get("3.1").String(), Level: int(info.Get("3.0").Int()), Up: info.Get("3.2").String(), }, } - ext := new(Extra) - err := json.Unmarshal([]byte(info.Get("0.15.extra").String()), ext) - if err != nil { - log.Error("parse danmuku extra failed") - } - d.Extra = *ext + d.Extra = ext + d.Emoticon = emo + d.Type = int(info.Get("0.12").Int()) d.Timestamp = info.Get("0.4").Int() } diff --git a/message/user.go b/message/user.go index 11b0a22..ea98422 100644 --- a/message/user.go +++ b/message/user.go @@ -3,7 +3,7 @@ package message type User struct { Uid int Uname string - Medal Medal + Medal *Medal } type Medal struct {