Fix: timer
This commit is contained in:
@ -337,7 +337,7 @@ func HandleGiveClue(w http.ResponseWriter, r *http.Request) {
|
|||||||
fi.Room.ThisTurnLimit = 9
|
fi.Room.ThisTurnLimit = 9
|
||||||
}
|
}
|
||||||
fi.Room.OpenedThisTurn = 0
|
fi.Room.OpenedThisTurn = 0
|
||||||
StartTurnTimer(fi.Room.ID, time.Duration(fi.Room.Settings.RoundTime)*time.Second)
|
StartTurnTimer(fi.Room.ID, fi.Room.Settings.RoundTime)
|
||||||
log.Debug("given clue", "clue", clue, "limit", fi.Room.ThisTurnLimit)
|
log.Debug("given clue", "clue", clue, "limit", fi.Room.ThisTurnLimit)
|
||||||
notify(models.NotifyBacklogPrefix+fi.Room.ID, clue+num)
|
notify(models.NotifyBacklogPrefix+fi.Room.ID, clue+num)
|
||||||
notifyBotIfNeeded(fi.Room)
|
notifyBotIfNeeded(fi.Room)
|
||||||
|
@ -2,7 +2,9 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"gralias/models"
|
"gralias/models"
|
||||||
|
"log/slog"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@ -18,29 +20,29 @@ var (
|
|||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
)
|
)
|
||||||
|
|
||||||
func StartTurnTimer(roomID string, duration time.Duration) {
|
func StartTurnTimer(roomID string, timeLeft uint32) {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
defer mu.Unlock()
|
defer mu.Unlock()
|
||||||
if _, exists := timers[roomID]; exists {
|
if _, exists := timers[roomID]; exists {
|
||||||
|
slog.Debug("trying to launch already running timer", "room_id", roomID)
|
||||||
return // Timer already running
|
return // Timer already running
|
||||||
}
|
}
|
||||||
ticker := time.NewTicker(1 * time.Second)
|
ticker := time.NewTicker(1 * time.Second)
|
||||||
done := make(chan bool)
|
done := make(chan bool)
|
||||||
timers[roomID] = &roomTimer{ticker: ticker, done: done}
|
timers[roomID] = &roomTimer{ticker: ticker, done: done}
|
||||||
go func() {
|
go func() {
|
||||||
room, err := repo.RoomGetByID(context.Background(), roomID)
|
|
||||||
if err != nil {
|
|
||||||
log.Error("failed to get room by id", "error", err)
|
|
||||||
StopTurnTimer(roomID)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
timeLeft := room.Settings.RoundTime
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-done:
|
case <-done:
|
||||||
return
|
return
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
if timeLeft <= 0 {
|
if timeLeft <= 0 {
|
||||||
|
room, err := repo.RoomGetByID(context.Background(), roomID)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("failed to get room by id", "error", err)
|
||||||
|
StopTurnTimer(roomID)
|
||||||
|
return
|
||||||
|
}
|
||||||
log.Info("turn time is over", "room_id", roomID)
|
log.Info("turn time is over", "room_id", roomID)
|
||||||
room.ChangeTurn()
|
room.ChangeTurn()
|
||||||
room.MimeDone = false
|
room.MimeDone = false
|
||||||
@ -53,7 +55,7 @@ func StartTurnTimer(roomID string, duration time.Duration) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
timeLeft--
|
timeLeft--
|
||||||
notify(models.NotifyRoomUpdatePrefix+roomID, "")
|
notify(models.NotifyTurnTimerPrefix+roomID, fmt.Sprintf("%d", timeLeft))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
Reference in New Issue
Block a user