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:

TRON97 B
Offset (hex)Bytes (hex)Meaning
0054 52 4F 4Emagic TRON
040F 02map leaf, size 2
068C 63617074757265 73txt "captures"
0F0F 0A 06000000 0400 0000map leaf, size 10: @6 → @4
197C 6C616E64696E67txt "landing"
2102 12000000000000 00i64 18
2A0F 0A 19000000 2100 0000map leaf, size 10: @19 → @21
347C 616C6C6F776564txt "allowed"
3C09bit true
3D0F 0A 34000000 3C00 0000map leaf, size 10: @34 → @3C
4707 12 10C00000 0F00 0000 2A000000 3D00 0000map branch, size 18, depth 0 (root), slots 4, 14, 15: @F → @2A → @3D

Define the messages

The game data installs duplex/checkers/drag in the events definition folder:

TRON821 B
Offset (hex)Bytes (hex)Meaning
00054 52 4F 4Emagic TRON
0045C 7069656365txt "piece"
00A7C 6C616E64696E67txt "landing"
0120E 11 00 0300 020000 00 04000000 0A0000 00array leaf, size 17, shift 0, bitmap 0003, count 2 (root): @4 → @A
0234C 74797065txt "type"
0286C 737472696E67txt "string"
02F0F 0A 23000000 2800 0000map leaf, size 10: @23 → @28
0397C 6D6178696D756Dtxt "maximum"
04102 1F000000000000 00i64 31
04A0F 0A 39000000 4100 0000map leaf, size 10: @39 → @41
0544C 74797065txt "type"
0597C 696E7465676572txt "integer"
0610F 0A 54000000 5900 0000map leaf, size 10: @54 → @59
06B7C 6D696E696D756Dtxt "minimum"
07302 00000000000000 00i64 0
07C0F 0A 6B000000 7300 0000map leaf, size 10: @6B → @73
08607 12 08840000 4A00 0000 61000000 7C00 0000map branch, size 18, depth 0 (root), slots 3, 10, 15: @4A → @61 → @7C
0985C 7069656365txt "piece"
09E0F 0A 98000000 2F00 0000map leaf, size 10: @98 → @2F
0A87C 6C616E64696E67txt "landing"
0B00F 0A A8000000 8600 0000map leaf, size 10: @A8 → @86
0BA07 0E 00600000 9E00 0000 B0000000map branch, size 14, depth 0 (root), slots 13, 14: @9E → @B0
0C8AC 70726F70657274 696573txt "properties"
0D30F 0A C8000000 BA00 0000map leaf, size 10: @C8 → @BA
0DD8C 72657175697265 64txt "required"
0E60F 0A DD000000 1200 0000map leaf, size 10: @DD → @12
0F014 14 616464697469 6F6E616C50726F70 657274696573txt "additionalProperties"
10601bit false
1070F 0A F0000000 0601 0000map leaf, size 10: @F0 → @106
1114C 74797065txt "type"
1166C 6F626A656374txt "object"
11D0F 0A 11010000 1601 0000map leaf, size 10: @111 → @116
Show all 81 nodes
Offset (hex)Bytes (hex)Meaning
12707 16 A8040000 D300 0000 E6000000 0701 0000 1D010000map branch, size 22, depth 0 (root), slots 3, 5, 7, 10: @D3 → @E6 → @107 → @11D
13D7C 6C616E64696E67txt "landing"
1457C 616C6C6F776564txt "allowed"
14D8C 63617074757265 73txt "captures"
1560E 15 00 0700 030000 00 3D010000 450100 00 4D010000array leaf, size 21, shift 0, bitmap 0007, count 3 (root): @13D → @145 → @14D
16B7C 6D6178696D756Dtxt "maximum"
17302 1F000000000000 00i64 31
17C0F 0A 6B010000 7301 0000map leaf, size 10: @16B → @173
1864C 74797065txt "type"
18B7C 696E7465676572txt "integer"
1930F 0A 86010000 8B01 0000map leaf, size 10: @186 → @18B
19D7C 6D696E696D756Dtxt "minimum"
1A502 00000000000000 00i64 0
1AE0F 0A 9D010000 A501 0000map leaf, size 10: @19D → @1A5
1B807 12 08840000 7C01 0000 93010000 AE01 0000map branch, size 18, depth 0 (root), slots 3, 10, 15: @17C → @193 → @1AE
1CA4C 74797065txt "type"
1CF7C 626F6F6C65616Etxt "boolean"
1D70F 0A CA010000 CF01 0000map leaf, size 10: @1CA → @1CF
1E14C 74797065txt "type"
1E67C 626F6F6C65616Etxt "boolean"
1EE0F 0A E1010000 E601 0000map leaf, size 10: @1E1 → @1E6
1F814 14 616464697469 6F6E616C50726F70 657274696573txt "additionalProperties"
20E0F 0A F8010000 EE01 0000map leaf, size 10: @1F8 → @1EE
2184C 74797065txt "type"
21D6C 6F626A656374txt "object"
2240F 0A 18020000 1D02 0000map leaf, size 10: @218 → @21D
22E07 0E 80040000 0E02 0000 24020000map branch, size 14, depth 0 (root), slots 7, 10: @20E → @224
23C8C 63617074757265 73txt "captures"
2450F 0A 3C020000 2E02 0000map leaf, size 10: @23C → @22E
24F7C 6C616E64696E67txt "landing"
2570F 0A 4F020000 B801 0000map leaf, size 10: @24F → @1B8
2617C 616C6C6F776564txt "allowed"
2690F 0A 61020000 D701 0000map leaf, size 10: @261 → @1D7
27307 12 10C00000 4502 0000 57020000 6902 0000map branch, size 18, depth 0 (root), slots 4, 14, 15: @245 → @257 → @269
285AC 70726F70657274 696573txt "properties"
2900F 0A 85020000 7302 0000map leaf, size 10: @285 → @273
29A8C 72657175697265 64txt "required"
2A30F 0A 9A020000 5601 0000map leaf, size 10: @29A → @156
2AD14 14 616464697469 6F6E616C50726F70 657274696573txt "additionalProperties"
2C301bit false
2C40F 0A AD020000 C302 0000map leaf, size 10: @2AD → @2C3
2CE4C 74797065txt "type"
2D36C 6F626A656374txt "object"
2DA0F 0A CE020000 D302 0000map leaf, size 10: @2CE → @2D3
2E407 16 A8040000 9002 0000 A3020000 C402 0000 DA020000map branch, size 22, depth 0 (root), slots 3, 5, 7, 10: @290 → @2A3 → @2C4 → @2DA
2FA8C 726573706F6E73 65txt "response"
3030F 0A FA020000 E402 0000map leaf, size 10: @2FA → @2E4
30D7C 72657175657374txt "request"
3150F 0A 0D030000 2701 0000map leaf, size 10: @30D → @127
31F07 0E 40040000 0303 0000 15030000map branch, size 14, depth 0 (root), slots 6, 10: @303 → @315
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

go1.6 kB
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")
	}
}