/
games.go
939 lines (750 loc) · 26.4 KB
/
games.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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
package biasgame
import (
"bytes"
"errors"
"fmt"
"image"
"image/draw"
"image/png"
"math/rand"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/Seklfreak/Robyul2/modules/plugins/idols"
"github.com/Seklfreak/Robyul2/cache"
"github.com/Seklfreak/Robyul2/helpers"
"github.com/bwmarrin/discordgo"
"github.com/nfnt/resize"
)
const (
IMAGE_RESIZE_HEIGHT = 150
LEFT_ARROW_EMOJI = "⬅"
RIGHT_ARROW_EMOJI = "➡"
ARROW_FORWARD_EMOJI = "▶"
ARROW_BACKWARD_EMOJI = "◀"
)
// misc images
var versesImage image.Image
var winnerBracket image.Image
var shadowBorder image.Image
// currently running single or multiplayer games
var currentSinglePlayerGames map[string]*singleBiasGame
var currentSinglePlayerGamesMutex sync.RWMutex
var currentMultiPlayerGames []*multiBiasGame
var currentMultiPlayerGamesMutex sync.RWMutex
// game configs
var allowedGameSizes map[int]bool
var allowedMultiGameSizes map[int]bool
// top 8 bracket
var bracketImageOffsets map[int]image.Point
var bracketImageResizeMap map[int]uint
/////////////////////////////////
// SINGLE GAME FUNCTIONS //
/////////////////////////////////
// createSinglePlayerGame will setup a singleplayer game for the user
func createOrGetSinglePlayerGame(msg *discordgo.Message, commandArgs []string) *singleBiasGame {
var singleGame *singleBiasGame
// check if the user has a current game already going.
// if so update the channel id for the game incase the user tried starting the game from another server
if game := getSinglePlayerGameByUserID(msg.Author.ID); game != nil {
// if the user already had a game going, let them know to avoid confusion if they
// tried starting another game a long time after the first
//
// -- not sure if i want this or not...
// msg, err := helpers.SendMessage(msg.ChannelID, "biasgame.game.resuming-game")
// if err == nil {
// go utils.DeleteImageWithDelay(msg, time.Second*10)
// }
game.ChannelID = msg.ChannelID
game.GuildID = msg.GuildID
singleGame = game
} else {
var biasChoices []*idols.Idol
gameGender := "mixed"
gameSize := 32
// validate game arguments
if len(commandArgs) > 0 {
for _, arg := range commandArgs {
// gender check
if gender, ok := gameGenders[arg]; ok == true {
gameGender = gender
continue
}
// game size check
if requestedGameSize, err := strconv.Atoi(arg); err == nil {
if _, ok := allowedGameSizes[requestedGameSize]; ok == true {
gameSize = requestedGameSize
continue
} else {
helpers.SendMessage(msg.ChannelID, helpers.GetText("plugins.biasgame.game.invalid-game-size"))
return nil
}
}
// if a arg was passed that didn't match any check, send invalid args message
helpers.SendMessage(msg.ChannelID, helpers.GetText("bot.arguments.invalid"))
return nil
}
}
// if this isn't a mixed game then filter all choices by the gender
if gameGender != "mixed" {
for _, bias := range idols.GetActiveIdols() {
if bias.Gender == gameGender {
biasChoices = append(biasChoices, bias)
}
}
} else {
biasChoices = idols.GetActiveIdols()
}
// confirm we have enough biases to choose from for the game size this should be
if len(biasChoices) < gameSize {
helpers.SendMessage(msg.ChannelID, helpers.GetText("plugins.biasgame.game.not-enough-idols"))
return nil
}
// show a warning if the game size is >= 256, wait for confirm
if gameSize >= 256 {
if !helpers.ConfirmEmbed(msg.GuildID, msg.ChannelID, msg.Author, helpers.GetText("plugins.biasgame.game.size-warning"), "✅", "🚫") {
return nil
}
// recheck if a game is still going on, see above
if game := getSinglePlayerGameByUserID(msg.Author.ID); game != nil {
game.ChannelID = msg.ChannelID
game.GuildID = msg.GuildID
return game
}
}
// create new game
singleGame = &singleBiasGame{
User: msg.Author,
ChannelID: msg.ChannelID,
IdolsRemaining: gameSize,
ReadyForReaction: false,
Gender: gameGender,
GuildID: msg.GuildID,
}
singleGame.GameImageIndex = make(map[string]int)
// get random biases for the game
usedIndexs := make(map[int]bool)
for true {
randomIndex := rand.Intn(len(biasChoices))
if usedIndexs[randomIndex] == false {
usedIndexs[randomIndex] = true
singleGame.BiasQueue = append(singleGame.BiasQueue, biasChoices[randomIndex])
if len(singleGame.BiasQueue) == gameSize {
break
}
}
}
// save game to current running games
singleGame.saveGame()
}
return singleGame
}
// processVote is called when a valid reaction is added to a game
func (g *singleBiasGame) processVote(reaction *discordgo.MessageReactionAdd) {
defer g.recoverGame()
// check if reaction was added to the message of the game
if g.ReadyForReaction == true && g.LastRoundMessage.ID == reaction.MessageID {
winnerIndex := 0
loserIndex := 0
validReaction := false
// check if the reaction added to the message was a left or right arrow
if LEFT_ARROW_EMOJI == reaction.Emoji.Name {
winnerIndex = 0
loserIndex = 1
validReaction = true
} else if RIGHT_ARROW_EMOJI == reaction.Emoji.Name {
winnerIndex = 1
loserIndex = 0
validReaction = true
}
if validReaction == true {
g.ReadyForReaction = false
g.IdolsRemaining--
// record winners and losers for stats
g.RoundLosers = append(g.RoundLosers, g.BiasQueue[loserIndex])
g.RoundWinners = append(g.RoundWinners, g.BiasQueue[winnerIndex])
// add winner to end of bias queue and remove first two
g.BiasQueue = append(g.BiasQueue, g.BiasQueue[winnerIndex])
g.BiasQueue = g.BiasQueue[2:]
// if there is only one bias left, they are the winner
if len(g.BiasQueue) == 1 {
g.finishSingleGame()
} else {
// save the last 8 for the chart
if len(g.BiasQueue) == 8 {
g.TopEight = g.BiasQueue
}
// Sleep a bit to allow other users to see what was chosen.
// This creates conversation while the game is going and makes it a overall better experience
//
// This will also allow me to call out and harshly judge players who don't choose nayoung <3
time.Sleep(time.Second / 5)
g.sendBiasGameRound()
}
}
}
}
// sendBiasGameRound will send the message for the round
func (g *singleBiasGame) sendBiasGameRound() {
if g == nil {
return
}
defer g.recoverGame()
// if a game only has one bias in queue, they are the winner and a round should not be attempted
if len(g.BiasQueue) == 1 {
g.finishSingleGame()
return
}
// if a round message has been sent, delete before sending the next one
if g.LastRoundMessage != nil {
go cache.GetSession().SessionForGuildS(g.GuildID).ChannelMessageDelete(g.LastRoundMessage.ChannelID, g.LastRoundMessage.ID)
}
// get random images
img1 := getSemiRandomIdolImage(g.BiasQueue[0], &g.GameImageIndex)
img2 := getSemiRandomIdolImage(g.BiasQueue[1], &g.GameImageIndex)
// create round message
messageString := fmt.Sprintf("**@%s**\nIdols Remaining: %d\n%s %s vs %s %s",
g.User.Username,
g.IdolsRemaining,
g.BiasQueue[0].GroupName,
g.BiasQueue[0].Name,
g.BiasQueue[1].GroupName,
g.BiasQueue[1].Name)
// encode the combined image and compress it
buf := new(bytes.Buffer)
encoder := new(png.Encoder)
encoder.CompressionLevel = -2
encoder.Encode(buf, MakeVSImage(img1, img2))
myReader := bytes.NewReader(buf.Bytes())
// send round message
fileSendMsg, err := helpers.SendFile(g.ChannelID, "combined_pic.png", myReader, messageString)
if err != nil {
if checkPermissionError(err, g.ChannelID) {
helpers.SendMessage(g.ChannelID, helpers.GetText("bot.errors.no-file"))
}
return
}
// update game state
g.LastRoundMessage = fileSendMsg[0]
g.ReadyForReaction = true
// add reactions
cache.GetSession().SessionForGuildS(g.GuildID).MessageReactionAdd(g.ChannelID, fileSendMsg[0].ID, LEFT_ARROW_EMOJI)
go cache.GetSession().SessionForGuildS(g.GuildID).MessageReactionAdd(g.ChannelID, fileSendMsg[0].ID, RIGHT_ARROW_EMOJI)
}
// sendWinnerMessage creates the top eight brackent sends the winning message to the user
func (g *singleBiasGame) sendWinnerMessage() {
// if a round message has been sent, delete before sending the next one
if g.LastRoundMessage != nil {
cache.GetSession().SessionForGuildS(g.GuildID).ChannelMessageDelete(g.LastRoundMessage.ChannelID, g.LastRoundMessage.ID)
}
// get last 7 from winners array and combine with topEight array
winners := g.RoundWinners[len(g.RoundWinners)-7 : len(g.RoundWinners)]
bracketInfo := append(g.TopEight, winners...)
// create final image with the bounds of the winner bracket
bracketImage := image.NewRGBA(winnerBracket.Bounds())
draw.Draw(bracketImage, winnerBracket.Bounds(), winnerBracket, image.Point{0, 0}, draw.Src)
// populate winner brackent image
for i, bias := range bracketInfo {
// adjust images sizing according to placement
resizeTo := uint(50)
if newResizeVal, ok := bracketImageResizeMap[i]; ok {
resizeTo = newResizeVal
}
ri := resize.Resize(0, resizeTo, getSemiRandomIdolImage(bias, &g.GameImageIndex), resize.Lanczos3)
draw.Draw(bracketImage, ri.Bounds().Add(bracketImageOffsets[i]), ri, image.ZP, draw.Over)
}
// compress bracket image
buf := new(bytes.Buffer)
encoder := new(png.Encoder)
encoder.CompressionLevel = -2 // -2 compression is best speed, -3 is best compression but end result isn't worth the slower encoding
encoder.Encode(buf, bracketImage)
myReader := bytes.NewReader(buf.Bytes())
messageString := fmt.Sprintf("%s\nWinner: %s %s!",
g.User.Mention(),
g.GameWinnerBias.GroupName,
g.GameWinnerBias.Name)
// send message
winnerMsgs, err := helpers.SendFile(g.ChannelID, "biasgame_winner.png", myReader, messageString)
helpers.Relax(err)
// if the winner is nayoung, add a nayoung emoji <3 <3 <3
if strings.ToLower(g.GameWinnerBias.GroupName) == "pristin" && strings.ToLower(g.GameWinnerBias.Name) == "nayoung" {
cache.GetSession().SessionForGuildS(g.GuildID).MessageReactionAdd(g.ChannelID, winnerMsgs[0].ID, getRandomNayoungEmoji()) // <3
}
}
// finishSingleGame sends the winner message, records stats, and deletes game
func (g *singleBiasGame) finishSingleGame() {
defer g.recoverGame()
// delete from current games
g.deleteGame()
// used to make sure the game finish isn't triggered twice
if g.GameWinnerBias != nil {
return
}
g.GameWinnerBias = g.BiasQueue[0]
g.sendWinnerMessage()
// record game stats
go func(g *singleBiasGame) {
defer helpers.Recover()
recordSingleGamesStats(g)
}(g)
}
// recoverGame if a panic was caused during the game, delete from current games
func (g *singleBiasGame) recoverGame() {
if r := recover(); r != nil {
// delete from current games
g.deleteGame()
// re-panic so it gets handled and logged correctly
panic(r)
}
}
// saveGame saves the game to the currently running games
func (g *singleBiasGame) saveGame() {
currentSinglePlayerGamesMutex.Lock()
defer currentSinglePlayerGamesMutex.Unlock()
currentSinglePlayerGames[g.User.ID] = g
}
// saveGame saves the game to the currently running games
func (g *singleBiasGame) deleteGame() {
currentSinglePlayerGamesMutex.Lock()
defer currentSinglePlayerGamesMutex.Unlock()
delete(currentSinglePlayerGames, g.User.ID)
}
/////////////////////////////////
// MULTI GAME FUNCTIONS //
/////////////////////////////////
// startMultiPlayerGame will create and start a multiplayer game
func startMultiPlayerGame(msg *discordgo.Message, commandArgs []string) {
// check if a multi game is already running in the current channel
if game := getMultiPlayerGameByChannelID(msg.ChannelID); game != nil {
// resume game if it was stopped
if game.GameIsRunning == false {
game.processMultiGame()
return
}
helpers.SendMessage(msg.ChannelID, helpers.GetText("plugins.biasgame.game.multi-game-running"))
return
}
commandArgs = commandArgs[1:]
gameGender := "mixed"
multiGameSize := 32
// validate multi game options
if len(commandArgs) > 0 {
for _, arg := range commandArgs {
// gender check
if gender, ok := gameGenders[arg]; ok == true {
gameGender = gender
continue
}
// game size check
if requestedGameSize, err := strconv.Atoi(arg); err == nil {
if _, ok := allowedMultiGameSizes[requestedGameSize]; ok == true {
multiGameSize = requestedGameSize
continue
} else {
helpers.SendMessage(msg.ChannelID, helpers.GetText("plugins.biasgame.game.invalid-game-size-multi"))
return
}
}
// if a arg was passed that didn't match any check, send invalid args message
helpers.SendMessage(msg.ChannelID, helpers.GetText("bot.arguments.invalid"))
return
}
}
var biasChoices []*idols.Idol
// if this isn't a mixed game then filter all choices by the gender
if gameGender != "mixed" {
for _, bias := range idols.GetActiveIdols() {
if bias.Gender == gameGender {
biasChoices = append(biasChoices, bias)
}
}
} else {
biasChoices = idols.GetActiveIdols()
}
// confirm we have enough biases for a multiplayer game
if len(biasChoices) < multiGameSize {
helpers.SendMessage(msg.ChannelID, helpers.GetText("plugins.biasgame.game.not-enough-idols"))
return
}
// create new game
multiGame := &multiBiasGame{
ChannelID: msg.ChannelID,
IdolsRemaining: multiGameSize,
Gender: gameGender,
RoundDelay: 5,
GameIsRunning: true,
guildID: msg.GuildID,
}
multiGame.GameImageIndex = make(map[string]int)
// get random biases for the game
usedIndexs := make(map[int]bool)
for true {
randomIndex := rand.Intn(len(biasChoices))
if usedIndexs[randomIndex] == false {
usedIndexs[randomIndex] = true
multiGame.BiasQueue = append(multiGame.BiasQueue, biasChoices[randomIndex])
if len(multiGame.BiasQueue) == multiGame.IdolsRemaining {
break
}
}
}
// save game to current running games
multiGame.saveGame()
multiGame.processMultiGame()
}
// sendMultiBiasGameRound sends the next round for the multi game
func (g *multiBiasGame) sendMultiBiasGameRound() error {
if g == nil {
return errors.New("Game is nil")
}
// if a round message has been sent, delete before sending the next one
if g.LastRoundMessage != nil {
cache.GetSession().SessionForGuildS(g.guildID).ChannelMessageDelete(g.LastRoundMessage.ChannelID, g.LastRoundMessage.ID)
}
// get random images to use
img1 := getSemiRandomIdolImage(g.BiasQueue[0], &g.GameImageIndex)
img2 := getSemiRandomIdolImage(g.BiasQueue[1], &g.GameImageIndex)
// create round message
messageString := fmt.Sprintf("**Multi Game**\nIdols Remaining: %d\n%s %s vs %s %s",
g.IdolsRemaining,
g.BiasQueue[0].GroupName,
g.BiasQueue[0].Name,
g.BiasQueue[1].GroupName,
g.BiasQueue[1].Name)
// encode the combined image and compress it
buf := new(bytes.Buffer)
encoder := new(png.Encoder)
encoder.CompressionLevel = -2 // -2 compression is best speed, -3 is best compression but end result isn't worth the slower encoding
encoder.Encode(buf, MakeVSImage(img1, img2))
myReader := bytes.NewReader(buf.Bytes())
// send round message
fileSendMsg, err := helpers.SendFile(g.ChannelID, "combined_pic.png", myReader, messageString)
if err != nil {
// check if error is a permissions error, if not retry send the round
if checkPermissionError(err, g.ChannelID) {
helpers.SendMessage(g.ChannelID, helpers.GetText("bot.errors.no-file"))
return errors.New("Could not send round")
} else {
for i := 0; i < 10; i++ {
time.Sleep(time.Second * 1)
myReader = bytes.NewReader(buf.Bytes())
fileSendMsg, err = helpers.SendFile(g.ChannelID, "combined_pic.png", myReader, messageString)
if err == nil {
break
}
}
// if after the retries an error still exists, return
if err != nil {
return err
}
}
}
// add reactions
cache.GetSession().SessionForGuildS(g.guildID).MessageReactionAdd(g.ChannelID, fileSendMsg[0].ID, LEFT_ARROW_EMOJI)
cache.GetSession().SessionForGuildS(g.guildID).MessageReactionAdd(g.ChannelID, fileSendMsg[0].ID, RIGHT_ARROW_EMOJI)
// update game state
g.CurrentRoundMessageId = fileSendMsg[0].ID
g.LastRoundMessage = fileSendMsg[0]
return nil
}
// start multi game loop. every 10 seconds count the number of arrow reactions. whichever side has most wins
func (g *multiBiasGame) processMultiGame() {
for g.IdolsRemaining != 1 {
// send next rounds and sleep
err := g.sendMultiBiasGameRound()
if err != nil {
g.GameIsRunning = false
return
}
time.Sleep(time.Second * time.Duration(g.RoundDelay))
g.GameIsRunning = true
// get current round message
message, err := cache.GetSession().SessionForGuildS(g.guildID).ChannelMessage(g.ChannelID, g.CurrentRoundMessageId)
if err != nil {
g.GameIsRunning = false
return
}
leftCount := 0
rightCount := 0
// check which reaction has most votes
for _, reaction := range message.Reactions {
// ignore reactions not from bot
if reaction.Me == false {
continue
}
if reaction.Emoji.Name == LEFT_ARROW_EMOJI {
leftCount = reaction.Count
}
if reaction.Emoji.Name == RIGHT_ARROW_EMOJI {
rightCount = reaction.Count
}
}
// adjust next round delay based on amount of votes
g.adjustRoundDelay(leftCount + rightCount)
winnerIndex := 0
loserIndex := 0
randomWin := false
// check if the reaction added to the message was a left or right arrow
if leftCount > rightCount {
winnerIndex = 0
loserIndex = 1
} else if leftCount < rightCount {
winnerIndex = 1
loserIndex = 0
} else {
// if votes are even, choose one at random
randomNumber := rand.Intn(100)
randomWin = true
if randomNumber >= 50 {
winnerIndex = 1
loserIndex = 0
} else {
winnerIndex = 0
loserIndex = 1
}
}
// if a random winner was chosen, display an arrow indication who the random winner was
if randomWin == true {
if winnerIndex == 1 {
cache.GetSession().SessionForGuildS(g.guildID).MessageReactionAdd(g.ChannelID, g.CurrentRoundMessageId, ARROW_FORWARD_EMOJI)
} else {
cache.GetSession().SessionForGuildS(g.guildID).MessageReactionAdd(g.ChannelID, g.CurrentRoundMessageId, ARROW_BACKWARD_EMOJI)
}
time.Sleep(time.Millisecond * 1500)
}
g.IdolsRemaining--
// record winners and losers for stats
g.RoundLosers = append(g.RoundLosers, g.BiasQueue[loserIndex])
g.RoundWinners = append(g.RoundWinners, g.BiasQueue[winnerIndex])
// add winner to end of bias queue and remove first two
g.BiasQueue = append(g.BiasQueue, g.BiasQueue[winnerIndex])
g.BiasQueue = g.BiasQueue[2:]
// save the last 8 for the chart
if len(g.BiasQueue) == 8 {
g.TopEight = g.BiasQueue
}
}
g.GameWinnerBias = g.BiasQueue[0]
g.sendWinnerMessage()
// record game stats
go func(g *multiBiasGame) {
defer helpers.Recover()
recordMultiGamesStats(g)
}(g)
g.deleteGame()
}
// removes game from current multi games
func (g *multiBiasGame) deleteGame() {
currentMultiPlayerGamesMutex.Lock()
currentMultiPlayerGamesMutex.Unlock()
for i, game := range currentMultiPlayerGames {
if game.CurrentRoundMessageId == g.CurrentRoundMessageId {
currentMultiPlayerGames = append(currentMultiPlayerGames[:i], currentMultiPlayerGames[i+1:]...)
}
}
}
// saveGame save to currently running multi games
func (g *multiBiasGame) saveGame() {
currentMultiPlayerGamesMutex.Lock()
currentMultiPlayerGamesMutex.Unlock()
currentMultiPlayerGames = append(currentMultiPlayerGames, g)
}
// sendWinnerMessage creates the top eight brackent sends the winning message to the user
//
// note: i realize this function is the exact same as the single game version,
// but im going to choose to keep these and seporate functions to make any
// future changes to the games easier
func (g *multiBiasGame) sendWinnerMessage() {
// if a round message has been sent, delete before sending the next one
if g.LastRoundMessage != nil {
cache.GetSession().SessionForGuildS(g.guildID).ChannelMessageDelete(g.LastRoundMessage.ChannelID, g.LastRoundMessage.ID)
}
// get last 7 from winners array and combine with topEight array
winners := g.RoundWinners[len(g.RoundWinners)-7 : len(g.RoundWinners)]
bracketInfo := append(g.TopEight, winners...)
// create final image with the bounds of the winner bracket
bracketImage := image.NewRGBA(winnerBracket.Bounds())
draw.Draw(bracketImage, winnerBracket.Bounds(), winnerBracket, image.Point{0, 0}, draw.Src)
// populate winner brackent image
for i, bias := range bracketInfo {
// adjust images sizing according to placement
resizeTo := uint(50)
if newResizeVal, ok := bracketImageResizeMap[i]; ok {
resizeTo = newResizeVal
}
ri := resize.Resize(0, resizeTo, getSemiRandomIdolImage(bias, &g.GameImageIndex), resize.Lanczos3)
draw.Draw(bracketImage, ri.Bounds().Add(bracketImageOffsets[i]), ri, image.ZP, draw.Over)
}
// compress bracket image
buf := new(bytes.Buffer)
encoder := new(png.Encoder)
encoder.CompressionLevel = -2 // -2 compression is best speed, -3 is best compression but end result isn't worth the slower encoding
encoder.Encode(buf, bracketImage)
myReader := bytes.NewReader(buf.Bytes())
messageString := fmt.Sprintf("**Multi Game**\nWinner: %s %s!",
g.GameWinnerBias.GroupName,
g.GameWinnerBias.Name)
// send message
helpers.SendFile(g.ChannelID, "biasgame_multi_winner.png", myReader, messageString)
}
// adjusts the next round delay based on the amount of votes
// will only adjust by 1 second at a time so changes are not drastic
func (g *multiBiasGame) adjustRoundDelay(lastRoundVoteCount int) {
// remove bots reaction count
lastRoundVoteCount -= 2
// amount of votes => max delay
voteCountDelayMap := map[int]int{
0: 3,
1: 4,
2: 5,
5: 6,
12: 7,
}
// get desired delay based on vote count
var targetDelay int
for reqVoteCount, delay := range voteCountDelayMap {
if lastRoundVoteCount >= reqVoteCount && delay > targetDelay {
targetDelay = delay
}
}
// if the new target delay is different than the current, adjust by 1 second
if g.RoundDelay < targetDelay {
g.RoundDelay++
} else if g.RoundDelay > targetDelay {
g.RoundDelay--
}
}
///////////////////////
// UTILITY FUNCTIONS //
///////////////////////
// gets a specific single player game for a UserID
// if the User currently has no game ongoing it will return nil
// will delete the game if a nil game is found
func getSinglePlayerGameByUserID(userID string) *singleBiasGame {
if userID == "" {
return nil
}
currentSinglePlayerGamesMutex.RLock()
game, ok := currentSinglePlayerGames[userID]
currentSinglePlayerGamesMutex.RUnlock()
// if a game is found and is nil, delete it
if ok && game == nil {
currentSinglePlayerGamesMutex.Lock()
delete(currentSinglePlayerGames, userID)
currentSinglePlayerGamesMutex.Unlock()
}
return game
}
// gets a specific multi player game for a channelID
// returns nil if no games were found in the channel
func getMultiPlayerGameByChannelID(channelID string) *multiBiasGame {
if channelID == "" {
return nil
}
for _, game := range getCurrentMultiPlayerGames() {
if game.ChannelID == channelID {
return game
}
}
return nil
}
// gets all currently ongoing single player games
func getCurrentSinglePlayerGames() map[string]*singleBiasGame {
currentSinglePlayerGamesMutex.RLock()
defer currentSinglePlayerGamesMutex.RUnlock()
// copy data to prevent race conditions
gamesCopy := make(map[string]*singleBiasGame)
for key, value := range currentSinglePlayerGames {
gamesCopy[key] = value
}
return gamesCopy
}
// gets all currently ongoing multi player games
func getCurrentMultiPlayerGames() []*multiBiasGame {
currentMultiPlayerGamesMutex.RLock()
defer currentMultiPlayerGamesMutex.RUnlock()
// copy data to prevent race conditions
gamesCopy := make([]*multiBiasGame, len(currentMultiPlayerGames))
for i, value := range currentMultiPlayerGames {
gamesCopy[i] = value
}
return gamesCopy
}
// startCacheRefreshLoop will refresh the cache for biasgames
func startCacheRefreshLoop() {
bgLog().Info("Starting biasgame current games cache loop")
go func() {
defer helpers.Recover()
for {
time.Sleep(time.Second * 30)
// save any currently running games
currentSinglePlayerGamesMutex.RLock()
err := setBiasGameCache("currentSinglePlayerGames", currentSinglePlayerGames, 0)
currentSinglePlayerGamesMutex.RUnlock()
helpers.Relax(err)
bgLog().Infof("Cached %d singleplayer biasgames to redis", len(currentSinglePlayerGames))
currentMultiPlayerGamesMutex.RLock()
err = setBiasGameCache("currentMultiPlayerGames", currentMultiPlayerGames, 0)
currentMultiPlayerGamesMutex.RUnlock()
helpers.Relax(err)
bgLog().Infof("Cached %d multiplayer biasgames to redis", len(currentMultiPlayerGames))
}
}()
}
// loadMiscImages handles loading other images besides the idol images
func loadMiscImages() {
var crown image.Image
validMiscImages := []string{
"verses.png",
"top-eight-bracket.png",
"shadow-border.png",
"crown.png",
}
miscImagesFolderPath := helpers.GetConfig().Path("assets_folder").Data().(string) + "biasgame/misc/"
// load misc images
for _, fileName := range validMiscImages {
// check if file exists
filePath := miscImagesFolderPath + fileName
if _, err := os.Stat(filePath); os.IsNotExist(err) {
helpers.Relax(err)
}
// open file and decode it
file, err := os.Open(filePath)
helpers.Relax(err)
img, _, err := image.Decode(file)
helpers.Relax(err)
// resize misc images as needed
switch fileName {
case "verses.png":
versesImage = resize.Resize(0, IMAGE_RESIZE_HEIGHT+30, img, resize.Lanczos3)
case "shadow-border.png":
shadowBorder = resize.Resize(0, IMAGE_RESIZE_HEIGHT+30, img, resize.Lanczos3)
case "crown.png":
crown = resize.Resize(IMAGE_RESIZE_HEIGHT/2, 0, img, resize.Lanczos3)
case "top-eight-bracket.png":
winnerBracket = img
}
bgLog().Infof("Loading biasgame misc image: %s", fileName)
}
// append crown to top eight
bracketImage := image.NewRGBA(winnerBracket.Bounds())
draw.Draw(bracketImage, winnerBracket.Bounds(), winnerBracket, image.Point{0, 0}, draw.Src)
draw.Draw(bracketImage, crown.Bounds().Add(image.Pt(230, 5)), crown, image.ZP, draw.Over)
winnerBracket = bracketImage.SubImage(bracketImage.Rect)
}
// MakeVSImage will make the image that shows for rounds in the biasgame
func MakeVSImage(img1, img2 image.Image) image.Image {
// resize images if needed
if img1.Bounds().Dy() != IMAGE_RESIZE_HEIGHT || img2.Bounds().Dy() != IMAGE_RESIZE_HEIGHT {
img1 = resize.Resize(0, IMAGE_RESIZE_HEIGHT, img1, resize.Lanczos3)
img2 = resize.Resize(0, IMAGE_RESIZE_HEIGHT, img2, resize.Lanczos3)
}
// give shadow border
img1 = giveImageShadowBorder(img1, 15, 15)
img2 = giveImageShadowBorder(img2, 15, 15)
// combind images
img1 = helpers.CombineTwoImages(img1, versesImage)
return helpers.CombineTwoImages(img1, img2)
}