First pass at implementing a speech history option for games.

This commit is contained in:
Storm Dragon
2025-09-23 17:21:33 -04:00
parent 5444ec4047
commit aed7ba523d
4 changed files with 283 additions and 11 deletions

View File

@@ -6,10 +6,13 @@ Provides centralized services to replace global variables:
- ConfigService: Manages game configuration
- VolumeService: Handles volume settings
- PathService: Manages file paths
- SpeechHistoryService: Manages speech history for navigation
"""
import configparser
import os
import time
from collections import deque
from xdg import BaseDirectory
# For backward compatibility
@@ -272,3 +275,134 @@ class PathService:
globalPath = self.globalPath
return self
class SpeechHistoryService:
"""Speech history management service for message navigation."""
_instance = None
@classmethod
def get_instance(cls):
"""Get or create the singleton instance."""
if cls._instance is None:
cls._instance = SpeechHistoryService()
return cls._instance
def __init__(self, max_size=10):
"""Initialize speech history.
Args:
max_size (int): Maximum number of messages to keep in history
"""
self.history = deque(maxlen=max_size)
self.current_index = -1
self.max_size = max_size
def add_message(self, text):
"""Add a message to the speech history.
Args:
text (str): The spoken message to add to history
"""
if not text or not text.strip():
return
# Add message with timestamp
message_entry = {
'text': text.strip(),
'timestamp': time.time()
}
self.history.append(message_entry)
# Reset current index when new message is added
self.current_index = -1
def get_current(self):
"""Get the current message in history.
Returns:
str or None: Current message text, or None if no history
"""
if not self.history:
return None
if self.current_index == -1:
# Return most recent message
return self.history[-1]['text']
else:
# Return message at current index
if 0 <= self.current_index < len(self.history):
return self.history[self.current_index]['text']
return None
def move_previous(self):
"""Navigate to the previous message in history.
Returns:
str or None: Previous message text, or None if at beginning
"""
if not self.history:
return None
if self.current_index == -1:
# Start from the most recent message
self.current_index = len(self.history) - 1
elif self.current_index > 0:
# Move backward in history
self.current_index -= 1
else:
# Already at the beginning, wrap to the end
self.current_index = len(self.history) - 1
return self.get_current()
def move_next(self):
"""Navigate to the next message in history.
Returns:
str or None: Next message text, or None if at end
"""
if not self.history:
return None
if self.current_index == -1:
# Already at most recent, wrap to beginning
self.current_index = 0
elif self.current_index < len(self.history) - 1:
# Move forward in history
self.current_index += 1
else:
# At the end, wrap to beginning
self.current_index = 0
return self.get_current()
def clear_history(self):
"""Clear all messages from history."""
self.history.clear()
self.current_index = -1
def set_max_size(self, max_size):
"""Change the maximum history size.
Args:
max_size (int): New maximum size for history buffer
"""
if max_size > 0:
self.max_size = max_size
# Create new deque with new max size, preserving recent messages
old_history = list(self.history)
self.history = deque(old_history[-max_size:], maxlen=max_size)
# Adjust current index if needed
if self.current_index >= len(self.history):
self.current_index = len(self.history) - 1
def get_history_size(self):
"""Get the current number of messages in history.
Returns:
int: Number of messages in history
"""
return len(self.history)