From 718c9c10be9d540dab8555b7921965358f37ac81 Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Sun, 6 Jul 2025 14:36:18 +0300 Subject: [PATCH] Enha: bot timer --- llmapi/main.go | 7 ++++- llmapi/timer.go | 81 +++++++++++++++++++++++++++++++++++++++++++++++++ models/main.go | 2 ++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 llmapi/timer.go diff --git a/llmapi/main.go b/llmapi/main.go index 22402b9..d13ba20 100644 --- a/llmapi/main.go +++ b/llmapi/main.go @@ -99,6 +99,7 @@ func (b *Bot) checkGuess(word string, room *models.Room) error { room.MimeDone = false room.OpenedThisTurn = 0 room.ThisTurnLimit = 0 + b.StopTurnTimer() } switch string(color) { case string(models.WordColorBlack): @@ -116,12 +117,14 @@ func (b *Bot) checkGuess(word string, room *models.Room) error { Action: models.ActionTypeGameOver, } room.ActionHistory = append(room.ActionHistory, action) + b.StopTurnTimer() case string(models.WordColorWhite), string(oppositeColor): // end turn room.TeamTurn = oppositeColor room.MimeDone = false room.OpenedThisTurn = 0 room.ThisTurnLimit = 0 + b.StopTurnTimer() } // check if no cards left => game over if room.BlueCounter == 0 { @@ -139,6 +142,7 @@ func (b *Bot) checkGuess(word string, room *models.Room) error { Action: models.ActionTypeGameOver, } room.ActionHistory = append(room.ActionHistory, action) + b.StopTurnTimer() } if room.RedCounter == 0 { // red won @@ -155,6 +159,7 @@ func (b *Bot) checkGuess(word string, room *models.Room) error { Action: models.ActionTypeGameOver, } room.ActionHistory = append(room.ActionHistory, action) + b.StopTurnTimer() } ctx, tx, err := repo.InitTx(context.Background()) // nolint: errcheck @@ -272,7 +277,7 @@ func (b *Bot) BotMove() { b.log.Error("failed to create action", "error", err) return } - // StartTurnTimer(fi.Room.ID, fi.Room.Settings.RoundTime) + b.StartTurnTimer(room.Settings.RoundTime) if err := saveRoom(room); err != nil { b.log.Error("failed to save room", "error", err) return diff --git a/llmapi/timer.go b/llmapi/timer.go new file mode 100644 index 0000000..4ea0475 --- /dev/null +++ b/llmapi/timer.go @@ -0,0 +1,81 @@ +package llmapi + +import ( + "context" + "gralias/broker" + "gralias/models" + "strconv" + "sync" + "time" +) + +type roomTimer struct { + ticker *time.Ticker + done chan bool +} + +var ( + timers = make(map[string]*roomTimer) + mu sync.Mutex +) + +func (b *Bot) StartTurnTimer(timeLeft uint32) { + mu.Lock() + defer mu.Unlock() + if _, exists := timers[b.RoomID]; exists { + b.log.Debug("trying to launch already running timer", "room_id", b.RoomID) + return // Timer already running + } + ticker := time.NewTicker(1 * time.Second) + done := make(chan bool) + timers[b.RoomID] = &roomTimer{ticker: ticker, done: done} + go func() { + for { + select { + case <-done: + return + case <-ticker.C: + if timeLeft <= 0 { + room, err := repo.RoomGetByID(context.Background(), b.RoomID) + if err != nil { + b.log.Error("failed to get room by id", "error", err) + b.StopTurnTimer() + return + } + b.log.Info("turn time is over", "room_id", b.RoomID) + room.ChangeTurn() + room.MimeDone = false + if err := repo.RoomUpdate(context.Background(), room); err != nil { + b.log.Error("failed to save room", "error", err) + } + broker.Notifier.Notifier <- broker.NotificationEvent{ + EventName: models.NotifyTurnTimerPrefix + room.ID, + Payload: strconv.FormatUint(uint64(room.Settings.RoundTime), 10), + } + // notifyBotIfNeeded(room) + if botName := room.WhichBotToMove(); botName != "" { + SignalChanMap[botName] <- true + } + b.StopTurnTimer() + return + } + timeLeft-- + // notify(models.NotifyTurnTimerPrefix+roomID, strconv.FormatUint(uint64(timeLeft), 10)) + broker.Notifier.Notifier <- broker.NotificationEvent{ + EventName: models.NotifyTurnTimerPrefix + b.RoomID, + Payload: strconv.FormatUint(uint64(timeLeft), 10), + } + } + } + }() +} + +func (b *Bot) StopTurnTimer() { + mu.Lock() + defer mu.Unlock() + if timer, exists := timers[b.RoomID]; exists { + timer.ticker.Stop() + close(timer.done) + delete(timers, b.RoomID) + } +} diff --git a/models/main.go b/models/main.go index 0845e35..bb17982 100644 --- a/models/main.go +++ b/models/main.go @@ -463,3 +463,5 @@ func (f *FullInfo) ExitRoom() *Room { f.Room = nil return resp } + +// =======