Skip to content

Commit

Permalink
allow squirt patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
tlanfer committed Apr 4, 2022
1 parent 542c92f commit 18ee11c
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 99 deletions.
22 changes: 19 additions & 3 deletions companion/adapter/inbound/yamlconfig/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@ func (l *loader) Load() (*companion.Config, error) {
return nil, errors.New("need either a twitch channel or a streamlabs token. see config.example.yaml for an example")
}

if c.Duration < 500*time.Millisecond {
return nil, errors.New("spray time must be at least 500ms")
if len(c.Duration) == 0 {
c.Duration = []time.Duration{1 * time.Second}
}

for _, duration := range c.Duration {
if duration < 500*time.Millisecond {
duration = 500 * time.Millisecond
}
}

if len(c.Events) == 0 && len(c.ChatTriggers) == 0 {
Expand All @@ -63,7 +69,7 @@ func (l *loader) Example() {

c := &companion.Config{
Cooldown: 5 * time.Second,
Duration: 1 * time.Second,
Duration: []time.Duration{1 * time.Second},
Squirters: []string{
"192.168.1.200",
},
Expand All @@ -75,6 +81,16 @@ func (l *loader) Example() {
{Type: companion.EventTypeDono, Min: 20, Max: 30},
{Type: companion.EventTypeDono, Min: 100},
{Type: companion.EventTypeSub, Min: 10},
{Type: companion.EventTypeSub, Min: 25, Pattern: companion.SquirtPattern{
3 * time.Second,
}},
{Type: companion.EventTypeSub, Min: 50, Pattern: companion.SquirtPattern{
1 * time.Second,
500 * time.Millisecond,
2 * time.Second,
500 * time.Millisecond,
3 * time.Second,
}},
},
ChatTriggers: []companion.ChatTrigger{
{
Expand Down
43 changes: 30 additions & 13 deletions companion/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func main() {
os.Exit(1)
}
}
//conf.Dump(os.Stdout)

converter, err := exchangerate.New(conf.Currency)
if err != nil {
Expand Down Expand Up @@ -93,21 +94,34 @@ func main() {

timeout := time.Now()

patternSquirt := func(p companion.SquirtPattern) {
timeout = time.Now()
for _, d := range p {
timeout = timeout.Add(d)
}

for i, d := range p {
if i%2 == 0 {
ui.SetActive(d)
squirters.Squirt(d)
}
time.Sleep(d)
}
}

for {
select {
case m := <-messages:
if conf.HasChatTrigger(m) && time.Now().After(timeout) {
hasTrigger, pattern := conf.GetChatTrigger(m)
if hasTrigger && time.Now().After(timeout) {
log.Printf("Message from %v: %v -> Squirt for %v", m.User, m.Message, conf.Duration)
ui.SetActive(conf.Duration)
squirters.Squirt(conf.Duration)
timeout = time.Now().Add(conf.Cooldown + conf.Duration)
go patternSquirt(*pattern)
}
case e := <-events:
if conf.HasEvent(e) && time.Now().After(timeout) {
hasEvent, pattern := conf.GetEvent(e)
if hasEvent && time.Now().After(timeout) {
log.Printf("Got %v (%v): Squirt for %v", e.EventType, e.Amount, conf.Duration)
ui.SetActive(conf.Duration)
squirters.Squirt(conf.Duration)
timeout = time.Now().Add(conf.Cooldown + conf.Duration)
go patternSquirt(*pattern)
}
case <-ui.OnQuit():
log.Println("Quitting!")
Expand All @@ -119,13 +133,16 @@ func main() {
}

func setupLogging() error {
if os.Getenv("LOG_CONSOLE") == "" {

file, err := os.OpenFile("companion.log", os.O_CREATE|os.O_RDWR|os.O_APPEND, os.ModePerm)
if err != nil {
return err
}
file, err := os.OpenFile("companion.log", os.O_CREATE|os.O_RDWR|os.O_APPEND, os.ModePerm)
if err != nil {
return err
}

log.SetOutput(file)

log.SetOutput(file)
}

return nil
}
56 changes: 42 additions & 14 deletions companion/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package companion
import (
"gopkg.in/yaml.v3"
"io"
"log"
"strings"
"time"
)
Expand All @@ -21,7 +22,7 @@ type ConfigLoader interface {

type Config struct {
Cooldown time.Duration `yaml:"cooldown"`
Duration time.Duration `yaml:"duration"`
Duration SquirtPattern `yaml:"duration"`

Twitch string `yaml:"twitch"`
Streamlabs string `yaml:"streamlabs"`
Expand All @@ -34,19 +35,25 @@ type Config struct {
}

type Event struct {
Type EventType `yaml:"type"`
Min int `yaml:"min"`
Max int `yaml:"max,omitempty"`
Type EventType `yaml:"type"`
Min int `yaml:"min"`
Max int `yaml:"max,omitempty"`
Pattern SquirtPattern `yaml:"duration,omitempty"`
}

type ChatTrigger struct {
Role ChatRole `yaml:"role"`
User string `yaml:"user,omitempty"`
Message string `yaml:"message"`
Role ChatRole `yaml:"role"`
User string `yaml:"user,omitempty"`
Message string `yaml:"message"`
Pattern SquirtPattern `yaml:"duration,omitempty"`
}

func (c Config) HasEvent(ev StreamEvent) bool {
for _, e := range c.Events {
func (c Config) GetEvent(ev StreamEvent) (bool, *SquirtPattern) {
defaultPattern := c.Duration

var match *Event

for i, e := range c.Events {

if ev.EventType != e.Type {
continue
Expand All @@ -60,13 +67,30 @@ func (c Config) HasEvent(ev StreamEvent) bool {
continue
}

return true
log.Println("found a match: ", e)

if match == nil {
match = &c.Events[i]
}

if e.Min > match.Min {
match = &e
}
}

return false
if match != nil {
if len(match.Pattern) == 0 {
return true, &defaultPattern
} else {
return true, &match.Pattern
}
}

return false, nil
}

func (c Config) HasChatTrigger(message ChatMessage) bool {
func (c Config) GetChatTrigger(message ChatMessage) (bool, *SquirtPattern) {
defaultPattern := c.Duration
for _, e := range c.ChatTriggers {

if message.Role < e.Role {
Expand All @@ -81,10 +105,14 @@ func (c Config) HasChatTrigger(message ChatMessage) bool {
continue
}

return true
if len(e.Pattern) == 0 {
return true, &defaultPattern
} else {
return true, &e.Pattern
}
}

return false
return false, nil
}

func (c Config) Dump(o io.Writer) {
Expand Down
Loading

0 comments on commit 18ee11c

Please sign in to comment.