From 5c08a9b1848399f79155790b78747f8a379acbbe Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Sun, 20 Oct 2024 23:20:23 +0200 Subject: [PATCH] break: update ServerMessage model MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - rename MaxSession to TotalSession (and JSON struct tags) 🤖 --- README.md | 8 ++++---- index.html | 4 ++-- internal/pomodoro/pomodoro.go | 8 ++++---- internal/pomodoro/timer.go | 18 +++++++++--------- pkg/models/server.go | 8 ++++---- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 39aee92..d34732d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.html b/index.html index 972e480..915812a 100644 --- a/index.html +++ b/index.html @@ -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 () { diff --git a/internal/pomodoro/pomodoro.go b/internal/pomodoro/pomodoro.go index f15f87c..3124b6c 100644 --- a/internal/pomodoro/pomodoro.go +++ b/internal/pomodoro/pomodoro.go @@ -52,10 +52,10 @@ func ResetPomodoro(clients map[*websocket.Conn]*models.Client) { // Broadcast the reset message to all clients broadcast.BroadcastMessage(clients, models.ServerMessage{ - Mode: "none", - Session: 0, - MaxSession: 0, - TimeLeft: 0, + Mode: "none", + Session: 0, + TotalSession: 0, + TimeLeft: 0, }) // Send a reset signal to stop any running timers diff --git a/internal/pomodoro/timer.go b/internal/pomodoro/timer.go index b9b0a70..f23d286 100644 --- a/internal/pomodoro/timer.go +++ b/internal/pomodoro/timer.go @@ -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, sessions 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: @@ -21,10 +21,10 @@ func startTimer(clients map[*websocket.Conn]*models.Client, remainingSeconds int // Broadcast the current state to all clients if !IsPomodoroPaused() { broadcast.BroadcastMessage(clients, models.ServerMessage{ - Mode: mode, - Session: session, - MaxSession: sessions, - TimeLeft: remainingSeconds, + Mode: mode, + Session: session, + TotalSession: totalSessions, + TimeLeft: remainingSeconds, }) time.Sleep(time.Second) remainingSeconds-- @@ -34,10 +34,10 @@ func startTimer(clients map[*websocket.Conn]*models.Client, remainingSeconds int // Final broadcast when time reaches zero broadcast.BroadcastMessage(clients, models.ServerMessage{ - Mode: mode, - Session: session, - MaxSession: sessions, - TimeLeft: 0, + Mode: mode, + Session: session, + TotalSession: totalSessions, + TimeLeft: 0, }) return true diff --git a/pkg/models/server.go b/pkg/models/server.go index 8c68258..b4de1c2 100644 --- a/pkg/models/server.go +++ b/pkg/models/server.go @@ -2,8 +2,8 @@ package models // ServerMessage represents the data sent to the client via WebSocket. 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 - TimeLeft int `json:"time_left"` // Remaining time in seconds + Mode string `json:"mode"` // "Work", "ShortBreak", or "LongBreak" + Session int `json:"session"` // Current session number + TotalSession int `json:"total_sessions"` // Total number of sessions + TimeLeft int `json:"time_left"` // Remaining time in seconds }