Update templates

parent 3930037f
......@@ -872,6 +872,22 @@ class APIClient(ThreadedComponent):
}
)
self.message_bus.publish(response_message)
# Check if this is a successful fixture update that might trigger game start
if endpoint.name == 'fastapi_main' and processed_data.get('synchronized_matches', 0) > 0:
logger.info(f"Fixture update successful - {processed_data.get('synchronized_matches')} matches synchronized")
# Send a message to trigger game start check
game_start_check_message = Message(
type=MessageType.SYSTEM_STATUS,
sender=self.name,
data={
'status': 'fixture_update_completed',
'synchronized_matches': processed_data.get('synchronized_matches', 0),
'downloaded_zips': processed_data.get('downloaded_zips', 0),
'timestamp': datetime.utcnow().isoformat()
}
)
self.message_bus.publish(game_start_check_message)
logger.debug(f"API request successful: {endpoint.name}")
......@@ -1042,25 +1058,29 @@ class APIClient(ThreadedComponent):
fastapi_endpoint = self.endpoints.get("fastapi_main")
if not fastapi_endpoint:
return
old_has_token = bool(old_token and old_token.strip())
new_has_token = bool(new_token and new_token.strip())
# Get current interval setting
current_api_config = self.config_manager.get_section_config("api") or {}
current_interval = current_api_config.get("api_interval", 1800) # 30 minutes default
if not old_has_token and new_has_token:
# Token was added - start timer
# Token was added - start timer and trigger immediate fixture update
fastapi_endpoint.enabled = True
fastapi_endpoint.interval = current_interval
fastapi_endpoint.retry_attempts = 10
fastapi_endpoint.retry_delay = 30
fastapi_endpoint.consecutive_failures = 0 # Reset failure count
fastapi_endpoint.last_request = None # Reset to trigger immediate first request
logger.info(f"FastAPI timer started - token configured, {current_interval} second intervals enabled")
# Trigger immediate fixture update check
logger.info("Token configured - triggering immediate fixture update check")
self._execute_endpoint_request(fastapi_endpoint)
# Send immediate status update
status_message = Message(
type=MessageType.SYSTEM_STATUS,
......@@ -1069,20 +1089,21 @@ class APIClient(ThreadedComponent):
"status": "timer_started",
"endpoint": "fastapi_main",
"reason": "token_configured",
"interval_seconds": current_interval
"interval_seconds": current_interval,
"immediate_update_triggered": True
}
)
self.message_bus.publish(status_message)
elif old_has_token and not new_has_token:
# Token was removed - stop timer
fastapi_endpoint.enabled = False
fastapi_endpoint.interval = 600 # Reset to 10 minutes (default)
fastapi_endpoint.retry_attempts = 3
fastapi_endpoint.retry_delay = 5
logger.info("FastAPI timer stopped - token removed, automatic requests disabled")
# Send status update
status_message = Message(
type=MessageType.SYSTEM_STATUS,
......@@ -1094,14 +1115,18 @@ class APIClient(ThreadedComponent):
}
)
self.message_bus.publish(status_message)
elif old_has_token and new_has_token and old_token != new_token:
# Token was changed - keep timer running but reset failure count
# Token was changed - keep timer running but reset failure count and trigger immediate update
fastapi_endpoint.consecutive_failures = 0
fastapi_endpoint.last_request = None # Trigger immediate request with new token
logger.info("FastAPI token updated - timer continues with new authentication")
# Trigger immediate fixture update check with new token
logger.info("Token updated - triggering immediate fixture update check with new token")
self._execute_endpoint_request(fastapi_endpoint)
except Exception as e:
logger.error(f"Failed to handle token change: {e}")
......
......@@ -42,6 +42,7 @@ class GamesThread(ThreadedComponent):
self.message_bus.subscribe(self.name, MessageType.MATCH_START, self._handle_match_start)
self.message_bus.subscribe(self.name, MessageType.PLAY_VIDEO_MATCH_DONE, self._handle_play_video_match_done)
self.message_bus.subscribe(self.name, MessageType.MATCH_DONE, self._handle_match_done)
self.message_bus.subscribe(self.name, MessageType.SYSTEM_STATUS, self._handle_system_status)
# Send ready status
ready_message = MessageBuilder.system_status(
......@@ -1313,6 +1314,33 @@ class GamesThread(ThreadedComponent):
except Exception as e:
logger.error(f"Failed to handle MATCH_DONE message: {e}")
def _handle_system_status(self, message: Message):
"""Handle SYSTEM_STATUS messages, particularly fixture update completion"""
try:
status = message.data.get("status")
if status == "fixture_update_completed":
synchronized_matches = message.data.get("synchronized_matches", 0)
downloaded_zips = message.data.get("downloaded_zips", 0)
logger.info(f"Fixture update completed: {synchronized_matches} matches synchronized, {downloaded_zips} ZIPs downloaded")
# Check if we should start a game now that fixtures are available
if synchronized_matches > 0 and not self.game_active:
logger.info("New fixtures available and no game is active - attempting to start game")
# Send START_GAME message to ourselves to trigger game start
start_game_message = Message(
type=MessageType.START_GAME,
sender=self.name,
recipient=self.name,
data={
"timestamp": time.time()
}
)
self.message_bus.publish(start_game_message)
except Exception as e:
logger.error(f"Failed to handle system status message: {e}")
def _set_match_status(self, match_id: int, status: str):
"""Set match status in database"""
try:
......
......@@ -2,7 +2,7 @@
<html>
<head>
<meta charset="utf-8">
<title>Text Message Overlay</title>
<title>MBetther system</title>
<style>
* {
margin: 0;
......@@ -214,8 +214,8 @@
<div class="overlay-container">
<div class="message-panel" id="messagePanel">
<div class="message-icon" id="messageIcon">📢</div>
<div class="message-title" id="messageTitle">Announcement</div>
<div class="message-content" id="messageContent">This is a custom message from the system.</div>
<div class="message-title" id="messageTitle">Mbetter Game</div>
<div class="message-content" id="messageContent">Waiting for game to start....</div>
</div>
</div>
......@@ -325,4 +325,4 @@
<script src="qrc:///qtwebchannel/qwebchannel.js"></script>
<script src="overlay://overlay.js"></script>
</body>
</html>
\ No newline at end of file
</html>
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