--- /dev/null
+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
+}
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()
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 {
}
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())
+ }
}
}
--- /dev/null
+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
+}
--- /dev/null
+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)
+ }
+ }
+}