This commit is contained in:
ilyastar9999
2025-12-03 13:52:28 +03:00
parent 07b338e823
commit 6f73f50f31
3 changed files with 57 additions and 10 deletions

View File

@@ -189,8 +189,9 @@ async def socketio_listener():
"""Listen to ForcAD scoreboard using Socket.IO"""
sio = socketio.AsyncClient(logger=False, engineio_logger=False)
# Cache for task names
# Cache for task and team names
task_names = {}
team_names = {}
@sio.event(namespace='/game_events')
async def update_scoreboard(data):
@@ -200,11 +201,20 @@ async def socketio_listener():
round_num = event_data.get('round', 0)
round_start = event_data.get('round_start', 0)
team_tasks = event_data.get('team_tasks', [])
teams_data = event_data.get('teams', [])
print(f"📊 Round {round_num} - Processing {len(team_tasks)} team updates")
conn = await db_pool.acquire()
try:
# Store team scores
for team in teams_data:
team_id = team.get('id')
await conn.execute("""
INSERT INTO team_scores (team_id, team_name, total_score, flag_points, round, timestamp)
VALUES ($1, $2, $3, $4, $5, NOW())
""", team_id, team_names.get(team_id, f'Team {team_id}'),
team.get('score', 0), team.get('flag_points', 0), round_num)
for team_task in team_tasks:
team_id = team_task.get('team_id')
task_id = team_task.get('task_id')
@@ -294,6 +304,10 @@ async def socketio_listener():
for task in tasks:
task_names[task.get('id')] = task.get('name')
# Cache team names
for team in teams:
team_names[team.get('id')] = team.get('name')
team_names_str = ', '.join([f"{t.get('name')} (ID:{t.get('id')})" for t in teams])
task_names_str = ', '.join([t.get('name') for t in tasks])
print(f" Teams: {team_names_str}")
@@ -390,25 +404,42 @@ async def get_stats():
@app.get("/attacks", dependencies=[Depends(verify_token)])
async def get_attacks(limit: int = 100, our_attacks: Optional[bool] = None, attacks_to_us: Optional[bool] = None):
"""Get recent attacks"""
"""Get recent attacks with team names"""
conn = await get_db()
try:
query = "SELECT * FROM attacks WHERE 1=1"
query = """
SELECT
a.*,
ts_attacker.team_name as attacker_team_name,
ts_victim.team_name as victim_team_name
FROM attacks a
LEFT JOIN (
SELECT DISTINCT ON (team_id) team_id, team_name
FROM team_scores
ORDER BY team_id, timestamp DESC
) ts_attacker ON a.attacker_team_id = ts_attacker.team_id
LEFT JOIN (
SELECT DISTINCT ON (team_id) team_id, team_name
FROM team_scores
ORDER BY team_id, timestamp DESC
) ts_victim ON a.victim_team_id = ts_victim.team_id
WHERE 1=1
"""
params = []
param_count = 0
if our_attacks is not None:
param_count += 1
query += f" AND is_our_attack = ${param_count}"
query += f" AND a.is_our_attack = ${param_count}"
params.append(our_attacks)
if attacks_to_us is not None:
param_count += 1
query += f" AND is_attack_to_us = ${param_count}"
query += f" AND a.is_attack_to_us = ${param_count}"
params.append(attacks_to_us)
param_count += 1
query += f" ORDER BY timestamp DESC LIMIT ${param_count}"
query += f" ORDER BY a.timestamp DESC LIMIT ${param_count}"
params.append(limit)
rows = await conn.fetch(query, *params)