89 lines
2.1 KiB
Go
89 lines
2.1 KiB
Go
package main // import "git.fromouter.space/mcg/mlp-server-tools/rulebot"
|
|
|
|
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", "rulebot", "Bot name")
|
|
gameFilter := flag.String("filter.game", "mlpccg-mcg", "What game to filter for (separated by comma)")
|
|
tagFilter := flag.String("filter.tag", "+rules", "What tags to filter for (separated by comma)")
|
|
flag.BoolVar(&logAll, "debug.log", false, "Log a lot of stuff")
|
|
flag.Parse()
|
|
|
|
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: "rulebot",
|
|
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 := NewRuleBot(wsbot, name)
|
|
wsbot.Listen(draftbot.OnMessage)
|
|
|
|
return errors.New("eof")
|
|
}
|