-
Notifications
You must be signed in to change notification settings - Fork 928
/
dice.go
63 lines (49 loc) · 1.22 KB
/
dice.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
package dice
import (
"errors"
"fmt"
"regexp"
"strings"
)
var (
MaxLoop int64 = 1000
)
var (
ErrTooManyLoops = errors.New("Too many loops, you either specified too many dices or sides")
)
type RollResult interface {
fmt.Stringer
Description() string
Int() int
}
type roller interface {
Pattern() *regexp.Regexp
Roll([]string) (RollResult, error)
}
type basicRollResult struct {
desc string
}
func (r basicRollResult) Description() string { return strings.Trim(r.desc, " \t\r\n") }
var rollHandlers []roller
func addRollHandler(handler roller) {
rollHandlers = append(rollHandlers, handler)
}
/*
open - regexp.MustCompile(`([0-9]+)d([0-9]+)(e)?o$`)
sil - regexp.MustCompile(`([0-9]+)d([0-9]+)(e)?s$`)
*/
func Roll(desc string) (RollResult, string, error) {
for _, rollHandler := range rollHandlers {
rollHandler.Pattern().Longest()
if r := rollHandler.Pattern().FindStringSubmatch(desc); r != nil {
result, err := rollHandler.Roll(r)
if err != nil {
return nil, "", err
}
indexes := rollHandler.Pattern().FindStringSubmatchIndex(desc)
reason := strings.Trim(desc[indexes[0]+len(r[0]):], " \t\r\n")
return result, reason, nil
}
}
return nil, "", errors.New("Bad roll format: " + desc)
}