49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
package llmapi
|
|
|
|
import (
|
|
"context"
|
|
"gralias/broker"
|
|
"gralias/models"
|
|
"gralias/repos"
|
|
"gralias/timer"
|
|
"strconv"
|
|
)
|
|
|
|
func (b *Bot) StartTurnTimer(timeLeft uint32) {
|
|
logger := b.log.With("room_id", b.RoomID)
|
|
|
|
onTurnEnd := func(ctx context.Context, roomID string) {
|
|
room, err := repos.RP.RoomGetByID(context.Background(), roomID)
|
|
if err != nil {
|
|
logger.Error("failed to get room by id", "error", err)
|
|
return
|
|
}
|
|
logger.Info("turn time is over")
|
|
room.ChangeTurn()
|
|
room.MimeDone = false
|
|
if err := repos.RP.RoomUpdate(context.Background(), room); err != nil {
|
|
logger.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
|
|
}
|
|
}
|
|
|
|
onTick := func(ctx context.Context, roomID string, currentLeft uint32) {
|
|
broker.Notifier.Notifier <- broker.NotificationEvent{
|
|
EventName: models.NotifyTurnTimerPrefix + roomID,
|
|
Payload: strconv.FormatUint(uint64(currentLeft), 10),
|
|
}
|
|
}
|
|
|
|
timer.StartTurnTimer(context.Background(), b.RoomID, timeLeft, onTurnEnd, onTick, logger)
|
|
}
|
|
|
|
func (b *Bot) StopTurnTimer() {
|
|
timer.StopTurnTimer(b.RoomID)
|
|
} |