From: Fl_GUI Date: Sun, 12 May 2024 14:26:12 +0000 (+0200) Subject: core client improvement X-Git-Url: https://git.openfl.eu/?a=commitdiff_plain;h=29329ade90d96f77e3198a9987404355264bd2ab;p=twitch-chat.git core client improvement --- diff --git a/twitch/core/messages/privmsg.go b/twitch/core/messages/privmsg.go new file mode 100644 index 0000000..3807571 --- /dev/null +++ b/twitch/core/messages/privmsg.go @@ -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 +} diff --git a/x/corechatclient/corechatclient b/x/corechatclient/corechatclient index 468bc3d..263cdfa 100755 Binary files a/x/corechatclient/corechatclient and b/x/corechatclient/corechatclient differ diff --git a/x/corechatclient/main.go b/x/corechatclient/main.go index a4745f5..8f7dffd 100644 --- a/x/corechatclient/main.go +++ b/x/corechatclient/main.go @@ -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 index 0000000..d2f978a --- /dev/null +++ b/x/corechatclient/runningmax.go @@ -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 index 0000000..4883144 --- /dev/null +++ b/x/corechatclient/runningmax_test.go @@ -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) + } + } +}