## Gun switching battle test: runs 10 rounds vs each enemy, captures [gun] lines ## from /tmp/modularbot_stdout.log (written by ModularBot.sh), reports scores ## and gun usage per battle. import std/[os, strutils, tables, sequtils] import test_framework/test_framework if not existsEnv("TR_SERVER_JAR") or not existsEnv("TR_BATTLE_RUNNER"): echo "Skipping: TR_SERVER_JAR / TR_BATTLE_RUNNER not set" quit(0) const modularBotDir = "/home/davide/Projects/SirRoboGarage/ModularBot_garage" sampleBots = "/home/davide/Downloads/robocode-tankroyale/sample-bots-nim-linux-1.0.7" let enemies = @[ ("Target", sampleBots / "Target"), ("SpinBot", sampleBots / "SpinBot"), ("Crazy", sampleBots / "Crazy"), ("PatternMover", "/home/davide/Projects/SirRoboGarage/PatternMover_garage"), ("RandomMover", "/home/davide/Projects/SirRoboGarage/RandomMover_garage"), ] const logFile = "/tmp/modularbot_stdout.log" proc readGunLines(since: int64): seq[string] = ## Returns all [gun] lines appended after `since` bytes. if not fileExists(logFile): return @[] let content = readFile(logFile) let tail = if since < content.len.int64: content[since.int ..< content.len] else: "" for line in tail.splitLines(): if "[gun]" in line: result.add line.strip() proc gunCounts(lines: seq[string]): Table[string, int] = for line in lines: # "[gun] switched: GunName" or "[gun] starting: GunName" let parts = line.split(' ') if parts.len >= 3: let name = parts[^1] result[name] = result.getOrDefault(name, 0) + 1 echo "" echo "=".repeat(70) echo "ModularBot Gun Switching Battle Report" echo "=".repeat(70) echo "" for (eName, eDir) in enemies: # Snapshot log size before battle let logSizeBefore: int64 = if fileExists(logFile): getFileSize(logFile) else: 0 var r: BattleResult try: r = runBattle(@[modularBotDir, eDir], rounds = 10, timeout = 180_000) except CatchableError as e: echo "BATTLE vs " & eName & " FAILED: " & e.msg continue # Collect gun lines printed during this battle let gunLines = readGunLines(logSizeBefore) let counts = gunCounts(gunLines) # Extract scores var myScore, theirScore: int for b in r.results: if b.name == "ModularBot": myScore = b.totalScore else: theirScore = b.totalScore let outcome = if "ModularBot" in r.winners: "WIN" else: "LOSS" echo "--- vs " & eName & " ---" echo " Score: ModularBot=" & $myScore & " " & eName & "=" & $theirScore & " [" & outcome & "]" echo " [gun] switches logged: " & $gunLines.len if counts.len > 0: echo " Gun usage:" for gun, cnt in counts.pairs: echo " " & gun & ": " & $cnt & " switch(es)" else: echo " (no [gun] lines captured)" echo ""