-
Notifications
You must be signed in to change notification settings - Fork 0
/
discordchess.go
514 lines (454 loc) · 11.2 KB
/
discordchess.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
package discordchess
import (
"bytes"
"fmt"
"image"
"image/color"
"image/gif"
"image/png"
"io"
"log"
"regexp"
"strings"
"time"
"github.com/DiscordGophers/discordchess/chessimage"
"github.com/bwmarrin/discordgo"
"github.com/notnil/chess"
"github.com/notnil/chess/uci"
)
type GameError string
func (e GameError) Error() string { return string(e) }
var ErrNoGame = GameError("No game in progress")
var help = "" +
"Help:\n" +
" `%[1]shelp` - show this\n" +
" `%[1]splay @player1 @player2` - starts a game\n" +
" `%[1]smove <move>` - do a move in algebraic notation\n" +
" `%[1]sboard` - shows the board\n" +
" `%[1]sresign` - resigns the game\n" +
" `%[1]sdraw` - offer draw\n"
type ChessHandler struct {
prefix string
channelRE *regexp.Regexp
adminRoles map[string]struct{}
drawer *chessimage.Drawer
states *state
}
func New(cmdPrefix, channelRe string, adminRoles []string) (*ChessHandler, error) {
re := regexp.MustCompile(channelRe)
roleMap := map[string]struct{}{}
for _, r := range adminRoles {
roleMap[r] = struct{}{}
}
drawer, err := chessimage.NewDrawer()
if err != nil {
return nil, err
}
c := &ChessHandler{
prefix: cmdPrefix,
channelRE: re,
adminRoles: roleMap,
drawer: drawer,
states: &state{
games: make(map[string]*game),
},
}
return c, nil
}
func (c *ChessHandler) MessageCreateHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
err := c.messageCreateHandler(s, m)
if e, ok := err.(GameError); ok {
s.MessageReactionAdd(m.ChannelID, m.ID, `❌`)
if e != "" {
s.ChannelMessageSendReply(
m.ChannelID,
string(e),
&discordgo.MessageReference{
ChannelID: m.ChannelID,
MessageID: m.ID,
})
}
return
}
if err != nil {
log.Println("unhandled error:", err)
}
}
func (c *ChessHandler) messageCreateHandler(s *discordgo.Session, m *discordgo.MessageCreate) error {
if !strings.HasPrefix(m.Content, c.prefix) {
return nil
}
cmd := strings.Fields(
strings.Replace(m.Content, c.prefix, "", 1),
)
if len(cmd) == 0 {
return nil
}
switch cmd[0] {
case "cool":
g := c.states.game(m.ChannelID)
if g == nil {
return ErrNoGame
}
return c.coolThing(g, s, m.ChannelID)
case "cancel":
g := c.states.game(m.ChannelID)
if g == nil {
return ErrNoGame
}
allow := false
// TODO: {lpf} I'm not sure if we need to include guildIDs or
// just role to avoid other guild admins to cancel the game in
// 'this' guild
for _, mr := range m.Member.Roles {
r := fmt.Sprintf("%s:%s", m.GuildID, mr)
if _, ok := c.adminRoles[r]; ok {
allow = true
break
}
}
if !allow {
return GameError("")
}
if err := g.Draw(chess.DrawOffer); err != nil {
return err
}
return c.checkOutcome(g, s, m.ChannelID)
case "say":
msg := strings.Replace(m.Content, c.prefix+"say", "", 1)
if msg == "" {
return nil
}
_, err := s.ChannelMessageSend(
m.ChannelID,
msg,
)
return err
case "help":
_, err := s.ChannelMessageSend(
m.ChannelID,
fmt.Sprintf(help, c.prefix),
)
return err
case "play":
if g := c.states.game(m.ChannelID); g != nil {
return GameError(fmt.Sprintf("Game in Process <@%s> vs <@%s>", g.whiteID, g.blackID))
}
// check for mentions
if len(cmd) != 3 || len(m.Mentions) != 2 {
return GameError(fmt.Sprintf("Start a game with `%splay @player1 @player2`", c.prefix))
}
// verify channel name
channel, err := s.Channel(m.ChannelID)
if err != nil {
return err
}
if !c.channelRE.MatchString(channel.Name) {
return GameError("wrong room")
}
if err := s.MessageReactionAdd(m.ChannelID, m.ID, "✅"); err != nil {
return err
}
// if one of the mentions is the bot we initialize internal stockfish in this game
initUCI := false
if m.Mentions[0].ID == s.State.User.ID || m.Mentions[1].ID == s.State.User.ID {
if _, err := s.ChannelMessageSend(m.ChannelID, "Trying to play with AI"); err != nil {
return err
}
initUCI = true
}
g, err := c.states.newGame(
m.ChannelID,
m.Mentions[0].ID,
m.Mentions[1].ID,
initUCI,
)
if err != nil {
return GameError(fmt.Sprint("Error starting game: ", err))
}
return c.checkOutcome(g, s, m.ChannelID)
case "move":
g := c.states.game(m.ChannelID)
if g == nil {
return ErrNoGame
}
if m.Author.ID != g.turn() {
return GameError("")
}
if len(cmd) < 2 {
_, err := s.ChannelMessageSendReply(
m.ChannelID,
fmt.Sprint("Valid moves:", validMovesStr(g)),
&discordgo.MessageReference{
ChannelID: m.ChannelID,
MessageID: m.ID,
},
)
return err
}
if err := g.MoveStr(cmd[1]); err != nil {
return GameError(fmt.Sprint("Invalid move\nAvailable: ", validMovesStr(g)))
}
if err := s.MessageReactionAdd(m.ChannelID, m.ID, "✅"); err != nil {
return err
}
return c.checkOutcome(g, s, m.ChannelID)
case "board":
g := c.states.game(m.ChannelID)
if g == nil {
return ErrNoGame
}
return c.checkOutcome(g, s, m.ChannelID)
// Useless for now but might be usefull if we have some kind of ranking
case "draw":
g := c.states.game(m.ChannelID)
if g == nil {
return ErrNoGame
}
if m.Author.ID != g.whiteID && m.Author.ID != g.blackID {
return GameError("")
}
if err := s.MessageReactionAdd(m.ChannelID, m.ID, "✅"); err != nil {
return err
}
if g.draw(m.Author.ID) {
g.Draw(chess.DrawOffer)
return c.checkOutcome(g, s, m.ChannelID)
}
other := g.whiteID
if other == m.Author.ID {
other = g.blackID
}
_, err := s.ChannelMessageSend(
m.ChannelID,
fmt.Sprintf("<@%s> send `%sdraw` to accept", other, c.prefix),
)
return err
case "resign":
g := c.states.game(m.ChannelID)
if g == nil {
return ErrNoGame
}
if m.Author.ID != g.turn() {
return GameError("")
}
g.Resign(g.Position().Turn())
return c.checkOutcome(g, s, m.ChannelID)
}
return nil
}
// checkOutcome will send the board, check for outcome and send the game status
// if game is over it will delete from game states
// if the turn() id is same as bot it will use uci to make a move and recheck
// outcome.
func (c *ChessHandler) checkOutcome(g *game, s *discordgo.Session, channelID string) error {
if err := c.sendBoard(g, s, channelID); err != nil {
log.Println("failed to rasterize the board:", err)
// Send the board in text mode if sendBoard fails
_, err := s.ChannelMessageSend(
channelID,
fmt.Sprintf("```\n%s\n```\n", g.Position().Board().Draw()),
)
if err != nil {
return err
}
}
if o := g.Outcome(); o != chess.NoOutcome {
return c.GameOver(g, s, channelID)
}
if _, err := s.ChannelMessageSend(channelID, fmt.Sprintf("<@%s> turn!", g.turn())); err != nil {
return err
}
// Bot move area
if s.State.User.ID != g.turn() {
return nil
}
err := g.eng.Run(
uci.CmdPosition{Position: g.Position()},
uci.CmdGo{MoveTime: time.Second / 10},
)
if err != nil {
return err
}
if err := g.Move(g.eng.SearchResults().BestMove); err != nil {
return err
}
// yeah check again cause bot moved, unless we are running @bot @bot
// which is virtually impossible, this should be safe
return c.checkOutcome(g, s, channelID)
}
// GameOver sends game finish Card.
func (c *ChessHandler) GameOver(g *game, s *discordgo.Session, channelID string) error {
defer c.states.done(channelID)
var winner string
method := g.Method().String()
whiteStatus, whiteEmoji := "draw", ""
blackStatus, blackEmoji := "draw", ""
switch g.Outcome() {
case chess.WhiteWon:
winner = g.whiteID
whiteStatus, whiteEmoji = "Win", ":tada:"
blackStatus, blackEmoji = "Lose", ":thumbsdown:"
case chess.BlackWon:
winner = g.blackID
whiteStatus, whiteEmoji = "Lose", ":thumbsdown:"
blackStatus, blackEmoji = "Win", ":tada:"
}
var avatarurl string
if winner != "" {
user, err := s.User(winner)
if err != nil {
return err
}
avatarurl = user.AvatarURL("128x128")
}
gi, err := c.boardGIF(g)
if err != nil {
return err
}
_, err = s.ChannelMessageSendEmbed(
channelID,
&discordgo.MessageEmbed{
Title: "Game over",
Description: method,
Color: 0x5d<<16 | 0xC9<<8 | 0xE2, // gopher color "#5DC9E2"
Thumbnail: &discordgo.MessageEmbedThumbnail{
URL: avatarurl,
},
Fields: []*discordgo.MessageEmbedField{
{
Name: whiteStatus,
Value: fmt.Sprintf("%s <@%s>", whiteEmoji, g.whiteID),
Inline: true,
},
{
Name: blackStatus,
Value: fmt.Sprintf("%s <@%s>", blackEmoji, g.blackID),
Inline: true,
},
{
Name: "Game:",
Value: g.String(),
Inline: false,
},
},
},
)
if err != nil {
return err
}
gifr, pw := io.Pipe()
go func() {
pw.CloseWithError(gif.EncodeAll(pw, gi))
}()
_, err = s.ChannelFileSend(channelID, "board.gif", gifr)
return err
}
func (c *ChessHandler) coolThing(g *game, s *discordgo.Session, channelID string) error {
gi, err := c.boardGIF(g)
if err != nil {
return err
}
pr, pw := io.Pipe()
go func() {
pw.CloseWithError(gif.EncodeAll(pw, gi))
}()
_, err = s.ChannelFileSend(channelID, "board.gif", pr)
return err
}
// Draw using the drawer :tada:
func (c *ChessHandler) sendBoard(g *game, s *discordgo.Session, channelID string) error {
pr, pw := io.Pipe()
defer pr.Close()
im, err := c.boardImage(g)
if err != nil {
return err
}
go func() {
pw.CloseWithError(png.Encode(pw, im))
}()
if _, err := s.ChannelFileSend(channelID, "board.png", pr); err != nil {
return err
}
if o := book.Find(g.Moves()); o != nil {
_, err = s.ChannelMessageSend(channelID, o.Title())
}
return err
}
func (c *ChessHandler) boardImage(g *game) (*image.RGBA, error) {
markColor := color.RGBA{55, 55, 155, 100}
marks := []chessimage.Mark{}
moves := g.Moves()
if len(moves) != 0 {
last := moves[len(moves)-1]
marks = []chessimage.Mark{
{
Color: markColor,
Pos: [][2]int{
{int(last.S1().File()), 7 - int(last.S1().Rank())},
{int(last.S2().File()), 7 - int(last.S2().Rank())},
},
},
}
}
return c.drawer.Image(g.Position().String(), marks...)
}
func (c *ChessHandler) boardGIF(g *game) (*gif.GIF, error) {
gi := gif.GIF{
Config: image.Config{
Width: 512,
Height: 512,
},
}
markColor := color.RGBA{100, 100, 200, 255}
moves := g.Moves()
gg := chess.NewGame()
frame, err := c.drawer.ImagePaletted(gg.Position().String())
if err != nil {
return nil, err
}
gi.Image = append(gi.Image, frame)
gi.Delay = append(gi.Delay, 150)
for i, m := range moves {
gg.Move(m)
frame, err := c.drawer.ImagePaletted(
gg.Position().String(),
chessimage.Mark{
Color: markColor,
Pos: [][2]int{
{int(m.S1().File()), 7 - int(m.S1().Rank())},
{int(m.S2().File()), 7 - int(m.S2().Rank())},
},
},
)
if err != nil {
return nil, err
}
gi.Image = append(gi.Image, frame)
delay := 150
if i == len(moves)-1 {
delay = 500
}
gi.Delay = append(gi.Delay, delay)
}
return &gi, nil
}
func validMovesStr(g *game) string {
buf := &bytes.Buffer{}
moves := g.ValidMoves()
fmt.Fprintf(buf, "\n```")
enc := chess.AlgebraicNotation{}
var lastPiece chess.Piece
for _, m := range moves {
p := g.Position().Board().Piece(m.S1())
if p != lastPiece {
fmt.Fprintf(buf, "\n%s - ", p.String())
lastPiece = p
}
fmt.Fprintf(buf, "%s ",
enc.Encode(g.Position(), m),
)
}
fmt.Fprintf(buf, "\n```")
return buf.String()
}