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:
committed true
revision 1
Define the messages
The game data installs request/checkers/move in the events definition folder:
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
move_one_piece :: proc(
game: ^Game,
alice, bob, rules: runtime.Owner,
subscription: runtime.Subscription(events.Checkers_Table_Cursor_Data),
) -> sd.Error {
worker := events.serve_checkers_move(rules) or_return
defer runtime.close_worker(worker)
turns := events.subscribe_checkers_table_turn(bob) or_return
defer runtime.unsubscribe(turns)
animations := events.subscribe_checkers_table_animate(bob) or_return
defer runtime.unsubscribe(animations)
caller := events.open_checkers_move(
alice,
{piece = "red7", landing = 15, expected = 0},
) or_return
exchange := runtime.accept(worker, time.Second) or_return
request := exchange.request
parameters := Map {
"?piece" = request.piece,
"?landing" = request.landing,
"?expected" = request.expected,
}
reply := commit(game, "checkers/move", parameters) or_return
assert(reply.committed && reply.revision == 1)
turn_delivery := events.publish_checkers_table_turn(
game.db,
{revision = 1, turn = "black"},
) or_return
animation_delivery := events.publish_checkers_table_animate(
game.db,
{revision = 1, piece = "red7", path = []i64{11, 15}},
) or_return
assert(turn_delivery.dropped == 0 && animation_delivery.dropped == 0)
runtime.send_response(
exchange,
{committed = reply.committed, revision = reply.revision},
) or_return
response, notice := runtime.read_response(caller, time.Second) or_return
assert(notice == .Message && response.committed && response.revision == 1)
turn := runtime.receive(turns, time.Second) or_return
animation := runtime.receive(animations, time.Second) or_return
assert(turn.data.revision == 1 && len(animation.data.path) == 2)
stale := commit(game, "checkers/move", parameters) or_return
assert(!stale.committed)
return nil
}