haive.games.mafia.state¶
Game state models for the Mafia game.
This module defines the core state model for the Mafia game, extending the base MultiPlayerGameState with Mafia-specific functionality.
- The state model tracks:
Player roles and statuses
Game phase and progression
Voting and action history
Public announcements
Night action outcomes
Examples
>>> from mafia.state import MafiaGameState
>>> from mafia.models import PlayerRole, GamePhase
>>>
>>> # Create a new game state
>>> state = MafiaGameState(
... players=["Player_1", "Player_2", "Narrator"],
... roles={"Player_1": PlayerRole.VILLAGER,
... "Player_2": PlayerRole.MAFIA,
... "Narrator": PlayerRole.NARRATOR},
... game_phase=GamePhase.SETUP
... )
Classes¶
State model for a Mafia game. |
Module Contents¶
- class haive.games.mafia.state.MafiaGameState(/, **data)[source]¶
Bases:
haive.games.framework.multi_player.state.MultiPlayerGameState
State model for a Mafia game.
This class extends MultiPlayerGameState to provide Mafia-specific state tracking, including roles, votes, and game progression.
- Parameters:
data (Any)
- roles¶
Mapping of player IDs to roles
- Type:
Dict[str, PlayerRole]
- player_states¶
Player state information
- Type:
Dict[str, PlayerState]
Examples
>>> state = MafiaGameState( ... players=["Player_1", "Player_2", "Narrator"], ... roles={"Player_1": PlayerRole.VILLAGER, ... "Player_2": PlayerRole.MAFIA, ... "Narrator": PlayerRole.NARRATOR}, ... game_phase=GamePhase.SETUP ... ) >>> print(state.game_phase) # Shows SETUP
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- add_public_announcement(announcement)[source]¶
Add an announcement to the public record.
- Parameters:
announcement (str) – The announcement to add
- Return type:
None
Examples
>>> state.add_public_announcement("Night falls on the village.") >>> print(state.public_announcements[-1])
- log_action(action)[source]¶
Log an action in the game history.
This method records player and narrator actions in both the action_history and move_history, ensuring proper serialization of complex objects.
- Parameters:
action (Union[MafiaAction, NarratorAction]) – Action to log
- Return type:
None
Examples
>>> action = MafiaAction( ... player_id="Player_1", ... action_type=ActionType.VOTE, ... target_id="Player_2", ... phase=GamePhase.DAY_VOTING, ... round_number=1 ... ) >>> state.log_action(action)
- model_copy(*, deep=False, **kwargs)[source]¶
Create a copy of the model.
- Parameters:
deep (bool, optional) – Whether to create a deep copy. Defaults to False.
**kwargs – Additional arguments to pass to model_copy
- Returns:
A copy of the current state
- Return type:
Examples
>>> new_state = state.model_copy(deep=True)
- update_alive_counts()[source]¶
Update the count of alive players in different roles.
This method recalculates the number of alive players in each role category based on the current player states.
Note
This should be called after any change that might affect player life status (e.g., night kills, voting execution).
Examples
>>> state.player_states["Player_1"].is_alive = False >>> state.update_alive_counts() >>> print(state.alive_village_count) # Shows updated count