sync module
State Synchronization
Pure functions for serializing world state and computing deltas.
The delta sync cycle works as follows:
serialize_world()captures the current world as a plain dict.compute_delta()diffs two snapshots, producing a change list.apply_delta()patches a snapshot with a change list.
The key invariant is:
apply_delta(old, compute_delta(old, new)) == new
- network.sync.apply_delta(state: dict, changes: list) dict
Apply a change list to a serialized state, returning a new dict.
Does not mutate the input; returns a deep copy with changes applied.
- Parameters:
state (dict) – The base state to patch.
changes (list[dict]) – Change list from
compute_delta().
- Returns:
A new state dict with changes applied.
- Return type:
dict
- network.sync.compute_delta(old_state: dict, new_state: dict) list
Compute a list of changes between two serialized world states.
Uses key-level diffing on tiles, entities, lore, and turn sections. Each change dict has keys
op,path, and (for add/update)value.- Parameters:
old_state (dict) – Previous state snapshot from
serialize_world().new_state (dict) – Current state snapshot from
serialize_world().
- Returns:
A list of change dicts, each with
op("add"/"update"/"remove"),path(e.g."tiles/0,1"), and optionallyvalue.- Return type:
list[dict]
- network.sync.serialize_entity(entity) dict
Serialize a single
GameEntity.Includes position if set on the entity.
- Parameters:
entity (GameEntity) – The entity to serialize.
- Returns:
A dict representation with an optional
positionkey.- Return type:
dict