Compare commits

...

2 commits

Author SHA1 Message Date
5c08a9b184 break: update ServerMessage model
- rename MaxSession to TotalSession (and JSON struct tags)

🤖
2024-10-20 23:20:23 +02:00
52634ed226 feat: allow clients to send pomodoro config
- allow clients to send custom configuration for pomodoro sessions
- update RunPomodoro to accept a configuration parameter
- modify startTimer to handle session count from config
- add default pomodoro configuration in client command handling

🤖
2024-10-20 23:18:35 +02:00
7 changed files with 49 additions and 42 deletions

View file

@ -39,14 +39,14 @@ These messages contain the following fields:
- mode: Indicates the current phase of the Pomodoro session ("Work", "ShortBreak", "LongBreak", or "none" if no session is running).
- session: The current session number (e.g., 1 for the first work session).
- max_session: The total number of sessions for the current Pomodoro cycle (e.g., 4 if the cycle consists of 4 work/break sessions).
- total_sessions: The total number of sessions for the current Pomodoro cycle (e.g., 4 if the cycle consists of 4 work/break sessions).
- time_left: The remaining time for the current mode, in seconds (e.g., 900 for 15 minutes).
| Message Type | Sent by Server | Example |
| --------------------- | ------------------------------------------------ | --------------------------------------------------- |
| Welcome Message | Upon initial client connection | `{"mode": "none", "session": 0, "max_session": 0, "time_left": 0}` |
| Session Update | Periodically during a session | `{"mode": "Work", "session": 1, "max_session": 4, "time_left": 900}` |
| Session End/Reset | After session completes or is reset | `{"mode": "none", "session": 0, "max_session": 0, "time_left": 0}` |
| Welcome Message | Upon initial client connection | `{"mode": "none", "session": 0, "total_sessions": 0, "time_left": 0}` |
| Session Update | Periodically during a session | `{"mode": "Work", "session": 1, "total_sessions": 4, "time_left": 900}` |
| Session End/Reset | After session completes or is reset | `{"mode": "none", "session": 0, "total_sessions": 0, "time_left": 0}` |
## Testing

View file

@ -41,11 +41,11 @@
var data = JSON.parse(event.data);
var mode = data.mode;
var session = data.session;
var maxSession = data.max_session;
var totalSession = data.total_sessions;
var timeLeft = data.time_left;
document.getElementById("timer").innerText =
mode + " Session " + session + "/" + maxSession + ": " + formatTime(timeLeft);
mode + " Session " + session + "/" + totalSession + ": " + formatTime(timeLeft);
};
ws.onclose = function () {
@ -61,7 +61,8 @@
// Start Button Click Event
document.getElementById("startButton").addEventListener("click", function () {
ws.send(JSON.stringify({command: "start"}));
// ws.send(JSON.stringify({command: "start"}));
ws.send(JSON.stringify({command: "start", config: {work: 10, shortBreak: 5, longBreak: 10, sessions: 2}}));
// Hide start button and show pause/resume and stop buttons
document.getElementById("startButton").style.display = "none";

View file

@ -7,13 +7,6 @@ import (
"sync"
)
var PomodoroConfig = models.GoTomatoPomodoroConfig{
Work: 15 * 60,
ShortBreak: 5 * 60,
LongBreak: 10 * 60,
Sessions: 4,
}
var pomodoroOngoing bool
var pomodoroPaused bool
@ -24,22 +17,22 @@ var pomodoroResumeChannel = make(chan bool, 1)
var mu sync.Mutex // to synchronize access to shared state
// RunPomodoro iterates the Pomodoro work/break sessions.
func RunPomodoro(clients map[*websocket.Conn]*models.Client) {
func RunPomodoro(clients map[*websocket.Conn]*models.Client, config models.GoTomatoPomodoroConfig) {
mu.Lock()
pomodoroOngoing = true
pomodoroPaused = false
mu.Unlock()
for session := 1; session <= PomodoroConfig.Sessions; session++ {
if !startTimer(clients, PomodoroConfig.Work, "Work", session) {
for session := 1; session <= config.Sessions; session++ {
if !startTimer(clients, config.Work, "Work", session, config.Sessions) {
break
}
if session == PomodoroConfig.Sessions {
if !startTimer(clients, PomodoroConfig.LongBreak, "LongBreak", session) {
if session == config.Sessions {
if !startTimer(clients, config.LongBreak, "LongBreak", session, config.Sessions) {
break
}
} else {
if !startTimer(clients, PomodoroConfig.ShortBreak, "ShortBreak", session) {
if !startTimer(clients, config.ShortBreak, "ShortBreak", session, config.Sessions) {
break
}
}
@ -61,7 +54,7 @@ func ResetPomodoro(clients map[*websocket.Conn]*models.Client) {
broadcast.BroadcastMessage(clients, models.ServerMessage{
Mode: "none",
Session: 0,
MaxSession: 0,
TotalSession: 0,
TimeLeft: 0,
})

View file

@ -8,7 +8,7 @@ import (
)
// startTimer runs the countdown and broadcasts every second.
func startTimer(clients map[*websocket.Conn]*models.Client, remainingSeconds int, mode string, session int) bool {
func startTimer(clients map[*websocket.Conn]*models.Client, remainingSeconds int, mode string, session int, totalSessions int) bool {
for remainingSeconds > 0 {
select {
case <-pomodoroResetChannel:
@ -23,7 +23,7 @@ func startTimer(clients map[*websocket.Conn]*models.Client, remainingSeconds int
broadcast.BroadcastMessage(clients, models.ServerMessage{
Mode: mode,
Session: session,
MaxSession: PomodoroConfig.Sessions,
TotalSession: totalSessions,
TimeLeft: remainingSeconds,
})
time.Sleep(time.Second)
@ -36,7 +36,7 @@ func startTimer(clients map[*websocket.Conn]*models.Client, remainingSeconds int
broadcast.BroadcastMessage(clients, models.ServerMessage{
Mode: mode,
Session: session,
MaxSession: PomodoroConfig.Sessions,
TotalSession: totalSessions,
TimeLeft: 0,
})

View file

@ -8,9 +8,19 @@ import (
"log"
)
var unsetPomodoroConfig models.GoTomatoPomodoroConfig // used to check if client passed a config json
// handleClientCommands listens for commands from WebSocket clients and dispatches to the timer.
func handleClientCommands(ws *websocket.Conn) {
for {
var clientCommand models.ClientCommand
var pomodoroConfig = models.GoTomatoPomodoroConfig{
Work: 25 * 60,
ShortBreak: 5 * 60,
LongBreak: 15 * 60,
Sessions: 4,
}
_, message, err := ws.ReadMessage()
if err != nil {
log.Printf("Client disconnected: %v", err)
@ -19,18 +29,20 @@ func handleClientCommands(ws *websocket.Conn) {
}
// Handle incoming commands
var command models.ClientCommand
err = json.Unmarshal(message, &command)
err = json.Unmarshal(message, &clientCommand)
if err != nil {
log.Printf("Error unmarshalling command: %v", err)
continue
}
// Process the command
switch command.Command {
switch clientCommand.Command {
case "start":
if !pomodoro.IsPomodoroOngoing() {
go pomodoro.RunPomodoro(Clients) // Start the timer with the list of clients
if clientCommand.Config != unsetPomodoroConfig {
pomodoroConfig = clientCommand.Config
}
go pomodoro.RunPomodoro(Clients, pomodoroConfig) // Start the timer with the list of clients
}
case "stop":
if pomodoro.IsPomodoroOngoing() {

View file

@ -9,6 +9,7 @@ import (
// ClientCommand represents a command from the client (start/stop).
type ClientCommand struct {
Command string `json:"command"`
Config GoTomatoPomodoroConfig `json:"config"`
}
type Client struct {

View file

@ -4,6 +4,6 @@ package models
type ServerMessage struct {
Mode string `json:"mode"` // "Work", "ShortBreak", or "LongBreak"
Session int `json:"session"` // Current session number
MaxSession int `json:"max_session"` // Total number of sessions
TotalSession int `json:"total_sessions"` // Total number of sessions
TimeLeft int `json:"time_left"` // Remaining time in seconds
}