Start intro managed

parent e9e2f6c9
...@@ -27,11 +27,52 @@ class GamesThread(ThreadedComponent): ...@@ -27,11 +27,52 @@ class GamesThread(ThreadedComponent):
self._shutdown_event = threading.Event() self._shutdown_event = threading.Event()
self.message_queue = None self.message_queue = None
def _cleanup_stale_ingame_matches(self):
"""Clean up any stale 'ingame' matches from previous crashed sessions"""
try:
session = self.db_manager.get_session()
try:
# Get today's date
today = datetime.now().date()
# Find all ingame matches from today that might be stale
stale_matches = session.query(MatchModel).filter(
MatchModel.start_time.isnot(None),
MatchModel.start_time >= datetime.combine(today, datetime.min.time()),
MatchModel.start_time < datetime.combine(today, datetime.max.time()),
MatchModel.status == 'ingame',
MatchModel.active_status == True
).all()
if not stale_matches:
logger.info("No stale ingame matches found")
return
logger.info(f"Found {len(stale_matches)} stale ingame matches - cleaning up")
# Change status to pending and set active_status to False
for match in stale_matches:
logger.info(f"Cleaning up stale match {match.match_number}: {match.fighter1_township} vs {match.fighter2_township}")
match.status = 'pending'
match.active_status = False
session.commit()
logger.info(f"Cleaned up {len(stale_matches)} stale ingame matches")
finally:
session.close()
except Exception as e:
logger.error(f"Failed to cleanup stale ingame matches: {e}")
def initialize(self) -> bool: def initialize(self) -> bool:
"""Initialize the games thread""" """Initialize the games thread"""
try: try:
logger.info("Initializing GamesThread...") logger.info("Initializing GamesThread...")
# Clean up any stale 'ingame' matches from previous crashed sessions
self._cleanup_stale_ingame_matches()
# Register with message bus first # Register with message bus first
self.message_queue = self.message_bus.register_component(self.name) self.message_queue = self.message_bus.register_component(self.name)
...@@ -274,6 +315,25 @@ class GamesThread(ThreadedComponent): ...@@ -274,6 +315,25 @@ class GamesThread(ThreadedComponent):
# Determine current game status # Determine current game status
game_status = self._determine_game_status() game_status = self._determine_game_status()
# If status is "already_active" but game is not active, activate the fixture
if game_status == "already_active" and not self.game_active:
logger.info("Status is 'already_active' but game is not active - activating fixture")
active_fixture = self._find_active_today_fixture()
if active_fixture:
# Create a dummy message for activation
dummy_message = Message(
type=MessageType.START_GAME,
sender=message.sender,
recipient=self.name,
data={"timestamp": time.time()},
correlation_id=message.correlation_id
)
self._activate_fixture(active_fixture, dummy_message)
# Update status after activation
game_status = "started"
else:
logger.warning("Could not find active fixture to activate")
# Send GAME_STATUS response back to the requester # Send GAME_STATUS response back to the requester
response = Message( response = Message(
type=MessageType.GAME_STATUS, type=MessageType.GAME_STATUS,
...@@ -548,9 +608,9 @@ class GamesThread(ThreadedComponent): ...@@ -548,9 +608,9 @@ class GamesThread(ThreadedComponent):
timer_running = self._is_timer_running_for_fixture(fixture_id) timer_running = self._is_timer_running_for_fixture(fixture_id)
if not timer_running: if not timer_running:
# Timer not running, change status to failed # Timer not running, change status to pending and set active_status to False
logger.info(f"Timer not running for fixture {fixture_id}, changing ingame matches to failed") logger.info(f"Timer not running for fixture {fixture_id}, changing ingame matches to pending and setting active_status to False")
self._change_fixture_matches_status(fixture_id, 'ingame', 'failed') self._change_fixture_matches_status(fixture_id, 'ingame', 'pending', active_status_to_set=False)
# Check if this was the only non-terminal fixture # Check if this was the only non-terminal fixture
if self._is_only_non_terminal_fixture(fixture_id): if self._is_only_non_terminal_fixture(fixture_id):
...@@ -593,7 +653,7 @@ class GamesThread(ThreadedComponent): ...@@ -593,7 +653,7 @@ class GamesThread(ThreadedComponent):
# In a real implementation, you'd check the match_timer component status # In a real implementation, you'd check the match_timer component status
return self.current_fixture_id == fixture_id and self.game_active return self.current_fixture_id == fixture_id and self.game_active
def _change_fixture_matches_status(self, fixture_id: str, from_status: str, to_status: str): def _change_fixture_matches_status(self, fixture_id: str, from_status: str, to_status: str, active_status_to_set: Optional[bool] = None):
"""Change status of matches in a fixture from one status to another""" """Change status of matches in a fixture from one status to another"""
try: try:
session = self.db_manager.get_session() session = self.db_manager.get_session()
...@@ -607,6 +667,9 @@ class GamesThread(ThreadedComponent): ...@@ -607,6 +667,9 @@ class GamesThread(ThreadedComponent):
for match in matches: for match in matches:
logger.info(f"Changing match {match.match_number} status from {from_status} to {to_status}") logger.info(f"Changing match {match.match_number} status from {from_status} to {to_status}")
match.status = to_status match.status = to_status
if active_status_to_set is not None:
match.active_status = active_status_to_set
logger.info(f"Setting active_status to {active_status_to_set} for match {match.match_number}")
session.commit() session.commit()
......
This diff is collapsed.
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