From 8dfb772a2bc72954de81fd74955743c04b4ebcb2 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Wed, 26 Jun 2019 11:05:49 +0200 Subject: [PATCH] Make sets have a lookup map for cards --- mlp/booster.go | 6 +++--- mlp/booster_test.go | 4 ++-- mlp/set.go | 39 +++++++++++++++++++++++++++++---------- mlp/set_test.go | 2 +- 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/mlp/booster.go b/mlp/booster.go index 020a5b8..f2273fe 100644 --- a/mlp/booster.go +++ b/mlp/booster.go @@ -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) } } } diff --git a/mlp/booster_test.go b/mlp/booster_test.go index c5efc0c..9bb2f4d 100644 --- a/mlp/booster_test.go +++ b/mlp/booster_test.go @@ -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) } diff --git a/mlp/set.go b/mlp/set.go index 414ccab..5cb4487 100644 --- a/mlp/set.go +++ b/mlp/set.go @@ -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 } diff --git a/mlp/set_test.go b/mlp/set_test.go index 723d105..d5296af 100644 --- a/mlp/set_test.go +++ b/mlp/set_test.go @@ -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") } }