Skip to content

Commit 9ca5509

Browse files
committed
Enh: Write out game metadata
1 parent f12ac8c commit 9ca5509

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

codeclash/games/abstract.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import getpass
2+
import json
23
import os
34
import subprocess
45
import time
@@ -26,6 +27,7 @@ class CodeGame(ABC):
2627
def __init__(self, config: dict):
2728
self.url_gh: str = f"git@github.com:{GH_ORG}/{self.name}.git"
2829
self.artifacts: list[Path] = []
30+
"""Artifact objects that we might want to clean up after the game."""
2931
self.scoreboard: list[tuple[int, str]] = []
3032
"""List of (round number, winner (player id))"""
3133
self.config: dict = config["game"]
@@ -77,8 +79,20 @@ def build_image(self):
7779
)
7880
raise RuntimeError(f"Failed to build Docker image: {result.stderr}")
7981

82+
def get_metadata(self) -> dict:
83+
"""This is what we write to metadata.json.
84+
You can subclass extend this to add more details for specific games.
85+
"""
86+
return {
87+
"name": self.name,
88+
"scoreboard": self.scoreboard,
89+
"config": self.config,
90+
"game_id": self.game_id,
91+
}
92+
8093
def end(self, cleanup: bool = False):
81-
self.logger.info(Counter([x[1] for x in self.scoreboard]))
94+
self.logger.info("Overall score: %s", Counter([x[1] for x in self.scoreboard]))
95+
(self.log_local / "metadata.json").write_text(json.dumps(self.get_metadata()))
8296
if cleanup:
8397
for artifact in self.artifacts:
8498
if artifact.exists():
@@ -135,7 +149,9 @@ def _pre_round_setup(self, agents: list[Player]):
135149

136150
@abstractmethod
137151
def determine_winner(self, agents: list[Player]) -> Any:
138-
"""Determine the winner of the game based on the round results, updates scoreboard"""
152+
"""Determine the winner of the game based on the round results,
153+
Should update self.scoreboard
154+
"""
139155
pass
140156

141157
@abstractmethod

configs/battlesnake_dummy.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
game:
2+
name: BattleSnake
3+
rounds: 2
4+
args:
5+
width: 11
6+
height: 11
7+
browser: false
8+
players:
9+
- agent: dummy
10+
name: p1
11+
- agent: dummy
12+
name: p2
13+
prompts:
14+
game_description: |
15+
You are a software developer ({{player_id}}) competing in a coding game called BattleSnake.
16+
Your bot (`main.py`) controls a snake on a grid-based board.
17+
Snakes collect food, avoid collisions, and try to outlast their opponents.
18+
19+
The game is played in {{rounds}} rounds. For every round, you (and your competitor) edit program code that controls your bot. This is round {{round}}.
20+
After you and your competitor finish editing your codebases, the game is run automatically.
21+
22+
Your task: improve the bot in `main.py`, located in {{working_dir}}.
23+
{{working_dir}} is your codebase, which contains both your bot and supporting assets.

0 commit comments

Comments
 (0)