Skip to content

Commit

Permalink
cardtrader/sealed: Support a few more editions and products
Browse files Browse the repository at this point in the history
Move preprocessing section to a separate file.
  • Loading branch information
kodawah committed Oct 6, 2023
1 parent ddcffe8 commit a2ab1b4
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 55 deletions.
115 changes: 115 additions & 0 deletions cardtrader/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,118 @@ func Preprocess(bp *Blueprint) (*mtgmatcher.Card, error) {
Variation: variant,
}, nil
}

var editionRename = map[string]string{
"2021 Arena Starter Kit": "AFR",
"Commander Legends: Commander Decks": "CMR",
"Commander: The Lord of the Rings - Tales of Middle-earth": "LTC",
"Core Set 2021 Theme Deck": "M21",
"Secret Lair Decks: Angels | They're Just Like Us but Cooler and with Wings": "SLD",
"Secret Lair: Ultimate Edition 2": "SLD",
"The Lord of the Rings: Tales of Middle-earth Holiday Release": "LTR",
"War of the Spark Prerelease": "WAR",
}

var sealedRenames = map[string]string{
"Ugin's Fate Booster": "Ugins Fate Event Booster Pack",
}

func preprocessSealed(edition, productName string) (string, error) {
switch edition {
case "Buy a Box",
"Magic the Gathering Products",
"Prerelease Promos", // Helvault
"Foreign White Bordered",
"Mystery Booster: Convention Edition Playtest Cards",
"Arena League Promos":
return "", errors.New("unsupported")
}
switch {
case strings.Contains(edition, "Creature Forge"),
strings.Contains(edition, "Relic Tokens"):
return "", errors.New("unsupported")
case strings.Contains(productName, "Promo Pack"),
strings.Contains(productName, "Oversized"),
strings.Contains(productName, "Salvat"),
strings.Contains(productName, "Land Pack"):
return "", errors.New("unsupported")
case strings.Contains(productName, "Secret Lair 30th Anniversary Countdown Kit") && mtgmatcher.ExtractYear(productName) != "":
return "", errors.New("unsupported")
}

switch {
case strings.Contains(edition, "Pro Tour") && strings.Contains(edition, "1996"):
edition = "PTC"
// Workaround WCD decks
case strings.HasPrefix(edition, "WCD"):
year := mtgmatcher.ExtractYear(edition)
if len(year) == 4 {
edition = "WC" + year[2:4]
}
case edition == "Challenger Decks":
edition = ""
}

rename, found := editionRename[edition]
if found {
edition = rename
}

// If edition is empty, do not return and instead loop through
var setCode string
set, err := mtgmatcher.GetSetByName(edition)
if err != nil {
if edition != "" {
return "", err
}
} else {
setCode = set.Code
}

rename, found = sealedRenames[productName]
if found {
productName = rename
}

var uuid string
for _, set := range mtgmatcher.GetSets() {
if setCode != "" && setCode != set.Code {
continue
}

for _, product := range set.SealedProduct {
if mtgmatcher.SealedEquals(product.Name, productName) {
uuid = product.UUID
break
}
// If not found, look if the a chunk of the name is present in the deck name
if uuid == "" {
switch {
case strings.HasSuffix(productName, "Booster"):
if mtgmatcher.SealedEquals(product.Name, productName+" Pack") {
uuid = product.UUID
}
case strings.Contains(productName, "Deck"),
strings.Contains(productName, "Intro Pack"):
decks, found := product.Contents["deck"]
if found {
for _, deck := range decks {
if mtgmatcher.SealedContains(productName, deck.Name) {
uuid = product.UUID
break
}
}
}
}
}
if uuid != "" {
break
}
}
if uuid != "" {
break
}
}

return uuid, nil
}
62 changes: 7 additions & 55 deletions cardtrader/sealed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cardtrader
import (
"fmt"
"io"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -103,23 +102,6 @@ func (ct *CardtraderSealed) processEntry(channel chan<- resultChan, expansionId
return err
}

switch {
case strings.Contains(expansionName, "Creature Forge"),
expansionName == "Arena League Promos":
return nil
// Workaround WCD decks
case strings.HasPrefix(expansionName, "WCD"):
year := mtgmatcher.ExtractYear(expansionName)
if len(year) == 4 {
expansionName = "WC" + year[2:4]
}
}

set, err := mtgmatcher.GetSetByName(expansionName)
if err != nil {
return fmt.Errorf("%s: %s", expansionName, err.Error())
}

var warned []string

for id, products := range allProducts {
Expand All @@ -128,49 +110,19 @@ func (ct *CardtraderSealed) processEntry(channel chan<- resultChan, expansionId
continue
}

switch {
case strings.Contains(blueprint.Name, "Promo Pack"),
strings.Contains(blueprint.Name, "Land Pack"):
continue
}

var uuid string
for _, sealedProduct := range set.SealedProduct {
if mtgmatcher.SealedEquals(sealedProduct.Name, blueprint.Name) {
uuid = sealedProduct.UUID
break
}
// If not found, look if the a chunk of the name is present in the deck name
if uuid == "" {
switch {
case strings.HasSuffix(blueprint.Name, "Booster"):
if mtgmatcher.SealedEquals(sealedProduct.Name, blueprint.Name+" Pack") {
uuid = sealedProduct.UUID
}
case strings.Contains(blueprint.Name, "Deck"),
strings.Contains(blueprint.Name, "Intro Pack"):
decks, found := sealedProduct.Contents["deck"]
if found {
for _, deck := range decks {
if mtgmatcher.SealedContains(blueprint.Name, deck.Name) {
uuid = sealedProduct.UUID
break
}
}
}
}
uuid, err := preprocessSealed(expansionName, blueprint.Name)
if err != nil {
if err.Error() == "unknown edition" {
return fmt.Errorf("%s: %s", expansionName, err.Error())
}
if uuid != "" {
break
if err.Error() == "unsupported" {
continue
}
}

if uuid == "" {
if slices.Contains(warned, blueprint.Name) {
continue
}
warned = append(warned, blueprint.Name)
ct.printf("No association for %s", blueprint.Name)
ct.printf("No association for '%s' in '%s'", blueprint.Name, expansionName)
continue
}

Expand Down
2 changes: 2 additions & 0 deletions mtgmatcher/editions.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ var EditionTable = map[string]string{
"Stained Glass": "Secret Lair Drop",
"Stained Glass Art": "Secret Lair Drop",
"Stained Glass Promo": "Secret Lair Drop",
"Secret Lair Drop Series": "Secret Lair Drop",

// Ponies
"Ponies: The Galloping": "Ponies: The Galloping",
Expand Down Expand Up @@ -453,6 +454,7 @@ var EditionTable = map[string]string{
"SLX Cards": "Universes Within",
"Universes Within": "Universes Within",
"Secret Lair Commander: From Cute to Brute": "From Cute to Brute",
"Secret Lair: From Cute to Brute": "From Cute to Brute",
"Secret Lair Commander: Heads I Win, Tales You Lose": "Heads I Win, Tails You Lose",
"Secret Lair - Planeswalker Symbol": "Heads I Win, Tails You Lose",

Expand Down

0 comments on commit a2ab1b4

Please sign in to comment.