-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcard_utils.go
347 lines (320 loc) · 11 KB
/
card_utils.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"sort"
"strings"
raven "github.com/getsentry/raven-go"
log "gopkg.in/inconshreveable/log15.v2"
)
// CardList represents the Scryfall List API when retrieving multiple cards
type CardList struct {
Object string `json:"object"`
TotalCards int `json:"total_cards"`
Warnings []string `json:"warnings"`
HasMore bool `json:"has_more"`
NextPage string `json:"next_page"`
Data []Card `json:"data"`
}
// CardRuling contains an individual ruling on a card
type CardRuling struct {
Object string `json:"object"`
OracleID string `json:"oracle_id"`
Source string `json:"source"`
PublishedAt string `json:"published_at"`
Comment string `json:"comment"`
}
func (ruling *CardRuling) formatRuling() string {
return fmt.Sprintf("%v: %v", ruling.PublishedAt, ruling.Comment)
}
// CardMetadata contains some extraneous extra information we sometimes retrieve
type CardMetadata struct {
PreviousPrintings []string
PreviousFlavourTexts []string
PreviousReminderTexts []string
}
// CardRulingResult represents the JSON returned by the /cards/{}/rulings Scryfall API
type CardRulingResult struct {
Object string `json:"object"`
HasMore bool `json:"has_more"`
Data []CardRuling `json:"data"`
}
// CommonCard stores the things common to both Card and CardFaces
type CommonCard struct {
ManaCost string `json:"mana_cost"`
TypeLine string `json:"type_line"`
PrintedTypeLine string `json:"printed_type_line,omitempty"`
ColorIndicators []string `json:"color_indicator"`
OracleText string `json:"oracle_text"`
PrintedText string `json:"printed_text,omitempty"`
Power string `json:"power"`
Toughness string `json:"toughness"`
Loyalty string `json:"loyalty"`
}
// CardFace represents the individual information for each face of a DFC
type CardFace struct {
CommonCard
Object string `json:"object"`
Name string `json:"name"`
FlavourText string `json:"flavor_text,omitempty"`
PrintedName string `json:"printed_name,omitempty"`
Watermark string `json:"watermark"`
Artist string `json:"artist"`
IllustrationID string `json:"illustration_id,omitempty"`
}
// Card represents the JSON returned by the /cards Scryfall API
type Card struct {
CommonCard
Object string `json:"object"`
ID string `json:"id"`
OracleID string `json:"oracle_id"`
MultiverseIds []int `json:"multiverse_ids"`
MtgoID int `json:"mtgo_id"`
MtgoFoilID int `json:"mtgo_foil_id"`
TcgplayerID int `json:"tcgplayer_id"`
Name string `json:"name"`
PrintedName string `json:"printed_name,omitempty"`
Lang string `json:"lang"`
ReleasedAt string `json:"released_at"`
URI string `json:"uri"`
ScryfallURI string `json:"scryfall_uri"`
Layout string `json:"layout"`
HighresImage bool `json:"highres_image"`
ImageUris struct {
Small string `json:"small"`
Normal string `json:"normal"`
Large string `json:"large"`
Png string `json:"png"`
ArtCrop string `json:"art_crop"`
BorderCrop string `json:"border_crop"`
} `json:"image_uris"`
Cmc float32 `json:"cmc"`
Colors []string `json:"colors"`
ColorIdentity []string `json:"color_identity"`
CardFaces []CardFace `json:"card_faces"`
Legalities struct {
Standard string `json:"standard"`
Future string `json:"future"`
Frontier string `json:"frontier"`
Modern string `json:"modern"`
Legacy string `json:"legacy"`
Pauper string `json:"pauper"`
Vintage string `json:"vintage"`
Penny string `json:"penny"`
Commander string `json:"commander"`
OneV1 string `json:"1v1"`
Duel string `json:"duel"`
Brawl string `json:"brawl"`
Pioneer string `json:"pioneer"`
} `json:"legalities"`
Games []string `json:"games"`
Reserved bool `json:"reserved"`
Foil bool `json:"foil"`
Nonfoil bool `json:"nonfoil"`
Oversized bool `json:"oversized"`
Promo bool `json:"promo"`
Reprint bool `json:"reprint"`
Set string `json:"set"`
SetName string `json:"set_name"`
SetType string `json:"set_type"`
SetURI string `json:"set_uri"`
SetSearchURI string `json:"set_search_uri"`
ScryfallSetURI string `json:"scryfall_set_uri"`
RulingsURI string `json:"rulings_uri"`
PrintsSearchURI string `json:"prints_search_uri"`
CollectorNumber string `json:"collector_number"`
Digital bool `json:"digital"`
Rarity string `json:"rarity"`
FlavourText string `json:"flavor_text"`
IllustrationID string `json:"illustration_id"`
Artist string `json:"artist"`
BorderColor string `json:"border_color"`
Frame string `json:"frame"`
FrameEffect string `json:"frame_effect"`
FullArt bool `json:"full_art"`
Timeshifted bool `json:"timeshifted"`
Colorshifted bool `json:"colorshifted"`
Futureshifted bool `json:"futureshifted"`
StorySpotlight bool `json:"story_spotlight"`
EdhrecRank int `json:"edhrec_rank"`
Usd string `json:"usd"`
Eur string `json:"eur"`
Tix string `json:"tix"`
RelatedUris struct {
Gatherer string `json:"gatherer"`
TcgplayerDecks string `json:"tcgplayer_decks"`
Edhrec string `json:"edhrec"`
Mtgtop8 string `json:"mtgtop8"`
} `json:"related_uris"`
PurchaseUris struct {
Tcgplayer string `json:"tcgplayer"`
Cardmarket string `json:"cardmarket"`
Cardhoarder string `json:"cardhoarder"`
} `json:"purchase_uris"`
Rulings []CardRuling
Metadata CardMetadata
}
// CardCatalog stores the result of the catalog/card-names API call
type CardCatalog struct {
Object string `json:"object"`
URI string `json:"uri"`
TotalValues int `json:"total_values"`
Data []string `json:"data"`
}
// CardSearchResult stores the result of an advanced Card search API call
type CardSearchResult struct {
Warnings []string `json:"warnings"`
Details string `json:"details"`
Status int `json:"status"`
Object string `json:"object"`
TotalCards int `json:"total_cards"`
HasMore bool `json:"has_more"`
NextPage string `json:"next_page"`
Data []Card `json:"data"`
}
// ShortCardName stores a dict reference that matches common card shorthands (bob, steve, etc)
// to the card's fully qualified name
type ShortCardName struct {
ShortName string `json:"shortname"`
FullyQualifiedName string `json:"fullname"`
}
func standardiseColorIndicator(ColorIndicators []string) string {
expandedColors := map[string]string{"W": "White",
"U": "Blue",
"B": "Black",
"R": "Red",
"G": "Green"}
mappedColors := map[string]int{"White": 0,
"Blue": 1,
"Black": 2,
"Red": 3,
"Green": 4}
var colorWords []string
for _, color := range ColorIndicators {
colorWords = append(colorWords, expandedColors[color])
}
sort.Slice(colorWords, func(i, j int) bool {
return mappedColors[colorWords[i]] < mappedColors[colorWords[j]]
})
return "[" + strings.Join(colorWords, "/") + "]"
}
func normaliseCardName(input string) string {
ret := nonAlphaRegex.ReplaceAllString(strings.ToLower(input), "")
// log.Debug("Normalising", "Input", input, "Output", ret)
return ret
}
func formatManaCost(input string) string {
return input
}
func replaceManaCostForSlack(input string) string {
manaString := strings.Replace(input, "{1000000}", ":mana-1000000-1::mana-1000000-2::mana-1000000-3::mana-1000000-4:", -1)
for _, match := range emojiRegex.FindAllString(manaString, -1) {
replacement := strings.Replace(match, "{", ":mana-", -1)
replacement = strings.Replace(replacement, "}", ":", -1)
replacement = strings.Replace(replacement, "/", "", -1)
manaString = strings.Replace(manaString, match, replacement, 1)
}
return manaString
}
// TODO: Have a command to see all printing information
func (card *Card) formatExpansions() string {
var ret []string
if card.Name != "Plains" && card.Name != "Island" && card.Name != "Swamp" && card.Name != "Mountain" && card.Name != "Forest" {
if len(card.Metadata.PreviousPrintings) > 0 {
if len(card.Metadata.PreviousPrintings) < 10 {
ret = card.Metadata.PreviousPrintings
} else {
ret = card.Metadata.PreviousPrintings[:5]
ret = append(ret, "[...]")
}
}
}
ret = append(ret, fmt.Sprintf("%s-%s", strings.ToUpper(card.Set), strings.ToUpper(card.Rarity[0:1])))
return strings.Join(sliceUniqMap(ret), ",")
}
func (card *Card) formatLegalities() string {
var ret []string
ret = append(ret, formatLegality(card.Legalities.Vintage, "Vin"))
ret = append(ret, formatLegality(card.Legalities.Commander, "Cmr"))
ret = append(ret, formatLegality(card.Legalities.Legacy, "Leg"))
ret = append(ret, formatLegality(card.Legalities.Modern, "Mod"))
ret = append(ret, formatLegality(card.Legalities.Pioneer, "Pio"))
ret = append(ret, formatLegality(card.Legalities.Standard, "Std"))
return strings.Join(removeEmptyStrings(ret), ",")
}
func formatLegality(input string, shortFormat string) string {
ret := ""
switch input {
case "legal":
ret = shortFormat
case "restricted":
ret = fmt.Sprintf("%s%s", shortFormat, "Res")
case "banned":
ret = fmt.Sprintf("%s%s", shortFormat, "Ban")
}
return ret
}
func lookupUniqueNamePrefix(input string) string {
ncn := normaliseCardName(input)
log.Debug("in lookupUniqueNamePrefix", "Input", input, "NCN", ncn, "Length of CN", len(cardNames))
var err error
if len(cardNames) == 0 {
log.Debug("In lookupUniqueNamePrefix -- Importing")
cardNames, err = importCardNames(false)
if err != nil {
log.Warn("Error importing card names", "Error", err)
return ""
}
}
//c := cardNames[:0]
var c []string
for _, x := range cardNames {
if strings.HasPrefix(normaliseCardName(x), ncn) {
log.Debug("In lookupUniqueNamePrefix", "Gottem", x)
c = append(c, x)
}
}
log.Debug("In lookupUniqueNamePrefix", "C", c)
if len(c) == 1 {
return c[0]
}
// Look for something legendary-ish
var i int
var j string
for _, x := range c {
if strings.Contains(x, ",") || strings.Contains(x, "the") {
i++
j = x
}
}
if i == 1 {
cardUniquePrefixHits.Add(1)
return j
}
return ""
}
func importShortCardNames() error {
log.Debug("In importCardShortNames")
content, err := ioutil.ReadFile(cardShortNameFile)
if err != nil {
raven.CaptureError(err, nil)
log.Warn("Error opening shortNames file", "Error", err)
return err
}
var tempCardShortNames []ShortCardName
err = json.Unmarshal(content, &tempCardShortNames)
if err != nil {
raven.CaptureError(err, nil)
log.Warn("Unable to parse shortNames file", "Error", err)
return err
}
for _, sn := range tempCardShortNames {
shortCardNames[sn.ShortName] = sn.FullyQualifiedName
}
log.Debug("Populated shortNames", "Length", len(shortCardNames))
return nil
}
func isDfc(card *Card) bool {
return card.Layout == "modal_dfc" || card.Layout == "transform"
}