2019-08-22 09:28:43 +00:00
|
|
|
package draftbot
|
2019-06-26 10:56:02 +00:00
|
|
|
|
|
|
|
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 {
|
2019-07-01 12:44:26 +00:00
|
|
|
Schema mlp.I8PSchema
|
|
|
|
ProblemPackSize int
|
|
|
|
Cards map[mlp.I8PType][]string
|
2019-06-26 10:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2019-07-01 12:44:26 +00:00
|
|
|
return mlp.MakeI8PCube(pool, cfg.Schema, cfg.ProblemPackSize), nil
|
2019-06-26 10:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|