88 lines
1.9 KiB
Go
88 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"git.fromouter.space/mcg/draft"
|
|
"git.fromouter.space/mcg/draft/mlp"
|
|
"git.fromouter.space/mcg/mlp-server-tools/draftbot/bot"
|
|
)
|
|
|
|
type session struct {
|
|
Options draftOptions
|
|
Players map[string]*draft.Player
|
|
Bots []*bot.Bot
|
|
Pod *draft.Pod
|
|
}
|
|
|
|
// Types of drafts
|
|
const (
|
|
draftBlock = "block"
|
|
draftSet = "set"
|
|
draftCube = "cube"
|
|
draftI8PCube = "i8pcube"
|
|
)
|
|
|
|
type draftOptions struct {
|
|
Type string `json:"type"`
|
|
|
|
// Block draft properties
|
|
Block string `json:"block,omitempty"`
|
|
|
|
// Set draft properties
|
|
Set string `json:"set,omitempty"`
|
|
|
|
// Cube draft properties
|
|
CubeURL string `json:"cube_url,omitempty"`
|
|
PackSize int `json:"pack_size,omitempty"`
|
|
|
|
// I8PCube properties
|
|
MainCount int `json:"main_count,omitempty"`
|
|
ProblemCount int `json:"problem_count,omitempty"`
|
|
|
|
// Shared
|
|
PackCount int `json:"pack_count,omitempty"` // Set and Cube
|
|
}
|
|
|
|
func (do draftOptions) getProvider() (draft.PackProvider, error) {
|
|
switch do.Type {
|
|
case draftBlock:
|
|
return mlp.BlockPacks(mlp.BlockID(do.Block))
|
|
case draftSet:
|
|
set, err := mlp.LoadSetHTTP(mlp.SetID(do.Set))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return draft.PacksFromSet(do.PackCount, set), nil
|
|
case draftCube:
|
|
cards, err := loadCube(do.CubeURL)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
cube := &draft.GenericCube{
|
|
Cards: cards,
|
|
PackSize: do.PackSize,
|
|
}
|
|
return draft.PacksFromSet(do.PackCount, cube), nil
|
|
case draftI8PCube:
|
|
cube, err := loadI8PCube(do.CubeURL)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return cube.PackProvider(do.MainCount, do.ProblemCount), nil
|
|
}
|
|
return nil, errors.New("unknown draft type")
|
|
}
|
|
|
|
func newSession(playerCount int, opt draftOptions) (*session, error) {
|
|
// Get pack provider for given options
|
|
provider, err := opt.getProvider()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &session{
|
|
Options: opt,
|
|
Pod: draft.MakePod(playerCount, provider),
|
|
}, nil
|
|
}
|