Eric Botti commited on
Commit
09ee734
·
1 Parent(s): da0ac71

game logging

Browse files
Files changed (3) hide show
  1. src/data_collection.py +5 -1
  2. src/game.py +34 -25
  3. src/game_chameleon.py +26 -23
src/data_collection.py CHANGED
@@ -4,8 +4,8 @@ from typing import NewType
4
 
5
  import pydantic
6
 
7
- import message
8
  import player
 
9
 
10
  from pydantic import BaseModel
11
 
@@ -26,10 +26,14 @@ def save(log_object: Model):
26
 
27
 
28
  def get_log_file(log_object: Model) -> str:
 
 
29
  if isinstance(log_object, message.AgentMessage):
30
  log_file = "messages.jsonl"
31
  elif isinstance(log_object, player.Player):
32
  log_file = "players.jsonl"
 
 
33
  else:
34
  raise ValueError(f"Unknown log object type: {type(log_object)}")
35
 
 
4
 
5
  import pydantic
6
 
 
7
  import player
8
+ import message
9
 
10
  from pydantic import BaseModel
11
 
 
26
 
27
 
28
  def get_log_file(log_object: Model) -> str:
29
+ from game import Game
30
+
31
  if isinstance(log_object, message.AgentMessage):
32
  log_file = "messages.jsonl"
33
  elif isinstance(log_object, player.Player):
34
  log_file = "players.jsonl"
35
+ elif isinstance(log_object, Game):
36
+ log_file = "games.jsonl"
37
  else:
38
  raise ValueError(f"Unknown log object type: {type(log_object)}")
39
 
src/game.py CHANGED
@@ -1,36 +1,41 @@
1
- from typing import Optional, Type, List
 
 
2
 
3
  from game_utils import *
4
- from player import Player
5
  from message import Message, MessageType
6
  from agent_interfaces import HumanAgentCLI, OpenAIAgentInterface, HumanAgentInterface
 
7
  from data_collection import save
8
 
9
  # Abstracting the Game Class is a WIP so that future games can be added
10
- class Game:
11
  """Base class for all games."""
12
 
13
- number_of_players: int
14
- """The number of players in the game."""
15
 
16
- def __init__(
17
- self,
18
- game_id: str,
19
- players: List[Player],
20
- observer: Optional[Player] = None
21
- ):
22
- self.players: List[Player] = players
23
- """The players in the game."""
24
- self.observer: Optional[Player] = observer
25
- """An observer who can see all public messages, but doesn't actually play."""
26
- self.game_id = game_id
27
- """The unique id of the game."""
28
-
29
- self.winner_id: str | None = None
30
- """The id of the player who has won the game."""
31
- self.game_state: str = "game_start"
32
- """Keeps track of the current state of the game."""
33
- self.awaiting_input: bool = False
 
 
 
 
34
 
35
  def player_from_id(self, player_id: str) -> Player:
36
  """Returns a player from their ID."""
@@ -89,6 +94,10 @@ class Game:
89
  for player in self.players:
90
  save(player)
91
 
 
 
 
 
92
  @classmethod
93
  def from_human_name(
94
  cls, human_name: str = None,
@@ -126,7 +135,7 @@ class Game:
126
  player_dict["interface"] = OpenAIAgentInterface(agent_id=player_id)
127
  player_dict["message_level"] = "info"
128
 
129
- players.append(Player(**player_dict))
130
 
131
  # Add Observer - an Agent who can see all the messages, but doesn't actually play
132
  if human_index is None:
@@ -134,7 +143,7 @@ class Game:
134
  else:
135
  observer = None
136
 
137
- return cls(game_id, players, observer)
138
 
139
 
140
 
 
1
+ from typing import Optional, Type, List, ClassVar
2
+
3
+ from pydantic import BaseModel, Field
4
 
5
  from game_utils import *
 
6
  from message import Message, MessageType
7
  from agent_interfaces import HumanAgentCLI, OpenAIAgentInterface, HumanAgentInterface
8
+ from player import Player
9
  from data_collection import save
10
 
11
  # Abstracting the Game Class is a WIP so that future games can be added
12
+ class Game(BaseModel):
13
  """Base class for all games."""
14
 
15
+ # Required
 
16
 
17
+ players: List[Player] = Field(exclude=True)
18
+ """The players in the game."""
19
+ observer: Optional[Player]
20
+ """An observer who can see all public messages, but doesn't actually play."""
21
+ game_id: str
22
+ """The unique id of the game."""
23
+
24
+ # Default
25
+
26
+ winner_id: str | None = None
27
+ """The id of the player who has won the game."""
28
+ game_state: str = Field("game_start", exclude=True)
29
+ """Keeps track of the current state of the game."""
30
+ awaiting_input: bool = Field(False, exclude=True)
31
+ """Whether the game is currently awaiting input from a player."""
32
+
33
+ # Class Variables
34
+
35
+ number_of_players: ClassVar[int]
36
+ """The number of players in the game."""
37
+ player_class: ClassVar[Type[Player]] = Player
38
+ """The class of the player used in the game."""
39
 
40
  def player_from_id(self, player_id: str) -> Player:
41
  """Returns a player from their ID."""
 
94
  for player in self.players:
95
  save(player)
96
 
97
+ save(self)
98
+
99
+
100
+
101
  @classmethod
102
  def from_human_name(
103
  cls, human_name: str = None,
 
135
  player_dict["interface"] = OpenAIAgentInterface(agent_id=player_id)
136
  player_dict["message_level"] = "info"
137
 
138
+ players.append(cls.player_class(**player_dict))
139
 
140
  # Add Observer - an Agent who can see all the messages, but doesn't actually play
141
  if human_index is None:
 
143
  else:
144
  observer = None
145
 
146
+ return cls(game_id=game_id, players=players, observer=observer)
147
 
148
 
149
 
src/game_chameleon.py CHANGED
@@ -1,4 +1,5 @@
1
  from collections import Counter
 
2
 
3
  from game_utils import random_index
4
  from output_formats import *
@@ -9,37 +10,39 @@ from game import Game
9
 
10
  # Default Values
11
  NUMBER_OF_PLAYERS = 6
12
- WINNING_SCORE = 6
 
 
 
 
13
 
14
 
15
  class ChameleonGame(Game):
16
  """The main game class, handles the game logic and player interactions."""
17
 
18
- number_of_players = NUMBER_OF_PLAYERS
19
 
20
- winning_score = WINNING_SCORE
21
  """The Number of points required to win the game."""
22
- available_animals = ["Dog", "Cat", "Mouse", "Hamster", "Monkey", "Rabbit", "Fox", "Bear", "Panda", "Koala", "Tiger", "Lion", "Cow", "Pig", "Frog", "Owl", "Duck", "Chicken", "Butterfly", "Turtle", "Snake", "Octopus", "Squid", "Hedgehog", "Elephant", "Rhinoceros", "Zebra", "Crocodile", "Whale", "Dolphin", "Camel", "Giraffe", "Deer", "Gorilla", "Goat", "Llama", "Horse", "Unicorn", "Flamingo", "Skunk", "Shark"]
23
  """The list of animals that can be chosen as the secret animal."""
24
-
25
- def __init__(self, *args, **kwargs):
26
-
27
- super().__init__(*args, **kwargs)
28
-
29
- # Convert the Players to ChameleonPlayers
30
- self.players: List[ChameleonPlayer] = [ChameleonPlayer.from_player(player) for player in self.players]
31
-
32
- # Instance Variables
33
- self.chameleon_ids: List[str] = []
34
- """Record of which player was the chameleon for each round."""
35
- self.herd_animals: List[str] = []
36
- """Record of what animal was the herd animal for each round."""
37
- self.all_animal_descriptions: List[List[dict]] = []
38
- """Record of the animal descriptions each player has given for each round."""
39
- self.chameleon_guesses: List[str] = []
40
- """Record of what animal the chameleon guessed for each round."""
41
- self.herd_vote_tallies: List[List[dict]] = []
42
- """Record of the votes of each herd member for the chameleon for each round."""
43
 
44
  @property
45
  def chameleon(self) -> ChameleonPlayer:
 
1
  from collections import Counter
2
+ from typing import ClassVar
3
 
4
  from game_utils import random_index
5
  from output_formats import *
 
10
 
11
  # Default Values
12
  NUMBER_OF_PLAYERS = 6
13
+ WINNING_SCORE = 7
14
+ AVAILABLE_ANIMALS = ["Dog", "Cat", "Mouse", "Hamster", "Monkey", "Rabbit", "Fox", "Bear", "Panda", "Koala", "Tiger",
15
+ "Lion", "Cow", "Pig", "Frog", "Owl", "Duck", "Chicken", "Butterfly", "Turtle", "Snake", "Octopus",
16
+ "Squid", "Hedgehog", "Elephant", "Rhinoceros", "Zebra", "Crocodile", "Whale", "Dolphin", "Camel",
17
+ "Giraffe", "Deer", "Gorilla", "Goat", "Llama", "Horse", "Unicorn", "Flamingo", "Skunk", "Shark"]
18
 
19
 
20
  class ChameleonGame(Game):
21
  """The main game class, handles the game logic and player interactions."""
22
 
23
+ # Defaults
24
 
25
+ winning_score: int = WINNING_SCORE
26
  """The Number of points required to win the game."""
27
+ available_animals: List[str] = Field(AVAILABLE_ANIMALS, exclude=True)
28
  """The list of animals that can be chosen as the secret animal."""
29
+ chameleon_ids: List[str] = []
30
+ """Record of which player was the chameleon for each round."""
31
+ herd_animals: List[str] = []
32
+ """Record of what animal was the herd animal for each round."""
33
+ all_animal_descriptions: List[List[dict]] = []
34
+ """Record of the animal descriptions each player has given for each round."""
35
+ chameleon_guesses: List[str] = []
36
+ """Record of what animal the chameleon guessed for each round."""
37
+ herd_vote_tallies: List[List[dict]] = []
38
+ """Record of the votes of each herd member for the chameleon for each round."""
39
+
40
+ # Class Variables
41
+
42
+ number_of_players: ClassVar[int] = NUMBER_OF_PLAYERS
43
+ """The number of players in the game."""
44
+ player_class: ClassVar[Type[Player]] = ChameleonPlayer
45
+ """The class of the player used in the game."""
 
 
46
 
47
  @property
48
  def chameleon(self) -> ChameleonPlayer: