mlp-server-tools/draftbot/main.go

81 lines
1.6 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"
"syscall"
"time"
2019-06-17 22:00:08 +00:00
"git.fromouter.space/Artificiale/moa/sd"
"git.fromouter.space/mcg/cardgage/client/bot"
"github.com/go-kit/kit/log"
)
var logger log.Logger
func main() {
consulAddr := flag.String("consul.addr", "consul:8500", "Consul address")
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() {
errs <- runBot()
}()
logger.Log("exit", <-errs)
}
func runBot() error {
// 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
2019-06-20 22:38:50 +00:00
wsbot, err := bot.NewBot(addr, bot.Params{
Name: "Draftbot",
MsgBufferSize: 10,
Logger: logger,
GameIDs: []string{"mlpccg-mcg"},
Tags: []string{"draft"},
})
2019-06-17 22:00:08 +00:00
if err != nil {
return err
}
draftbot := newDraftBot()
wsbot.Listen(draftbot.onMessage)
return errors.New("eof")
}