diff --git a/scoreboard_injector/main.py b/scoreboard_injector/main.py index 03f4f2b..e9432a1 100644 --- a/scoreboard_injector/main.py +++ b/scoreboard_injector/main.py @@ -189,27 +189,79 @@ async def socketio_listener(): """Listen to ForcAD scoreboard using Socket.IO""" sio = socketio.AsyncClient(logger=False, engineio_logger=False) + # Cache for task names + task_names = {} + @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) + round_start = event_data.get('round_start', 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}") + # Get task names from previous init_scoreboard or fetch them + conn = await db_pool.acquire() + try: + 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) + score = team_task.get('score', 0) + status = team_task.get('status', 0) + + # Only process if there are stolen or lost flags + if stolen > 0 or lost > 0: + # Determine service name + service_name = task_names.get(task_id, f"task_{task_id}") + + # Create attack records based on stolen/lost changes + timestamp = datetime.utcnow() + + # If flags were stolen BY this team + if stolen > 0: + is_our_attack = team_id == OUR_TEAM_ID + # Generate attack record for each stolen flag (simplified) + attack_id = f"r{round_num}_stolen_team{team_id}_task{task_id}_{int(timestamp.timestamp())}" + + # For stolen flags, we are the attacker + if is_our_attack: + # We stole from someone (we don't know exact victim from this event) + await conn.execute(""" + INSERT INTO attacks (attack_id, attacker_team_id, victim_team_id, service_name, flag, timestamp, points, is_our_attack, is_attack_to_us) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) + ON CONFLICT (attack_id) DO NOTHING + """, attack_id, team_id, 0, service_name, f"ROUND_{round_num}", timestamp, stolen, True, False) + print(f" ✅ Our team stole {stolen} flags from {service_name}") + + # If flags were lost BY this team + if lost > 0: + is_attack_to_us = team_id == OUR_TEAM_ID + attack_id = f"r{round_num}_lost_team{team_id}_task{task_id}_{int(timestamp.timestamp())}" + + # For lost flags, we are the victim + if is_attack_to_us: + # Someone stole from us + await conn.execute(""" + INSERT INTO attacks (attack_id, attacker_team_id, victim_team_id, service_name, flag, timestamp, points, is_our_attack, is_attack_to_us) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) + ON CONFLICT (attack_id) DO NOTHING + """, attack_id, 0, team_id, service_name, f"ROUND_{round_num}", timestamp, lost, False, True) + print(f" ⚠️ We lost {lost} flags on {service_name}") + + # Check for alerts + await check_and_create_alerts(conn, 0, service_name) + finally: + await db_pool.release(conn) except Exception as e: print(f"Error processing update_scoreboard: {e}") + import traceback + traceback.print_exc() @sio.event(namespace='/game_events') async def init_scoreboard(data): @@ -220,10 +272,14 @@ async def socketio_listener(): teams = event_data.get('teams', []) tasks = event_data.get('tasks', []) - team_names = ', '.join([f"{t.get('name')} (ID:{t.get('id')})" for t in teams]) - task_names = ', '.join([t.get('name') for t in tasks]) - print(f" Teams: {team_names}") - print(f" Tasks: {task_names}") + # Cache task names + for task in tasks: + task_names[task.get('id')] = task.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}") + print(f" Tasks: {task_names_str}") except Exception as e: print(f"Error processing init_scoreboard: {e}")