From 1464808fcc81d0c2e01e41201ad80f6f83a9571c Mon Sep 17 00:00:00 2001 From: "tom.hempel" Date: Thu, 13 Nov 2025 11:21:12 +0100 Subject: [PATCH] Add SetDuration method and enhance command handling in TaskTimer - Introduced SetDuration method to allow setting the timer duration when not running. - Updated ProcessTimerCommand to handle ADDTIME and SETDURATION commands with appropriate validation. - Improved logging for command processing and added warnings for invalid inputs. --- Unity/Assets/Scripts/TaskTimer.cs | 55 +++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/Unity/Assets/Scripts/TaskTimer.cs b/Unity/Assets/Scripts/TaskTimer.cs index bd8395e..953017c 100644 --- a/Unity/Assets/Scripts/TaskTimer.cs +++ b/Unity/Assets/Scripts/TaskTimer.cs @@ -1,6 +1,5 @@ using UnityEngine; using UnityEngine.UI; -using UnityEngine.SceneManagement; using TMPro; public class TaskTimer : MonoBehaviour @@ -94,9 +93,24 @@ public class TaskTimer : MonoBehaviour Debug.Log("TaskTimer: Timer reset"); } + public void SetDuration(float seconds) + { + if (isRunning) + { + Debug.LogWarning("TaskTimer: Cannot set duration while timer is running. Use AddTime() instead."); + return; + } + + taskDuration = seconds; + timeRemaining = taskDuration; + UpdateTimerDisplay(); + Debug.Log($"TaskTimer: Duration set to {seconds} seconds"); + } + public void AddTime(float seconds) { timeRemaining += seconds; + UpdateTimerDisplay(); Debug.Log($"TaskTimer: Added {seconds} seconds. New time: {timeRemaining}"); } @@ -139,10 +153,9 @@ public class TaskTimer : MonoBehaviour private void OnTimerExpired() { isRunning = false; - Debug.Log("TaskTimer: Time expired! Returning to Lobby..."); + Debug.Log("TaskTimer: Time expired!"); - // Load Lobby scene - SceneManager.LoadScene("Lobby"); + // Timer expired - no automatic scene switching } // Public methods for external control @@ -164,7 +177,37 @@ public class TaskTimer : MonoBehaviour // Public method to process timer commands (called from UDP listener in TaskButtonManager) public void ProcessTimerCommand(string command) { - command = command.ToUpper(); + command = command.ToUpper().Trim(); + + // Check for ADDTIME command with value (e.g., "ADDTIME 30") + if (command.StartsWith("ADDTIME")) + { + string[] parts = command.Split(' '); + if (parts.Length >= 2 && float.TryParse(parts[1], out float seconds)) + { + AddTime(seconds); + } + else + { + Debug.LogWarning("TaskTimer: ADDTIME command requires a numeric value (e.g., 'ADDTIME 30')"); + } + return; + } + + // Check for SETDURATION command with value (e.g., "SETDURATION 300") + if (command.StartsWith("SETDURATION")) + { + string[] parts = command.Split(' '); + if (parts.Length >= 2 && float.TryParse(parts[1], out float seconds)) + { + SetDuration(seconds); + } + else + { + Debug.LogWarning("TaskTimer: SETDURATION command requires a numeric value (e.g., 'SETDURATION 300')"); + } + return; + } switch (command) { @@ -181,7 +224,7 @@ public class TaskTimer : MonoBehaviour ResetTimer(); break; default: - Debug.LogWarning($"TaskTimer: Unknown command: {command}. Valid commands: START, PAUSE, RESUME, RESET"); + Debug.LogWarning($"TaskTimer: Unknown command: {command}. Valid commands: START, PAUSE, RESUME, RESET, ADDTIME , SETDURATION "); break; } }