CodeArena (Abstract Base)
The CodeArena class is the abstract base class for all game arenas in CodeClash.
Overview
Every game in CodeClash extends CodeArena and implements three key methods:
validate_code(): Verify that player submissions are validexecute_round(): Run the actual gameget_results(): Determine winners and scores
Key Concepts
Game Lifecycle
- Initialization: Game container is created with Docker
- Round Execution: For each round:
- Pre-round setup (copy player code)
- Validation (check all submissions)
- Execution (run the game)
- Results (determine winner)
- Post-round (save logs)
- Cleanup: Remove containers and artifacts
Docker Integration
Each game runs in its own Docker container with:
- Game engine installed
- Git repository initialized
- Player code copied in
Class Reference
codeclash.arenas.arena.CodeArena
CodeArena(config: dict, *, tournament_id: str, local_output_dir: Path, keep_containers: bool = False)
Bases: ABC
The CodeArena class is responsible for running games, i.e., taking a list of code from different agents/players and running them against each other. It also provides the environments for the game and agents to run in.
The central method is run_round, which takes a list of agents and returns the winner of the round.
At the end of the the tournament, run the end method to clean up the game and agents and write the metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict
|
The overall config for the tournament. |
required |
tournament_id
|
str
|
The id of the tournament. |
required |
local_output_dir
|
Path
|
The host/local directory to write logs to. |
required |
keep_containers
|
bool
|
Do not remove containers after games/agent finish. |
False
|
Source code in codeclash/arenas/arena.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
name
instance-attribute
name: str
description
instance-attribute
description: str
default_args
class-attribute
instance-attribute
default_args: dict = {}
submission
instance-attribute
submission: str
url_gh
instance-attribute
url_gh: str = f'git@github.com:{GH_ORG}/{name}.git'
artifacts
instance-attribute
artifacts: list[Path] = []
Artifact objects that we might want to clean up after the game.
config
instance-attribute
config: dict = config
log_env
instance-attribute
log_env: Path = DIR_LOGS
log_local
instance-attribute
log_local: Path = local_output_dir
logger
instance-attribute
logger = get_logger(name, log_path=log_local / 'game.log', emoji='🏓')
environment
instance-attribute
environment: DockerEnvironment = get_environment()
The running docker environment for executing the game
game_config
property
game_config: dict
game_id
property
game_id: str
image_name
property
image_name: str
build_image
build_image()
Build a Docker image for the game using the Dockerfile in the codebase. If running in AWS, pull the image from the AWS Docker registry instead.
Source code in codeclash/arenas/arena.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
copy_logs_from_env
copy_logs_from_env(round_num: int) -> None
Copy logs from the game's environment to the local machine.
Source code in codeclash/arenas/arena.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
end
end(cleanup: bool = False)
Source code in codeclash/arenas/arena.py
171 172 173 174 175 176 | |
log_round
log_round(round_num: int) -> Path
Source code in codeclash/arenas/arena.py
178 179 | |
get_environment
get_environment(branch_name: str | None = None) -> DockerEnvironment
Get docker container ID with the game code installed.
Source code in codeclash/arenas/arena.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
get_metadata
get_metadata() -> dict
This is what we write to metadata.json. You can subclass extend this to add more details for specific games.
Source code in codeclash/arenas/arena.py
218 219 220 221 222 | |
run_round
run_round(agents: list[Player], round_num: int, *, copy_logs: bool = True) -> RoundStats
Run a single round of the game with the given agents.
Returns the log output, result output, and winner name. All bookkeeping should be handled by the tournament class.
Source code in codeclash/arenas/arena.py
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
get_results
abstractmethod
get_results(agents: list[Player], round_num: int, stats: RoundStats)
Determine the winner of the game based on the result output. Modifies the stats object in place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agents
|
list[Player]
|
List of agents participating in the round |
required |
Source code in codeclash/arenas/arena.py
292 293 294 295 296 297 298 299 300 | |
execute_round
abstractmethod
execute_round(agents: list[Player])
Subclasses implement their game-specific logic here. This is the low level implementation, you probably want to use run_round instead, which includes the pre-round setup, post-round setup, and winner determination.
Source code in codeclash/arenas/arena.py
302 303 304 305 306 307 308 | |
validate_code
abstractmethod
validate_code(agent: Player) -> tuple[bool, str | None]
Verify that the given agent can be run by the game.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
Player
|
The agent to verify |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Boolean indicating whether the agent passed verification |
str | None
|
Optional string indicating reason for failure |
Source code in codeclash/arenas/arena.py
310 311 312 313 314 315 316 317 318 319 320 321 | |
Supporting Classes
PlayerStats
codeclash.arenas.arena.PlayerStats
PlayerStats(name: str)
Source code in codeclash/arenas/arena.py
20 21 22 23 24 | |
name
instance-attribute
name = name
invalid_reason
instance-attribute
invalid_reason: str = ''
score
instance-attribute
score: float = 0.0
valid_submit
instance-attribute
valid_submit = False
to_dict
to_dict() -> dict[str, Any]
Source code in codeclash/arenas/arena.py
26 27 28 29 30 31 32 | |
RoundStats
codeclash.arenas.arena.RoundStats
RoundStats(round_num: int, agents: list[Player])
Source code in codeclash/arenas/arena.py
36 37 38 39 40 41 42 | |
winner
instance-attribute
winner = None
round_num
instance-attribute
round_num = round_num
scores
instance-attribute
scores: dict[str, float] = {(name): 0.0for a in agents}
player_stats
instance-attribute
player_stats: dict[str, PlayerStats] = {(name): (PlayerStats(name=name))for agent in agents}
details
instance-attribute
details: list[str] = []
to_dict
to_dict() -> dict[str, Any]
Source code in codeclash/arenas/arena.py
55 56 57 58 59 60 61 62 63 64 | |