Make sets have a lookup map for cards
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
Hamcha 2019-06-26 11:05:49 +02:00
parent 143ee76cc7
commit 8dfb772a2b
4 changed files with 35 additions and 16 deletions

View file

@ -118,9 +118,9 @@ func (set *Set) ProviderByRarity(rarity Rarity) draft.CardProvider {
}
collection = rr
} else {
for _, card := range set.Cards {
if card.Rarity == rarity {
collection = append(collection, draft.Card{ID: card.ID})
for _, card := range set.CardList {
if set.CardData[card.ID].Rarity == rarity {
collection = append(collection, card)
}
}
}

View file

@ -30,7 +30,7 @@ func TestAlternates(t *testing.T) {
// Find all Premiere URs
prURs := []string{}
for _, card := range prSet.Cards {
for _, card := range prSet.CardData {
if card.Rarity == mlp.RarityUltraRare {
prURs = append(prURs, card.ID)
}
@ -38,7 +38,7 @@ func TestAlternates(t *testing.T) {
// Find all CN URs
cnURs := []string{}
for _, card := range cnSet.Cards {
for _, card := range cnSet.CardData {
if card.Rarity == mlp.RarityUltraRare {
cnURs = append(cnURs, card.ID)
}

View file

@ -13,9 +13,10 @@ import (
// Set is a set/expansion of MLP:CCG
type Set struct {
ID SetID
Name string
Cards []Card
ID SetID
Name string
CardList []draft.Card
CardData map[string]Card
}
// Card is a single MLP:CCG card in a set
@ -44,6 +45,24 @@ func (c Card) ToDraftCard() draft.Card {
}
}
// jsonSet is the set as serialized in the JSON files
type jsonSet struct {
ID SetID
Name string
Cards []Card
}
func (j *jsonSet) toSet() (s Set) {
s.Name = j.Name
s.CardData = make(map[string]Card)
s.CardList = make([]draft.Card, len(j.Cards))
for i, card := range j.Cards {
s.CardData[card.ID] = card
s.CardList[i] = draft.Card{ID: card.ID}
}
return
}
// PowerRequirement denotes one or more power requirements, colored or not
type PowerRequirement map[string]int
@ -87,15 +106,15 @@ func LoadSetBytes(id SetID, setdata []byte) (*Set, error) {
return set, nil
}
var set Set
err := json.Unmarshal(setdata, &set)
set.ID = id
// If set loaded fine, cache it
if err == nil {
loadedSets[set.ID] = &set
var jsonset jsonSet
err := json.Unmarshal(setdata, &jsonset)
if err != nil {
return nil, err
}
set := jsonset.toSet()
set.ID = id
loadedSets[set.ID] = &set
return &set, err
}

View file

@ -136,7 +136,7 @@ func TestLoadCache(t *testing.T) {
}
// Check that loaded set via HTTP is the dummy cache and not the real thing
if len(loadedset.Cards) != 0 {
if len(loadedset.CardList) != 0 {
t.Fatalf("[LoadSetHTTP] Set not loaded from cache")
}
}