81 lines
1.6 KiB
Go
81 lines
1.6 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"io/ioutil"
|
||
|
"net/http"
|
||
|
"strings"
|
||
|
|
||
|
"git.fromouter.space/mcg/draft"
|
||
|
"git.fromouter.space/mcg/draft/mlp"
|
||
|
)
|
||
|
|
||
|
func loadCube(cubeURL string) ([]draft.Card, error) {
|
||
|
// Fetch document
|
||
|
resp, err := http.Get(cubeURL)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
respBytes, err := ioutil.ReadAll(resp.Body)
|
||
|
resp.Body.Close()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
// Each line is a card ID
|
||
|
cardids := strings.Split(string(respBytes), "\n")
|
||
|
cards := make([]draft.Card, 0)
|
||
|
// Convert to draft.Card
|
||
|
for _, cardid := range cardids {
|
||
|
cardid := strings.TrimSpace(cardid)
|
||
|
if len(cardid) < 1 {
|
||
|
// Skip empty lines
|
||
|
continue
|
||
|
}
|
||
|
cards = append(cards, draft.Card{ID: cardid})
|
||
|
}
|
||
|
return cards, nil
|
||
|
}
|
||
|
|
||
|
// I8PCubeConfig is an external JSON doc with the I8PCube pack seeding schema and card list
|
||
|
type I8PCubeConfig struct {
|
||
|
Schema mlp.I8PSchema
|
||
|
Cards map[mlp.I8PType][]string
|
||
|
}
|
||
|
|
||
|
// ToCube creates a I8PCube from the config
|
||
|
func (cfg *I8PCubeConfig) ToCube() (*mlp.I8PCube, error) {
|
||
|
// Load cards from given list
|
||
|
var err error
|
||
|
pool := make(mlp.I8PPool)
|
||
|
for typ, cardids := range cfg.Cards {
|
||
|
pool[typ], err = mlp.LoadCardList(cardids, true)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Make cube and return it
|
||
|
return mlp.MakeI8PCube(pool, cfg.Schema), nil
|
||
|
}
|
||
|
|
||
|
func loadI8PCube(cubeURL string) (*mlp.I8PCube, error) {
|
||
|
// Fetch document
|
||
|
resp, err := http.Get(cubeURL)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
// Deserialize response to JSON object
|
||
|
var cubeconf I8PCubeConfig
|
||
|
err = json.NewDecoder(resp.Body).Decode(&cubeconf)
|
||
|
resp.Body.Close()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
// Create cube from config
|
||
|
return cubeconf.ToCube()
|
||
|
}
|