mlp-server-tools/draftbot/main.go

90 lines
2.1 KiB
Go
Raw Normal View History

2019-06-17 22:00:08 +00:00
package main // import "git.fromouter.space/mcg/mlp-server-tools/draftbot"
import (
"errors"
"flag"
"fmt"
"math/rand"
2019-06-17 22:00:08 +00:00
"os"
"os/signal"
"strings"
2019-06-17 22:00:08 +00:00
"syscall"
"time"
2019-06-17 22:00:08 +00:00
"git.fromouter.space/Artificiale/moa/sd"
botapi "git.fromouter.space/mcg/cardgage/client/bot"
2019-06-17 22:00:08 +00:00
"github.com/go-kit/kit/log"
)
var logger log.Logger
2019-06-28 14:15:25 +00:00
var logAll bool
2019-06-17 22:00:08 +00:00
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)")
2019-06-28 14:15:25 +00:00
flag.BoolVar(&logAll, "debug.log", false, "Log a lot of stuff")
flag.Parse()
2019-06-17 22:00:08 +00:00
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())
2019-06-17 22:00:08 +00:00
// 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)
2019-06-17 22:00:08 +00:00
}()
logger.Log("exit", <-errs)
}
func runBot(name string, games, tags []string) error {
2019-06-17 22:00:08 +00:00
// Search for roomsvc
roomsvc, err := sd.GetOne("roomsvc")
if err != nil {
return err
}
2019-06-20 22:38:50 +00:00
addr := fmt.Sprintf("ws://%s:%d/bot", roomsvc.Service.Address, roomsvc.Service.Port)
2019-06-17 22:00:08 +00:00
wsbot, err := botapi.NewBot(addr, botapi.Params{
Name: name,
2019-06-20 22:38:50 +00:00
MsgBufferSize: 10,
Logger: logger,
GameIDs: games,
Tags: tags,
2019-06-20 22:38:50 +00:00
})
2019-06-17 22:00:08 +00:00
if err != nil {
return err
}
2019-06-28 09:03:42 +00:00
draftbot := NewDraftBot(wsbot, name)
wsbot.Listen(draftbot.OnMessage)
2019-06-27 15:12:39 +00:00
2019-06-17 22:00:08 +00:00
return errors.New("eof")
}