forked from keybase/managed-bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
142 lines (129 loc) · 3.8 KB
/
handler.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
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
package triviabot
import (
"fmt"
"strings"
"sync"
_ "github.com/go-sql-driver/mysql"
"github.com/malware-unicorn/go-keybase-chat-bot/kbchat"
"github.com/malware-unicorn/go-keybase-chat-bot/kbchat/types/chat1"
"github.com/malware-unicorn/managed-bots/base"
)
type Handler struct {
*base.DebugOutput
sync.Mutex
stats *base.StatsRegistry
kbc *kbchat.API
debugConfig *base.ChatDebugOutputConfig
db *DB
sessions map[chat1.ConvIDStr]*session
}
var _ base.Handler = (*Handler)(nil)
func NewHandler(stats *base.StatsRegistry, kbc *kbchat.API, debugConfig *base.ChatDebugOutputConfig, db *DB) *Handler {
return &Handler{
DebugOutput: base.NewDebugOutput("Handler", debugConfig),
stats: stats.SetPrefix("Handler"),
kbc: kbc,
debugConfig: debugConfig,
db: db,
sessions: make(map[chat1.ConvIDStr]*session),
}
}
func (h *Handler) handleStart(cmd string, msg chat1.MsgSummary) {
h.Lock()
defer h.Unlock()
convID := msg.ConvID
session := newSession(h.kbc, h.debugConfig, h.db, convID)
doneCb, err := session.start(0)
if err != nil {
h.ChatErrorf(convID, "handleState: failed to start: %s", err)
}
h.sessions[convID] = session
base.GoWithRecover(h.DebugOutput, func() {
<-doneCb
h.ChatEcho(convID, "Session complete, here are the top players")
err := h.handleTop(convID)
if err != nil {
h.ChatErrorf(msg.ConvID, err.Error())
}
})
}
func (h *Handler) handleStop(cmd string, msg chat1.MsgSummary) {
h.Lock()
defer h.Unlock()
convID := msg.ConvID
session, ok := h.sessions[convID]
if !ok {
h.ChatEcho(convID, "No trivia session currently running")
return
}
session.stop()
delete(h.sessions, convID)
h.ChatEcho(convID, "Session stopped")
}
func (h *Handler) handleTop(convID chat1.ConvIDStr) error {
users, err := h.db.TopUsers(convID)
if err != nil {
return fmt.Errorf("handleTop: failed to get top users: %s", err)
}
var resLines []string
if len(users) == 0 {
resLines = []string{"No answers yet"}
}
for index, u := range users {
resLines = append(resLines, fmt.Sprintf("%d. @%s (%d points, %d correct, %d incorrect)",
index+1, u.username, u.points, u.correct, u.incorrect))
}
h.ChatEcho(convID, strings.Join(resLines, "\n"))
return nil
}
func (h *Handler) handleReset(cmd string, msg chat1.MsgSummary) error {
convID := msg.ConvID
if err := h.db.ResetConv(convID); err != nil {
return fmt.Errorf("handleReset: failed to reset: %s", err)
}
h.ChatEcho(convID, "Leaderboard reset")
return nil
}
func (h *Handler) handleAnswer(convID chat1.ConvIDStr, reaction chat1.MessageReaction, sender string) {
h.Lock()
defer h.Unlock()
session, ok := h.sessions[convID]
if !ok {
h.Debug("handleAnswer: no session for convID: %s", convID)
return
}
session.answerCh <- answer{
selection: base.EmojiToNumber(reaction.Body) - 1,
msgID: reaction.MessageID,
username: sender,
}
}
func (h *Handler) HandleNewConv(conv chat1.ConvSummary) error {
welcomeMsg := "Are you up to the challenge? Try `!trivia begin` to find out."
return base.HandleNewTeam(h.stats, h.DebugOutput, h.kbc, conv, welcomeMsg)
}
func (h *Handler) HandleCommand(msg chat1.MsgSummary) error {
if msg.Content.Reaction != nil && msg.Sender.Username != h.kbc.GetUsername() {
h.handleAnswer(msg.ConvID, *msg.Content.Reaction, msg.Sender.Username)
return nil
}
if msg.Content.Text == nil {
return nil
}
cmd := strings.TrimSpace(msg.Content.Text.Body)
switch {
case strings.HasPrefix(cmd, "!trivia begin"):
h.stats.Count("start")
h.handleStart(cmd, msg)
case strings.HasPrefix(cmd, "!trivia end"):
h.stats.Count("stop")
h.handleStop(cmd, msg)
case strings.HasPrefix(cmd, "!trivia top"):
h.stats.Count("top")
return h.handleTop(msg.ConvID)
case strings.HasPrefix(cmd, "!trivia reset"):
h.stats.Count("reset")
return h.handleReset(cmd, msg)
}
return nil
}