Preview a drag with duplex
It is Red's turn again. Alice drags red7 across squares 18, 19 and 11. A duplex exchange lets her send positions while receiving allowed-square and capture previews. It starts without initial data, just like the push exchange.
For each position, the rules worker queries the legal candidates and replies with landing, allowed and a captures map. The worker allows squares 18 and 19 in this position. It rejects square 11 because that choice would move a non-king Red piece backward. None of these previews moves a stored piece.
Alice finishes her sending direction after the final position. She can still read responses until the worker finishes its direction. The example reads both finish notices and checks that the board remains at revision two. Cancellation would end the whole exchange instead of closing only one direction.
Use a request or completed push turn to confirm a choice. A preview can become stale between a query and the later commit. Check the expected revision in the commit mutation.
The preview for square 18 is:
{
"landing": 18,
"allowed": true,
"captures": {}
}
Define the messages
The game data installs duplex/checkers/drag in the events definition folder:
{
"request": {
"type": "object",
"required": [
"piece",
"landing"
],
"additionalProperties": false,
"properties": {
"piece": {
"type": "string"
},
"landing": {
"type": "integer",
"minimum": 0,
"maximum": 31
}
}
},
"response": {
"type": "object",
"required": [
"landing",
"allowed",
"captures"
],
"additionalProperties": false,
"properties": {
"landing": {
"type": "integer",
"minimum": 0,
"maximum": 31
},
"allowed": {
"type": "boolean"
},
"captures": {
"type": "object",
"additionalProperties": {
"type": "boolean"
}
}
}
}
}
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
func previewADrag(game *Game, alice, rules *events.Owner) {
worker, err := events.ServeCheckersDrag(rules)
check(game.t, err)
defer worker.Close()
caller, err := events.OpenCheckersDrag(alice)
check(game.t, err)
exchange, err := worker.Accept(time.Second)
check(game.t, err)
positions := []int64{18, 19, 11}
for index := range positions {
landing := positions[index]
check(game.t, caller.Send(events.CheckersDragRequest{
Piece: "red7",
Landing: landing,
}))
update, finished, err := exchange.Read(time.Second)
check(game.t, err)
if finished {
game.t.Fatal("drag ended early")
}
captures, allowed := map[string]any{}, false
moves := game.legal(update.Piece)
for moveIndex := range moves {
move := moves[moveIndex]
if move.Landing == update.Landing {
allowed = true
if move.Captures != nil {
captures = move.Captures
}
}
}
check(game.t, exchange.Send(events.CheckersDragResponse{
Landing: update.Landing,
Allowed: allowed,
Captures: captures,
}))
preview, finished, err := caller.Read(time.Second)
check(game.t, err)
if finished || preview.Allowed != (landing != 11) {
game.t.Fatal("incorrect preview")
}
}
check(game.t, caller.Finish())
request, finished, err := exchange.Read(time.Second)
check(game.t, err)
if !finished || request.Piece != "" {
game.t.Fatal("caller did not finish")
}
check(game.t, exchange.Finish())
response, finished, err := caller.Read(time.Second)
check(game.t, err)
if !finished || response.Allowed || game.revision() != 2 {
game.t.Fatal("worker did not finish")
}
}