forked from teeworlds-go/protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.go
More file actions
206 lines (173 loc) · 4.72 KB
/
bot.go
File metadata and controls
206 lines (173 loc) · 4.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package bot
import (
"context"
"errors"
"fmt"
"io"
"log"
"log/slog"
"strconv"
"strings"
"time"
"github.com/teeworlds-go/protocol/messages7"
"github.com/teeworlds-go/protocol/teeworlds7"
)
type Bot struct {
sf ServerFactory
qf QuestionFactory
backoff BackoffFunc
}
func NewBot() *Bot {
return &Bot{
qf: NewQuestionFactory(),
sf: NewServerFactory(),
backoff: newDefaultBackoffPolicy(time.Second, 5*time.Minute),
}
}
func (b *Bot) Run(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
// continue
}
_ = b.retry(ctx, func() (cont bool, err error) {
server, err := b.sf.Next(ctx)
if err != nil {
return true, err
}
address := server.Addresses[0]
parts := strings.SplitN(address, ":", 3)
if len(parts) != 3 {
return false, fmt.Errorf("invalid address: %s", address)
}
ip := strings.TrimLeft(parts[1], "/")
port, err := strconv.ParseInt(parts[2], 10, 16)
if err != nil {
return false, fmt.Errorf("invalid port: %s", address)
}
b.start(ctx, ip, int(port))
return false, nil
})
}
}
func (b *Bot) start(ctx context.Context, ip string, port int) error {
ctx, cancelCause := context.WithCancelCause(ctx)
defer cancelCause(nil)
client := teeworlds7.NewClient()
client.Logger = log.New(io.Discard, "", 0) // an example on how to discard protocol debugging logs
client.OnUnknown(func(msg *messages7.Unknown, defaultAction teeworlds7.DefaultAction) error {
return nil
})
client.OnMotd(func(msg *messages7.SvMotd, defaultAction teeworlds7.DefaultAction) error {
return nil
})
state := NewState(&b.qf)
client.OnChat(b.statefulOnChat(ctx, client, state))
client.OnChat(func(msg *messages7.SvChat, defaultAction teeworlds7.DefaultAction) error {
if msg.ClientId < 0 {
return nil
}
switch msg.Message {
case "!leave":
cancelCause(errors.New("leave command !leave received"))
return nil
case "!help", "!h":
return client.SendChat("Available commands: !trivia, !top, !score and !leave")
}
return nil
})
client.OnDisconnect(func(msg *messages7.CtrlClose, defaultAction teeworlds7.DefaultAction) error {
cancelCause(errors.New("disconnected"))
return defaultAction()
})
client.OnClientInfo(func(msg *messages7.SvClientInfo, defaultAction teeworlds7.DefaultAction) error {
client.Game.Players[msg.ClientId].Info = *msg
if msg.Local {
client.LocalClientId = msg.ClientId
}
return nil
})
client.OnServerInfo(func(msg *messages7.ServerInfo, defaultAction teeworlds7.DefaultAction) error {
return nil
})
client.OnBroadcast(func(msg *messages7.SvBroadcast, defaultAction teeworlds7.DefaultAction) error {
return nil
})
slog.Info(fmt.Sprintf("connecting to server: %s:%d", ip, port), "ip", ip, "port", port)
defer func() {
slog.Info(fmt.Sprintf("disconnected from server: %s:%d", ip, port), "ip", ip, "port", port)
}()
return client.ConnectContext(ctx, ip, int(port))
}
func (b *Bot) statefulOnChat(ctx context.Context, client *teeworlds7.Client, state *State) func(msg *messages7.SvChat, defaultAction teeworlds7.DefaultAction) error {
return func(msg *messages7.SvChat, _ teeworlds7.DefaultAction) error {
if msg.ClientId < 0 {
// world chat, skip that
return nil
}
if msg.Message == "!trivia" {
chatLine, err := state.Start(ctx)
if err != nil {
return client.SendChat(fmt.Sprintf("Failed to start trivia: %v", err))
}
err = client.SendChat(chatLine)
if err != nil {
return err
}
return nil
} else if msg.Message == "!top" {
return client.SendChat(state.Top())
} else if msg.Message == "!score" {
playerName := client.Game.Players[msg.ClientId].Info.Name
return client.SendChat(state.Score(playerName))
}
if !state.Running() {
return nil
}
playerName := client.Game.Players[msg.ClientId].Info.Name
correctAnswer, correct := state.Answer(playerName, msg.Message)
if !correct {
return nil
}
// player answered correctly
return client.SendChat(fmt.Sprintf("%s answered correctly: %s", playerName, correctAnswer))
}
}
func (b *Bot) retry(ctx context.Context, f func() (cont bool, err error)) error {
// fast path
cont, err := f()
if err != nil {
return err
}
if !cont {
return nil
}
// continue second try with backoff timer overhead
var (
retry = 1
timer = time.NewTimer(b.backoff(retry))
drained = false
)
defer closeTimer(timer, &drained)
for {
select {
case <-timer.C:
// at this point we know that the timer channel has been drained
drained = true
// try again
cont, err = f()
if err != nil {
return err
}
if !cont {
return nil
}
retry++
resetTimer(timer, b.backoff(retry), &drained)
case <-ctx.Done():
return errors.Join(err, ctx.Err())
}
}
}