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
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
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}/{self.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(self.name, log_path=self.log_local / 'game.log', emoji='🏓')
environment
instance-attribute
environment: DockerEnvironment = self.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
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 155 156 157 158 159 160 161 162 163 164 165 166 | |
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
168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
end
end(cleanup: bool = False)
Source code in codeclash/arenas/arena.py
183 184 185 186 187 188 | |
log_round
log_round(round_num: int) -> Path
Source code in codeclash/arenas/arena.py
190 191 | |
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
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |
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
230 231 232 233 234 | |
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
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 291 292 293 294 295 296 297 298 299 300 301 302 | |
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
304 305 306 307 308 309 310 311 312 | |
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
314 315 316 317 318 319 320 | |
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
322 323 324 325 326 327 328 329 330 331 332 333 | |
Supporting Classes
PlayerStats
codeclash.arenas.arena.PlayerStats
PlayerStats(name: str)
Source code in codeclash/arenas/arena.py
26 27 28 29 30 | |
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
32 33 34 35 36 37 38 | |
RoundStats
codeclash.arenas.arena.RoundStats
RoundStats(round_num: int, agents: list[Player])
Source code in codeclash/arenas/arena.py
42 43 44 45 46 47 48 | |
winner
instance-attribute
winner = None
round_num
instance-attribute
round_num = round_num
scores
instance-attribute
scores: dict[str, float] = {(a.name): 0.0 for a in agents}
player_stats
instance-attribute
player_stats: dict[str, PlayerStats] = {(agent.name): (PlayerStats(name=agent.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
61 62 63 64 65 66 67 68 69 70 | |