games.mafia.state_manager¶
State management for the Mafia game.
This module provides the core state management functionality for the Mafia game, handling game state transitions, move validation, and game progression logic.
- The state manager is responsible for:
Game initialization and setup
Phase transitions (day/night cycles)
Move validation and application
Game state filtering for information hiding
Win condition checking
Classes¶
Manager for the Mafia game state. |
Module Contents¶
- class games.mafia.state_manager.MafiaStateManager¶
Bases:
haive.games.framework.multi_player.state_manager.MultiPlayerGameStateManager
[haive.games.mafia.state.MafiaGameState
]Manager for the Mafia game state.
This class extends MultiPlayerGameStateManager to provide Mafia-specific state management functionality. It handles game progression, move validation, and state transitions.
- The manager maintains game state including:
Player roles and alive/dead status
Day/night cycle progression
Vote tracking and resolution
Night action resolution (kills, saves, investigations)
Win condition checking
Note
This class is designed to be used statically, with all methods being class methods that take and return game states.
- classmethod advance_phase(state)¶
Advance the game to the next phase.
- This method handles the transition between game phases, including:
Setup → Night (first night)
Night → Day Discussion (with night action resolution)
Day Discussion → Day Voting
Day Voting → Night (with vote resolution)
Game Over checks at appropriate points
- Parameters:
state (haive.games.mafia.state.MafiaGameState) – Current game state
- Returns:
Updated game state with new phase and relevant changes
- Return type:
- classmethod apply_move(state, player_id, move)¶
Apply a move to the game state.
This method validates and applies a player’s move or narrator’s action to the game state, updating all relevant state fields.
- Parameters:
state (haive.games.mafia.state.MafiaGameState) – Current game state
player_id (str) – ID of the player making the move
move (haive.games.mafia.models.MafiaAction | haive.games.mafia.models.NarratorAction) – Move to apply
- Returns:
Updated game state after applying the move
- Return type:
- classmethod check_game_status(state)¶
Check if the game has ended and determine the winner.
- This method checks win conditions:
Village wins if all mafia are dead
Mafia wins if they equal/outnumber villagers
- Parameters:
state (haive.games.mafia.state.MafiaGameState) – Current game state
- Returns:
Updated state with game status and winner if game is over
- Return type:
- classmethod filter_state_for_player(state, player_id)¶
Filter the state to include only information visible to a specific player.
This method implements information hiding, ensuring players only see information they should have access to based on their role and the game phase.
- Parameters:
state (haive.games.mafia.state.MafiaGameState) – Full game state
player_id (str) – ID of the player to filter for
- Returns:
Filtered state containing only visible information
- Return type:
- classmethod get_legal_moves(state, player_id)¶
Get legal moves for a specific player.
- This method determines what moves are legal for a player based on:
Current game phase
Player’s role
Player’s alive/dead status
Previous actions in the current phase
- Parameters:
state (haive.games.mafia.state.MafiaGameState) – Current game state
player_id (str) – ID of the player to get moves for
- Returns:
List of legal moves (MafiaAction or NarratorAction)
- Return type:
list[haive.games.mafia.models.MafiaAction | haive.games.mafia.models.NarratorAction]
- classmethod handle_phase_transition(state)¶
Handle phase transition with error handling.
This method safely transitions the game phase, handling any errors that might occur during the transition.
- Parameters:
state (haive.games.mafia.state.MafiaGameState) – Current game state
- Returns:
Updated state after phase transition
- Raises:
ValueError – If critical game state fields are missing
- Return type:
- classmethod initialize(player_names, **kwargs)¶
Initialize a new Mafia game with the given players.
- This method sets up a new game state with:
Random role assignment
Initial player states
Game phase setup
Role knowledge distribution
- Parameters:
- Returns:
Initial game state
- Raises:
ValueError – If there aren’t enough players (minimum 4)
- Return type:
- classmethod resolve_night_actions(state)¶
Resolve night actions and determine outcomes.
- This method processes all night actions in the correct order:
Mafia kill attempt
Doctor save attempt
Detective investigation results
- Parameters:
state (haive.games.mafia.state.MafiaGameState) – Current game state with night actions recorded
- Returns:
Updated state with night actions resolved
- Return type: