Files
SirRoboGarage/ModularBot_garage/tests/test_gauntlet_guns.nim
T

69 lines
2.6 KiB
Nim

## Gauntlet with [gun] line capture via raw runner output.
## nim c -r --path:../../common_libs tests/test_gauntlet_guns.nim
import std/[os, strutils, strformat, tables]
import test_framework/server_manager
import test_framework/bot_compiler
import test_framework/runner_process
import test_framework/battle_result
const
modularBotDir = currentSourcePath().parentDir.parentDir
samplesDir = "/home/davide/Downloads/robocode-tankroyale/sample-bots-nim-linux-1.0.7"
waveSurferDir = "/home/davide/Projects/SirRoboGarage/common_libs/test_framework/adversaries/WaveSurfer_garage"
spinBotDir = samplesDir / "SpinBot"
crazyDir = samplesDir / "Crazy"
if not fileExists("/home/davide/Projects/tank-royale/server/build/libs/robocode-tankroyale-server-0.35.5-all.jar") or
not fileExists("/home/davide/Projects/tank-royale/runner/examples/lib/robocode-tankroyale-runner.jar"):
echo "Skipping: Tank Royale JARs not found"
quit(0)
proc gunCounts(raw: string): Table[string, int] =
## Count [gun] switched: <Name> occurrences
for line in raw.splitLines():
if "[gun] switched:" in line:
let parts = line.split(":")
if parts.len >= 2:
let name = parts[^1].strip()
result[name] = result.getOrDefault(name, 0) + 1
proc runAndReport(label, enemyName, enemyDir: string) =
let sep = "=".repeat(50)
echo "\n" & sep
echo " " & label
echo sep
let bots = @[modularBotDir, enemyDir]
discard compileBots(bots)
let raw = runBattleRunner(getServerUrl(), bots, 10, 240000, true)
let r = parseServerOutput(raw)
var mbScore = 0; var enScore = 0
for b in r.results:
if b.name == "ModularBot": mbScore = b.totalScore
if b.name == enemyName: enScore = b.totalScore
let winner = if mbScore > enScore: "ModularBot" else: enemyName
echo " Score: ModularBot " & $mbScore & " " & enemyName & " " & $enScore & " -> " & winner & " wins"
let guns = gunCounts(raw)
if guns.len > 0:
echo " Gun switches:"
for name, count in guns.pairs:
echo fmt" [gun] {name}: {count}x"
else:
echo " (no [gun] switch lines in output)"
# Show all [gun] lines for visibility
var gunLines: seq[string]
for line in raw.splitLines():
if line.contains("[gun]"): gunLines.add(line.strip())
if gunLines.len > 0:
echo fmt" First 20 [gun] lines:"
for l in gunLines[0..min(19, gunLines.high)]: echo fmt" {l}"
ensureServer()
runAndReport("Battle 1/3: vs WaveSurfer", "WaveSurfer", waveSurferDir)
runAndReport("Battle 2/3: vs SpinBot", "SpinBot", spinBotDir)
runAndReport("Battle 3/3: vs Crazy", "Crazy", crazyDir)
echo "\nGauntlet complete."