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:
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 00 | 54 52 4F 4E | magic TRON |
| 04 | 0F 02 | map leaf, size 2 |
| 06 | 8C 63617074757265 73 | txt "captures" |
| 0F | 0F 0A 06000000 0400 0000 | map leaf, size 10: @6 → @4 |
| 19 | 7C 6C616E64696E67 | txt "landing" |
| 21 | 02 12000000000000 00 | i64 18 |
| 2A | 0F 0A 19000000 2100 0000 | map leaf, size 10: @19 → @21 |
| 34 | 7C 616C6C6F776564 | txt "allowed" |
| 3C | 09 | bit true |
| 3D | 0F 0A 34000000 3C00 0000 | map leaf, size 10: @34 → @3C |
| 47 | 07 12 10C00000 0F00 0000 2A000000 3D00 0000 | map branch, size 18, depth 0 (root), slots 4, 14, 15: @F → @2A → @3D |
| 59 | 47000000 00000000 | root → @47; previous: none |
Define the messages
The game data installs duplex/checkers/drag in the events definition folder:
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 000 | 54 52 4F 4E | magic TRON |
| 004 | 5C 7069656365 | txt "piece" |
| 00A | 7C 6C616E64696E67 | txt "landing" |
| 012 | 0E 11 00 0300 020000 00 04000000 0A0000 00 | array leaf, size 17, shift 0, bitmap 0003, count 2 (root): @4 → @A |
| 023 | 4C 74797065 | txt "type" |
| 028 | 6C 737472696E67 | txt "string" |
| 02F | 0F 0A 23000000 2800 0000 | map leaf, size 10: @23 → @28 |
| 039 | 7C 6D6178696D756D | txt "maximum" |
| 041 | 02 1F000000000000 00 | i64 31 |
| 04A | 0F 0A 39000000 4100 0000 | map leaf, size 10: @39 → @41 |
| 054 | 4C 74797065 | txt "type" |
| 059 | 7C 696E7465676572 | txt "integer" |
| 061 | 0F 0A 54000000 5900 0000 | map leaf, size 10: @54 → @59 |
| 06B | 7C 6D696E696D756D | txt "minimum" |
| 073 | 02 00000000000000 00 | i64 0 |
| 07C | 0F 0A 6B000000 7300 0000 | map leaf, size 10: @6B → @73 |
| 086 | 07 12 08840000 4A00 0000 61000000 7C00 0000 | map branch, size 18, depth 0 (root), slots 3, 10, 15: @4A → @61 → @7C |
| 098 | 5C 7069656365 | txt "piece" |
| 09E | 0F 0A 98000000 2F00 0000 | map leaf, size 10: @98 → @2F |
| 0A8 | 7C 6C616E64696E67 | txt "landing" |
| 0B0 | 0F 0A A8000000 8600 0000 | map leaf, size 10: @A8 → @86 |
| 0BA | 07 0E 00600000 9E00 0000 B0000000 | map branch, size 14, depth 0 (root), slots 13, 14: @9E → @B0 |
| 0C8 | AC 70726F70657274 696573 | txt "properties" |
| 0D3 | 0F 0A C8000000 BA00 0000 | map leaf, size 10: @C8 → @BA |
| 0DD | 8C 72657175697265 64 | txt "required" |
| 0E6 | 0F 0A DD000000 1200 0000 | map leaf, size 10: @DD → @12 |
| 0F0 | 14 14 616464697469 6F6E616C50726F70 657274696573 | txt "additionalProperties" |
| 106 | 01 | bit false |
| 107 | 0F 0A F0000000 0601 0000 | map leaf, size 10: @F0 → @106 |
| 111 | 4C 74797065 | txt "type" |
| 116 | 6C 6F626A656374 | txt "object" |
| 11D | 0F 0A 11010000 1601 0000 | map leaf, size 10: @111 → @116 |
Show all 81 nodes
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 127 | 07 16 A8040000 D300 0000 E6000000 0701 0000 1D010000 | map branch, size 22, depth 0 (root), slots 3, 5, 7, 10: @D3 → @E6 → @107 → @11D |
| 13D | 7C 6C616E64696E67 | txt "landing" |
| 145 | 7C 616C6C6F776564 | txt "allowed" |
| 14D | 8C 63617074757265 73 | txt "captures" |
| 156 | 0E 15 00 0700 030000 00 3D010000 450100 00 4D010000 | array leaf, size 21, shift 0, bitmap 0007, count 3 (root): @13D → @145 → @14D |
| 16B | 7C 6D6178696D756D | txt "maximum" |
| 173 | 02 1F000000000000 00 | i64 31 |
| 17C | 0F 0A 6B010000 7301 0000 | map leaf, size 10: @16B → @173 |
| 186 | 4C 74797065 | txt "type" |
| 18B | 7C 696E7465676572 | txt "integer" |
| 193 | 0F 0A 86010000 8B01 0000 | map leaf, size 10: @186 → @18B |
| 19D | 7C 6D696E696D756D | txt "minimum" |
| 1A5 | 02 00000000000000 00 | i64 0 |
| 1AE | 0F 0A 9D010000 A501 0000 | map leaf, size 10: @19D → @1A5 |
| 1B8 | 07 12 08840000 7C01 0000 93010000 AE01 0000 | map branch, size 18, depth 0 (root), slots 3, 10, 15: @17C → @193 → @1AE |
| 1CA | 4C 74797065 | txt "type" |
| 1CF | 7C 626F6F6C65616E | txt "boolean" |
| 1D7 | 0F 0A CA010000 CF01 0000 | map leaf, size 10: @1CA → @1CF |
| 1E1 | 4C 74797065 | txt "type" |
| 1E6 | 7C 626F6F6C65616E | txt "boolean" |
| 1EE | 0F 0A E1010000 E601 0000 | map leaf, size 10: @1E1 → @1E6 |
| 1F8 | 14 14 616464697469 6F6E616C50726F70 657274696573 | txt "additionalProperties" |
| 20E | 0F 0A F8010000 EE01 0000 | map leaf, size 10: @1F8 → @1EE |
| 218 | 4C 74797065 | txt "type" |
| 21D | 6C 6F626A656374 | txt "object" |
| 224 | 0F 0A 18020000 1D02 0000 | map leaf, size 10: @218 → @21D |
| 22E | 07 0E 80040000 0E02 0000 24020000 | map branch, size 14, depth 0 (root), slots 7, 10: @20E → @224 |
| 23C | 8C 63617074757265 73 | txt "captures" |
| 245 | 0F 0A 3C020000 2E02 0000 | map leaf, size 10: @23C → @22E |
| 24F | 7C 6C616E64696E67 | txt "landing" |
| 257 | 0F 0A 4F020000 B801 0000 | map leaf, size 10: @24F → @1B8 |
| 261 | 7C 616C6C6F776564 | txt "allowed" |
| 269 | 0F 0A 61020000 D701 0000 | map leaf, size 10: @261 → @1D7 |
| 273 | 07 12 10C00000 4502 0000 57020000 6902 0000 | map branch, size 18, depth 0 (root), slots 4, 14, 15: @245 → @257 → @269 |
| 285 | AC 70726F70657274 696573 | txt "properties" |
| 290 | 0F 0A 85020000 7302 0000 | map leaf, size 10: @285 → @273 |
| 29A | 8C 72657175697265 64 | txt "required" |
| 2A3 | 0F 0A 9A020000 5601 0000 | map leaf, size 10: @29A → @156 |
| 2AD | 14 14 616464697469 6F6E616C50726F70 657274696573 | txt "additionalProperties" |
| 2C3 | 01 | bit false |
| 2C4 | 0F 0A AD020000 C302 0000 | map leaf, size 10: @2AD → @2C3 |
| 2CE | 4C 74797065 | txt "type" |
| 2D3 | 6C 6F626A656374 | txt "object" |
| 2DA | 0F 0A CE020000 D302 0000 | map leaf, size 10: @2CE → @2D3 |
| 2E4 | 07 16 A8040000 9002 0000 A3020000 C402 0000 DA020000 | map branch, size 22, depth 0 (root), slots 3, 5, 7, 10: @290 → @2A3 → @2C4 → @2DA |
| 2FA | 8C 726573706F6E73 65 | txt "response" |
| 303 | 0F 0A FA020000 E402 0000 | map leaf, size 10: @2FA → @2E4 |
| 30D | 7C 72657175657374 | txt "request" |
| 315 | 0F 0A 0D030000 2701 0000 | map leaf, size 10: @30D → @127 |
| 31F | 07 0E 40040000 0303 0000 15030000 | map branch, size 14, depth 0 (root), slots 6, 10: @303 → @315 |
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 32D | 1F030000 00000000 | root → @31F; previous: none |
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")
}
}