Skip to content

laife.web.snapshot

The world as JSON: what GET /state returns.

Classes:

  • HistoryView –

    One action a player took on its mission, and what came of it.

  • PlayerView –

    What a client sees of a player.

  • UtensilView –

    One utensil in a player's inventory.

  • WorldSnapshot –

    The whole world at one moment.

Functions:

  • take_snapshot –

    Return the world held by runner, stamped with event number seq.

Attributes:

  • HISTORY_SHOWN –

    How many of a player's latest history entries a snapshot carries.

HISTORY_SHOWN module-attribute

HISTORY_SHOWN = 8

How many of a player's latest history entries a snapshot carries.

HistoryView

Bases: BaseModel

One action a player took on its mission, and what came of it.

action is the action's own fields; action_type is its class name, as the action log event writes it.

PlayerView

Bases: BaseModel

What a client sees of a player.

Methods:

  • from_player –

    Build the view of player, with its latest HISTORY_SHOWN history entries.

from_player classmethod

from_player(player: Player) -> PlayerView

Build the view of player, with its latest HISTORY_SHOWN history entries.

Source code in src/laife/web/snapshot.py
@classmethod
def from_player(cls, player: Player) -> PlayerView:
    """Build the view of *player*, with its latest ``HISTORY_SHOWN`` history entries."""
    return cls(
        name=player.name,
        player_type=player.player_type,
        position=player.position,
        size=player.size,
        state=player.state,
        turn=player.turn,
        mission=player.mission,
        inventory=[
            UtensilView(name=u.name, description=u.description) for u in player.inventory
        ],
        history=[
            HistoryView(
                action_type=type(entry.action).__name__,
                action=entry.action.model_dump(mode="json"),
                result=entry.result,
            )
            for entry in player.history.history[-HISTORY_SHOWN:]
        ],
    )

UtensilView

Bases: BaseModel

One utensil in a player's inventory.

WorldSnapshot

Bases: BaseModel

The whole world at one moment.

seq is the number of the last event published before the snapshot was taken; a client drops streamed events at or below it. paused is None when the run has no pause control.

take_snapshot

take_snapshot(runner: WorldRunner, sim_control: SimControl | None, seq: int) -> WorldSnapshot

Return the world held by runner, stamped with event number seq.

Source code in src/laife/web/snapshot.py
def take_snapshot(runner: WorldRunner, sim_control: SimControl | None, seq: int) -> WorldSnapshot:
    """Return the world held by *runner*, stamped with event number *seq*."""
    return WorldSnapshot(
        seq=seq,
        paused=None if sim_control is None else sim_control.paused,
        terrains=list(runner.terrains),
        buildings=list(runner.buildings),
        players=[PlayerView.from_player(p) for p in runner.players],
    )