Enha: bot timer
This commit is contained in:
@ -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
|
||||
|
81
llmapi/timer.go
Normal file
81
llmapi/timer.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
@ -463,3 +463,5 @@ func (f *FullInfo) ExitRoom() *Room {
|
||||
f.Room = nil
|
||||
return resp
|
||||
}
|
||||
|
||||
// =======
|
||||
|
Reference in New Issue
Block a user