summaryrefslogtreecommitdiff
path: root/danmaku/dmmsg/danmu.go
diff options
context:
space:
mode:
authorKeuin <[email protected]>2022-09-07 12:45:46 +0800
committerKeuin <[email protected]>2022-09-07 12:47:25 +0800
commit00595609af53514ffd39d9aeab0d33e6d84cbdb5 (patch)
treeacb89f406dade1662090facd740a7ccc5d90433c /danmaku/dmmsg/danmu.go
parentc78edaa0ffa28bb360663f172e98540b7978e9b2 (diff)
More comprehensive danmaku message handling.
Diffstat (limited to 'danmaku/dmmsg/danmu.go')
-rw-r--r--danmaku/dmmsg/danmu.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/danmaku/dmmsg/danmu.go b/danmaku/dmmsg/danmu.go
new file mode 100644
index 0000000..2cc5dfa
--- /dev/null
+++ b/danmaku/dmmsg/danmu.go
@@ -0,0 +1,55 @@
+package dmmsg
+
+/*
+Decoder of raw danmaku messages.
+*/
+
+import (
+ "fmt"
+)
+
+type RawDanMuMessage = BaseRawMessage[[]interface{}, interface{}]
+
+type DanMuMessage struct {
+ Content string
+ SourceUser struct {
+ Nickname string
+ UID int64
+ }
+}
+
+func (dm DanMuMessage) String() string {
+ return fmt.Sprintf("(user: %v, uid: %v) %v",
+ dm.SourceUser.Nickname, dm.SourceUser.UID, dm.Content)
+}
+
+const kInvalidDanmakuJson = "invalid danmaku JSON document"
+
+func ParseDanmakuMessage(body RawDanMuMessage) (dmm DanMuMessage, err error) {
+ if len(body.Info) != 16 {
+ err = fmt.Errorf("%s: \"info\" length != 16", kInvalidDanmakuJson)
+ return
+ }
+
+ dmm.Content, err = castValue[string](body.Info[1])
+ if err != nil {
+ return
+ }
+
+ userInfo, err := castValue[[]interface{}](body.Info[2])
+
+ var ok bool
+ uid, ok := userInfo[0].(float64)
+ if !ok {
+ err = fmt.Errorf("%s: uid is not a float64: %v", kInvalidDanmakuJson, userInfo[0])
+ return
+ }
+ dmm.SourceUser.UID = int64(uid)
+
+ dmm.SourceUser.Nickname, ok = userInfo[1].(string)
+ if !ok {
+ err = fmt.Errorf("%s: nickname is not a string: %v", kInvalidDanmakuJson, userInfo[1])
+ return
+ }
+ return
+}