Fix: notify bot

This commit is contained in:
Grail Finder
2025-05-22 16:47:49 +03:00
parent 31f721cd43
commit c155654d5f
7 changed files with 84 additions and 90 deletions

View File

@ -10,6 +10,7 @@ import (
"io"
"log/slog"
"net/http"
"os"
"strings"
)
@ -18,6 +19,14 @@ import (
// MIME: llm needs to know all the cards, colors and previous actions
// GUESSER: llm needs to know all the cards and previous actions
// channels are not serializable
var (
// botname -> channel
SignalChanMap = make(map[string]chan bool)
DoneChanMap = make(map[string]chan bool)
)
type Bot struct {
Role string // gueeser | mime
Team string
@ -26,15 +35,17 @@ type Bot struct {
BotName string
log *slog.Logger
// channels for communicaton
SignalsCh chan bool
DoneCh chan bool
// channels are not serializable
// SignalsCh chan bool
// DoneCh chan bool
}
// StartBot
func (b *Bot) StartBot() {
for {
select {
case <-b.SignalsCh:
case <-SignalChanMap[b.BotName]:
b.log.Debug("got signal", "bot-team", b.Team, "bot-role", b.Role)
// get room cards and actions
room, err := getRoomByID(b.RoomID)
if err != nil {
@ -53,7 +64,7 @@ func (b *Bot) StartBot() {
// if mime -> give clue
// if guesser -> open card (does opening one card prompting new loop?)
// send notification to sse broker
case <-b.DoneCh:
case <-DoneChanMap[b.BotName]:
b.log.Debug("got done signal", "bot-name", b.BotName)
return
}
@ -69,6 +80,10 @@ func NewBot(role, team, name, roomID string, cfg *config.Config) (*Bot, error) {
BotName: name,
Team: team,
cfg: cfg,
log: slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.LevelDebug,
AddSource: true,
})),
}
// add to room
room, err := getRoomByID(bot.RoomID)
@ -108,10 +123,25 @@ func NewBot(role, team, name, roomID string, cfg *config.Config) (*Bot, error) {
if err := saveRoom(room); err != nil {
return nil, err
}
if err := saveBot(bot); err != nil {
return nil, err
}
SignalChanMap[bot.BotName] = make(chan bool)
DoneChanMap[bot.BotName] = make(chan bool)
go bot.StartBot() // run bot routine
return bot, nil
}
func saveBot(bot *Bot) error {
key := "botkey_" + bot.RoomID + bot.BotName
data, err := json.Marshal(bot)
if err != nil {
return err
}
cache.MemCache.Set(key, data)
return nil
}
func getRoomByID(roomID string) (*models.Room, error) {
roomBytes, err := cache.MemCache.Get(models.CacheRoomPrefix + roomID)
if err != nil {