Commit one move with request

Alice chooses red7 and submits landing square 15 with expected revision zero. A request exchange carries this single input to the rules worker. The worker runs the stored checkers/move mutation and returns one result. The event exchange itself does not commit a move.

The mutation checks the whole starting position and changes the piece, turn and revision together. The first attempt returns committed: true at revision one. Running the same parameters again returns committed: false, because that starting position no longer exists. The example checks both results.

Only after a successful commit does the worker publish a turn notification and an animation cue. Both carry revision one so Bob can associate them with the stored turn. The animation path is [11, 15], whose order describes movement. Bob can avoid repeating an animation for the same revision or read the board when a revision is missing.

A database commit and event publication are separate operations. If the process stops between them, the board contains the move without its notification. A missing response also does not prove that the move failed. Read the board before deciding whether to retry.

The stored request/checkers/move definition supplies schemas for request and response messages. Schema validation checks message shape, while the mutation checks whether the turn can commit. The worker unregisters its route when this example exchange ends.

The successful response is:

dust27 B
committed true
revision  1

Define the messages

The game data installs request/checkers/move in the events definition folder:

dust762 B
request  {type                 object
          required             [piece landing expected]
          additionalProperties false
          properties           {piece    {type string}
                                landing  {type    integer
                                          minimum 0
                                          maximum 31}
                                expected {type    integer
                                          minimum 0}}}
response {type                 object
          required             [committed revision]
          additionalProperties false
          properties           {committed {type boolean}
                                revision  {type    integer
                                           minimum 0}}}

The SDK plugin derives the payload types and route helpers from this definition. The Events plugin validates each message against its stored schema. The example below imports the generated SDK.

Code

python1.2 kB
async def move_one_piece(game, alice, bob, rules, subscription):
    worker = await events.serve_checkers_move(rules)

    turns = await events.subscribe_checkers_table_turn(bob)
    animations = await events.subscribe_checkers_table_animate(bob)

    caller = await events.open_checkers_move(
        alice, {"piece": "red7", "landing": 15, "expected": 0}
    )
    exchange = await worker.accept(WAIT)
    reply = await game.commit("checkers/move", exchange.request)
    assert reply == {"committed": True, "revision": 1}

    await events.publish_checkers_table_turn(game.db, {"revision": 1, "turn": "black"})
    await events.publish_checkers_table_animate(
        game.db, {"revision": 1, "piece": "red7", "path": [11, 15]}
    )
    await exchange.respond(reply)

    assert await caller.response(WAIT) == reply
    assert (await turns.receive(WAIT))["data"] == {"revision": 1, "turn": "black"}
    assert (await animations.receive(WAIT))["data"]["path"] == [11, 15]
    assert await game.commit("checkers/move", exchange.request) == {
        "committed": False,
        "revision": 1,
    }

    await turns.close()
    await animations.close()
    await worker.close()