Commit a multi-jump turn with push

Bob's turn continues through landing squares 13 and 4. A push exchange lets him send those landings in order and receive one result for the whole turn. Open the exchange without initial data, then send each landing as a message. Call finish when the input stream is complete.

The worker collects landings without changing the database. Every message identifies the same piece and expected revision. The example checks that the board remains at revision one while the worker reads both messages. Its twelve-landing bound limits the draft held by this worker.

After the worker receives finish, it passes the ordered path to checkers/jump. The mutation validates the complete demonstrated path, moves black1 to square 4, removes both captured pieces, changes the turn and advances the revision. Those changes commit together. An incomplete path cannot match that mutation.

The worker then publishes revision-two turn and animation cues and sends its one response. The animation path is [20, 13, 4]. If an exchange is canceled, discard its collected draft and do not run the mutation. A timeout while reading means no message arrived within that wait, so application code must choose whether to wait again or cancel.

The complete turn returns:

TRON75 B
Offset (hex)Bytes (hex)Meaning
0054 52 4F 4Emagic TRON
048C 7265766973696F 6Etxt "revision"
0D02 02000000000000 00i64 2
160F 0A 04000000 0D00 0000map leaf, size 10: @4 → @D
209C 636F6D6D697474 6564txt "committed"
2A09bit true
2B0F 0A 20000000 2A00 0000map leaf, size 10: @20 → @2A
3507 0E 00030000 1600 0000 2B000000map branch, size 14, depth 0 (root), slots 8, 9: @16 → @2B

Define the messages

The game data installs push/checkers/jump in the events definition folder:

TRON769 B
Offset (hex)Bytes (hex)Meaning
00054 52 4F 4Emagic TRON
0045C 7069656365txt "piece"
00A7C 6C616E64696E67txt "landing"
0128C 65787065637465 64txt "expected"
01B0E 15 00 0700 030000 00 04000000 0A0000 00 12000000array leaf, size 21, shift 0, bitmap 0007, count 3 (root): @4 → @A → @12
0304C 74797065txt "type"
0356C 737472696E67txt "string"
03C0F 0A 30000000 3500 0000map leaf, size 10: @30 → @35
0467C 6D6178696D756Dtxt "maximum"
04E02 1F000000000000 00i64 31
0570F 0A 46000000 4E00 0000map leaf, size 10: @46 → @4E
0614C 74797065txt "type"
0667C 696E7465676572txt "integer"
06E0F 0A 61000000 6600 0000map leaf, size 10: @61 → @66
0787C 6D696E696D756Dtxt "minimum"
08002 00000000000000 00i64 0
0890F 0A 78000000 8000 0000map leaf, size 10: @78 → @80
09307 12 08840000 5700 0000 6E000000 8900 0000map branch, size 18, depth 0 (root), slots 3, 10, 15: @57 → @6E → @89
0A54C 74797065txt "type"
0AA7C 696E7465676572txt "integer"
0B20F 0A A5000000 AA00 0000map leaf, size 10: @A5 → @AA
0BC7C 6D696E696D756Dtxt "minimum"
0C402 00000000000000 00i64 0
0CD0F 0A BC000000 C400 0000map leaf, size 10: @BC → @C4
0D707 0E 00840000 B200 0000 CD000000map branch, size 14, depth 0 (root), slots 10, 15: @B2 → @CD
0E58C 65787065637465 64txt "expected"
0EE0F 0A E5000000 D700 0000map leaf, size 10: @E5 → @D7
0F85C 7069656365txt "piece"
0FE0F 0A F8000000 3C00 0000map leaf, size 10: @F8 → @3C
1087C 6C616E64696E67txt "landing"
1100F 0A 08010000 9300 0000map leaf, size 10: @108 → @93
11A07 12 00610000 EE00 0000 FE000000 1001 0000map branch, size 18, depth 0 (root), slots 8, 13, 14: @EE → @FE → @110
Show all 76 nodes
Offset (hex)Bytes (hex)Meaning
12CAC 70726F70657274 696573txt "properties"
1370F 0A 2C010000 1A01 0000map leaf, size 10: @12C → @11A
1418C 72657175697265 64txt "required"
14A0F 0A 41010000 1B00 0000map leaf, size 10: @141 → @1B
15414 14 616464697469 6F6E616C50726F70 657274696573txt "additionalProperties"
16A01bit false
16B0F 0A 54010000 6A01 0000map leaf, size 10: @154 → @16A
1754C 74797065txt "type"
17A6C 6F626A656374txt "object"
1810F 0A 75010000 7A01 0000map leaf, size 10: @175 → @17A
18B07 16 A8040000 3701 0000 4A010000 6B01 0000 81010000map branch, size 22, depth 0 (root), slots 3, 5, 7, 10: @137 → @14A → @16B → @181
1A19C 636F6D6D697474 6564txt "committed"
1AB8C 7265766973696F 6Etxt "revision"
1B40E 11 00 0300 020000 00 A1010000 AB0100 00array leaf, size 17, shift 0, bitmap 0003, count 2 (root): @1A1 → @1AB
1C54C 74797065txt "type"
1CA7C 626F6F6C65616Etxt "boolean"
1D20F 0A C5010000 CA01 0000map leaf, size 10: @1C5 → @1CA
1DC4C 74797065txt "type"
1E17C 696E7465676572txt "integer"
1E90F 0A DC010000 E101 0000map leaf, size 10: @1DC → @1E1
1F37C 6D696E696D756Dtxt "minimum"
1FB02 00000000000000 00i64 0
2040F 0A F3010000 FB01 0000map leaf, size 10: @1F3 → @1FB
20E07 0E 00840000 E901 0000 04020000map branch, size 14, depth 0 (root), slots 10, 15: @1E9 → @204
21C8C 7265766973696F 6Etxt "revision"
2250F 0A 1C020000 0E02 0000map leaf, size 10: @21C → @20E
22F9C 636F6D6D697474 6564txt "committed"
2390F 0A 2F020000 D201 0000map leaf, size 10: @22F → @1D2
24307 0E 00030000 2502 0000 39020000map branch, size 14, depth 0 (root), slots 8, 9: @225 → @239
251AC 70726F70657274 696573txt "properties"
25C0F 0A 51020000 4302 0000map leaf, size 10: @251 → @243
2668C 72657175697265 64txt "required"
26F0F 0A 66020000 B401 0000map leaf, size 10: @266 → @1B4
27914 14 616464697469 6F6E616C50726F70 657274696573txt "additionalProperties"
28F01bit false
2900F 0A 79020000 8F02 0000map leaf, size 10: @279 → @28F
29A4C 74797065txt "type"
29F6C 6F626A656374txt "object"
2A60F 0A 9A020000 9F02 0000map leaf, size 10: @29A → @29F
2B007 16 A8040000 5C02 0000 6F020000 9002 0000 A6020000map branch, size 22, depth 0 (root), slots 3, 5, 7, 10: @25C → @26F → @290 → @2A6
2C68C 726573706F6E73 65txt "response"
2CF0F 0A C6020000 B002 0000map leaf, size 10: @2C6 → @2B0
2D97C 72657175657374txt "request"
2E10F 0A D9020000 8B01 0000map leaf, size 10: @2D9 → @18B
2EB07 0E 40040000 CF02 0000 E1020000map branch, size 14, depth 0 (root), slots 6, 10: @2CF → @2E1
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

go2.1 kB
func makeTwoJumps(game *Game, bob, rules *events.Owner) {
	incomplete := game.commit("checkers/jump", map[string]any{
		"?piece":    "black1",
		"?landings": []int{13},
		"?expected": 1,
	})
	if incomplete["committed"] != false {
		game.t.Fatal("incomplete turn committed")
	}

	worker, err := events.ServeCheckersJump(rules)
	check(game.t, err)
	defer worker.Close()

	caller, err := events.OpenCheckersJump(bob)
	check(game.t, err)

	exchange, err := worker.Accept(time.Second)
	check(game.t, err)

	steps := []int64{13, 4}
	for index := range steps {
		landing := steps[index]
		check(game.t, caller.Send(events.CheckersJumpRequest{
			Piece:    "black1",
			Landing:  landing,
			Expected: 1,
		}))
	}

	check(game.t, caller.Finish())

	var landings []int64
	for {
		jump, finished, err := exchange.Read(time.Second)
		check(game.t, err)
		if finished {
			break
		}
		if len(landings) >= 12 || jump.Piece != "black1" || jump.Expected != 1 {
			game.t.Fatal("invalid jump stream")
		}

		landings = append(landings, jump.Landing)
		if game.revision() != 1 {
			game.t.Fatal("partial jump committed")
		}
	}

	committed := game.commit("checkers/jump", map[string]any{"?piece": "black1", "?landings": landings, "?expected": 1})
	reply := events.CheckersJumpResponse{
		Committed: committed["committed"].(bool),
		Revision:  game.revision(),
	}
	if !reply.Committed || reply.Revision != 2 {
		game.t.Fatal("complete jump did not commit")
	}
	delivery, err := events.PublishCheckersTableTurn(game.db, events.CheckersTableTurnData{
		Revision: 2,
		Turn:     "red",
	})
	check(game.t, err)
	if delivery.Dropped != 0 {
		game.t.Fatal("turn dropped")
	}
	delivery, err = events.PublishCheckersTableAnimate(game.db, events.CheckersTableAnimateData{
		Revision: 2,
		Piece:    "black1",
		Path:     []int64{20, 13, 4},
	})
	check(game.t, err)
	if delivery.Dropped != 0 {
		game.t.Fatal("animation dropped")
	}

	check(game.t, exchange.Respond(reply))

	response, err := caller.Response(time.Second)
	check(game.t, err)
	if response != reply {
		game.t.Fatal("incorrect jump reply")
	}
}