draft/mlp/set.go
2019-05-31 01:16:15 +02:00

63 lines
1.5 KiB
Go

package mlp
import (
"encoding/json"
"io/ioutil"
"net/http"
"strings"
)
// 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"`
}
// 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")
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return LoadSet(id, data)
}