|
1 | 1 | import getpass |
| 2 | +import json |
2 | 3 | import os |
3 | 4 | import subprocess |
4 | 5 | import time |
@@ -26,6 +27,7 @@ class CodeGame(ABC): |
26 | 27 | def __init__(self, config: dict): |
27 | 28 | self.url_gh: str = f"git@github.com:{GH_ORG}/{self.name}.git" |
28 | 29 | self.artifacts: list[Path] = [] |
| 30 | + """Artifact objects that we might want to clean up after the game.""" |
29 | 31 | self.scoreboard: list[tuple[int, str]] = [] |
30 | 32 | """List of (round number, winner (player id))""" |
31 | 33 | self.config: dict = config["game"] |
@@ -77,8 +79,20 @@ def build_image(self): |
77 | 79 | ) |
78 | 80 | raise RuntimeError(f"Failed to build Docker image: {result.stderr}") |
79 | 81 |
|
| 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 | + |
80 | 93 | 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())) |
82 | 96 | if cleanup: |
83 | 97 | for artifact in self.artifacts: |
84 | 98 | if artifact.exists(): |
@@ -135,7 +149,9 @@ def _pre_round_setup(self, agents: list[Player]): |
135 | 149 |
|
136 | 150 | @abstractmethod |
137 | 151 | 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 | + """ |
139 | 155 | pass |
140 | 156 |
|
141 | 157 | @abstractmethod |
|
0 commit comments