mlp-server-tools/draftbot/main.go
Hamcha 9b5ad472c7
All checks were successful
continuous-integration/drone/push Build is passing
Refactor messages handling
2019-06-28 11:03:42 +02:00

88 lines
2.1 KiB
Go

package main // import "git.fromouter.space/mcg/mlp-server-tools/draftbot"
import (
"errors"
"flag"
"fmt"
"math/rand"
"os"
"os/signal"
"strings"
"syscall"
"time"
"git.fromouter.space/Artificiale/moa/sd"
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)
logger = log.With(logger, "caller", log.DefaultCaller)
// Register with consul
registrar := sd.Register(*consulAddr, sd.Options{
Name: "draftbot",
Tags: []string{
"bot", // I am a room bot
},
}, logger)
defer registrar.Deregister()
// Seed RNG
rand.Seed(time.Now().UnixNano())
// Initialize consul client for service discovery
sd.InitClient(*consulAddr)
errs := make(chan error)
go func() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errs <- fmt.Errorf("%s", <-c)
}()
go func() {
games := strings.Split(*gameFilter, ",")
tags := strings.Split(*tagFilter, ",")
errs <- runBot(*botName, games, tags)
}()
logger.Log("exit", <-errs)
}
func runBot(name string, games, tags []string) error {
// Search for roomsvc
roomsvc, err := sd.GetOne("roomsvc")
if err != nil {
return err
}
addr := fmt.Sprintf("ws://%s:%d/bot", roomsvc.Service.Address, roomsvc.Service.Port)
wsbot, err := botapi.NewBot(addr, botapi.Params{
Name: name,
MsgBufferSize: 10,
Logger: logger,
GameIDs: games,
Tags: tags,
})
if err != nil {
return err
}
draftbot := NewDraftBot(wsbot, name)
wsbot.Listen(draftbot.OnMessage)
return errors.New("eof")
}