protocol module
Multiplayer Protocol
Defines the wire protocol for DM-to-player communication.
All messages are serialized as JSON strings. The protocol is versioned
(see PROTOCOL_VERSION) so clients and servers can detect
incompatible changes.
Message Types
The 12 message types cover the full session lifecycle:
Handshake: HELLO, WELCOME
Entity ownership: CLAIM_ENTITY, ENTITY_CLAIMED
State synchronization: FULL_STATE, STATE_DELTA
Gameplay: ACTION_REQUEST, ACTION_RESULT, TURN_CHANGE
Communication: CHAT
Control: ERROR, DISCONNECT
- class network.protocol.ErrorCode(value)
Bases:
str,EnumStandard error codes sent in ERROR messages.
- ENTITY_ALREADY_CLAIMED = 'ENTITY_ALREADY_CLAIMED'
- INVALID_ACTION = 'INVALID_ACTION'
- INVALID_MESSAGE = 'INVALID_MESSAGE'
- NOT_YOUR_TURN = 'NOT_YOUR_TURN'
- UNKNOWN_ENTITY = 'UNKNOWN_ENTITY'
- VERSION_MISMATCH = 'VERSION_MISMATCH'
- class network.protocol.Message(type: MessageType, payload: dict = <factory>, seq: int = 0, timestamp: str = '')
Bases:
objectA single protocol message exchanged between host and client.
- Parameters:
type (MessageType) – The message type.
payload (dict) – Arbitrary message data (varies per type).
seq (int) – Sequence number for ordering.
timestamp (str) – ISO-8601 UTC timestamp, auto-set if empty.
- classmethod from_json(data: str) Message
Deserialize a JSON string into a Message.
- Parameters:
data (str) – JSON string to parse.
- Returns:
The deserialized message.
- Return type:
- Raises:
ValueError – If the JSON is invalid or the message type is unknown.
- payload: dict
- seq: int = 0
- timestamp: str = ''
- to_json() str
Serialize this message to a JSON string.
- Returns:
JSON representation of the message.
- Return type:
str
- type: MessageType
- class network.protocol.MessageType(value)
Bases:
str,EnumEnumeration of all supported network message types.
- ACTION_REQUEST = 'ACTION_REQUEST'
- ACTION_RESULT = 'ACTION_RESULT'
- CHAT = 'CHAT'
- CLAIM_ENTITY = 'CLAIM_ENTITY'
- DISCONNECT = 'DISCONNECT'
- ENTITY_CLAIMED = 'ENTITY_CLAIMED'
- ERROR = 'ERROR'
- FULL_STATE = 'FULL_STATE'
- HELLO = 'HELLO'
- STATE_DELTA = 'STATE_DELTA'
- TURN_CHANGE = 'TURN_CHANGE'
- WELCOME = 'WELCOME'
- network.protocol.PROTOCOL_VERSION = '1.0'
Current protocol version string, checked during the HELLO/WELCOME handshake.
- network.protocol.make_action_request(action_type: str, params: dict) Message
Create an ACTION_REQUEST from a player.
- Parameters:
action_type (str) – The type of action (e.g.
"move","attack").params (dict) – Action-specific parameters.
- Returns:
An ACTION_REQUEST message.
- Return type:
- network.protocol.make_action_result(success: bool, result: dict, state_delta: list) Message
Create an ACTION_RESULT response from the host.
- Parameters:
success (bool) – Whether the action succeeded.
result (dict) – Action outcome details.
state_delta (list[dict]) – State changes caused by the action.
- Returns:
An ACTION_RESULT message.
- Return type:
- network.protocol.make_chat(sender: str, message: str) Message
Create a CHAT message.
- Parameters:
sender (str) – Display name of the sender.
message (str) – Chat text.
- Returns:
A CHAT message.
- Return type:
- network.protocol.make_claim_entity(entity_id: str) Message
Create a CLAIM_ENTITY request.
- Parameters:
entity_id (str) – Name/ID of the entity the player wants to control.
- Returns:
A CLAIM_ENTITY message.
- Return type:
- network.protocol.make_disconnect() Message
Create a DISCONNECT message.
- Returns:
A DISCONNECT message with an empty payload.
- Return type:
- network.protocol.make_entity_claimed(entity_id: str, player_id: str) Message
Create an ENTITY_CLAIMED broadcast.
- Parameters:
entity_id (str) – The claimed entity’s name/ID.
player_id (str) – The player who claimed it.
- Returns:
An ENTITY_CLAIMED message.
- Return type:
- network.protocol.make_full_state(world_data: dict, entities: list, turn_data: dict) Message
Create a FULL_STATE snapshot message.
Sent to newly connected clients so they have the complete world state.
- Parameters:
world_data (dict) – Serialized world (tiles, entities, lore, turn).
entities (list[dict]) – List of serialized entity dicts.
turn_data (dict) – Current turn/round information.
- Returns:
A FULL_STATE message.
- Return type:
- network.protocol.make_hello(player_name: str) Message
Create a HELLO handshake message.
- Parameters:
player_name (str) – The connecting player’s display name.
- Returns:
A HELLO message containing the player name and protocol version.
- Return type:
- network.protocol.make_state_delta(changes: list) Message
Create a STATE_DELTA incremental update message.
- Parameters:
changes (list[dict]) – List of change dicts from
network.sync.compute_delta().- Returns:
A STATE_DELTA message.
- Return type:
- network.protocol.make_turn_change(current_entity: str, round_number: int) Message
Create a TURN_CHANGE broadcast.
- Parameters:
current_entity (str) – Name of the entity whose turn it is.
round_number (int) – Current round number.
- Returns:
A TURN_CHANGE message.
- Return type:
- network.protocol.make_welcome(session_id: str, player_id: str, entities: list) Message
Create a WELCOME response to a successful handshake.
- Parameters:
session_id (str) – The session the player has joined.
player_id (str) – Unique identifier assigned to the player.
entities (list[dict]) – List of entity summary dicts available for claiming.
- Returns:
A WELCOME message.
- Return type: