/
model.go
302 lines (280 loc) · 7.84 KB
/
model.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package bot
import (
"sync"
"time"
"github.com/asdine/storm/v3"
U "github.com/ashyaa/birtho/util"
DG "github.com/bwmarrin/discordgo"
LR "github.com/sirupsen/logrus"
)
const (
DefaultPrefix = "b!"
DefaultMinDelay = 120
DefaultVariableDelay = 781
DefaultStayTime = 5 * time.Second
HistoryDepth = 10
)
var DefaultMemberPermissions int64 = DG.PermissionManageServer
type MonsterSpawn struct {
ID string
Message string
Expected string
}
type Game struct {
On bool
Monsters map[string]MonsterSpawn
NextSpawn time.Time
MinDelay time.Duration
StayTime time.Duration
SpawnRate int
VariableDelay int
Finished bool
LastMessages History
Winner string
}
func (g *Game) Spawns(rng U.RNG) bool {
if !g.LastMessages.HasSeveralAuthors() || g.LastMessages.Span() > time.Hour {
// if g.LastMessages.Span() > time.Hour { // for debug
return false
}
return rng.PercentChance(g.SpawnRate)
}
func (g *Game) IncreaseSpawnRate(rng U.RNG, msg *DG.Message) {
g.LastMessages = g.LastMessages.Update(msg)
span := g.LastMessages.Span()
if span > time.Hour {
g.ResetSpawnRate()
g.SpawnRate += rng.Intn(2)
} else if g.LastMessages.Span() > 15*time.Minute {
return
} else {
g.SpawnRate += rng.Intn(3)
}
}
func (g *Game) ResetSpawnRate() {
g.SpawnRate = 0
}
type Server struct {
ID string
Prefix string
G Game
Channels []string
Admins []string
Users map[string][]string
Lb Leaderboard
}
// CanSpawn returns true only if an item can spawn in the given channel
func (s Server) CanSpawn(cid string) bool {
_, channelHasSpawn := s.G.Monsters[cid]
// If the game is not on, the channel is not a spawn channel, or the channel already has
// a spawn, a spawn cannot happen
if !s.G.On || !U.Contains(s.Channels, cid) || channelHasSpawn {
return false
}
// cooldown check
delayOk := time.Now().Local().After(s.G.NextSpawn)
return delayOk
}
// Cooldown sets the server game on cooldown
func (s *Server) Cooldown(rng U.RNG) {
randomDelay := time.Duration(rng.Intn(s.G.VariableDelay)) * time.Second
s.G.NextSpawn = time.Now().Local().Add(s.G.MinDelay) // Base cooldown of 2mn
s.G.NextSpawn = s.G.NextSpawn.Add(randomDelay) // variable cooldown, up to 15mn total
}
func (s Server) IsAdmin(uid string) bool {
// Everyone is an admin when no admins are registered
if len(s.Admins) == 0 {
return true
}
return U.Contains(s.Admins, uid)
}
type Bot struct {
s *DG.Session
db *storm.DB
Log *LR.Logger
UserID string
Mention string
Items map[string]Item
Monsters map[string]Monster
MonsterIds []string
Menus map[string]Menu
EqualMonsterChances bool
InteractionHandlers InteractionHandlers
Commands []Command
mutex sync.Mutex
rng U.RNG
}
type BotAction func(*Bot, CommandParameters)
type Command struct {
Name string
Action BotAction
appCmd *DG.ApplicationCommand
Options Options
Admin bool
AlwaysTrigger bool
ModifiesServer bool
}
func (c Command) ID() string {
if c.appCmd == nil {
return ""
}
return c.appCmd.ID
}
var commandList = []Command{
// Game commands
{
Name: "trick",
Action: Grab,
Options: Options{},
ModifiesServer: true,
},
{
Name: "treat",
Action: Grab,
Options: Options{},
ModifiesServer: true,
},
{
Name: "spawn",
Action: Spawn,
appCmd: &DG.ApplicationCommand{Description: "Forces a random monster to appear (for testing purposes)"},
Options: Options{},
AlwaysTrigger: true,
ModifiesServer: true,
},
// Score commands
{
Name: "leaderboard",
Action: ShowLeaderboard,
appCmd: &DG.ApplicationCommand{Description: "Show the server leaderboard"},
Options: Options{},
ModifiesServer: true,
},
{
Name: "score",
Action: ShowScore,
appCmd: &DG.ApplicationCommand{Description: "Get your scoreboard"},
Options: Options{},
ModifiesServer: true,
},
// Help command
{
Name: "help",
Action: Help,
appCmd: &DG.ApplicationCommand{Description: "Information about the bot and how to play the game"},
Options: Options{},
},
// Administration commands
{
Name: "prefix",
Action: Prefix,
appCmd: &DG.ApplicationCommand{Description: "Display the bot prefix"},
Options: Options{},
Admin: true,
},
{
Name: "setprefix",
Action: SetPrefix,
appCmd: &DG.ApplicationCommand{Description: "Change the bot prefix"},
Options: Options{{"prefix", "new prefix to use", TypeString}},
Admin: true,
ModifiesServer: true,
},
{
Name: "setcd",
Action: SetCooldown,
appCmd: &DG.ApplicationCommand{Description: "Change the spawn cooldown"},
Options: Options{
{"minimum", "minimum cooldown duration (in seconds)", TypeInteger},
{"maximum", "maximum cooldown duration (in seconds)", TypeInteger},
},
Admin: true,
ModifiesServer: true,
},
{
Name: "setstay",
Action: SetStay,
appCmd: &DG.ApplicationCommand{Description: "Change how long a monsters stays idle"},
Options: Options{{"duration", "duration in seconds", TypeInteger}},
Admin: true,
ModifiesServer: true,
},
{
Name: "addchan",
Action: AddChannel,
appCmd: &DG.ApplicationCommand{Description: "Add a channel where monsters can spawn"},
Options: Options{{"channel", "channel to add", TypeChannel}},
Admin: true,
ModifiesServer: true,
},
{
Name: "rmvchan",
Action: RemoveChannel,
appCmd: &DG.ApplicationCommand{Description: "Remove a channel where monsters can spawn"},
Options: Options{{"channel", "channel to remove", TypeChannel}},
Admin: true,
ModifiesServer: true,
},
{
Name: "chanlist",
Action: Channels,
appCmd: &DG.ApplicationCommand{Description: "Display the list of channels where monsters can spawn"},
Options: Options{},
Admin: true,
},
{
Name: "addadmin",
Action: AddAdmin,
appCmd: &DG.ApplicationCommand{Description: "Add a bot administrator"},
Options: Options{{"user", "user that shall be an administrator", TypeUser}},
Admin: true,
ModifiesServer: true,
},
{
Name: "rmvadmin",
Action: RemoveAdmin,
appCmd: &DG.ApplicationCommand{Description: "Remove a bot administrator"},
Options: Options{{"user", "user that be removed from the administrators list", TypeUser}},
Admin: true,
ModifiesServer: true,
},
{
Name: "admins",
Action: Admins,
appCmd: &DG.ApplicationCommand{Description: "Display the list of bot administrators"},
Options: Options{},
Admin: true,
},
{
Name: "info",
Action: Info,
appCmd: &DG.ApplicationCommand{Description: "Information about the bot configuration"},
Options: Options{},
Admin: true,
},
{
Name: "play",
Action: Play,
appCmd: &DG.ApplicationCommand{Description: "Start or stop the game"},
Options: Options{{"state", "game status: \"on\" or \"off\"", TypeString}},
Admin: true,
ModifiesServer: true,
},
{
Name: "reset",
Action: Reset,
appCmd: &DG.ApplicationCommand{Description: "Reset the game for all users in the server"},
Options: Options{},
Admin: true,
ModifiesServer: true,
},
{
Name: "give",
Action: GiveNew,
appCmd: &DG.ApplicationCommand{Description: "Give a random item (for testing purposes)"},
Options: Options{},
Admin: true,
ModifiesServer: true,
},
}
type InteractionHandlers map[string]func(*DG.Session, *DG.InteractionCreate)