full scoreboard update
This commit is contained in:
@@ -8,6 +8,7 @@ import asyncio
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Optional, Dict, Any
|
||||
import aiohttp
|
||||
import socketio
|
||||
from fastapi import FastAPI, HTTPException, Depends, Header
|
||||
from pydantic import BaseModel
|
||||
import asyncpg
|
||||
@@ -16,9 +17,11 @@ from contextlib import asynccontextmanager
|
||||
# Configuration
|
||||
DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://adctrl:adctrl@postgres:5432/adctrl")
|
||||
SECRET_TOKEN = os.getenv("SECRET_TOKEN", "change-me-in-production")
|
||||
SCOREBOARD_URL = os.getenv("SCOREBOARD_URL", "http://10.60.0.1:8080") # Base URL for Socket.IO
|
||||
SCOREBOARD_WS_URL = os.getenv("SCOREBOARD_WS_URL", "ws://scoreboard:8080/api/events")
|
||||
SCOREBOARD_API_URL = os.getenv("SCOREBOARD_API_URL", "http://10.60.0.1:8080/api")
|
||||
USE_HTTP_POLLING = os.getenv("USE_HTTP_POLLING", "false").lower() == "true"
|
||||
USE_SOCKETIO = os.getenv("USE_SOCKETIO", "true").lower() == "true" # Use Socket.IO by default
|
||||
POLLING_INTERVAL = int(os.getenv("POLLING_INTERVAL", "10")) # seconds
|
||||
OUR_TEAM_ID = int(os.getenv("OUR_TEAM_ID", "1"))
|
||||
ALERT_THRESHOLD_POINTS = float(os.getenv("ALERT_THRESHOLD_POINTS", "100"))
|
||||
@@ -272,6 +275,69 @@ async def http_polling_listener():
|
||||
|
||||
await asyncio.sleep(POLLING_INTERVAL)
|
||||
|
||||
async def socketio_listener():
|
||||
"""Listen to ForcAD scoreboard using Socket.IO"""
|
||||
sio = socketio.AsyncClient(logger=False, engineio_logger=False)
|
||||
|
||||
@sio.event(namespace='/game_events')
|
||||
async def update_scoreboard(data):
|
||||
"""Handle scoreboard update events"""
|
||||
try:
|
||||
event_data = data.get('data', {})
|
||||
round_num = event_data.get('round', 0)
|
||||
team_tasks = event_data.get('team_tasks', [])
|
||||
|
||||
print(f"📊 Round {round_num} - Processing {len(team_tasks)} team updates")
|
||||
|
||||
for team_task in team_tasks:
|
||||
team_id = team_task.get('team_id')
|
||||
task_id = team_task.get('task_id')
|
||||
stolen = team_task.get('stolen', 0)
|
||||
lost = team_task.get('lost', 0)
|
||||
|
||||
if team_id == OUR_TEAM_ID and (stolen > 0 or lost > 0):
|
||||
print(f" Team {team_id} Task {task_id}: stolen={stolen}, lost={lost}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error processing update_scoreboard: {e}")
|
||||
|
||||
@sio.event(namespace='/game_events')
|
||||
async def init_scoreboard(data):
|
||||
"""Handle initial scoreboard data"""
|
||||
try:
|
||||
print("📡 Received initial scoreboard data")
|
||||
event_data = data.get('data', {})
|
||||
teams = event_data.get('teams', [])
|
||||
tasks = event_data.get('tasks', [])
|
||||
|
||||
print(f" Teams: {', '.join([f\"{t.get('name')} (ID:{t.get('id')})\" for t in teams])}")
|
||||
print(f" Tasks: {', '.join([t.get('name') for t in tasks])}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error processing init_scoreboard: {e}")
|
||||
|
||||
@sio.event
|
||||
async def connect():
|
||||
print("✅ Connected to ForcAD scoreboard Socket.IO")
|
||||
|
||||
@sio.event
|
||||
async def disconnect():
|
||||
print("❌ Disconnected from scoreboard")
|
||||
|
||||
while True:
|
||||
try:
|
||||
print(f"Connecting to {SCOREBOARD_URL}/socket.io ...")
|
||||
await sio.connect(
|
||||
SCOREBOARD_URL,
|
||||
namespaces=['/game_events'],
|
||||
transports=['websocket']
|
||||
)
|
||||
await sio.wait()
|
||||
except Exception as e:
|
||||
print(f"Socket.IO error: {e}")
|
||||
print("Reconnecting in 5 seconds...")
|
||||
await asyncio.sleep(5)
|
||||
|
||||
async def websocket_listener():
|
||||
"""Listen to scoreboard WebSocket for events"""
|
||||
reconnect_delay = 5
|
||||
@@ -336,8 +402,14 @@ async def lifespan(app: FastAPI):
|
||||
global db_pool, ws_task
|
||||
db_pool = await asyncpg.create_pool(DATABASE_URL, min_size=2, max_size=10)
|
||||
|
||||
print(f"Our team ID: {OUR_TEAM_ID}")
|
||||
|
||||
# Start listener based on configuration
|
||||
if USE_HTTP_POLLING:
|
||||
if USE_SOCKETIO:
|
||||
print(f"Starting Socket.IO mode")
|
||||
print(f"Scoreboard URL: {SCOREBOARD_URL}")
|
||||
ws_task = asyncio.create_task(socketio_listener())
|
||||
elif USE_HTTP_POLLING:
|
||||
print(f"Starting HTTP polling mode (interval: {POLLING_INTERVAL}s)")
|
||||
print(f"Scoreboard API: {SCOREBOARD_API_URL}")
|
||||
ws_task = asyncio.create_task(http_polling_listener())
|
||||
|
||||
Reference in New Issue
Block a user