-
Notifications
You must be signed in to change notification settings - Fork 2
/
roll.go
309 lines (246 loc) · 7.27 KB
/
roll.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
package main
import (
crand "crypto/rand"
"encoding/binary"
"fmt"
"log"
"math/rand"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"github.com/bwmarrin/discordgo"
)
// ____ __ __ ___
// _________ / / / / /_/ /_ ___ ____/ (_)_______
// / ___/ __ \/ / /_____/ __/ __ \/ _ \______/ __ / / ___/ _ \
// / / / /_/ / / /_____/ /_/ / / / __/_____/ /_/ / / /__/ __/
// /_/ \____/_/_/ \__/_/ /_/\___/ \__,_/_/\___/\___/
type rtdInfo struct {
ChannelID string `json:"channel_id,omitempty"`
Sides []int `json:"sides,omitempty"`
Wandering rollWandering `json:"wandering,omitempty"`
}
type rollOutcome struct {
Exact int `json:"exact,omitempty"`
Range rollOutcomeRange `json:"range,omitempty"`
}
type rollOutcomeRange struct {
Min int `json:"min"`
Max int `json:"max"`
}
type rollWandering struct {
Damage wanderingSettings `json:"damage,omitempty"`
Loot wanderingSettings `json:"loot,omitempty"`
Lute wanderingSettings `json:"lute,omitempty"`
}
func rollTheDiceInit() {
}
func getSeed() (int64, error) {
c := 8
b := make([]byte, c)
_, err := crand.Read(b)
if err != nil {
return 0, err
}
return (int64)(binary.BigEndian.Uint64(b)), nil
}
func rollHandler(messageContent string) (response string, discordEmbed discordgo.MessageEmbed, sendToDM bool) {
if strings.TrimPrefix(messageContent, chn.Prefix+"roll") == "" || !strings.HasPrefix(messageContent, chn.Prefix+"roll ") {
response = "How to use Roll the Dice\n`!roll (dice)d(sides)[+/-][proficiency]x[multiple times]` I.E. `!roll 1d20+3x5`\n `!roll stats` to roll a base set of stats."
return
}
switch strings.TrimPrefix(messageContent, chn.Prefix+"roll ") {
case "wandering dmg":
log.Printf("rolling wandering damage")
response, discordEmbed, sendToDM = rollWanderingDamage()
case "wandering loot":
log.Printf("rolling wandering loot")
response, discordEmbed, sendToDM = rollWanderingLoot()
case "wandering lute":
log.Printf("rolling wandering lute")
response, discordEmbed, sendToDM = rollWanderingLute()
case "stats":
log.Printf("stats")
response, sendToDM = rollStats("")
default:
log.Printf("rolling the dice")
response, sendToDM = rollTheDice(strings.TrimPrefix(messageContent, chn.Prefix+"roll "))
}
return
}
func rollTheDice(message string) (response string, sendToDM bool) {
var err error
// a users proficiency
var proficiency int
// if a roll is to be run multiple times
multiRoll := 1
log.Printf("roll the dice")
// Example !roll 1d6+2
validID, err := regexp.Compile(`(\d+)\s?d\s?(\d+)\s?(?:(\+|\-)\s?(\d*))?(?:\s?(?:x\s?)(\d*)|)`)
if err != nil {
log.Printf("There was an error compiling the regex for the roll command")
return
}
dieInfo := validID.FindStringSubmatch(message)
if len(dieInfo) == 0 {
return
}
rollCount, err := strconv.Atoi(dieInfo[1])
if err != nil {
log.Printf("There was an error converting the number of rolls")
}
dieValue, err := strconv.Atoi(dieInfo[2])
if err != nil {
log.Printf("There was an error converting the number of sides")
}
if dieInfo[4] != "" {
proficiency, err = strconv.Atoi(dieInfo[4])
if err != nil {
log.Printf("There was an error converting proficiency")
}
}
if !hasElem(chn.RTD.Sides, dieValue) {
log.Printf("The amount of sides %d, is not supported", dieValue)
response = fmt.Sprintf("Only dice with %s sides are supported.", arrayToString(chn.RTD.Sides))
return
}
if rollCount > 10 {
response = fmt.Sprintf("rolls are limited to 10 at a time")
return
}
if dieInfo[5] != "" {
// log.Printf("rolling %s sets", dieInfo[5])
multiRoll, err = strconv.Atoi(dieInfo[5])
if err != nil {
log.Printf("There was an error converting the number of rolls")
}
response = fmt.Sprintf("I have rolled %d sets of rolls for you coming out with \n", multiRoll)
}
if multiRoll > 5 {
response = fmt.Sprintf("Sorry I only support up to 5 sets of rolls.")
return
}
for i := 1; i <= multiRoll; i++ {
response = response + rollDie(dieInfo[3], dieValue, rollCount, proficiency)
}
return
}
func roll(rollCount int, dieValue int) (rolls []int) {
seed, err := getSeed()
if err != nil {
log.Print("Error generating seed")
}
rand.Seed(seed)
for i := 0; i < rollCount; i++ {
rolls = append(rolls, rand.Intn(dieValue)+1)
}
// log.Printf("%d", rolls)
return
}
func flipCoin() (response string, sendToDM bool) {
side := total(roll(1, 2))
// log.Printf("side = %d", side)
if side == 1 {
response = "I have flipped the coin getting a heads"
} else if side == 2 {
response = "I have flipped the coin getting a tails"
}
return
}
func rollDie(addSub string, dieValue, rollCount, proficiency int) (response string) {
// strings that are sent back
var prettyRolls string
var profString string
// log.Printf("rolling a %d sided die %d times", dieValue, rollCount)
allRolls := roll(rollCount, dieValue)
prettyRolls = arrayToString(allRolls)
rollTotal := total(allRolls)
// log.Printf("roll total = %d", rollTotal)
if addSub == "" {
// log.Printf("No profeciency was added to the roll")
} else {
if addSub == "+" {
log.Printf("Adding %d to the roll", proficiency)
rollTotal = rollTotal + proficiency
profString = fmt.Sprintf("adding %d ", proficiency)
} else if addSub == "-" {
log.Printf("subtracting %d to the roll", proficiency)
rollTotal = rollTotal - proficiency
profString = fmt.Sprintf("subtracting %d ", proficiency)
} else {
}
}
if rollCount == 1 {
response = fmt.Sprintf("You have rolled a %s \n", prettyRolls)
} else {
response = fmt.Sprintf("You have rolled %s %sfor a total of %d \n", prettyRolls, profString, rollTotal)
}
return
}
func rollStats(message string) (response string, sendToDM bool) {
log.Printf("rolling stats for the user")
var allRolls []int
var rerollOne bool
if message == "" {
rerollOne = true
}
for len(allRolls) < 6 {
// log.Printf("roll set %d", len(allRolls)+1)
allRolled := []int{}
for len(allRolled) < 4 {
rolled := total(roll(1, 6))
for rolled <= 1 && rerollOne {
// log.Printf("rerolling a 1 that was rolled")
rolled = total(roll(1, 6))
}
allRolled = append(allRolled, rolled)
}
// log.Printf("%d", allRolled)
sort.Ints(allRolled)
// log.Printf("%d", allRolled)
allRolled = allRolled[1:]
// log.Printf("%d", allRolled)
allRolls = append(allRolls, total(allRolled))
}
log.Printf("all roll set totals '%d'", allRolls)
response = fmt.Sprintf("I have rolled the dice and return the following stat rolls for you. %d", allRolls)
return
}
func total(dice []int) (total int) {
for _, die := range dice {
total = total + die
}
// log.Printf("total = %d", total)
return
}
func between(min, max, num int) (isBetween bool) {
if max >= num && num >= min {
isBetween = true
}
return
}
func arrayToString(intArray []int) (pretty string) {
for rtdi, val := range intArray {
pretty = pretty + strconv.Itoa(val)
if rtdi == len(intArray)-2 {
pretty = pretty + ", and "
} else if rtdi != len(intArray)-1 {
pretty = pretty + ", "
}
}
return
}
// if array has an element
func hasElem(array interface{}, elem interface{}) bool {
arrV := reflect.ValueOf(array)
if arrV.Kind() == reflect.Slice {
for i := 0; i < arrV.Len(); i++ {
if arrV.Index(i).Interface() == elem {
return true
}
}
}
return false
}