Compare commits
2 commits
8dfb772a2b
...
ac1ae4f506
Author | SHA1 | Date | |
---|---|---|---|
ac1ae4f506 | |||
e4ab093861 |
5 changed files with 136 additions and 31 deletions
76
mlp/card.go
Normal file
76
mlp/card.go
Normal 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
58
mlp/card_test.go
Normal 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())
|
||||||
|
}
|
||||||
|
}
|
|
@ -126,7 +126,7 @@ func (s *I8PSet) ProviderByType(typ I8PType) draft.CardProvider {
|
||||||
if len(s.Cards[typ]) < n {
|
if len(s.Cards[typ]) < n {
|
||||||
n = len(s.Cards[typ])
|
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
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
29
mlp/set.go
29
mlp/set.go
|
@ -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
|
||||||
|
|
|
@ -3,7 +3,7 @@ package mlp
|
||||||
import "git.fromouter.space/mcg/draft"
|
import "git.fromouter.space/mcg/draft"
|
||||||
|
|
||||||
// Converts multiple mlp.Card to draft.Card
|
// 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))
|
out := make([]draft.Card, len(cards))
|
||||||
for i, card := range cards {
|
for i, card := range cards {
|
||||||
out[i] = card.ToDraftCard()
|
out[i] = card.ToDraftCard()
|
||||||
|
|
Loading…
Reference in a new issue