-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.go
96 lines (77 loc) · 2.43 KB
/
button.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
package main
import (
"log"
"strconv"
"strings"
tb "gopkg.in/tucnak/telebot.v2"
)
var (
// Buttons
startMatchBtn = &tb.Btn{Text: "▶ Start Match"}
continueMatchBtn = &tb.Btn{Text: "▶ Continue Match"}
numCardsBtn4 = &tb.Btn{Text: "4"}
numCardsBtn5 = &tb.Btn{Text: "5"}
selectCardBtn = &tb.Btn{Unique: "select_card_btn"}
addNumberCardBtn = &tb.Btn{Text: "🔢 Number", Unique: "add_card_number"}
addColorCardBtn = &tb.Btn{Text: "🎨 Color", Unique: "add_card_color"}
removeInfoBtn = &tb.Btn{Text: "❌ Remove Info", Unique: "remove_info"}
selectNumberCardBtn = &tb.Btn{Unique: "add_card_number_info"}
selectColorCardBtn = &tb.Btn{Unique: "add_card_color_info"}
)
// ButtonData contains all data about button
type ButtonData struct {
CardIndex *int
SelectedNumberData *NumberType
SelectedColorData *ColorType
}
// newButton initialize a new button with the initial values
func newButton(text, unique string, data ButtonData) *tb.Btn {
return &tb.Btn{
Text: text,
Unique: unique,
Data: data.ToString(),
}
}
// ToString convert all button data to string
func (btn *ButtonData) ToString() (btnDataString string) {
syncBtnValues(btn, &btnDataString)
return
}
// Parse parse string data to button struct
func (btn *ButtonData) Parse(btnDataString string) {
syncBtnValues(btn, &btnDataString)
}
// syncBtnValues sync a button data to struct or string
func syncBtnValues(btnData *ButtonData, btnDataString *string) {
var (
btnDataArray []string
err error
)
if *btnDataString != "" {
btnDataArray = strings.Split(*btnDataString, "|")
} else {
btnDataArray = make([]string, 3)
}
if btnData.CardIndex != nil {
btnDataArray[0] = ValueToString(btnData.CardIndex)
} else if btnDataArray[0] != "" {
var cardIndex int64
if cardIndex, err = strconv.ParseInt(btnDataArray[0], 10, 0); err != nil {
log.Fatal(err)
}
btnData.CardIndex = GetIntPointer(int(cardIndex))
}
if btnData.SelectedColorData != nil {
btnDataArray[1] = ValueToString(btnData.SelectedColorData)
} else if btnDataArray[1] != "" {
var colorData = ColorType(btnDataArray[1])
btnData.SelectedColorData = &colorData
}
if btnData.SelectedNumberData != nil {
btnDataArray[2] = ValueToString(btnData.SelectedNumberData)
} else if btnDataArray[2] != "" {
var numberData = NumberType(btnDataArray[2])
btnData.SelectedNumberData = &numberData
}
*btnDataString = strings.Join(btnDataArray, "|")
}