-
Notifications
You must be signed in to change notification settings - Fork 0
/
card.go
137 lines (110 loc) · 3.18 KB
/
card.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
package main
import (
"log"
tb "gopkg.in/tucnak/telebot.v2"
)
// ColorType represents a type of card color
type ColorType string
// NumberType represents a type of card number
type NumberType string
// unknown represents unknown data about card
const unknown = "❓"
// Card contains all data about card
type Card struct {
Color ColorType
Number NumberType
}
// HasAnyInfo verify if exists any info about card
func (card *Card) HasAnyInfo() bool {
return card.Color != unknown || card.Number != unknown
}
// newCard initialize a new card with the initial values
func newCard() Card {
return Card{Color: unknown, Number: unknown}
}
// Handlers
func addNumberCardHandler(c *tb.Callback) {
var (
btnData ButtonData
numberInfoSelector = bot.NewMarkup()
err error
)
btnData.Parse(c.Data)
for ii := 1; ii <= 5; ii++ {
btnData.SelectedNumberData = &numbersEmojis[ii]
btn := newButton(string(numbersEmojis[ii]), selectNumberCardBtn.Unique, btnData).Inline()
numberInfoSelector.InlineKeyboard = append(numberInfoSelector.InlineKeyboard, []tb.InlineButton{*btn})
}
if _, err = bot.Edit(c.Message, "What number to add a "+positionsText[*btnData.CardIndex]+" Card?", numberInfoSelector); err != nil {
log.Fatal(err)
}
if err = bot.Respond(c, &tb.CallbackResponse{}); err != nil {
log.Fatal(err)
}
}
func selectNumberCardHandler(c *tb.Callback) {
var (
btnData ButtonData
err error
)
btnData.Parse(c.Data)
for ii := range numbersEmojis {
if numbersEmojis[ii] == NumberType(*btnData.SelectedNumberData) {
matches[c.Sender.ID].Cards[*btnData.CardIndex].Number = numbersEmojis[ii]
break
}
}
matches[c.Sender.ID].ShowCards(c.Sender, c.Message)
if err = bot.Respond(c, &tb.CallbackResponse{}); err != nil {
log.Fatal(err)
}
}
func addColorCardHandler(c *tb.Callback) {
var (
btnData ButtonData
err error
)
btnData.Parse(c.Data)
colorInfoSelector := bot.NewMarkup()
for ii := 0; ii < len(colorEmojis); ii++ {
btnData.SelectedColorData = &colorEmojis[ii]
btn := newButton(string(colorEmojis[ii]), selectColorCardBtn.Unique, btnData).Inline()
colorInfoSelector.InlineKeyboard = append(colorInfoSelector.InlineKeyboard, []tb.InlineButton{*btn})
}
if _, err = bot.Edit(c.Message, "What color to add a "+positionsText[*btnData.CardIndex]+" Card?", colorInfoSelector); err != nil {
log.Fatal(err)
}
if err = bot.Respond(c, &tb.CallbackResponse{}); err != nil {
log.Fatal(err)
}
}
func selectColorCardHandler(c *tb.Callback) {
var (
btnData ButtonData
err error
)
btnData.Parse(c.Data)
for ii := range numbersEmojis {
if colorEmojis[ii] == ColorType(*btnData.SelectedColorData) {
matches[c.Sender.ID].Cards[*btnData.CardIndex].Color = colorEmojis[ii]
break
}
}
matches[c.Sender.ID].ShowCards(c.Sender, c.Message)
if err = bot.Respond(c, &tb.CallbackResponse{}); err != nil {
log.Fatal(err)
}
}
func removeInfoCardHandler(c *tb.Callback) {
var (
btnData ButtonData
err error
)
btnData.Parse(c.Data)
var match = matches[c.Sender.ID]
match.Cards[*btnData.CardIndex] = newCard()
match.ShowCards(c.Sender, c.Message)
if err = bot.Respond(c, &tb.CallbackResponse{}); err != nil {
log.Fatal(err)
}
}