-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhellodolly.go
More file actions
161 lines (135 loc) · 3.72 KB
/
hellodolly.go
File metadata and controls
161 lines (135 loc) · 3.72 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package hellodolly
import (
"fmt"
"math/rand"
"strings"
"sync"
"time"
"github.com/FrameworkOSS/event"
"github.com/FrameworkOSS/feature_commands/handler"
"github.com/FrameworkOSS/feature_hellodolly/metadata"
)
type HelloDolly struct {
lockView sync.Mutex
lockResp sync.Mutex
views map[int]int //line:views
resps []*event.Event
lastResp time.Time
processor *handler.EventCommandHandler
}
func NewHelloDolly() (hd *HelloDolly) {
hd = new(HelloDolly)
hd.views = make(map[int]int)
hd.resps = make([]*event.Event, 0)
hd.processor = handler.NewEventCommandHandler()
hd.processor.GetCommandHandler().
Handle(hd.cmdHelloDolly, metadata.CmdHelloDolly)
return
}
func (hd *HelloDolly) cmdHelloDolly(cmd *handler.Command, e *event.Event) (err error) {
var r *event.Event
switch cmd.GetID() {
case metadata.CmdHelloDolly.GetID():
line := 0
if lyric := cmd.GetArgument("lyric"); lyric != nil {
line = lyric.GetValueNumber()
}
isRandom := false
lines := strings.Split(metadata.Lyrics, "\n")
if line < 1 || line >= len(lines) {
//Pick a random lyric!
line = rand.Intn(len(lines))
isRandom = true
} else {
line--
}
lineS := lines[line]
views := hd.viewCount(line)
plural := ""
if views != 1 {
plural = "s"
}
random := ""
if isRandom {
random = "random "
}
lineFmt := fmt.Sprintf("<code>%s</code>\n\nThis %slyric (line %d) has reached <u>%d</u> view%s!\nThe last response was generated <i>%s</i> ago.", lineS, random, line+1, views, plural, time.Since(hd.lastResp).String())
r = event.NewEventResponse(hd.ID(), []byte(lineFmt))
case metadata.CmdHelloDolly.GetID() + " " + metadata.CmdTest.GetID():
r = event.NewEventResponse(hd.ID(), []byte(
fmt.Sprintf("This is a test response from the <u>Hello Dolly</u> feature's test subcommand!\n\nThe last response was generated <i>%s</i> ago.", time.Since(hd.lastResp).String())),
)
default:
err = fmt.Errorf("hellodolly: unknown command: %s", cmd.GetID())
}
if r != nil {
if r.GetChannel() == "" {
r.SetChannel(e.GetChannel())
}
if r.GetProducer() == "" {
r.SetProducer(hd.ID())
}
r.AddParticipants(e.GetParticipants()...)
r.AddParticipants(e.GetProducer())
go hd.storeResp(r)
}
return
}
func (hd *HelloDolly) storeResp(e *event.Event) {
now := time.Now()
hd.lockResp.Lock()
hd.lastResp = now
hd.resps = append(hd.resps, e)
hd.lockResp.Unlock()
}
func (hd *HelloDolly) readResp() (e *event.Event) {
if len(hd.resps) > 0 {
hd.lockResp.Lock()
e = hd.resps[0]
hd.resps = hd.resps[1:]
hd.lockResp.Unlock()
}
return
}
func (hd *HelloDolly) viewCount(line int) int {
hd.lockView.Lock()
defer hd.lockView.Unlock()
views := 1
if count, exists := hd.views[line]; exists {
views = count + 1
}
hd.views[line] = views
return views
}
func (hd *HelloDolly) API() int {
return metadata.API
}
func (hd *HelloDolly) ID() string {
return metadata.ID
}
func (hd *HelloDolly) Name() string {
return metadata.Name
}
func (hd *HelloDolly) Authors() []string {
return strings.Split(metadata.Authors, ",")
}
func (hd *HelloDolly) Description() string {
return metadata.Description
}
func (hd *HelloDolly) Version() string {
return metadata.Version
}
func (hd *HelloDolly) Open() error {
hd.storeResp(handler.NewEventCommandAdd(hd.ID(), metadata.CmdHelloDolly))
hd.storeResp(event.NewEventReady(hd.ID(), true))
return nil
}
func (hd *HelloDolly) Close() (errs []error, retry bool) {
return
}
func (hd *HelloDolly) Input(e *event.Event) error {
return hd.processor.Process(e)
}
func (hd *HelloDolly) Output() (*event.Event, error) {
return hd.readResp(), nil
}