Skip to content

Commit

Permalink
mtgmatcher/backend: Add and API to retrieve any kind of name
Browse files Browse the repository at this point in the history
  • Loading branch information
kodawah committed Jul 24, 2024
1 parent f5947be commit 2f6d5d3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
23 changes: 23 additions & 0 deletions mtgmatcher/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,29 @@ func AllPromoTypes() []string {
return backend.AllPromoTypes
}

// Return a slice of all names loaded up, in three different fashion
// normalized, lowercase, or canonical
func AllNames(variant string, sealed bool) []string {
switch variant {
case "normalized":
if sealed {
return backend.AllSealed
}
return backend.AllNames
case "canonical":
if sealed {
return backend.AllCanonicalSealed
}
return backend.AllCanonicalNames
case "lowercase":
if sealed {
return backend.AllLowerSealed
}
return backend.AllLowerNames
}
return nil
}

func SearchEquals(name string) ([]string, error) {
if name == "" {
return backend.AllUUIDs, nil
Expand Down
31 changes: 28 additions & 3 deletions mtgmatcher/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,18 @@ var backend struct {

// Slice with every uniquely normalized name
AllNames []string
// Slice with every unique name, as it would appear on a card
AllCanonicalNames []string
// Slice with every unique name, lower case
AllLowerNames []string

// Slice with every uniquely normalized product name
AllSealed []string
// Slice with every unique product name, as defined by mtgjson
AllCanonicalSealed []string
// Slice with every unique product name, lower case
AllLowerSealed []string

// Map of normalized names to slice of uuids
Hashes map[string][]string

Expand Down Expand Up @@ -749,18 +759,20 @@ func NewDatastore(ap mtgjson.AllPrintings) {

// Add all names and associated uuids to the global names and hashes arrays
hashes := map[string][]string{}
var names []string
var sealed []string
var names, fullNames, lowerNames []string
var sealed, fullSealed, lowerSealed []string
for uuid, card := range uuids {
norm := Normalize(card.Name)
_, found := hashes[norm]
if !found {
if card.Sealed {
sealed = append(sealed, norm)
fullSealed = append(fullSealed, card.Name)
lowerSealed = append(lowerSealed, strings.ToLower(card.Name))
} else {
names = append(names, norm)
fullNames = append(fullNames, card.Name)
lowerNames = append(lowerNames, strings.ToLower(lowerNames))
lowerNames = append(lowerNames, strings.ToLower(card.Name))
}
}
hashes[norm] = append(hashes[norm], uuid)
Expand All @@ -770,6 +782,8 @@ func NewDatastore(ap mtgjson.AllPrintings) {
for altName, altProps := range alternates {
altNorm := Normalize(altName)
altNames = append(altNames, altNorm)
fullNames = append(fullNames, altName)
lowerNames = append(lowerNames, strings.ToLower(altName))
if altProps.IsFlavor {
// Retrieve all the uuids with a FlavorName attached
allAltUUIDs := hashes[Normalize(altProps.OriginalName)]
Expand Down Expand Up @@ -820,12 +834,23 @@ func NewDatastore(ap mtgjson.AllPrintings) {
sort.Strings(promoTypes)
sort.Strings(allSets)

sort.Strings(names)
sort.Strings(fullNames)
sort.Strings(lowerNames)

backend.Hashes = hashes
backend.AllSets = allSets
backend.AllUUIDs = allUUIDs
backend.AllSealedUUIDs = allSealedUUIDs

backend.AllNames = names
backend.AllCanonicalNames = fullNames
backend.AllLowerNames = lowerNames

backend.AllSealed = sealed
backend.AllCanonicalSealed = fullSealed
backend.AllLowerSealed = lowerSealed

backend.Sets = ap.Data
backend.Cards = cards
backend.Tokens = tokens
Expand Down

0 comments on commit 2f6d5d3

Please sign in to comment.