Skip to content

Commit

Permalink
move game engine fields to state
Browse files Browse the repository at this point in the history
  • Loading branch information
Luncenok committed Oct 20, 2024
1 parent 8c0e851 commit e90ead9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/game/game_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
class GameEngine(BaseModel):
state: GameState = Field(default_factory=GameState)
nobody: HumanPlayer = Field(default_factory=lambda: HumanPlayer(name="Nobody"))
save_playthrough: str = ""
DEBUG: bool = Field(default=False)
gui: Optional[Any] = None

def load_players(self, players: List[Player], impostor_count: int = 1) -> None:
Expand Down Expand Up @@ -104,11 +102,11 @@ def get_player_actions(self) -> list[GameAction]:
if player.state.life == PlayerState.ALIVE:
possible_actions = self.get_actions(player)
possible_actions_str = [action.text for action in possible_actions]
if self.DEBUG:
if self.state.DEBUG:
print(f"Player {player} actions: {possible_actions_str}")
action_int = player.prompt_action(possible_actions_str)
choosen_actions.append(possible_actions[action_int])
if self.DEBUG:
if self.state.DEBUG:
print(f"Player {player} choosen action: {choosen_actions[-1]}")
return choosen_actions

Expand Down Expand Up @@ -155,7 +153,7 @@ def update_game_state(self, chosen_actions: list[GameAction]) -> bool:
):
playthrough_text = f"Player {player} saw action {action.spectator} when {player} were in {HUMAN_READABLE_LOCATIONS[action.player.state.location]}"
self.state.playthrough.append(playthrough_text)
if self.DEBUG:
if self.state.DEBUG:
print(playthrough_text)
player.state.seen_actions.append(
f"you saw {action.spectator} when you were in {HUMAN_READABLE_LOCATIONS[action.player.state.location]}"
Expand All @@ -173,7 +171,7 @@ def update_game_state(self, chosen_actions: list[GameAction]) -> bool:

playthrough_text = f"Player {player} is in {HUMAN_READABLE_LOCATIONS[player.state.location]} with {players_in_room}"
self.state.playthrough.append(playthrough_text)
if self.DEBUG:
if self.state.DEBUG:
print(playthrough_text)
if players_in_room:
player.state.player_in_room = f"Players in room with you: {', '.join([str(player) for player in players_in_room])}"
Expand Down Expand Up @@ -247,7 +245,7 @@ def discussion_loop(self) -> None:
self.gui.update_gui()
self.state.playthrough.append(f"Discussion log:")
self.state.playthrough.append("\n".join(discussion_log))
if self.DEBUG:
if self.state.DEBUG:
print("Discussion log:")
print("\n".join(discussion_log))

Expand Down Expand Up @@ -277,7 +275,7 @@ def go_to_voting(self) -> None:
f"{player} voted for {possible_actions[action].target}"
)
self.state.playthrough.append(playthrough_text)
if self.DEBUG:
if self.state.DEBUG:
print(playthrough_text)

votes_counter = Counter(votes.values())
Expand Down Expand Up @@ -387,8 +385,8 @@ def check_game_over_action_crewmate(self) -> bool:
return False

def _save_playthrough(self) -> None:
if self.save_playthrough:
with open(self.save_playthrough, "w") as f:
if self.state.save_playthrough:
with open(self.state.save_playthrough, "w") as f:
f.write("\n".join(self.state.playthrough))

def end_game(self) -> None:
Expand Down
2 changes: 2 additions & 0 deletions src/game/game_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class GameState(BaseModel):
players: List[Player] = Field(default_factory=list)
game_stage: GamePhase = GamePhase.MAIN_MENU
playthrough: List[str] = Field(default_factory=list)
save_playthrough: str = ""
DEBUG: bool = Field(default=False)

def add_player(self, player: Player):
self.players.append(player)
Expand Down

0 comments on commit e90ead9

Please sign in to comment.