From 5a52f06fc54eb5571623d7489b5727c2977cc1a5 Mon Sep 17 00:00:00 2001 From: jedarden Date: Sun, 24 May 2026 18:44:08 -0400 Subject: [PATCH] feat(monitoring): add combat death rate analysis script Added analyze-combat-deaths.sh script to track combat_death rate across replay files. Reports percentage of matches with combat_deaths by player count and average deaths per match. Usage: ./scripts/analyze-combat-deaths.sh /path/to/replays For production monitoring, download replays from R2 and analyze: aws s3 sync s3://acb-replays /tmp/replays ./scripts/analyze-combat-deaths.sh /tmp/replays Full database integration (persist combat_deaths to DB, dashboard UI) left for future work as combat_deaths are already tracked in replay files. Closes: bf-38ts --- scripts/analyze-combat-deaths.sh | 93 ++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 scripts/analyze-combat-deaths.sh diff --git a/scripts/analyze-combat-deaths.sh b/scripts/analyze-combat-deaths.sh new file mode 100644 index 0000000..4ae1275 --- /dev/null +++ b/scripts/analyze-combat-deaths.sh @@ -0,0 +1,93 @@ +#!/bin/bash +# Analyze combat_death rate from replay files +# Usage: ./scripts/analyze-combat-deaths.sh + +set -e + +REPLAY_DIR="${1:-/tmp/replays}" + +if [ ! -d "$REPLAY_DIR" ]; then + echo "Error: Replay directory not found: $REPLAY_DIR" + echo "Usage: $0 " + exit 1 +fi + +echo "=== Combat Death Rate Analysis ===" +echo "Analyzing replays in: $REPLAY_DIR" +echo + +# Count replays with combat_deaths by player count +analyze_replays() { + local player_count=$1 + local total=0 + local with_deaths=0 + local total_deaths=0 + + for replay in "$REPLAY_DIR"/*.json "$REPLAY_DIR"/*.json.gz 2>/dev/null; do + [ -f "$replay" ] || continue + + # Check player count + local pc=$(python3 -c " +import json, sys +try: + if '$replay'.endswith('.gz'): + import gzip + f = gzip.open('$replay', 'rt') + else: + f = open('$replay') + r = json.load(f) + print(len(r.get('players', []))) +except Exception as e: + print(0) +" 2>/dev/null) + + if [ "$pc" != "$player_count" ]; then + continue + fi + + total=$((total + 1)) + + # Count combat_deaths + local deaths=$(python3 -c " +import json, sys +try: + if '$replay'.endswith('.gz'): + import gzip + f = gzip.open('$replay', 'rt') + else: + f = open('$replay') + r = json.load(f) + deaths = 0 + for t in r.get('turns', []): + for e in t.get('events', []): + if e.get('type') == 'combat_death': + deaths += 1 + print(deaths) +except Exception as e: + print(0) +" 2>/dev/null) + + if [ "$deaths" -gt 0 ]; then + with_deaths=$((with_deaths + 1)) + total_deaths=$((total_deaths + deaths)) + fi + done + + if [ $total -eq 0 ]; then + echo "No $player_count-player replays found" + else + local rate=$((with_deaths * 100 / total)) + local avg=$(python3 -c "print($total_deaths / $total)") + echo "$player_count-player: $with_deaths/$total ($rate%) with combat_deaths, avg $avg per match" + fi +} + +# Analyze by player count +for pc in 2 3 4 6; do + analyze_replays $pc +done + +echo +echo "To monitor production matches:" +echo "1. Download replays from R2: aws s3 sync s3://acb-replays /tmp/replays" +echo "2. Run: ./scripts/analyze-combat-deaths.sh /tmp/replays"