44 lines
1.2 KiB
Go
44 lines
1.2 KiB
Go
package pomodoro
|
|
|
|
import (
|
|
"git.smsvc.net/pomodoro/GoTomato/internal/broadcast"
|
|
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
|
"github.com/gorilla/websocket"
|
|
"time"
|
|
)
|
|
|
|
// startTimer runs the countdown and broadcasts every second.
|
|
func startTimer(clients map[*websocket.Conn]*models.Client, remainingSeconds int, mode string, session int, totalSessions int) bool {
|
|
for remainingSeconds > 0 {
|
|
select {
|
|
case <-pomodoroResetChannel:
|
|
return false
|
|
case <-pomodoroPauseChannel:
|
|
// Nothing to set here, just waiting for the signal
|
|
case <-pomodoroResumeChannel:
|
|
// Nothing to set here, just waiting for the signal
|
|
default:
|
|
// Broadcast the current state to all clients
|
|
if !IsPomodoroPaused() {
|
|
broadcast.BroadcastMessage(clients, models.ServerMessage{
|
|
Mode: mode,
|
|
Session: session,
|
|
TotalSession: totalSessions,
|
|
TimeLeft: remainingSeconds,
|
|
})
|
|
time.Sleep(time.Second)
|
|
remainingSeconds--
|
|
}
|
|
}
|
|
}
|
|
|
|
// Final broadcast when time reaches zero
|
|
broadcast.BroadcastMessage(clients, models.ServerMessage{
|
|
Mode: mode,
|
|
Session: session,
|
|
TotalSession: totalSessions,
|
|
TimeLeft: 0,
|
|
})
|
|
|
|
return true
|
|
}
|