Skip to content

Commit

Permalink
add support for normal subs, different tiers etc
Browse files Browse the repository at this point in the history
  • Loading branch information
tlanfer committed Jun 4, 2022
1 parent 18ee11c commit 79ec186
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 13 deletions.
33 changes: 28 additions & 5 deletions companion/adapter/inbound/twitchchat/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,21 @@ func (t *twitchChat) FakerShaker(interval time.Duration) {

for {
time.Sleep(interval)
switch rand.Intn(1) {
switch rand.Intn(5) {
case 0:
t.client.Say("channel", fmt.Sprintf("bits --bitscount %v Woohoo!", rand.Intn(500)))
//fallthrough
case 1:
t.client.Say("channel", fmt.Sprintf("subgift --tier %v --username glEnd2", rand.Intn(3)+1))
//fallthrough
case 2:
t.client.Say("channel", fmt.Sprintf("submysterygift --count %v --username zebiniasis", rand.Intn(15)+1))
//fallthrough
case 3:
t.client.Say("channel", fmt.Sprintf("subscription --tier %v --username glEnd2", rand.Intn(3)+1))
t.client.Say("channel", fmt.Sprintf("subscription --tier %v", rand.Intn(3)+1))
//fallthrough
case 4:
t.client.Say("channel", "resubscription")
t.client.Say("channel", fmt.Sprintf("resubscription --tier %v", rand.Intn(3)+1))
}

}
Expand Down Expand Up @@ -105,10 +109,18 @@ func (t *twitchChat) Connect(events chan<- companion.StreamEvent, messages chan<
EventType: companion.EventTypeSub,
Amount: count,
}
case "resub":
fallthrough

case "sub":
fallthrough

case "resub":
plan, _ := strconv.Atoi(message.MsgParams["msg-param-sub-plan"])
months, _ := strconv.Atoi(message.MsgParams["msg-param-cumulative-months"])
events <- companion.StreamEvent{
EventType: planToEventType(plan),
Amount: months,
}

case "subgift":
events <- companion.StreamEvent{
EventType: companion.EventTypeSub,
Expand All @@ -126,3 +138,14 @@ func (t *twitchChat) Connect(events chan<- companion.StreamEvent, messages chan<

return nil
}

func planToEventType(plan int) companion.EventType {
switch plan {
case 3000:
return companion.EventTypeT3Sub
case 2000:
return companion.EventTypeT2Sub
default:
return companion.EventTypeT1Sub
}
}
2 changes: 1 addition & 1 deletion companion/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func main() {
}

if conf.Twitch != "" {
//tc := twitchchat.New(conf.Twitch, twitchchat.WithFdgt(), twitchchat.WithFaker(3270*time.Millisecond))
//tc := twitchchat.New(conf.Twitch, twitchchat.WithFdgt(), twitchchat.WithFaker(1*time.Second))
tc := twitchchat.New(conf.Twitch)
err := tc.Connect(events, messages)

Expand Down
7 changes: 5 additions & 2 deletions companion/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,20 @@ func (c Config) GetEvent(ev StreamEvent) (bool, *SquirtPattern) {
continue
}

log.Println("found a match: ", e)
//log.Println("Looks good:", c.Events[i], "-", i)

if match == nil {
match = &c.Events[i]
log.Println("Mark match")
}

if e.Min > match.Min {
match = &e
match = &c.Events[i]
log.Println("Move match")
}
}

log.Println("found a match: ", match)
if match != nil {
if len(match.Pattern) == 0 {
return true, &defaultPattern
Expand Down
34 changes: 32 additions & 2 deletions companion/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ func TestConfig_HasEvent(t *testing.T) {
sqTwo := SquirtPattern{2 * time.Second}
sqThree := SquirtPattern{3 * time.Second}
sqFour := SquirtPattern{4 * time.Second}
sqFive := SquirtPattern{1234 * time.Millisecond}
sqSix := SquirtPattern{2345 * time.Millisecond}
sqSeven := SquirtPattern{3456 * time.Millisecond}
sq8 := SquirtPattern{8 * time.Millisecond}

c := Config{
Duration: sqDefault,
Expand Down Expand Up @@ -45,6 +49,26 @@ func TestConfig_HasEvent(t *testing.T) {
Type: EventTypeDono,
Min: 200,
},
{
Type: EventTypeT3Sub,
Pattern: sqFive,
},
{
Type: EventTypeT3Sub,
Pattern: sqSix,
Min: 15,
},
{
Type: EventTypeT1Sub,
Pattern: sqSeven,
Min: 10,
Max: 10,
},
{
Type: EventTypeT2Sub,
Pattern: sq8,
Min: 2,
},
}}

tests := []struct {
Expand All @@ -59,15 +83,21 @@ func TestConfig_HasEvent(t *testing.T) {
{StreamEvent{EventTypeDono, 25}, true, &sqThree},
{StreamEvent{EventTypeDono, 150}, true, &sqFour},
{StreamEvent{EventTypeDono, 500}, true, &sqDefault},
{StreamEvent{EventTypeT3Sub, 10}, true, &sqFive},
{StreamEvent{EventTypeT3Sub, 20}, true, &sqSix},
{StreamEvent{EventTypeT2Sub, 1}, false, nil},
{StreamEvent{EventTypeT1Sub, 10}, true, &sqSeven},
{StreamEvent{EventTypeT1Sub, 11}, false, nil},
}

for i, tt := range tests {
t.Run(fmt.Sprint(i), func(t *testing.T) {
for _, tt := range tests {
t.Run(fmt.Sprintf("%v_%v", tt.ev.EventType, tt.ev.Amount), func(t *testing.T) {
got, p := c.GetEvent(tt.ev)
if got != tt.want {
t.Errorf("HasEvent() = %v, want %v", got, tt.want)
}

t.Logf("Event: %v", tt.ev)
t.Logf("Want: %v", tt.wantPattern)
t.Logf("Got: %v", p)

Expand Down
9 changes: 6 additions & 3 deletions companion/streamevent.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import "fmt"
type EventType string

const (
EventTypeBits = "bits"
EventTypeDono = "dono"
EventTypeSub = "subs"
EventTypeBits = "bits"
EventTypeDono = "dono"
EventTypeSub = "subs"
EventTypeT1Sub = "tier1"
EventTypeT2Sub = "tier2"
EventTypeT3Sub = "tier3"
)

type StreamEvent struct {
Expand Down

0 comments on commit 79ec186

Please sign in to comment.