Eric Botti commited on
Commit
46ba8c8
·
1 Parent(s): c2392fe

new messaging system tweaks

Browse files
Files changed (1) hide show
  1. src/game.py +89 -83
src/game.py CHANGED
@@ -1,5 +1,6 @@
1
  import os
2
  from datetime import datetime
 
3
 
4
  from colorama import Fore, Style
5
 
@@ -11,7 +12,6 @@ from prompts import fetch_prompt, format_prompt
11
  # Default Values
12
  NUMBER_OF_PLAYERS = 5
13
 
14
- game_type = "streamlit"
15
 
16
  class Game:
17
  log_dir = os.path.join(os.pardir, "experiments")
@@ -88,18 +88,28 @@ class Game:
88
  return formatted_responses
89
 
90
 
91
- def game_message(self, message: str, excluded_player: Player = None):
92
- """Sends a message to all players. No response is expected."""
93
- for player in self.players:
94
- if player != excluded_player:
95
- player.prompt_queue.append(message)
96
- if player.controller_type == "human":
97
- print(message)
 
 
 
 
 
 
 
 
 
98
 
99
  @staticmethod
100
  async def instructional_message(message: str, player: Player, output_format: Type[BaseModel]):
101
  """Sends a message to a specific player and gets their response."""
102
-
 
103
  response = await player.respond_to(message, output_format)
104
  return response
105
 
@@ -109,10 +119,10 @@ class Game:
109
  if self.verbose:
110
  print(Fore.GREEN + message + Style.RESET_ALL)
111
 
112
- def debug_message(self, message: str):
113
- """Sends a message for a human observer. These messages contain secret information about the players such as their role."""
114
- if self.debug:
115
- print(Fore.YELLOW + message + Style.RESET_ALL)
116
 
117
  # def game_setup(self):
118
  # """Sets up the game. This includes assigning roles and gathering player names."""
@@ -131,99 +141,95 @@ class Game:
131
  self.player_responses = []
132
  herd_animal = random_animal()
133
 
134
- winner = None
 
 
 
 
135
 
136
- while not winner:
137
- # Phase I: Collect Player Animal Descriptions
138
- self.game_message(f"Each player will now take turns describing themselves.")
139
- for current_player in self.players:
140
- if current_player.controller_type != "human":
141
- self.verbose_message(f"It's {current_player.name}'s turn to describe the animal.")
142
- self.verbose_message(f"{current_player.name} is thinking...")
143
-
144
- if current_player.role == "chameleon":
145
- prompt = format_prompt("chameleon_animal", player_responses=self.format_responses())
146
- else:
147
- prompt = format_prompt("herd_animal", animal=herd_animal, player_responses=self.format_responses())
148
-
149
- # Get Player Animal Description
150
- response = await self.instructional_message(prompt, current_player, AnimalDescriptionModel)
151
-
152
- self.player_responses.append({"sender": current_player.name, "response": response.description})
153
 
154
- self.game_message(f"{current_player.name}: {response.description}", current_player)
 
155
 
 
156
 
157
- # Phase II: Chameleon Decides if they want to guess the animal (secretly)
158
- self.game_message("All players have spoken. Now the chameleon will decide if they want to guess the animal or not.")
159
- if self.human_index != self.chameleon_index:
160
- self.verbose_message("The chameleon is thinking...")
161
 
162
- chameleon = self.players[self.chameleon_index]
163
- prompt = format_prompt("chameleon_guess_decision", player_responses=self.format_responses(exclude=chameleon.name))
164
- response = await chameleon.respond_to(prompt, ChameleonGuessDecisionModel)
165
 
166
- if response.decision.lower() == "guess":
167
- chameleon_will_guess = True
168
- else:
169
- chameleon_will_guess = False
170
 
171
- # Phase III: Chameleon Guesses Animal or All Players Vote for Chameleon
172
- if chameleon_will_guess:
173
- # Chameleon Guesses Animal
174
- self.game_message(f"{chameleon.name} has revealed themselves to be the chameleon and is guessing the animal...", chameleon)
175
 
176
- prompt = fetch_prompt("chameleon_guess_animal")
 
 
 
177
 
178
- response = await self.players[self.chameleon_index].respond_to(prompt, ChameleonGuessAnimalModel)
 
 
 
179
 
180
- self.game_message(f"The chameleon guesses: {response.animal}")
181
 
182
- if response.animal.lower() == herd_animal.lower():
183
- self.game_message(f"The Chameleon has guessed the correct animal! The Chameleon wins!")
184
- winner = "chameleon"
185
- else:
186
- self.game_message(f"The Chameleon is incorrect, the true animal is a {herd_animal}. The Herd wins!")
187
- winner = "herd"
188
 
 
189
 
 
 
 
190
  else:
191
- # All Players Vote for Chameleon
192
- self.game_message("The chameleon has decided not to guess the animal. Now all players will vote on who they think the chameleon is.")
193
 
194
- player_votes = []
195
- for player in self.players:
196
- if player.controller_type != "human":
197
- self.verbose_message(f"It's {player.name}'s turn to vote.")
198
- self.verbose_message(f"{player.name} is thinking...")
199
 
200
- prompt = format_prompt("vote", player_responses=self.format_responses(exclude=player.name))
 
 
 
201
 
202
- # Get Player Vote
203
- response = await player.respond_to(prompt, VoteModel)
204
 
205
- # check if a valid player was voted for...
 
206
 
207
- # Add Vote to Player Votes
208
- player_votes.append(response.vote)
209
 
210
- self.game_message("All players have voted!")
211
- self.game_message(f"Votes: {player_votes}")
212
 
213
- # Count Votes
214
- accused_player = count_chameleon_votes(player_votes)
215
 
216
- if accused_player:
217
- self.game_message(f"The Herd has accused {accused_player} of being the Chameleon!")
218
- if accused_player == self.players[self.chameleon_index].name:
219
- self.game_message(f"{accused_player} is the Chameleon! The Herd wins!")
220
- winner = "herd"
221
- else:
222
- self.game_message(f"{accused_player} is not the Chameleon! The Chameleon wins!")
223
- self.game_message(f"The real Chameleon was {chameleon.name}.")
224
- winner = "chameleon"
225
  else:
226
- self.game_message("The Herd could not come to a consensus. We will play another round!")
 
 
 
 
 
227
 
228
 
229
  # Assign Points
 
1
  import os
2
  from datetime import datetime
3
+ from typing import Optional, Type
4
 
5
  from colorama import Fore, Style
6
 
 
12
  # Default Values
13
  NUMBER_OF_PLAYERS = 5
14
 
 
15
 
16
  class Game:
17
  log_dir = os.path.join(os.pardir, "experiments")
 
88
  return formatted_responses
89
 
90
 
91
+ def game_message(
92
+ self, message: str,
93
+ recipient: Optional[Player] = None, # If None, message is broadcast to all players
94
+ exclude: bool = False # If True, the message is broadcast to all players except the chosen player
95
+ ):
96
+ """Sends a message to a player. No response is expected, however it will be included next time the player is prompted"""
97
+ if exclude or not recipient:
98
+ for player in self.players:
99
+ if player != recipient:
100
+ player.prompt_queue.append(message)
101
+ if player.controller_type == "human":
102
+ print(message)
103
+ else:
104
+ recipient.prompt_queue.append(message)
105
+ if recipient.controller_type == "human":
106
+ print(message)
107
 
108
  @staticmethod
109
  async def instructional_message(message: str, player: Player, output_format: Type[BaseModel]):
110
  """Sends a message to a specific player and gets their response."""
111
+ if player.controller_type == "human":
112
+ print(message)
113
  response = await player.respond_to(message, output_format)
114
  return response
115
 
 
119
  if self.verbose:
120
  print(Fore.GREEN + message + Style.RESET_ALL)
121
 
122
+ # def debug_message(self, message: str):
123
+ # """Sends a message for a human observer. These messages contain secret information about the players such as their role."""
124
+ # if self.debug:
125
+ # print(Fore.YELLOW + message + Style.RESET_ALL)
126
 
127
  # def game_setup(self):
128
  # """Sets up the game. This includes assigning roles and gathering player names."""
 
141
  self.player_responses = []
142
  herd_animal = random_animal()
143
 
144
+ # Phase I: Collect Player Animal Descriptions
145
+ self.game_message(f"Each player will now take turns describing themselves.")
146
+ for current_player in self.players:
147
+ if current_player.controller_type != "human":
148
+ self.verbose_message(f"{current_player.name} is thinking...")
149
 
150
+ if current_player.role == "chameleon":
151
+ prompt = format_prompt("chameleon_animal", player_responses=self.format_responses())
152
+ else:
153
+ prompt = format_prompt("herd_animal", animal=herd_animal, player_responses=self.format_responses())
 
 
 
 
 
 
 
 
 
 
 
 
 
154
 
155
+ # Get Player Animal Description
156
+ response = await self.instructional_message(prompt, current_player, AnimalDescriptionModel)
157
 
158
+ self.player_responses.append({"sender": current_player.name, "response": response.description})
159
 
160
+ self.game_message(f"{current_player.name}: {response.description}", current_player, exclude=True)
 
 
 
161
 
 
 
 
162
 
163
+ # Phase II: Chameleon Decides if they want to guess the animal (secretly)
164
+ self.game_message("All players have spoken. Now the chameleon will decide if they want to guess the animal or not.")
165
+ if self.human_index != self.chameleon_index:
166
+ self.verbose_message("The chameleon is thinking...")
167
 
168
+ chameleon = self.players[self.chameleon_index]
169
+ prompt = format_prompt("chameleon_guess_decision", player_responses=self.format_responses(exclude=chameleon.name))
170
+ response = await self.instructional_message(prompt, chameleon, ChameleonGuessDecisionModel)
 
171
 
172
+ if response.decision.lower() == "guess":
173
+ chameleon_will_guess = True
174
+ else:
175
+ chameleon_will_guess = False
176
 
177
+ # Phase III: Chameleon Guesses Animal or All Players Vote for Chameleon
178
+ if chameleon_will_guess:
179
+ # Chameleon Guesses Animal
180
+ self.game_message(f"{chameleon.name} has revealed themselves to be the chameleon and is guessing the animal...", chameleon, exclude=True)
181
 
182
+ prompt = fetch_prompt("chameleon_guess_animal")
183
 
184
+ response = await self.instructional_message(prompt, chameleon, ChameleonGuessAnimalModel)
 
 
 
 
 
185
 
186
+ self.game_message(f"The Chameleon guesses you are pretending to be a {response.animal}", chameleon, exclude=True)
187
 
188
+ if response.animal.lower() == herd_animal.lower():
189
+ self.game_message(f"The Chameleon has guessed the correct animal! The Chameleon wins!")
190
+ winner = "chameleon"
191
  else:
192
+ self.game_message(f"The Chameleon is incorrect, the true animal is a {herd_animal}. The Herd wins!")
193
+ winner = "herd"
194
 
195
+ else:
196
+ # All Players Vote for Chameleon
197
+ print("vote time")
198
+ self.game_message("The chameleon has decided not to guess the animal. Now all players will vote on who they think the chameleon is.")
 
199
 
200
+ player_votes = []
201
+ for player in self.players:
202
+ if player.controller_type != "human":
203
+ self.verbose_message(f"{player.name} is thinking...")
204
 
205
+ prompt = format_prompt("vote", player_responses=self.format_responses(exclude=player.name))
 
206
 
207
+ # Get Player Vote
208
+ response = await self.instructional_message(prompt, player, VoteModel)
209
 
210
+ # check if a valid player was voted for...
 
211
 
212
+ # Add Vote to Player Votes
213
+ player_votes.append(response.vote)
214
 
215
+ self.game_message("All players have voted!")
216
+ self.game_message(f"Votes: {player_votes}")
217
 
218
+ # Count Votes
219
+ accused_player = count_chameleon_votes(player_votes)
220
+
221
+ if accused_player:
222
+ self.game_message(f"The Herd has accused {accused_player} of being the Chameleon!")
223
+ if accused_player == self.players[self.chameleon_index].name:
224
+ self.game_message(f"{accused_player} is the Chameleon! The Herd wins!")
225
+ winner = "herd"
 
226
  else:
227
+ self.game_message(f"{accused_player} is not the Chameleon! The Chameleon wins!")
228
+ self.game_message(f"The real Chameleon was {chameleon.name}.")
229
+ winner = "chameleon"
230
+ else:
231
+ self.game_message("The Herd could not come to a consensus. The Chameleon wins!")
232
+ winner = "chameleon"
233
 
234
 
235
  # Assign Points