refactor: Consolidate message types - rename START_GAMES to SCHEDULE_GAMES

- Rename START_GAMES message type to SCHEDULE_GAMES for clarity
- Add schedule_games() method to MessageBuilder class
- Update all references in games_thread.py, match_timer.py, routes.py, and tests
- Improve message naming consistency:
  * START_GAME: Actually starts games/timers
  * SCHEDULE_GAMES: Changes status to scheduled (doesn't start timer)
- All message types now follow consistent naming convention
- Fixes potential confusion between starting and scheduling actions
parent 20dbe062
This diff is collapsed.
......@@ -38,7 +38,7 @@ class MatchTimerComponent(ThreadedComponent):
# Register message handlers
self.message_bus.subscribe(self.name, MessageType.START_GAME, self._handle_start_game)
self.message_bus.subscribe(self.name, MessageType.START_GAMES, self._handle_start_games)
self.message_bus.subscribe(self.name, MessageType.SCHEDULE_GAMES, self._handle_schedule_games)
self.message_bus.subscribe(self.name, MessageType.CUSTOM, self._handle_custom_message)
logger.info("MatchTimer component initialized")
......@@ -141,10 +141,10 @@ class MatchTimerComponent(ThreadedComponent):
except Exception as e:
logger.error(f"Failed to handle START_GAME message: {e}")
def _handle_start_games(self, message: Message):
"""Handle START_GAMES message"""
def _handle_schedule_games(self, message: Message):
"""Handle SCHEDULE_GAMES message"""
try:
logger.info("Received START_GAMES message")
logger.info("Received SCHEDULE_GAMES message")
# Get match interval from configuration
match_interval = self._get_match_interval()
......@@ -153,7 +153,7 @@ class MatchTimerComponent(ThreadedComponent):
self._start_timer(match_interval * 60, None)
except Exception as e:
logger.error(f"Failed to handle START_GAMES message: {e}")
logger.error(f"Failed to handle SCHEDULE_GAMES message: {e}")
def _handle_custom_message(self, message: Message):
"""Handle custom messages (like timer state requests)"""
......
......@@ -62,7 +62,7 @@ class MessageType(Enum):
# Game messages
START_GAME = "START_GAME"
START_GAMES = "START_GAMES"
SCHEDULE_GAMES = "SCHEDULE_GAMES"
START_GAME_DELAYED = "START_GAME_DELAYED"
MATCH_START = "MATCH_START"
GAME_STATUS = "GAME_STATUS"
......@@ -550,6 +550,17 @@ class MessageBuilder:
}
)
@staticmethod
def schedule_games(sender: str, fixture_id: Optional[str] = None) -> Message:
"""Create SCHEDULE_GAMES message"""
return Message(
type=MessageType.SCHEDULE_GAMES,
sender=sender,
data={
"fixture_id": fixture_id
}
)
@staticmethod
def start_game_delayed(sender: str, fixture_id: Optional[str] = None, delay_minutes: Optional[int] = None) -> Message:
"""Create START_GAME_DELAYED message"""
......
......@@ -2119,11 +2119,11 @@ def control_match_timer():
return jsonify({"error": "Action is required"}), 400
if action == 'start':
# Send START_GAMES message to start the timer
# Send SCHEDULE_GAMES message to start the timer
if api_bp.message_bus:
from ..core.message_bus import MessageBuilder, MessageType
start_message = MessageBuilder.start_games(sender="web_dashboard")
start_message = MessageBuilder.schedule_games(sender="web_dashboard")
api_bp.message_bus.publish(start_message)
return jsonify({
......
#!/usr/bin/env python3
"""
Test script for the GamesThread component
"""
import sys
import os
import time
import logging
# Add the project root to Python path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from mbetterclient.core.games_thread import GamesThread
from mbetterclient.core.message_bus import MessageBus, Message, MessageType
from mbetterclient.database.manager import DatabaseManager
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def test_games_thread():
"""Test the GamesThread component"""
try:
logger.info("Testing GamesThread component...")
# Create a message bus
message_bus = MessageBus()
# Create a mock database manager (we won't actually connect to DB)
class MockDBManager:
def get_session(self):
class MockSession:
def query(self, model):
return self
def filter(self, *args):
return self
def order_by(self, *args):
return self
def first(self):
return None # No matches for this test
def close(self):
pass
return MockSession()
db_manager = MockDBManager()
# Create games thread
games_thread = GamesThread(
name="test_games_thread",
message_bus=message_bus,
db_manager=db_manager
)
# Test initialization
logger.info("Testing initialization...")
if not games_thread.initialize():
logger.error("Failed to initialize GamesThread")
return False
logger.info("GamesThread initialized successfully")
# Test START_GAME message handling
logger.info("Testing START_GAME message handling...")
start_game_message = Message(
type=MessageType.START_GAME,
sender="test",
data={
"fixture_id": "test_fixture_123"
}
)
# Send the message
message_bus.publish(start_game_message)
# Give it a moment to process
time.sleep(0.5)
# Test START_GAMES message handling
logger.info("Testing START_GAMES message handling...")
start_games_message = Message(
type=MessageType.SCHEDULE_GAMES,
sender="test",
data={
"fixture_id": "test_fixture_456"
}
)
# Send the message
message_bus.publish(start_games_message)
# Give it a moment to process
time.sleep(0.5)
# Test shutdown
logger.info("Testing shutdown...")
games_thread.shutdown()
logger.info("GamesThread test completed successfully")
return True
except Exception as e:
logger.error(f"GamesThread test failed: {e}")
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
success = test_games_thread()
sys.exit(0 if success else 1)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment