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:

TRON88 B
Offset (hex)Bytes (hex)Meaning
0054 52 4F 4Emagic TRON
044C 72656432txt "red2"
0909bit true
0A0F 0A 04000000 0900 0000map leaf, size 10: @4 → @9
148C 63617074757265 73txt "captures"
1D0F 0A 14000000 0A00 0000map leaf, size 10: @14 → @A
277C 6C616E64696E67txt "landing"
2F02 0D000000000000 00i64 13
380F 0A 27000000 2F00 0000map leaf, size 10: @27 → @2F
4207 0E 10400000 1D00 0000 38000000map branch, size 14, depth 0 (root), slots 4, 14: @1D → @38

Define the messages

The game data installs pull/checkers/legal in the events definition folder:

TRON625 B
Offset (hex)Bytes (hex)Meaning
00054 52 4F 4Emagic TRON
0045C 7069656365txt "piece"
00A0E 0D 00 0100 010000 00 04000000array leaf, size 13, shift 0, bitmap 0001, count 1 (root): @4
0174C 74797065txt "type"
01C6C 737472696E67txt "string"
0230F 0A 17000000 1C00 0000map leaf, size 10: @17 → @1C
02D5C 7069656365txt "piece"
0330F 0A 2D000000 2300 0000map leaf, size 10: @2D → @23
03DAC 70726F70657274 696573txt "properties"
0480F 0A 3D000000 3300 0000map leaf, size 10: @3D → @33
0528C 72657175697265 64txt "required"
05B0F 0A 52000000 0A00 0000map leaf, size 10: @52 → @A
06514 14 616464697469 6F6E616C50726F70 657274696573txt "additionalProperties"
07B01bit false
07C0F 0A 65000000 7B00 0000map leaf, size 10: @65 → @7B
0864C 74797065txt "type"
08B6C 6F626A656374txt "object"
0920F 0A 86000000 8B00 0000map leaf, size 10: @86 → @8B
09C07 16 A8040000 4800 0000 5B000000 7C00 0000 92000000map branch, size 22, depth 0 (root), slots 3, 5, 7, 10: @48 → @5B → @7C → @92
0B27C 6C616E64696E67txt "landing"
0BA8C 63617074757265 73txt "captures"
0C30E 11 00 0300 020000 00 B2000000 BA0000 00array leaf, size 17, shift 0, bitmap 0003, count 2 (root): @B2 → @BA
0D47C 6D6178696D756Dtxt "maximum"
0DC02 1F000000000000 00i64 31
0E50F 0A D4000000 DC00 0000map leaf, size 10: @D4 → @DC
0EF4C 74797065txt "type"
0F47C 696E7465676572txt "integer"
0FC0F 0A EF000000 F400 0000map leaf, size 10: @EF → @F4
1067C 6D696E696D756Dtxt "minimum"
10E02 00000000000000 00i64 0
1170F 0A 06010000 0E01 0000map leaf, size 10: @106 → @10E
12107 12 08840000 E500 0000 FC000000 1701 0000map branch, size 18, depth 0 (root), slots 3, 10, 15: @E5 → @FC → @117
Show all 61 nodes
Offset (hex)Bytes (hex)Meaning
1334C 74797065txt "type"
1387C 626F6F6C65616Etxt "boolean"
1400F 0A 33010000 3801 0000map leaf, size 10: @133 → @138
14A14 14 616464697469 6F6E616C50726F70 657274696573txt "additionalProperties"
1600F 0A 4A010000 4001 0000map leaf, size 10: @14A → @140
16A4C 74797065txt "type"
16F6C 6F626A656374txt "object"
1760F 0A 6A010000 6F01 0000map leaf, size 10: @16A → @16F
18007 0E 80040000 6001 0000 76010000map branch, size 14, depth 0 (root), slots 7, 10: @160 → @176
18E8C 63617074757265 73txt "captures"
1970F 0A 8E010000 8001 0000map leaf, size 10: @18E → @180
1A17C 6C616E64696E67txt "landing"
1A90F 0A A1010000 2101 0000map leaf, size 10: @1A1 → @121
1B307 0E 10400000 9701 0000 A9010000map branch, size 14, depth 0 (root), slots 4, 14: @197 → @1A9
1C1AC 70726F70657274 696573txt "properties"
1CC0F 0A C1010000 B301 0000map leaf, size 10: @1C1 → @1B3
1D68C 72657175697265 64txt "required"
1DF0F 0A D6010000 C300 0000map leaf, size 10: @1D6 → @C3
1E914 14 616464697469 6F6E616C50726F70 657274696573txt "additionalProperties"
1FF01bit false
2000F 0A E9010000 FF01 0000map leaf, size 10: @1E9 → @1FF
20A4C 74797065txt "type"
20F6C 6F626A656374txt "object"
2160F 0A 0A020000 0F02 0000map leaf, size 10: @20A → @20F
22007 16 A8040000 CC01 0000 DF010000 0002 0000 16020000map branch, size 22, depth 0 (root), slots 3, 5, 7, 10: @1CC → @1DF → @200 → @216
2368C 726573706F6E73 65txt "response"
23F0F 0A 36020000 2002 0000map leaf, size 10: @236 → @220
2497C 72657175657374txt "request"
2510F 0A 49020000 9C00 0000map leaf, size 10: @249 → @9C
25B07 0E 40040000 3F02 0000 51020000map branch, size 14, depth 0 (root), slots 6, 10: @23F → @251
Offset (hex)Bytes (hex)Meaning

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

go915 B
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")
	}
}