Skip to content

Commit

Permalink
mtgmatcher: Prepare image URLs as needed
Browse files Browse the repository at this point in the history
  • Loading branch information
kodawah committed Aug 7, 2024
1 parent 7c7baae commit 0888b92
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 11 deletions.
83 changes: 72 additions & 11 deletions mtgmatcher/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,37 @@ func sortPrintings(ap AllPrintings, printings []string) {
})
}

func scryfallImageURL(card Card, version string) string {
number := card.Number

// Retrieve the original number if present
dupe, found := card.Identifiers["originalScryfallNumber"]
if found {
number = dupe
}

// Support BAN's custom sets
code := strings.ToLower(card.SetCode)
if strings.HasSuffix(code, "ita") {
code = strings.TrimSuffix(code, "ita")
number += "/it"
} else if strings.HasSuffix(code, "jpn") {
code = strings.TrimSuffix(code, "jpn")
number += "/ja"
}
code = strings.TrimSuffix(code, "alt")

return fmt.Sprintf("https://api.scryfall.com/cards/%s/%s?format=image&version=%s", code, number, version)
}

func sealedImageURL(card Card) string {
tcgId, found := card.Identifiers["tcgplayerProductId"]
if !found {
return ""
}
return "https://product-images.tcgplayer.com/" + tcgId + ".jpg"
}

func (ap AllPrintings) Load() cardBackend {
uuids := map[string]CardObject{}
cardInfo := map[string]cardinfo{}
Expand Down Expand Up @@ -429,6 +460,11 @@ func (ap AllPrintings) Load() cardBackend {
continue
}

card.Images = map[string]string{}
card.Images["full"] = scryfallImageURL(card, "normal")
card.Images["thumbnail"] = scryfallImageURL(card, "small")
card.Images["crop"] = scryfallImageURL(card, "art_crop")

// Custom modifications or skips
switch set.Code {
// Override non-English Language
Expand Down Expand Up @@ -734,18 +770,25 @@ func (ap AllPrintings) Load() cardBackend {
}

for _, product := range set.SealedProduct {
card := Card{
UUID: product.UUID,
Name: product.Name,
SetCode: code,
Identifiers: product.Identifiers,
Rarity: "Product",
Layout: product.Category,
Side: product.Subtype,
// Will be filled later
SourceProducts: map[string][]string{},
Images: map[string]string{},
}

card.Images["full"] = sealedImageURL(card)
card.Images["thumbnail"] = sealedImageURL(card)
card.Images["crop"] = sealedImageURL(card)

uuids[product.UUID] = CardObject{
Card: Card{
UUID: product.UUID,
Name: product.Name,
SetCode: code,
Identifiers: product.Identifiers,
Rarity: "Product",
Layout: product.Category,
Side: product.Subtype,
// Will be filled later
SourceProducts: map[string][]string{},
},
Card: card,
Sealed: true,
Edition: set.Name,
}
Expand Down Expand Up @@ -1054,6 +1097,12 @@ func duplicate(sets map[string]*Set, cardInfo map[string]cardinfo, uuids map[str
dup.Cards[i].SetCode = dup.Code
dup.Cards[i].Language = langs[tag]

// Update images
dup.Cards[i].Images = map[string]string{}
dup.Cards[i].Images["full"] = scryfallImageURL(dup.Cards[i], "normal")
dup.Cards[i].Images["thumbnail"] = scryfallImageURL(dup.Cards[i], "small")
dup.Cards[i].Images["crop"] = scryfallImageURL(dup.Cards[i], "art_crop")

// Update printings for the CardInfo map
ci := cardInfo[Normalize(dup.Cards[i].Name)]
ci.Printings = printings
Expand Down Expand Up @@ -1093,6 +1142,12 @@ func duplicateCards(sets map[string]*Set, uuids map[string]CardObject, code, tag
dupeCard.UUID = mainUUID + "_" + strings.ToLower(tag)
dupeCard.Language = langs[tag]

// Update images
dupeCard.Images = map[string]string{}
dupeCard.Images["full"] = scryfallImageURL(dupeCard, "normal")
dupeCard.Images["thumbnail"] = scryfallImageURL(dupeCard, "small")
dupeCard.Images["crop"] = scryfallImageURL(dupeCard, "art_crop")

duplicates = append(duplicates, dupeCard)

// Add the new uuid to the UUID map
Expand Down Expand Up @@ -1155,6 +1210,12 @@ func spinoffFoils(sets map[string]*Set, uuids map[string]CardObject, code string
dupeCard.Number += mtgjson.SuffixSpecial
dupeCard.Finishes = []string{"foil"}

// Update images
dupeCard.Images = map[string]string{}
dupeCard.Images["full"] = scryfallImageURL(dupeCard, "normal")
dupeCard.Images["thumbnail"] = scryfallImageURL(dupeCard, "small")
dupeCard.Images["crop"] = scryfallImageURL(dupeCard, "art_crop")

// Update or create the new card object, add the new card to the list
co.Card = dupeCard
uuids[dupeCard.UUID] = co
Expand Down
1 change: 1 addition & 0 deletions mtgmatcher/lorcana.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func (lj LorcanaJSON) Load() cardBackend {
SetCode: card.SetCode,
Finishes: finishes,
Number: fmt.Sprint(card.Number),
Images: card.Images,

Colors: []string{card.Color},
Rarity: card.Rarity,
Expand Down
4 changes: 4 additions & 0 deletions mtgmatcher/mtgjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ type Card struct {
UUID string `json:"uuid"`
Variations []string `json:"variations"`
Watermark string `json:"watermark"`

// A list of URLs containing the image of the card
// At a minimum "full" and "thumbnail" versions should be provided
Images map[string]string
}

// Card implements the Stringer interface
Expand Down

0 comments on commit 0888b92

Please sign in to comment.