From cbe38e9413fb6486a09120cdc8df912606c081c4 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Thu, 27 Jun 2019 13:49:57 +0200 Subject: [PATCH] Add necessary messages for a drafting session --- draftbot/draftbot.go | 32 ++++++++++++++++++++-------- draftbot/go.mod | 2 +- draftbot/go.sum | 10 +++++++++ draftbot/main.go | 24 ++++++++++++++------- draftbot/session.go | 50 +++++++++++++++++++++++++++++++++++++------- 5 files changed, 92 insertions(+), 26 deletions(-) diff --git a/draftbot/draftbot.go b/draftbot/draftbot.go index 864c0e6..c7224e5 100644 --- a/draftbot/draftbot.go +++ b/draftbot/draftbot.go @@ -5,11 +5,13 @@ import ( ) type draftBot struct { + Name string Sessions map[string]session } -func newDraftBot() *draftBot { +func newDraftBot(name string) *draftBot { return &draftBot{ + Name: name, Sessions: make(map[string]session), } } @@ -17,14 +19,26 @@ func newDraftBot() *draftBot { func (d *draftBot) onMessage(msg room.ServerMessage) { switch msg.Type { case room.MsgMessage: - logger.Log("event", "message", - "roomid", msg.RoomID, - "from", msg.Message.From, - "to", msg.Message.To, - "content", msg.Message.Message) + if *logAll { + logger.Log("event", "message", + "roomid", msg.RoomID, + "from", msg.Message.From, + "to", msg.Message.To, + "content", msg.Message.Message) + } + // Only consider messages that speak directly to me + if msg.Message.To == d.Name { + d.handleMessage(*msg.Message) + } case room.MsgEvent: - logger.Log("event", "event", - "roomid", msg.RoomID, - "content", msg.Event.Message) + if *logAll { + logger.Log("event", "event", + "roomid", msg.RoomID, + "content", msg.Event.Message) + } } } + +func (d *draftBot) handleMessage(msg room.Message) { + //TODO +} diff --git a/draftbot/go.mod b/draftbot/go.mod index 57d3f28..15051fd 100644 --- a/draftbot/go.mod +++ b/draftbot/go.mod @@ -4,7 +4,7 @@ go 1.12 require ( git.fromouter.space/Artificiale/moa v0.0.1-p2 - git.fromouter.space/mcg/cardgage v0.0.1-p2 + git.fromouter.space/mcg/cardgage v0.0.2 git.fromouter.space/mcg/draft v0.0.2 github.com/go-kit/kit v0.8.0 ) diff --git a/draftbot/go.sum b/draftbot/go.sum index 4637b78..bb699b7 100644 --- a/draftbot/go.sum +++ b/draftbot/go.sum @@ -3,6 +3,8 @@ git.fromouter.space/Artificiale/moa v0.0.1-p2 h1:KhoRQeYCFIpHZEucrXz142O5zfSsyEx git.fromouter.space/Artificiale/moa v0.0.1-p2/go.mod h1:dHYul6vVMwDCzre18AFs6NmI22yeI7AE0iQC1jFEQi0= git.fromouter.space/mcg/cardgage v0.0.1-p2 h1:u65ofEmtDHHQ8nbai97DW9kcrYa4x0I3NHv2oXhw8dI= git.fromouter.space/mcg/cardgage v0.0.1-p2/go.mod h1:vCmJ9HRdRGSWg2YQW9oNG7geYACdgWYmzL+zZdrsYhQ= +git.fromouter.space/mcg/cardgage v0.0.2 h1:u3Wz+UJx0wEix7vlqMlDX9Kg8nplCy8A0+nIKdNNPp0= +git.fromouter.space/mcg/cardgage v0.0.2/go.mod h1:vCmJ9HRdRGSWg2YQW9oNG7geYACdgWYmzL+zZdrsYhQ= git.fromouter.space/mcg/draft v0.0.2 h1:OT1uztgfCZnZCOg3uOtjQKH2WdwRAxNdYr4KXklVgXY= git.fromouter.space/mcg/draft v0.0.2/go.mod h1:QQmDm9FgAZL3b2/pIDd4Eo608SxMiCQQe5vIybe/CDY= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= @@ -16,6 +18,7 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24 github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932/go.mod h1:NOuUCSz6Q9T7+igc/hlvDOUdtWKryOrtFyIVABv/p7k= github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4= +github.com/cenkalti/backoff v2.0.0+incompatible h1:5IIPUHhlnUZbcHQsQou5k1Tn58nJkeJL9U+ig5CHJbY= github.com/cenkalti/backoff v2.0.0+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag= github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I= @@ -23,6 +26,7 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= @@ -47,6 +51,7 @@ github.com/gorilla/mux v1.7.2 h1:zoNxOV7WjqXptQOVngLmcSQgXmgk4NMz1HibBchjl/I= github.com/gorilla/mux v1.7.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed h1:5upAirOpQc1Q53c0bnx2ufif5kANL7bfZWcc6VJWJd8= github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4= github.com/hashicorp/consul/api v1.1.0 h1:BNQPM9ytxj6jbjjdRPioQ94T6YXriSopn0i8COv6SRA= github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q= @@ -89,6 +94,7 @@ github.com/hashicorp/serf v0.8.3 h1:MWYcmct5EtKz0efYooPcL0yNkem+7kWxqXDi/UIh+8k= github.com/hashicorp/serf v0.8.3/go.mod h1:UpNcs7fFbpKIyZaUuSW6EPiH+eZC7OuyFD+wc1oal+k= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 h1:T+h1c/A9Gawja4Y9mFVWj2vyii2bbUNDw3kt9VxK2EY= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= @@ -117,6 +123,7 @@ github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +github.com/opentracing/opentracing-go v1.0.2 h1:3jA2P6O1F9UOrWVpwrIo17pu01KWvNWg4X946/Y5Zwg= github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY= @@ -135,6 +142,7 @@ github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFo github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 h1:nn5Wsu0esKSJiIVhscUtVbo7ada43DJhG55ua/hjS5I= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/sirupsen/logrus v1.0.6/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc= +github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -182,8 +190,10 @@ google.golang.org/grpc v1.21.0 h1:G+97AoqBnmZIT91cLG/EkCoK9NSelj64P8bOHHNmGn0= google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fatih/pool.v2 v2.0.0 h1:xIFeWtxifuQJGk/IEPKsTduEKcKvPmhoiVDGpC40nKg= gopkg.in/fatih/pool.v2 v2.0.0/go.mod h1:8xVGeu1/2jr2wm5V9SPuMht2H5AEmf5aFMGSQixtjTY= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/rethinkdb/rethinkdb-go.v5 v5.0.1 h1:cBTUuUFTllRYtInK6FtBRa+tOBlZqpeDTnZF54xjGbg= gopkg.in/rethinkdb/rethinkdb-go.v5 v5.0.1/go.mod h1:x+1XKi70FH0kHCpvPQ78hGBCCxoNdE7sP+kEFdKgN6A= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/draftbot/main.go b/draftbot/main.go index f5994e2..7c3183d 100644 --- a/draftbot/main.go +++ b/draftbot/main.go @@ -7,18 +7,24 @@ import ( "math/rand" "os" "os/signal" + "strings" "syscall" "time" "git.fromouter.space/Artificiale/moa/sd" - "git.fromouter.space/mcg/cardgage/client/bot" + botapi "git.fromouter.space/mcg/cardgage/client/bot" "github.com/go-kit/kit/log" ) var logger log.Logger +var logAll *bool func main() { consulAddr := flag.String("consul.addr", "consul:8500", "Consul address") + botName := flag.String("bot.name", "draftbot", "Bot name") + gameFilter := flag.String("filter.game", "mlpccg-mcg", "What game to filter for (separated by comma)") + tagFilter := flag.String("filter.tag", "draft", "What tags to filter for (separated by comma)") + logAll = flag.Bool("debug.log", false, "Log a lot of stuff") logger = log.NewLogfmtLogger(os.Stderr) logger = log.With(logger, "ts", log.DefaultTimestampUTC) @@ -47,13 +53,15 @@ func main() { }() go func() { - errs <- runBot() + games := strings.Split(*gameFilter, ",") + tags := strings.Split(*tagFilter, ",") + errs <- runBot(*botName, games, tags) }() logger.Log("exit", <-errs) } -func runBot() error { +func runBot(name string, games, tags []string) error { // Search for roomsvc roomsvc, err := sd.GetOne("roomsvc") if err != nil { @@ -62,18 +70,18 @@ func runBot() error { addr := fmt.Sprintf("ws://%s:%d/bot", roomsvc.Service.Address, roomsvc.Service.Port) - wsbot, err := bot.NewBot(addr, bot.Params{ - Name: "Draftbot", + wsbot, err := botapi.NewBot(addr, botapi.Params{ + Name: name, MsgBufferSize: 10, Logger: logger, - GameIDs: []string{"mlpccg-mcg"}, - Tags: []string{"draft"}, + GameIDs: games, + Tags: tags, }) if err != nil { return err } - draftbot := newDraftBot() + draftbot := newDraftBot(name) wsbot.Listen(draftbot.onMessage) return errors.New("eof") diff --git a/draftbot/session.go b/draftbot/session.go index e92e418..4e37bb1 100644 --- a/draftbot/session.go +++ b/draftbot/session.go @@ -2,9 +2,12 @@ package main import ( "errors" + "fmt" "math" "math/rand" + room "git.fromouter.space/mcg/cardgage/room/api" + "git.fromouter.space/mcg/draft" "git.fromouter.space/mcg/draft/mlp" "git.fromouter.space/mcg/mlp-server-tools/draftbot/bot" @@ -23,7 +26,8 @@ type session struct { Pod *draft.Pod // Channels for communication while the session is running - exit chan bool + messages chan<- room.Message + exit chan bool } // Types of drafts @@ -147,17 +151,26 @@ func (s *session) Start() error { } } + // Prepare order to be broadcasted after all spots have been assigned + order := make([]string, len(s.Pod.Players)) + // 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] + order[i] = "player:" + name } else { s.Bots = append(s.Bots, bot.MakeBot(s.Pod.Players[i])) + order[i] = "bot" } } // Notify players of the order - //TODO + s.messages <- room.Message{ + Channel: "draft", + Type: "draft-order", + Data: order, + } // Start handling packs go s.handlePicks() @@ -166,25 +179,45 @@ func (s *session) Start() error { func (s *session) handlePicks() { // Pack loop, this `for` handles an entire draft session + totalPacks := len(s.Pod.Players[0].Packs) + currentPack := 0 for { err := s.Pod.OpenPacks() if err != nil { if err == draft.ErrNoPacksLeft { // Notify players that the draft is over - //TODO + s.messages <- room.Message{ + Channel: "draft", + Type: "draft-finish", + Message: "No more packs, the draft is over!", + } return - } else { - //TODO } + // 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), } // Pick loop, this `for` handles exactly one round of packs for { - // Notify players that their next pack is ready - //TODO // Make bots pick their cards for _, bot := range s.Bots { bot.PickNext() } + // 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), + } + } select { case <-s.Pod.ReadyNextPick: // Pass packs around @@ -194,8 +227,9 @@ func (s *session) handlePicks() { // No more picks to do for this round of packs, go to next break } else { - // Something wrong! + // Something is wrong! //TODO + return } } case <-s.Pod.ReadyNextPack: