]> git.openfl.eu Git - twitch-chat.git/commitdiff
core client improvement
authorFl_GUI <flor.guilini@hotmail.com>
Sun, 12 May 2024 14:26:12 +0000 (16:26 +0200)
committerFl_GUI <flor.guilini@hotmail.com>
Sun, 12 May 2024 14:26:12 +0000 (16:26 +0200)
twitch/core/messages/privmsg.go [new file with mode: 0644]
x/corechatclient/corechatclient
x/corechatclient/main.go
x/corechatclient/runningmax.go [new file with mode: 0644]
x/corechatclient/runningmax_test.go [new file with mode: 0644]

diff --git a/twitch/core/messages/privmsg.go b/twitch/core/messages/privmsg.go
new file mode 100644 (file)
index 0000000..3807571
--- /dev/null
@@ -0,0 +1,19 @@
+package messages
+
+import (
+       "twitchchat/twitch/core/commands"
+)
+
+type PrivMsg Message
+
+func IsPrivateMessage(m Message) bool {
+       return string(m.Command) == commands.PrivMsg
+}
+
+func (p PrivMsg) User() string {
+       return p.Prefix.Name
+}
+
+func (p PrivMsg) Text() string {
+       return p.Params.Trailing
+}
index 468bc3df45b6f1f87d48cdf5d0dca036a3da9b99..263cdfa1d6a5382ca52ef065ee51f771de13e5b5 100755 (executable)
Binary files a/x/corechatclient/corechatclient and b/x/corechatclient/corechatclient differ
index a4745f5e5a2b481a48e0e85badc5bd5faa52d815..8f7dffdd1a5399d2611caa60fcfc3c421fe7e5c4 100644 (file)
@@ -2,20 +2,25 @@ package main
 
 import (
        "errors"
+       "flag"
        "fmt"
-       "os"
+       "strings"
        "twitchchat/twitch/core"
        "twitchchat/twitch/core/messages"
 )
 
+var channel = flag.String("channel", "fl_gui", "channel to read")
+
 func main() {
+       flag.Parse()
+
        conn, err := core.Dial("http://localhost")
        if err != nil {
                panic(err)
        }
        defer conn.Close()
 
-       core.DebugLogger = os.Stdout
+       //core.DebugLogger = os.Stdout
 
        msgs := conn.ReadMessages()
 
@@ -26,7 +31,9 @@ func main() {
                panic(err)
        }
 
-       if msgs, err = conn.WithCapability(msgs, core.MembershipCapability, "Unknown", "foobar"); err != nil {
+       msgs = conn.PingPong(msgs)
+
+       if msgs, err = conn.WithCapability(msgs, core.MembershipCapability); err != nil {
                if errors.Is(err, core.UnsupportedCapabilitiesError) {
                        fmt.Printf("%#v\n", err.(core.UnsupportedCapabilities))
                } else {
@@ -35,12 +42,20 @@ func main() {
        }
 
        var members core.ChannelMembers
-       if msgs, members, err = conn.Join(msgs, "fl_gui"); err != nil {
+       if msgs, members, err = conn.Join(msgs, *channel); err != nil {
                panic(err)
        }
-  fmt.Printf("current members: %s", members)
+       fmt.Printf("current members: %s\n", strings.Join(members[*channel], ", "))
 
+       var format string
+       rmax := NewRunningMax(50)
        for m := range msgs {
-               fmt.Println(m)
+               if messages.IsPrivateMessage(m) {
+                       priv := messages.PrivMsg(m)
+                       u := priv.User()
+                       mx := rmax.Push(len(u))
+                       format = fmt.Sprintf("%%%ds: %%s\n", mx)
+                       fmt.Printf(format, u, priv.Text())
+               }
        }
 }
diff --git a/x/corechatclient/runningmax.go b/x/corechatclient/runningmax.go
new file mode 100644 (file)
index 0000000..d2f978a
--- /dev/null
@@ -0,0 +1,42 @@
+package main
+
+type RunningMax struct {
+       data    []int
+       size    int
+       pos     int
+       maximum int
+}
+
+func NewRunningMax(length int) RunningMax {
+       return RunningMax{
+               make([]int, length),
+               length,
+               0,
+               0,
+       }
+}
+
+func (r *RunningMax) Push(d int) (currentMax int) {
+       evicted := r.data[r.pos]
+
+       r.data[r.pos] = d
+       r.pos = (r.pos + 1) % r.size
+
+       if evicted == r.maximum {
+               r.maximum = r.calc()
+       }
+       if d > r.maximum {
+               r.maximum = d
+       }
+       return r.maximum
+}
+
+func (r RunningMax) calc() int {
+       m := 0
+       for _, d := range r.data {
+               if d > m {
+                       m = d
+               }
+       }
+       return m
+}
diff --git a/x/corechatclient/runningmax_test.go b/x/corechatclient/runningmax_test.go
new file mode 100644 (file)
index 0000000..4883144
--- /dev/null
@@ -0,0 +1,23 @@
+package main
+
+import "testing"
+
+func TestRunningMax(t *testing.T) {
+       rmx := NewRunningMax(3)
+       data := []struct {
+               push, expect int
+       }{
+               {1, 1},
+               {3, 3},
+               {2, 3},
+               {1, 3},
+               {0, 2},
+       }
+       for i, d := range data {
+               out := rmx.Push(d.push)
+               if out != d.expect {
+                       t.Log(rmx.data, rmx.pos)
+                       t.Errorf("%d_ Expected %d, got %d\n", i, d.expect, out)
+               }
+       }
+}