-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrules.go
223 lines (193 loc) · 6.83 KB
/
rules.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
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"strconv"
raven "github.com/getsentry/raven-go"
log "gopkg.in/inconshreveable/log15.v2"
)
const rulesEndpointURL = "https://api.academyruins.com/cr/"
const glossaryEndpointURL = "https://api.academyruins.com/cr/glossary/"
const examplesEndpointURL = "https://api.academyruins.com/cr/example/"
const specificRuleEndpointURL = "https://yawgatog.com/resources/magic-rules/#R"
var tooLongRules = []string{"205.3i", "205.3j", "205.3m", "205.3n"}
// Rule stores the result of Rules queries from API
type Rule struct {
RuleNumber string `json:"ruleNumber"`
RuleText string `json:"ruleText"`
ExampleTexts []string `json:"examples"`
}
// Rule stores the result of Glossary queries from API
type GlossaryTerm struct {
Term string `json:"term"`
Definition string `json:"definition"`
}
func tryFindSeeMoreRule(input string) string {
log.Debug("TFSMR: This is input", "Input", input)
if strings.Contains(input, "A keyword ability that lets a player attach an Equipment") {
matches := seeRuleRegexp.FindAllStringSubmatch(input, -1)
return "\n" + handleRulesQuery(matches[1][1]+"a")
}
if strings.Contains(input, "See rule") && !strings.Contains(input, "See rules") && !strings.Contains(input, "and rule") {
matches := seeRuleRegexp.FindAllStringSubmatch(input, -1)
if strings.Contains(input, "The object that dealt that damage") {
return "\n" + handleRulesQuery(matches[0][1]+"a")
}
// Doing a couple things here:
// First, we want to match mana ability/ies, but too narrow to bother with regex
// Second, the rules reference in this definition DOES match our regex, so
// I'd rather use that match instead of hardcore 605.1a (as of 31/12/19).
if strings.Contains(input, "Mana Abilit") {
return "\n" + handleRulesQuery(matches[0][1]+".1a")
}
if strings.Contains(input, "Monarch") {
return "\n" + handleRulesQuery(matches[0][1]+".2")
}
if strings.Contains(input, "Destroy") {
return "\n" + handleRulesQuery(matches[0][1]+"b")
}
if len(matches) > 0 {
return "\n" + handleRulesQuery(matches[0][1])
}
}
return ""
}
func findRule(input string, which string) (Rule, error) {
var endpoint string
switch which {
case "example":
endpoint = examplesEndpointURL
case "rule":
endpoint = rulesEndpointURL
default:
log.Error("findRule", "Called with unknown mode", which)
return Rule{}, errors.New("Unknown mode")
}
url := endpoint + input
log.Debug("findRule: Attempting to fetch", "URL", url)
resp, err := http.Get(url)
if err != nil {
raven.CaptureError(err, nil)
log.Debug("HTTP request to Rules Endpoint failed", "Error", err)
return Rule{}, err
}
defer resp.Body.Close()
var foundRule Rule
if resp.StatusCode == 200 {
if err := json.NewDecoder(resp.Body).Decode(&foundRule); err != nil {
raven.CaptureError(err, nil)
log.Debug("Failed decoding the response", "Error", err)
return Rule{}, err
}
return foundRule, nil
}
return Rule{}, errors.New("Whatever you requested failed")
}
func handleExampleQuery(input string) string {
var (
foundRuleNum string
exampleIndex int
err error
)
exampleRequests.Add(1)
fss := ruleExampleRegexp.FindStringSubmatch(input)
foundRuleNum = fss[2] + fss[3] + fss[5]
if fss[1] == "" && fss[4] == "" {
exampleIndex = 0
} else {
exampleIndex, err = strconv.Atoi(fss[1] + fss[4])
if err != nil {
return "Unable to parse example number"
}
}
log.Debug("In handleExampleQuery", "Example matched on", foundRuleNum)
foundExample, err := findRule(foundRuleNum, "example")
if err != nil || foundExample.ExampleTexts == nil {
return "Example not found"
}
if len(foundExample.ExampleTexts) < exampleIndex {
return fmt.Sprintf("Too few examples for rule found (wanted %d, got %d)", exampleIndex, len(foundExample.ExampleTexts))
}
textsToPrint := foundExample.ExampleTexts
if exampleIndex > 0 {
textsToPrint = []string{textsToPrint[exampleIndex-1]}
}
var formattedExample []string
exampleNumber := "<b>[" + foundExample.RuleNumber + "] Example:</b> "
for _, e := range textsToPrint {
formattedExample = append(formattedExample, exampleNumber+e+"\n")
}
return strings.TrimSpace(strings.Join(formattedExample, ""))
}
func handleGlossaryQuery(input string) string {
defineRequests.Add(1)
split := strings.SplitN(input, " ", 2)
log.Debug("In handleGlossaryQuery", "Define matched on", split)
query := TryCoerceGlossaryQuery(strings.ToLower(split[1]))
url := glossaryEndpointURL + query + "?fuzzy=true"
log.Debug("findGlossary: Attempting to fetch", "URL", url)
resp, err := http.Get(url)
if err != nil {
raven.CaptureError(err, nil)
log.Debug("HTTP request to Glossary Endpoint failed", "Error", err)
return ""
}
defer resp.Body.Close()
var foundGlossaryTerm GlossaryTerm
if resp.StatusCode == 200 {
if err := json.NewDecoder(resp.Body).Decode(&foundGlossaryTerm); err != nil {
raven.CaptureError(err, nil)
log.Debug("Failed decoding the response", "Error", err)
return ""
}
} else {
return ""
}
// Some crappy workaround/s
if foundGlossaryTerm.Term != "Dies" {
foundGlossaryTerm.Definition += tryFindSeeMoreRule(foundGlossaryTerm.Definition)
}
return fmt.Sprintf("<b>%s</b>: %s", foundGlossaryTerm.Term, strings.TrimSpace(foundGlossaryTerm.Definition))
}
func TryCoerceGlossaryQuery(query string) string {
if query == "cda" {
query = "characteristic-defining ability"
}
if query == "source" {
query = "source of damage"
}
return query
}
func handleRulesQuery(input string) string {
log.Debug("in handleRulesQuery", "Input", input)
// Hit examples first so it doesn't get consumed as a rule
if (strings.HasPrefix(input, "ex") || strings.HasPrefix(input, "example")) && ruleRegexp.MatchString(input) {
return handleExampleQuery(input)
}
if ruleRegexp.MatchString(input) {
rulesRequests.Add(1)
foundRuleNum := ruleRegexp.FindAllStringSubmatch(input, -1)[0][1]
log.Debug("In handleRulesQuery", "Rules matched on", foundRuleNum)
if stringSliceContains(tooLongRules, foundRuleNum) {
foundRuleFragment := strings.Replace(foundRuleNum, ".", "", -1)
return fmt.Sprintf("<b>%s.</b> <i>[This subtype list is too long for chat. Please see %s ]</i>", foundRuleNum, specificRuleEndpointURL+foundRuleFragment)
}
foundRule, err := findRule(foundRuleNum, "rule")
if err != nil {
return "Rule not found"
}
ruleText := foundRule.RuleText
ruleNumber := []string{"<b>", foundRule.RuleNumber, ".</b> "}
ruleWithNumber := append(ruleNumber, ruleText, "\n")
return strings.TrimSpace(strings.Join(ruleWithNumber, ""))
}
// Glossary stuff in case someone's silly and did 'rule deathtouch'
if strings.HasPrefix(input, "def ") || strings.HasPrefix(input, "define ") || strings.HasPrefix(input, "rule ") || strings.HasPrefix(input, "r ") || strings.HasPrefix(input, "cr ") {
return handleGlossaryQuery(input)
}
// Somehow nothing matched?
return ""
}