Skip to content

Commit cf13439

Browse files
committed
Skip ampersand unless previous command was valid
1 parent e5725ce commit cf13439

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

main.go

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,12 @@ func tokeniseAndDispatchInput(fp *fryatogParams, cardGetFunction CardGetter, dum
163163
}
164164

165165
// Only start processing from the first ! or [[, not from the first &
166-
if strings.Contains(input, "&") {
167-
firstCmdIdx := max(cappedMin(strings.Index(input, "!"), strings.Index(input, "[["), -1), -1)
168-
if strings.Index(input, "&") < firstCmdIdx {
169-
input = input[firstCmdIdx:]
170-
}
171-
}
166+
//if strings.Contains(input, "&") {
167+
// firstCmdIdx := max(cappedMin(strings.Index(input, "!"), strings.Index(input, "[["), -1), -1)
168+
// if strings.Index(input, "&") < firstCmdIdx {
169+
// input = input[firstCmdIdx:]
170+
// }
171+
//}
172172

173173
// Little bit of hackery for PMs
174174
if !strings.Contains(input, "!") && !strings.Contains(input, "[[") {
@@ -227,31 +227,23 @@ func tokeniseAndDispatchInput(fp *fryatogParams, cardGetFunction CardGetter, dum
227227
}
228228
}
229229

230-
skipNext := false
230+
previousCommandWasValidBang := false
231231
for _, message := range commandList {
232232
log.Debug("Processing:", "Command", message)
233-
if skipNext {
234-
log.Info("Double Iffy NextAmpersand Skip")
235-
skipNext = false
236-
continue
237-
}
233+
238234
totalQueries.Add(1)
239235
if isIRC {
240236
ircQueries.Add(1)
241237
} else {
242238
slackQueries.Add(1)
243239
}
244-
if nonTextRegex.MatchString(message) || strings.HasPrefix(message, " ") {
245-
log.Info("Iffy skip", "Message", message)
246-
continue
247-
}
240+
248241
if strings.HasPrefix(message, "! ") {
249242
log.Info("Double Iffy Skip", "Message", message)
250-
whereWereAt := strings.Index(input, message)
251-
nextAnd := strings.Index(input[whereWereAt:], "&")
252-
if nextAnd > -1 {
253-
skipNext = true
254-
}
243+
continue
244+
}
245+
if strings.HasPrefix(message, "&") && !previousCommandWasValidBang {
246+
log.Info("Triple Iffy Skip", "Message", message)
255247
continue
256248
}
257249
if wordEndingInBang.MatchString(message) && !wordStartingWithBang.MatchString(message) {
@@ -261,6 +253,7 @@ func tokeniseAndDispatchInput(fp *fryatogParams, cardGetFunction CardGetter, dum
261253
message = strings.TrimSpace(message)
262254
// Strip the command prefix
263255
if strings.HasPrefix(message, "!") || strings.HasPrefix(message, "&") {
256+
previousCommandWasValidBang = true
264257
message = message[1:]
265258
}
266259
if strings.HasPrefix(message, "[[") && strings.HasSuffix(message, "]]") {
@@ -271,6 +264,7 @@ func tokeniseAndDispatchInput(fp *fryatogParams, cardGetFunction CardGetter, dum
271264
message = message[1 : len(message)-1]
272265
}
273266
if message == "" {
267+
previousCommandWasValidBang = false
274268
continue
275269
}
276270
message = strings.TrimPrefix(message, "card ")

main_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,16 @@ func TestPolicy(t *testing.T) {
109109

110110
func TestTokens(t *testing.T) {
111111
var emptyStringSlice []string
112+
nilStringSlice := []string{}
112113
var testCardExpected = "\x02CARD\x0F · · · TESTSET-T · "
113114
var testRandomCardExpected = "\x02RANDOMCARD\x0F · · · RANDOMTESTSET-R · "
115+
var tarmogoyfRulesText = "\x02Tarmogoyf\x0f {1}{G} · Creature — Lhurgoyf · */1+* · Tarmogoyf's power is equal to the number of card types among cards in all graveyards and its toughness is equal to that number plus 1. · UMA-M · Vin,Cmr,Leg,Mod"
114116
tables := []struct {
115117
input string
116118
output []string
117119
}{
118120
{"Hello! ", emptyStringSlice},
121+
{"Hello! ", emptyStringSlice},
119122
{"! ", emptyStringSlice},
120123
{"Test!", emptyStringSlice},
121124
{"'Test!'", emptyStringSlice},
@@ -149,11 +152,14 @@ func TestTokens(t *testing.T) {
149152
{"!search o:test", []string{testRandomCardExpected + "\n" + testRandomCardExpected}},
150153
{"Player != Planeswalker", emptyStringSlice},
151154
{"Trying to bring = up a !Planeswalker =card", []string{testCardExpected}},
152-
{"https://scryfall.com/search?q=cmc%3D9+f%3Avintage&unique=cards&as=grid&order=name It can go grab any of this fun stuff!", emptyStringSlice},
155+
{"https://scryfall.com/search?q=cmc%3D9+f%3Avintage&unique=cards&as=grid&order=name It can go grab any of this fun stuff!", nilStringSlice},
153156
{"B&R today!", emptyStringSlice},
154157
{"!wc WrongPlaceUser", []string{"WrongPlaceUser: Rules questions belong in the rules channel, not in here. Click #magicjudges-rules or type '/join #magicjudges-rules' (without the quotes) to get there"}},
155158
{"!wc", []string{"Rules questions belong in the rules channel, not in here. Click #magicjudges-rules or type '/join #magicjudges-rules' (without the quotes) to get there"}},
156159
{"Met a retired realtor who suggested doing what he did and buying with a sibling! So I might approach my sisters & brothers-in-law about lending me a hand financially...", emptyStringSlice},
160+
{"Hello! Me & John & Tim are playing a game...", emptyStringSlice},
161+
{"Hello! I control !island & swamp ..", []string{testCardExpected, testCardExpected}},
162+
{"Hello! I saw [[Tarmogoyf]] & I was wondering...", []string{tarmogoyfRulesText}},
157163
}
158164
for _, table := range tables {
159165
got := tokeniseAndDispatchInput(&fryatogParams{m: &hbot.Message{Content: table.input}}, fakeGetCard, fakeGetCard, fakeGetRandomCard, fakeFindCards)

0 commit comments

Comments
 (0)