Add function to load cards from a list

This commit is contained in:
Hamcha 2019-06-26 12:14:36 +02:00
parent 8dfb772a2b
commit e4ab093861
3 changed files with 134 additions and 29 deletions

76
mlp/card.go Normal file
View file

@ -0,0 +1,76 @@
package mlp
import (
"errors"
"strings"
"git.fromouter.space/mcg/draft"
)
// Card is a single MLP:CCG card in a set
type Card struct {
ID string
Name string
Subname string
Element []string
Keywords []string
Traits []string
Requirement PowerRequirement `json:",omitempty"`
Cost *int `json:",omitempty"`
Power *int `json:",omitempty"`
Type string
Text string
Rarity Rarity
ProblemBonus *int `json:",omitempty"`
ProblemOpponentPower int `json:",omitempty"`
ProblemRequirement PowerRequirement `json:",omitempty"`
}
// ToDraftCard converts cards to draft.Card
func (c Card) ToDraftCard() draft.Card {
return draft.Card{
ID: c.ID,
}
}
// PowerRequirement denotes one or more power requirements, colored or not
type PowerRequirement map[string]int
// Errors involving loading cards
var (
ErrInvalidCardID = errors.New("invalid card id")
ErrCardNotFound = errors.New("card not found in their set")
)
// LoadCardList loads cards from sets, fetching them from HTTP if needed/wanted
func LoadCardList(list []string, fetch bool) (out []Card, err error) {
out = make([]Card, len(list))
for i, card := range list {
if len(card) < 3 {
err = ErrInvalidCardID
return
}
// Get Set ID
setid := SetID(strings.ToUpper(card[:2]))
// Get set
var set *Set
if fetch {
set, err = LoadSetHTTP(setid)
} else {
set, err = LoadSetMemory(setid)
}
if err != nil {
return
}
cdata, ok := set.CardData[card]
if !ok {
err = ErrCardNotFound
return
}
out[i] = cdata
}
return
}

58
mlp/card_test.go Normal file
View file

@ -0,0 +1,58 @@
package mlp_test
import (
"testing"
"git.fromouter.space/mcg/draft/mlp"
)
func TestLoadCardList(t *testing.T) {
// Fetch two random cards
cards, err := mlp.LoadCardList([]string{"ff3", "pr54"}, true)
if err != nil {
t.Fatalf("Error while fetching cards: %s", err.Error())
}
// Check that fetched cards are the real deal
if cards[0].Name != "Ocellus" {
t.Fatalf("First card (ff3) is not Ocellus but %s", cards[0].Name)
}
if cards[1].Name != "Comet Tail" {
t.Fatalf("Second card (pr54) is not Comet Tail but %s", cards[1].Name)
}
}
func TestLoadCardListErrors(t *testing.T) {
// Clean all loaded sets
mlp.CleanSetCache()
// Fetch a card from an inexistant set
_, err := mlp.LoadCardList([]string{"E"}, true)
if err == nil {
t.Fatalf("LoadCardList succeeded fetching a card with an invalid ID")
} else if err != mlp.ErrInvalidCardID {
t.Fatalf("[E] Expected ErrInvalidCardID but got: %s", err.Error())
}
// Fetch a card from an inexistant set
_, err = mlp.LoadCardList([]string{"oof3"}, true)
if err == nil {
t.Fatalf("LoadCardList succeeded fetching a card from a non-existant set")
}
// Fetch an non-existant card in a real set
_, err = mlp.LoadCardList([]string{"ff3000"}, true)
if err == nil {
t.Fatalf("LoadCardList succeeded fetching a non-existant card from a real set")
} else if err != mlp.ErrCardNotFound {
t.Fatalf("[ff3000] Expected ErrCardNotFound but got: %s", err.Error())
}
// Try fetching from non-loaded sets
_, err = mlp.LoadCardList([]string{"pr10"}, false)
if err == nil {
t.Fatalf("LoadCardList succeeded fetching from a set that wasn't loaded")
} else if err != mlp.ErrSetNotLoaded {
t.Fatalf("Expected ErrSetNotLoaded but got: %s", err.Error())
}
}

View file

@ -19,32 +19,6 @@ type Set struct {
CardData map[string]Card CardData map[string]Card
} }
// Card is a single MLP:CCG card in a set
type Card struct {
ID string
Name string
Subname string
Element []string
Keywords []string
Traits []string
Requirement PowerRequirement `json:",omitempty"`
Cost *int `json:",omitempty"`
Power *int `json:",omitempty"`
Type string
Text string
Rarity Rarity
ProblemBonus *int `json:",omitempty"`
ProblemOpponentPower int `json:",omitempty"`
ProblemRequirement PowerRequirement `json:",omitempty"`
}
// ToDraftCard converts cards to draft.Card
func (c Card) ToDraftCard() draft.Card {
return draft.Card{
ID: c.ID,
}
}
// jsonSet is the set as serialized in the JSON files // jsonSet is the set as serialized in the JSON files
type jsonSet struct { type jsonSet struct {
ID SetID ID SetID
@ -63,9 +37,6 @@ func (j *jsonSet) toSet() (s Set) {
return return
} }
// PowerRequirement denotes one or more power requirements, colored or not
type PowerRequirement map[string]int
var loadedSets = make(map[SetID]*Set) var loadedSets = make(map[SetID]*Set)
// Errors // Errors