#!/usr/bin/env python3 """Score recorded DevControlBot battles from the OBSERVER ground truth. python3 tools/elm_bench/score_battles.py /tmp/rec_Elm_DrussGT [...] For every recording it counts, per bot, the bullets it fired (BulletFiredEvent), how many of them hit a BOT (BulletHitBotEvent), and the damage those hits dealt (BulletHitBotEvent.damage). Nothing here uses the bot's own opinion: the observer stream is the only source, which is the whole point of recording. Hit RATE is hits / (fired - still in flight at the end of the round) is NOT attempted: a bullet still in flight when the round ends is counted in the denominator as a miss, which is conservative but identical for both guns. """ import gzip, json, sys, glob, os, math from collections import defaultdict def wrap(a): while a > math.pi: a -= 2 * math.pi while a < -math.pi: a += 2 * math.pi return a def score(path): name_of = {} stats = defaultdict(lambda: dict(fired=0, hits=0, dmg=0.0, rounds_won=0, rounds=0)) prev_turn = {} noncontig = 0 with gzip.open(path, "rt") as fh: for line in fh: if not line.strip(): continue m = json.loads(line) if m.get("type") != "TickEventForObserver": if m.get("type") == "RoundEndedEventForObserver": for s in stats.values(): s["rounds"] += 1 continue rnd, turn = m["roundNumber"], m["turnNumber"] if prev_turn.get(rnd) not in (None, turn - 1): noncontig += 1 prev_turn[rnd] = turn for b in m.get("botStates", []): name_of[b["id"]] = b["name"] for e in m.get("events", []): t = e.get("type") if t == "BulletFiredEvent": stats[e["bullet"]["ownerId"]]["fired"] += 1 elif t == "BulletHitBotEvent": s = stats[e["bullet"]["ownerId"]] s["hits"] += 1 s["dmg"] += e.get("damage", 0.0) return name_of, stats, noncontig def main(): rows = [] for d in sys.argv[1:]: files = sorted(glob.glob(os.path.join(d, "*.battle.gz"))) if not files: print("no recording in", d) continue tot = defaultdict(lambda: dict(fired=0, hits=0, dmg=0.0, rounds=0)) noncontig = 0 for f in files: name_of, stats, nc = score(f) noncontig += nc for bid, s in stats.items(): n = name_of.get(bid, "?") for k in tot[n]: tot[n][k] += s[k] variant, opponent = os.path.basename(d)[4:].split("_", 1) for n, s in tot.items(): tag = "ELM" if variant == "Elm" else "heuristic" if n == "DevControlBot": rows.append((opponent, tag, s, noncontig)) rows.sort() print(f"{'opponent':<14}{'gun':<11}{'shots':>7}{'hits':>7}{'hit%':>8}{'damage':>9}{'rounds':>8}") for opp, tag, s, nc in rows: hr = 100.0 * s["hits"] / s["fired"] if s["fired"] else 0.0 print(f"{opp:<14}{tag:<11}{s['fired']:>7}{s['hits']:>7}{hr:>7.1f}%{s['dmg']:>9.0f}{s['rounds']:>8}") if nc: print(f" !! {nc} turnNumber discontinuities") if __name__ == "__main__": main()