Skip to content

Commit

Permalink
mtgmatcher: Load commond Rarities and Colors information
Browse files Browse the repository at this point in the history
  • Loading branch information
kodawah committed Aug 9, 2024
1 parent 3c2901d commit 13e6c7a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
56 changes: 56 additions & 0 deletions mtgmatcher/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ func (ap AllPrintings) Load() cardBackend {

for code, set := range ap.Data {
var filteredCards []Card
var rarities, colors []string

allSets = append(allSets, code)

Expand Down Expand Up @@ -738,11 +739,38 @@ func (ap AllPrintings) Load() cardBackend {
promoTypes = append(promoTypes, promoType)
}
}

// Add possible rarities and colors
if !slices.Contains(rarities, card.Rarity) {
rarities = append(rarities, card.Rarity)
}
for _, color := range card.Colors {
if !slices.Contains(colors, mtgColorNameMap[color]) {
colors = append(colors, mtgColorNameMap[color])
}
}
if len(card.Colors) == 0 && !slices.Contains(colors, "colorless") {
colors = append(colors, "colorless")
}
if len(card.Colors) > 1 && !slices.Contains(colors, "multicolor") {
colors = append(colors, "multicolor")
}

}

// Replace the original array with the filtered one
set.Cards = filteredCards

// Assign the rarities and colors present in the set
sort.Slice(rarities, func(i, j int) bool {
return mtgRarityMap[rarities[i]] > mtgRarityMap[rarities[j]]
})
set.Rarities = rarities
sort.Slice(colors, func(i, j int) bool {
return mtgColorMap[colors[i]] > mtgColorMap[colors[j]]
})
set.Colors = colors

// Adjust the setBaseSize to take into account the cards with
// the same name in the same set (also make sure that it is
// correctly initialized)
Expand Down Expand Up @@ -924,6 +952,34 @@ func (ap AllPrintings) Load() cardBackend {
return backend
}

var mtgRarityMap = map[string]int{
"token": 1,
"common": 2,
"uncommon": 3,
"rare": 4,
"mythic": 5,
"special": 6,
"oversize": 7,
}

var mtgColorNameMap = map[string]string{
"W": "white",
"U": "blue",
"B": "black",
"R": "red",
"G": "green",
}

var mtgColorMap = map[string]int{
"white": 7,
"blue": 6,
"black": 5,
"red": 4,
"green": 3,
"colorless": 2,
"multicolor": 1,
}

func fillinSLDdecks(set *Set) []string {
var output []string
for _, product := range set.SealedProduct {
Expand Down
26 changes: 26 additions & 0 deletions mtgmatcher/lorcana.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func (lj LorcanaJSON) Load() cardBackend {

// Update any reamining details on Sets after Cards loading
for code := range backend.Sets {
var rarities, colors []string
backend.Sets[code].IsFoilOnly = true
backend.Sets[code].IsNonFoilOnly = true
for _, card := range backend.Sets[code].Cards {
Expand All @@ -194,8 +195,33 @@ func (lj LorcanaJSON) Load() cardBackend {
if !card.HasFinish("nonfoil") {
backend.Sets[code].IsFoilOnly = false
}

if !slices.Contains(rarities, card.Rarity) {
rarities = append(rarities, card.Rarity)
}
if len(card.Colors) > 0 && !slices.Contains(colors, card.Colors[0]) {
colors = append(colors, card.Colors[0])
}
}

sort.Slice(rarities, func(i, j int) bool {
return lorcanaRarityMap[rarities[i]] > lorcanaRarityMap[rarities[j]]
})
backend.Sets[code].Rarities = rarities

sort.Strings(colors)
backend.Sets[code].Colors = colors
}

return backend
}

var lorcanaRarityMap = map[string]int{
"common": 1,
"uncommon": 2,
"rare": 3,
"super rare": 4,
"legendary": 5,
"enchanted": 6,
"special": 7,
}
5 changes: 5 additions & 0 deletions mtgmatcher/mtgjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ type Set struct {
Tokens []Card `json:"tokens"`
Type string `json:"type"`

// List of rarities present in the set
Rarities []string
// List of card colors present in the set
Colors []string

TCGPlayerGroupId int `json:"tcgplayerGroupId"`

Booster map[string]Booster `json:"booster"`
Expand Down

0 comments on commit 13e6c7a

Please sign in to comment.