2019-06-17 22:00:08 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-06-26 10:56:02 +00:00
|
|
|
"errors"
|
2019-06-27 11:49:57 +00:00
|
|
|
"fmt"
|
2019-06-26 12:59:55 +00:00
|
|
|
"math"
|
|
|
|
"math/rand"
|
2019-06-26 10:56:02 +00:00
|
|
|
|
2019-06-27 11:49:57 +00:00
|
|
|
room "git.fromouter.space/mcg/cardgage/room/api"
|
|
|
|
|
2019-06-17 22:00:08 +00:00
|
|
|
"git.fromouter.space/mcg/draft"
|
2019-06-26 10:56:02 +00:00
|
|
|
"git.fromouter.space/mcg/draft/mlp"
|
|
|
|
"git.fromouter.space/mcg/mlp-server-tools/draftbot/bot"
|
2019-06-17 22:00:08 +00:00
|
|
|
)
|
|
|
|
|
2019-06-26 12:59:55 +00:00
|
|
|
// Session-related errors
|
|
|
|
var (
|
|
|
|
ErrTooManyPlayers = errors.New("too many players")
|
|
|
|
ErrNotEnoughPlayers = errors.New("not enough players")
|
|
|
|
)
|
|
|
|
|
2019-06-17 22:00:08 +00:00
|
|
|
type session struct {
|
2019-06-26 10:56:02 +00:00
|
|
|
Options draftOptions
|
|
|
|
Players map[string]*draft.Player
|
|
|
|
Bots []*bot.Bot
|
|
|
|
Pod *draft.Pod
|
2019-06-26 12:59:55 +00:00
|
|
|
|
2019-06-27 14:59:52 +00:00
|
|
|
// State management variables
|
|
|
|
started bool // Has the draft started already?
|
|
|
|
|
2019-06-26 12:59:55 +00:00
|
|
|
// Channels for communication while the session is running
|
2019-06-27 11:49:57 +00:00
|
|
|
messages chan<- room.Message
|
|
|
|
exit chan bool
|
2019-06-26 10:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Types of drafts
|
|
|
|
const (
|
|
|
|
draftBlock = "block"
|
|
|
|
draftSet = "set"
|
|
|
|
draftCube = "cube"
|
|
|
|
draftI8PCube = "i8pcube"
|
|
|
|
)
|
|
|
|
|
2019-06-26 12:59:55 +00:00
|
|
|
// Ways in which players can be positioned along the draft pod
|
|
|
|
const (
|
|
|
|
posRandom = "random" // Place players randomly
|
|
|
|
posEven = "even" // Place players spaced as evenly as possible
|
|
|
|
)
|
|
|
|
|
2019-06-26 10:56:02 +00:00
|
|
|
type draftOptions struct {
|
2019-06-26 12:59:55 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
Positioning string `json:"positioning"`
|
2019-06-26 10:56:02 +00:00
|
|
|
|
|
|
|
// 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
|
2019-06-26 12:59:55 +00:00
|
|
|
|
2019-06-26 10:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
2019-06-26 12:59:55 +00:00
|
|
|
exit: make(chan bool),
|
2019-06-26 10:56:02 +00:00
|
|
|
}, nil
|
2019-06-17 22:00:08 +00:00
|
|
|
}
|
2019-06-26 12:59:55 +00:00
|
|
|
|
|
|
|
func (s *session) Start() error {
|
|
|
|
// Figure out how many players there are vs spots to be filled
|
|
|
|
spots := len(s.Pod.Players)
|
|
|
|
players := len(s.Players)
|
|
|
|
if players > spots {
|
|
|
|
return ErrTooManyPlayers
|
|
|
|
}
|
|
|
|
|
|
|
|
if players < 1 {
|
|
|
|
return ErrNotEnoughPlayers
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign players to their spot on the drafting pod
|
|
|
|
playerSpot := make(map[int]string)
|
|
|
|
|
|
|
|
switch s.Options.Positioning {
|
|
|
|
case posRandom:
|
|
|
|
// Assign a random number to each player
|
|
|
|
for pname := range s.Players {
|
|
|
|
var pos int
|
|
|
|
for {
|
|
|
|
pos = rand.Intn(spots)
|
|
|
|
// Make sure chosen number wasn't already picked
|
|
|
|
if _, ok := playerSpot[pos]; !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
playerSpot[pos] = pname
|
|
|
|
}
|
|
|
|
case posEven:
|
|
|
|
// Space players evenly
|
|
|
|
playerRatio := float64(spots) / float64(players)
|
|
|
|
i := 0
|
|
|
|
for name := range s.Players {
|
|
|
|
pos := int(math.Floor(playerRatio * float64(i)))
|
|
|
|
playerSpot[pos] = name
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 11:49:57 +00:00
|
|
|
// Prepare order to be broadcasted after all spots have been assigned
|
|
|
|
order := make([]string, len(s.Pod.Players))
|
|
|
|
|
2019-06-26 12:59:55 +00:00
|
|
|
// Assign player instances and make bots where needed
|
|
|
|
for i := range s.Pod.Players {
|
|
|
|
if name, ok := playerSpot[i]; ok {
|
|
|
|
s.Players[name] = s.Pod.Players[i]
|
2019-06-27 11:49:57 +00:00
|
|
|
order[i] = "player:" + name
|
2019-06-26 12:59:55 +00:00
|
|
|
} else {
|
|
|
|
s.Bots = append(s.Bots, bot.MakeBot(s.Pod.Players[i]))
|
2019-06-27 11:49:57 +00:00
|
|
|
order[i] = "bot"
|
2019-06-26 12:59:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify players of the order
|
2019-06-27 11:49:57 +00:00
|
|
|
s.messages <- room.Message{
|
|
|
|
Channel: "draft",
|
|
|
|
Type: "draft-order",
|
|
|
|
Data: order,
|
|
|
|
}
|
2019-06-26 12:59:55 +00:00
|
|
|
|
|
|
|
// Start handling packs
|
2019-06-27 14:59:52 +00:00
|
|
|
s.started = true
|
2019-06-26 12:59:55 +00:00
|
|
|
go s.handlePicks()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *session) handlePicks() {
|
|
|
|
// Pack loop, this `for` handles an entire draft session
|
2019-06-27 11:49:57 +00:00
|
|
|
totalPacks := len(s.Pod.Players[0].Packs)
|
|
|
|
currentPack := 0
|
2019-06-26 12:59:55 +00:00
|
|
|
for {
|
|
|
|
err := s.Pod.OpenPacks()
|
|
|
|
if err != nil {
|
|
|
|
if err == draft.ErrNoPacksLeft {
|
|
|
|
// Notify players that the draft is over
|
2019-06-27 11:49:57 +00:00
|
|
|
s.messages <- room.Message{
|
|
|
|
Channel: "draft",
|
|
|
|
Type: "draft-finish",
|
|
|
|
Message: "No more packs, the draft is over!",
|
|
|
|
}
|
2019-06-28 09:20:52 +00:00
|
|
|
// Send each player their deck
|
|
|
|
for pname, pdata := range s.Players {
|
|
|
|
s.messages <- room.Message{
|
|
|
|
To: pname,
|
|
|
|
Type: "draft-picks",
|
|
|
|
Data: draft.Pack(pdata.Picks).IDs(),
|
|
|
|
Message: fmt.Sprintf("Your picks are: %s", pdata.Picks),
|
|
|
|
}
|
|
|
|
}
|
2019-06-26 12:59:55 +00:00
|
|
|
return
|
|
|
|
}
|
2019-06-27 11:49:57 +00:00
|
|
|
// Something is wrong!
|
|
|
|
//TODO
|
|
|
|
return
|
|
|
|
}
|
|
|
|
currentPack++
|
|
|
|
s.messages <- room.Message{
|
|
|
|
Channel: "draft",
|
|
|
|
Type: "draft-newpack",
|
|
|
|
Message: fmt.Sprintf("Opening pack %d (of %d)", currentPack, totalPacks),
|
2019-06-26 12:59:55 +00:00
|
|
|
}
|
|
|
|
// Pick loop, this `for` handles exactly one round of packs
|
|
|
|
for {
|
|
|
|
// Make bots pick their cards
|
|
|
|
for _, bot := range s.Bots {
|
|
|
|
bot.PickNext()
|
|
|
|
}
|
2019-06-27 11:49:57 +00:00
|
|
|
// Tell every players their new cards
|
|
|
|
for _, player := range s.Players {
|
|
|
|
s.messages <- room.Message{
|
|
|
|
Channel: "draft",
|
|
|
|
Type: "draft-newpick",
|
|
|
|
Data: player.CurrentPack,
|
|
|
|
Message: fmt.Sprintf("You got these cards: %s", player.CurrentPack),
|
|
|
|
}
|
|
|
|
}
|
2019-06-26 12:59:55 +00:00
|
|
|
select {
|
|
|
|
case <-s.Pod.ReadyNextPick:
|
|
|
|
// Pass packs around
|
|
|
|
err := s.Pod.NextPacks()
|
|
|
|
if err != nil {
|
|
|
|
if err == draft.ErrNoPendingPack {
|
|
|
|
// No more picks to do for this round of packs, go to next
|
|
|
|
break
|
|
|
|
} else {
|
2019-06-27 11:49:57 +00:00
|
|
|
// Something is wrong!
|
2019-06-26 12:59:55 +00:00
|
|
|
//TODO
|
2019-06-27 11:49:57 +00:00
|
|
|
return
|
2019-06-26 12:59:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-s.Pod.ReadyNextPack:
|
|
|
|
// Break of pick loop, get next packs
|
|
|
|
break
|
|
|
|
case <-s.exit:
|
|
|
|
// Room closed, exit early
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|