draft/mlp/card.go

76 lines
1.7 KiB
Go

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
}