Stream legal landings with pull
It is now Black's turn. Bob selects black1, and a pull exchange sends that one selection to the rules worker. The worker runs checkers/legal against the current revision and sends each candidate as a response message. It calls finish after the last candidate.
Bob reads until he receives kind: finish. The candidate for this position lands on 13 and captures red2. Bob collects candidates in a map keyed by landing square. The collection has no order, even though the transport delivers messages in sequence.
This fixture has one candidate for Black's first jump. A larger result can use the same send-and-finish pattern without changing the request. Treat the result as a preview of the queried revision. The commit mutation must still check the board because another turn could change it before Bob acts.
The streamed candidate is:
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 00 | 54 52 4F 4E | magic TRON |
| 04 | 4C 72656432 | txt "red2" |
| 09 | 09 | bit true |
| 0A | 0F 0A 04000000 0900 0000 | map leaf, size 10: @4 → @9 |
| 14 | 8C 63617074757265 73 | txt "captures" |
| 1D | 0F 0A 14000000 0A00 0000 | map leaf, size 10: @14 → @A |
| 27 | 7C 6C616E64696E67 | txt "landing" |
| 2F | 02 0D000000000000 00 | i64 13 |
| 38 | 0F 0A 27000000 2F00 0000 | map leaf, size 10: @27 → @2F |
| 42 | 07 0E 10400000 1D00 0000 38000000 | map branch, size 14, depth 0 (root), slots 4, 14: @1D → @38 |
| 50 | 42000000 00000000 | root → @42; previous: none |
Define the messages
The game data installs pull/checkers/legal in the events definition folder:
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 000 | 54 52 4F 4E | magic TRON |
| 004 | 5C 7069656365 | txt "piece" |
| 00A | 0E 0D 00 0100 010000 00 04000000 | array leaf, size 13, shift 0, bitmap 0001, count 1 (root): @4 |
| 017 | 4C 74797065 | txt "type" |
| 01C | 6C 737472696E67 | txt "string" |
| 023 | 0F 0A 17000000 1C00 0000 | map leaf, size 10: @17 → @1C |
| 02D | 5C 7069656365 | txt "piece" |
| 033 | 0F 0A 2D000000 2300 0000 | map leaf, size 10: @2D → @23 |
| 03D | AC 70726F70657274 696573 | txt "properties" |
| 048 | 0F 0A 3D000000 3300 0000 | map leaf, size 10: @3D → @33 |
| 052 | 8C 72657175697265 64 | txt "required" |
| 05B | 0F 0A 52000000 0A00 0000 | map leaf, size 10: @52 → @A |
| 065 | 14 14 616464697469 6F6E616C50726F70 657274696573 | txt "additionalProperties" |
| 07B | 01 | bit false |
| 07C | 0F 0A 65000000 7B00 0000 | map leaf, size 10: @65 → @7B |
| 086 | 4C 74797065 | txt "type" |
| 08B | 6C 6F626A656374 | txt "object" |
| 092 | 0F 0A 86000000 8B00 0000 | map leaf, size 10: @86 → @8B |
| 09C | 07 16 A8040000 4800 0000 5B000000 7C00 0000 92000000 | map branch, size 22, depth 0 (root), slots 3, 5, 7, 10: @48 → @5B → @7C → @92 |
| 0B2 | 7C 6C616E64696E67 | txt "landing" |
| 0BA | 8C 63617074757265 73 | txt "captures" |
| 0C3 | 0E 11 00 0300 020000 00 B2000000 BA0000 00 | array leaf, size 17, shift 0, bitmap 0003, count 2 (root): @B2 → @BA |
| 0D4 | 7C 6D6178696D756D | txt "maximum" |
| 0DC | 02 1F000000000000 00 | i64 31 |
| 0E5 | 0F 0A D4000000 DC00 0000 | map leaf, size 10: @D4 → @DC |
| 0EF | 4C 74797065 | txt "type" |
| 0F4 | 7C 696E7465676572 | txt "integer" |
| 0FC | 0F 0A EF000000 F400 0000 | map leaf, size 10: @EF → @F4 |
| 106 | 7C 6D696E696D756D | txt "minimum" |
| 10E | 02 00000000000000 00 | i64 0 |
| 117 | 0F 0A 06010000 0E01 0000 | map leaf, size 10: @106 → @10E |
| 121 | 07 12 08840000 E500 0000 FC000000 1701 0000 | map branch, size 18, depth 0 (root), slots 3, 10, 15: @E5 → @FC → @117 |
Show all 61 nodes
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 133 | 4C 74797065 | txt "type" |
| 138 | 7C 626F6F6C65616E | txt "boolean" |
| 140 | 0F 0A 33010000 3801 0000 | map leaf, size 10: @133 → @138 |
| 14A | 14 14 616464697469 6F6E616C50726F70 657274696573 | txt "additionalProperties" |
| 160 | 0F 0A 4A010000 4001 0000 | map leaf, size 10: @14A → @140 |
| 16A | 4C 74797065 | txt "type" |
| 16F | 6C 6F626A656374 | txt "object" |
| 176 | 0F 0A 6A010000 6F01 0000 | map leaf, size 10: @16A → @16F |
| 180 | 07 0E 80040000 6001 0000 76010000 | map branch, size 14, depth 0 (root), slots 7, 10: @160 → @176 |
| 18E | 8C 63617074757265 73 | txt "captures" |
| 197 | 0F 0A 8E010000 8001 0000 | map leaf, size 10: @18E → @180 |
| 1A1 | 7C 6C616E64696E67 | txt "landing" |
| 1A9 | 0F 0A A1010000 2101 0000 | map leaf, size 10: @1A1 → @121 |
| 1B3 | 07 0E 10400000 9701 0000 A9010000 | map branch, size 14, depth 0 (root), slots 4, 14: @197 → @1A9 |
| 1C1 | AC 70726F70657274 696573 | txt "properties" |
| 1CC | 0F 0A C1010000 B301 0000 | map leaf, size 10: @1C1 → @1B3 |
| 1D6 | 8C 72657175697265 64 | txt "required" |
| 1DF | 0F 0A D6010000 C300 0000 | map leaf, size 10: @1D6 → @C3 |
| 1E9 | 14 14 616464697469 6F6E616C50726F70 657274696573 | txt "additionalProperties" |
| 1FF | 01 | bit false |
| 200 | 0F 0A E9010000 FF01 0000 | map leaf, size 10: @1E9 → @1FF |
| 20A | 4C 74797065 | txt "type" |
| 20F | 6C 6F626A656374 | txt "object" |
| 216 | 0F 0A 0A020000 0F02 0000 | map leaf, size 10: @20A → @20F |
| 220 | 07 16 A8040000 CC01 0000 DF010000 0002 0000 16020000 | map branch, size 22, depth 0 (root), slots 3, 5, 7, 10: @1CC → @1DF → @200 → @216 |
| 236 | 8C 726573706F6E73 65 | txt "response" |
| 23F | 0F 0A 36020000 2002 0000 | map leaf, size 10: @236 → @220 |
| 249 | 7C 72657175657374 | txt "request" |
| 251 | 0F 0A 49020000 9C00 0000 | map leaf, size 10: @249 → @9C |
| 25B | 07 0E 40040000 3F02 0000 51020000 | map branch, size 14, depth 0 (root), slots 6, 10: @23F → @251 |
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 269 | 5B020000 00000000 | root → @25B; 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 listLegalMoves(game *Game, bob, rules *events.Owner) {
worker, err := events.ServeCheckersLegal(rules)
check(game.t, err)
defer worker.Close()
caller, err := events.OpenCheckersLegal(bob, events.CheckersLegalRequest{Piece: "black1"})
check(game.t, err)
exchange, err := worker.Accept(time.Second)
check(game.t, err)
movesToSend := game.legal(exchange.Request().Piece)
for index := range movesToSend {
move := movesToSend[index]
check(game.t, exchange.Send(events.CheckersLegalResponse{
Landing: move.Landing,
Captures: move.Captures,
}))
}
check(game.t, exchange.Finish())
moves := map[int64]map[string]any{}
for {
move, finished, err := caller.Read(time.Second)
check(game.t, err)
if finished {
break
}
moves[move.Landing] = move.Captures
}
if !reflect.DeepEqual(moves, map[int64]map[string]any{13: {"red2": true}}) {
game.t.Fatal("incorrect legal moves")
}
}