/
percent-command.go
103 lines (95 loc) · 2.08 KB
/
percent-command.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
package app
import (
"fmt"
"regexp"
"strconv"
"strings"
)
type PercentResult struct {
results []PResult
reason string
}
type PResult struct {
percent string
result bool
}
func (app *RollBot) PercentCommand(vk VKReq) (Resulter, error) {
h := PercentResult{
results: make([]PResult, 0),
reason: "",
}
tmpString := vk.Object.Message.Text
reason, err := GetReason(tmpString)
if err != nil {
return nil, err
}
if reason != "" {
tmpString = strings.ReplaceAll(tmpString, reason, "")
}
reason = strings.ReplaceAll(strings.ReplaceAll(reason, "(", ""), ")", "")
args := strings.Split(tmpString, " ")
for _, val := range args {
ok, err := regexp.Match("^\\d{1,3}%$", []byte(val))
if err != nil {
return nil, err
}
if !ok {
ok, err = regexp.Match("^\\d{1,3}$", []byte(val))
if err != nil {
return nil, err
}
}
if ok {
pr := PResult{
percent: val,
result: false,
}
val = strings.ReplaceAll(val, "%", "")
p, err := strconv.Atoi(val)
if err != nil {
return nil, err
}
p = 100 - p
r, err := app.Generator.Roll(1, 100)
if err != nil {
return nil, err
}
if r[0] > int64(p) {
pr.result = true
}
h.results = append(h.results, pr)
}
}
return &h, nil
}
func (h *PercentResult) VKString() string {
s := ""
if len(h.results) == 0 {
return "Не удалось распарсить ни один из аргументов. Введите в формате XX%"
}
for _, v := range h.results {
if v.result {
s += fmt.Sprintf("%s : Успех\n", v.percent)
continue
}
s += fmt.Sprintf("%s : Провал\n", v.percent)
}
return s
}
func (h *PercentResult) Comment() string {
return h.reason
}
func (h *PercentResult) HTML() string {
s := ""
if len(h.results) == 0 {
return "Не удалось распарсить ни один из аргументов. Введите в формате XX%"
}
for _, v := range h.results {
if v.result {
s += fmt.Sprintf("<div>%s : Успех</div>", v.percent)
continue
}
s += fmt.Sprintf("<div>%s : Провал</div>", v.percent)
}
return s
}