-
Notifications
You must be signed in to change notification settings - Fork 3
/
combos.go
90 lines (72 loc) · 1.61 KB
/
combos.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"errors"
"strings"
"sync"
)
var ErrComboDuplicate = errors.New("user has already participated in combo")
var combos = Combos{}
type comboVariant struct {
modifiers []string
count int
}
type Combos struct {
lock sync.Mutex
emote string
count int
variants map[string]*comboVariant
participants map[string]struct{}
}
func (c *Combos) Transform(msg *EventDataOut) error {
c.lock.Lock()
defer c.lock.Unlock()
if !isEmoteMessage(msg) {
c.reset()
return nil
}
emote := msg.Entities.Emotes[0]
// if the combo was broken by another emote message reset
if c.emote != emote.Name {
c.reset()
}
if _, ok := c.participants[msg.Nick]; ok {
return ErrComboDuplicate
}
c.emote = emote.Name
c.count++
c.participants[msg.Nick] = struct{}{}
variant := strings.Join(emote.Modifiers, ":")
if _, ok := c.variants[variant]; !ok {
c.variants[variant] = &comboVariant{
modifiers: emote.Modifiers,
count: 0,
}
}
c.variants[variant].count++
// if this was the first emote in the combo don't mark a combo yet
if c.count == 1 {
return nil
}
emote.Combo = c.count
topVariantCount := -1
for _, v := range c.variants {
if v.count > topVariantCount {
topVariantCount = c.count
emote.Modifiers = v.modifiers
}
}
return nil
}
func (c *Combos) reset() {
c.emote = ""
c.count = 0
c.variants = map[string]*comboVariant{}
c.participants = map[string]struct{}{}
}
func isEmoteMessage(msg *EventDataOut) bool {
if len(msg.Entities.Emotes) != 1 {
return false
}
b := msg.Entities.Emotes[0].Bounds
return b[0] == 0 && b[1] == len(msg.Data)
}