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.
This commit is contained in:
tom.hempel
2025-11-13 11:21:12 +01:00
parent b8bf4d1ff8
commit 1464808fcc

View File

@ -1,6 +1,5 @@
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
using UnityEngine.SceneManagement;
using TMPro; using TMPro;
public class TaskTimer : MonoBehaviour public class TaskTimer : MonoBehaviour
@ -94,9 +93,24 @@ public class TaskTimer : MonoBehaviour
Debug.Log("TaskTimer: Timer reset"); 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) public void AddTime(float seconds)
{ {
timeRemaining += seconds; timeRemaining += seconds;
UpdateTimerDisplay();
Debug.Log($"TaskTimer: Added {seconds} seconds. New time: {timeRemaining}"); Debug.Log($"TaskTimer: Added {seconds} seconds. New time: {timeRemaining}");
} }
@ -139,10 +153,9 @@ public class TaskTimer : MonoBehaviour
private void OnTimerExpired() private void OnTimerExpired()
{ {
isRunning = false; isRunning = false;
Debug.Log("TaskTimer: Time expired! Returning to Lobby..."); Debug.Log("TaskTimer: Time expired!");
// Load Lobby scene // Timer expired - no automatic scene switching
SceneManager.LoadScene("Lobby");
} }
// Public methods for external control // 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 method to process timer commands (called from UDP listener in TaskButtonManager)
public void ProcessTimerCommand(string command) 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) switch (command)
{ {
@ -181,7 +224,7 @@ public class TaskTimer : MonoBehaviour
ResetTimer(); ResetTimer();
break; break;
default: 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 <seconds>, SETDURATION <seconds>");
break; break;
} }
} }