This commit is contained in:
ilyastar9999
2025-12-03 14:28:45 +03:00
parent be8174ceea
commit 1a59806940
2 changed files with 25 additions and 15 deletions

View File

@@ -28,6 +28,7 @@ CREATE TABLE IF NOT EXISTS scoreboard_state (
service_name VARCHAR(255) NOT NULL,
stolen_flags INTEGER DEFAULT 0,
lost_flags INTEGER DEFAULT 0,
fp_score FLOAT DEFAULT 0,
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE(team_id, service_name)
);

View File

@@ -231,69 +231,78 @@ async def socketio_listener():
task_id = team_task.get('task_id')
current_stolen = team_task.get('stolen', 0)
current_lost = team_task.get('lost', 0)
current_fp_score = team_task.get('score', 0)
service_name = task_names.get(task_id, f"task_{task_id}")
# Get previous state from database
prev_state = await conn.fetchrow(
"SELECT stolen_flags, lost_flags FROM scoreboard_state WHERE team_id = $1 AND service_name = $2",
"SELECT stolen_flags, lost_flags, fp_score FROM scoreboard_state WHERE team_id = $1 AND service_name = $2",
team_id, service_name
)
prev_stolen = prev_state['stolen_flags'] if prev_state else 0
prev_lost = prev_state['lost_flags'] if prev_state else 0
prev_fp_score = prev_state['fp_score'] if prev_state else 0
# Calculate NEW flags (difference from previous state)
# Calculate NEW flags and FP changes (difference from previous state)
new_stolen = current_stolen - prev_stolen
new_lost = current_lost - prev_lost
fp_change = current_fp_score - prev_fp_score
# Update current state in database
await conn.execute("""
INSERT INTO scoreboard_state (team_id, service_name, stolen_flags, lost_flags, last_updated)
VALUES ($1, $2, $3, $4, NOW())
INSERT INTO scoreboard_state (team_id, service_name, stolen_flags, lost_flags, fp_score, last_updated)
VALUES ($1, $2, $3, $4, $5, NOW())
ON CONFLICT (team_id, service_name)
DO UPDATE SET stolen_flags = $3, lost_flags = $4, last_updated = NOW()
""", team_id, service_name, current_stolen, current_lost)
DO UPDATE SET stolen_flags = $3, lost_flags = $4, fp_score = $5, last_updated = NOW()
""", team_id, service_name, current_stolen, current_lost, current_fp_score)
# Create attack record only for NEW stolen flags
# Create attack record only for NEW stolen flags (FP gained)
if new_stolen > 0:
timestamp = datetime.utcnow()
attack_id = f"r{round_num}_stolen_team{team_id}_{service_name}_{int(timestamp.timestamp())}"
is_our_attack = team_id == OUR_TEAM_ID
# Calculate FP per flag for this attack (positive FP change)
fp_for_attack = max(0, fp_change) if new_stolen > 0 else 0
await conn.execute("""
INSERT INTO attacks (attack_id, attacker_team_id, service_name, timestamp, points, is_our_attack, is_attack_to_us)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (attack_id) DO NOTHING
""", attack_id, team_id, service_name, timestamp, float(new_stolen), is_our_attack, False)
""", attack_id, team_id, service_name, timestamp, float(fp_for_attack), is_our_attack, False)
if is_our_attack:
print(f" ✅ We stole {new_stolen} NEW flags from {service_name} (total: {current_stolen})")
print(f" ✅ We stole {new_stolen} NEW flags from {service_name} (+{fp_for_attack:.2f} FP)")
else:
print(f" 📌 Team {team_id} stole {new_stolen} NEW flags from {service_name} (total: {current_stolen})")
print(f" 📌 Team {team_id} stole {new_stolen} NEW flags from {service_name} (+{fp_for_attack:.2f} FP)")
# Create attack record only for NEW lost flags
# Create attack record only for NEW lost flags (FP lost)
if new_lost > 0:
timestamp = datetime.utcnow()
attack_id = f"r{round_num}_lost_team{team_id}_{service_name}_{int(timestamp.timestamp())}"
is_attack_to_us = team_id == OUR_TEAM_ID
# Calculate FP lost for this attack (negative FP change)
fp_for_attack = abs(min(0, fp_change)) if new_lost > 0 else 0
await conn.execute("""
INSERT INTO attacks (attack_id, victim_team_id, service_name, timestamp, points, is_our_attack, is_attack_to_us)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (attack_id) DO NOTHING
""", attack_id, team_id, service_name, timestamp, float(new_lost), False, is_attack_to_us)
""", attack_id, team_id, service_name, timestamp, float(fp_for_attack), False, is_attack_to_us)
if is_attack_to_us:
print(f" ⚠️ We LOST {new_lost} NEW flags on {service_name} (total lost: {current_lost})")
print(f" ⚠️ We LOST {new_lost} NEW flags on {service_name} (-{fp_for_attack:.2f} FP)")
# Check for alerts on high-value attacks
if new_lost >= ALERT_THRESHOLD_POINTS:
if fp_for_attack >= ALERT_THRESHOLD_POINTS:
await check_and_create_alerts(conn, 0, service_name)
else:
print(f" 📌 Team {team_id} lost {new_lost} NEW flags on {service_name} (total: {current_lost})")
print(f" 📌 Team {team_id} lost {new_lost} NEW flags on {service_name} (-{fp_for_attack:.2f} FP)")
finally:
await db_pool.release(conn)