~kris/9p

glenda

glenda/x/mux/roll.go -rw-r--r-- 2.6 KiB
48661367 — Kris Yotam chore: sync local state after restore (push updates, no pull) a month ago
                                                                                
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
package mux

import (
	"github.com/bwmarrin/discordgo"
	"strings"
	"fmt"
	"strconv"
	"math/big"
	"crypto/rand"
)

// Roll dice in the form XdY
func (m *Mux) Roll(ds *discordgo.Session, dm *discordgo.Message, ctx *Context) {
	resp := ".\n"
	input := dm.Content
	usage := func(s string) {
		ds.ChannelMessageSend(dm.ChannelID, fmt.Sprintf("Malformed input. %s. \n\nExample usage: \n`g!roll 2d6 3d4` to roll two six-sided die and three four-sided die.", s))
	}

	explode := false
	infExplode := false
	if strings.HasSuffix(input, "!!") {
		infExplode = true
		input = input[:len(input)-2]
	} else if strings.HasSuffix(input, "!") {
		input = input[:len(input)-1]
		explode = true
	}

	fields := strings.Fields(input)
	if len(fields) < 2 {
		usage("No arguments provided")
		return
	}
	// Strip g!roll
	fields = fields[1:]

	for _, entry := range fields {
		resp += entry + ":\n"
		resp += "```\n"

		// {2, 6}
		parts := strings.Split(entry, "d")
		//fmt.Println(parts)
		if len(parts) != 2 {
			usage("Die entries must be in the form XdY where X and Y are valid, positive, integers")
			return
		}

		count, err := strconv.ParseInt(parts[0], 10, 64)
		if err != nil {
			usage("Invalid numeric for count: " + err.Error())
			return
		}
		if count <= 0 || count > 200 {
			usage("Count must be > 0 and <= 200")
			return
		}

		sides, err := strconv.ParseInt(parts[1], 10, 64)
		if err != nil {
			usage("Invalid numeric for sides: " + err.Error())
			return
		}
		if sides <= 0 {
			usage("Sides must be > 0")
			return
		}

		// Do the rolls
		for i := int64(0); i < count; i++ {
			bSides := big.NewInt(sides)

			roll := func() (*big.Int, error) {
					n, err := rand.Int(rand.Reader, bSides)
					if err != nil {
						usage("Could not perform rng")
						fmt.Println("rng fail →", err)
						return n, err
					}
					n.Add(n, big.NewInt(int64(1)))
					return n, err
			}

			insert := func(r string, n *big.Int) string {
				return r + fmt.Sprintf("%s	", n.String())
			}


			n, err := roll()
			if err != nil {
				return
			}
			resp = insert(resp, n)

			if explode && n.Cmp(bSides) == 0 {
				n, err := roll()
				if err != nil {
					return
				}
				resp = insert(resp, n)
			} else if infExplode && n.Cmp(bSides) == 0 {
				inf:
				for {
					n, err := roll()
					if err != nil {
						return
					}
					resp = insert(resp, n)
					
					if n.Cmp(bSides) == 0 {
						continue inf	
					} else {
						break inf
					}
				}
			}

		}
		resp += "\n```\n"
	}

	if len(resp) > 2000 {
		resp = "Error: output exceeded Discordâ„¢ 2000 character limit."
	}

	ds.ChannelMessageSend(dm.ChannelID, resp)

	return
}