Compare commits
No commits in common. "ac1ae4f5068af3fd0f6ef62485a21b9da1551530" and "8dfb772a2bc72954de81fd74955743c04b4ebcb2" have entirely different histories.
ac1ae4f506
...
8dfb772a2b
5 changed files with 31 additions and 136 deletions
76
mlp/card.go
76
mlp/card.go
|
@ -1,76 +0,0 @@
|
|||
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
|
||||
}
|
|
@ -1,58 +0,0 @@
|
|||
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())
|
||||
}
|
||||
}
|
|
@ -126,7 +126,7 @@ func (s *I8PSet) ProviderByType(typ I8PType) draft.CardProvider {
|
|||
if len(s.Cards[typ]) < n {
|
||||
n = len(s.Cards[typ])
|
||||
}
|
||||
out, s.Cards[typ] = ToDraft(s.Cards[typ][:n]), s.Cards[typ][n:]
|
||||
out, s.Cards[typ] = toDraft(s.Cards[typ][:n]), s.Cards[typ][n:]
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
29
mlp/set.go
29
mlp/set.go
|
@ -19,6 +19,32 @@ type Set struct {
|
|||
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
|
||||
type jsonSet struct {
|
||||
ID SetID
|
||||
|
@ -37,6 +63,9 @@ func (j *jsonSet) toSet() (s Set) {
|
|||
return
|
||||
}
|
||||
|
||||
// PowerRequirement denotes one or more power requirements, colored or not
|
||||
type PowerRequirement map[string]int
|
||||
|
||||
var loadedSets = make(map[SetID]*Set)
|
||||
|
||||
// Errors
|
||||
|
|
|
@ -3,7 +3,7 @@ package mlp
|
|||
import "git.fromouter.space/mcg/draft"
|
||||
|
||||
// Converts multiple mlp.Card to draft.Card
|
||||
func ToDraft(cards []Card) []draft.Card {
|
||||
func toDraft(cards []Card) []draft.Card {
|
||||
out := make([]draft.Card, len(cards))
|
||||
for i, card := range cards {
|
||||
out[i] = card.ToDraftCard()
|
||||
|
|
Loading…
Reference in a new issue