draft/mlp/set.go

77 lines
1.8 KiB
Go
Raw Normal View History

2019-05-30 23:16:15 +00:00
package mlp
import (
"encoding/json"
2019-05-31 13:13:13 +00:00
"fmt"
2019-05-30 23:16:15 +00:00
"io/ioutil"
"net/http"
"strings"
2019-05-31 12:21:50 +00:00
"git.fromouter.space/mcg/draft"
2019-05-30 23:16:15 +00:00
)
// Set is a set/expansion of MLP:CCG
type Set struct {
ID SetID
Name string
Cards []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"`
}
2019-05-31 12:21:50 +00:00
// ToDraftCard converts cards to draft.Card
func (c Card) ToDraftCard() draft.Card {
return draft.Card{
ID: c.ID,
}
}
2019-05-30 23:16:15 +00:00
// PowerRequirement denotes one or more power requirements, colored or not
type PowerRequirement map[string]int
// LoadSet loads a set with a specified ID from JSON
func LoadSet(id SetID, setdata []byte) (*Set, error) {
var set Set
err := json.Unmarshal(setdata, &set)
set.ID = id
return &set, err
}
// LoadSetHTTP loads a set using MCG's remote server
func LoadSetHTTP(id SetID) (*Set, error) {
// Get SetID as string and make it lowercase
setid := strings.ToLower(string(id))
resp, err := http.Get("https://mcg.zyg.ovh/setdata/" + setid + ".json")
2019-05-31 13:13:13 +00:00
if err != nil || resp.StatusCode != 200 {
if err == nil {
err = fmt.Errorf("server returned non-200 response code (%d)", resp.StatusCode)
}
2019-05-30 23:16:15 +00:00
return nil, err
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return LoadSet(id, data)
}