39 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			39 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package handlers
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"gralias/models"
 | |
| 	"gralias/timer"
 | |
| 	"log/slog"
 | |
| 	"strconv"
 | |
| )
 | |
| 
 | |
| func StartTurnTimer(roomID string, timeLeft uint32) {
 | |
| 	logger := slog.Default().With("room_id", roomID)
 | |
| 
 | |
| 	onTurnEnd := func(ctx context.Context, roomID string) {
 | |
| 		room, err := repo.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 := repo.RoomUpdate(context.Background(), room); err != nil {
 | |
| 			logger.Error("failed to save room", "error", err)
 | |
| 		}
 | |
| 		notify(models.NotifyTurnTimerPrefix+room.ID, strconv.FormatUint(uint64(room.Settings.RoundTime), 10))
 | |
| 		notifyBotIfNeeded(room)
 | |
| 	}
 | |
| 
 | |
| 	onTick := func(ctx context.Context, roomID string, currentLeft uint32) {
 | |
| 		notify(models.NotifyTurnTimerPrefix+roomID, strconv.FormatUint(uint64(currentLeft), 10))
 | |
| 	}
 | |
| 
 | |
| 	timer.StartTurnTimer(context.Background(), roomID, timeLeft, onTurnEnd, onTick, logger)
 | |
| }
 | |
| 
 | |
| func StopTurnTimer(roomID string) {
 | |
| 	timer.StopTurnTimer(roomID)
 | |
| } | 
