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:
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 00 | 54 52 4F 4E | magic TRON |
| 04 | 8C 7265766973696F 6E | txt "revision" |
| 0D | 02 02000000000000 00 | i64 2 |
| 16 | 0F 0A 04000000 0D00 0000 | map leaf, size 10: @4 → @D |
| 20 | 9C 636F6D6D697474 6564 | txt "committed" |
| 2A | 09 | bit true |
| 2B | 0F 0A 20000000 2A00 0000 | map leaf, size 10: @20 → @2A |
| 35 | 07 0E 00030000 1600 0000 2B000000 | map branch, size 14, depth 0 (root), slots 8, 9: @16 → @2B |
| 43 | 35000000 00000000 | root → @35; previous: none |
Define the messages
The game data installs push/checkers/jump 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 | 8C 65787065637465 64 | txt "expected" |
| 01B | 0E 15 00 0700 030000 00 04000000 0A0000 00 12000000 | array leaf, size 21, shift 0, bitmap 0007, count 3 (root): @4 → @A → @12 |
| 030 | 4C 74797065 | txt "type" |
| 035 | 6C 737472696E67 | txt "string" |
| 03C | 0F 0A 30000000 3500 0000 | map leaf, size 10: @30 → @35 |
| 046 | 7C 6D6178696D756D | txt "maximum" |
| 04E | 02 1F000000000000 00 | i64 31 |
| 057 | 0F 0A 46000000 4E00 0000 | map leaf, size 10: @46 → @4E |
| 061 | 4C 74797065 | txt "type" |
| 066 | 7C 696E7465676572 | txt "integer" |
| 06E | 0F 0A 61000000 6600 0000 | map leaf, size 10: @61 → @66 |
| 078 | 7C 6D696E696D756D | txt "minimum" |
| 080 | 02 00000000000000 00 | i64 0 |
| 089 | 0F 0A 78000000 8000 0000 | map leaf, size 10: @78 → @80 |
| 093 | 07 12 08840000 5700 0000 6E000000 8900 0000 | map branch, size 18, depth 0 (root), slots 3, 10, 15: @57 → @6E → @89 |
| 0A5 | 4C 74797065 | txt "type" |
| 0AA | 7C 696E7465676572 | txt "integer" |
| 0B2 | 0F 0A A5000000 AA00 0000 | map leaf, size 10: @A5 → @AA |
| 0BC | 7C 6D696E696D756D | txt "minimum" |
| 0C4 | 02 00000000000000 00 | i64 0 |
| 0CD | 0F 0A BC000000 C400 0000 | map leaf, size 10: @BC → @C4 |
| 0D7 | 07 0E 00840000 B200 0000 CD000000 | map branch, size 14, depth 0 (root), slots 10, 15: @B2 → @CD |
| 0E5 | 8C 65787065637465 64 | txt "expected" |
| 0EE | 0F 0A E5000000 D700 0000 | map leaf, size 10: @E5 → @D7 |
| 0F8 | 5C 7069656365 | txt "piece" |
| 0FE | 0F 0A F8000000 3C00 0000 | map leaf, size 10: @F8 → @3C |
| 108 | 7C 6C616E64696E67 | txt "landing" |
| 110 | 0F 0A 08010000 9300 0000 | map leaf, size 10: @108 → @93 |
| 11A | 07 12 00610000 EE00 0000 FE000000 1001 0000 | map branch, size 18, depth 0 (root), slots 8, 13, 14: @EE → @FE → @110 |
Show all 76 nodes
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 12C | AC 70726F70657274 696573 | txt "properties" |
| 137 | 0F 0A 2C010000 1A01 0000 | map leaf, size 10: @12C → @11A |
| 141 | 8C 72657175697265 64 | txt "required" |
| 14A | 0F 0A 41010000 1B00 0000 | map leaf, size 10: @141 → @1B |
| 154 | 14 14 616464697469 6F6E616C50726F70 657274696573 | txt "additionalProperties" |
| 16A | 01 | bit false |
| 16B | 0F 0A 54010000 6A01 0000 | map leaf, size 10: @154 → @16A |
| 175 | 4C 74797065 | txt "type" |
| 17A | 6C 6F626A656374 | txt "object" |
| 181 | 0F 0A 75010000 7A01 0000 | map leaf, size 10: @175 → @17A |
| 18B | 07 16 A8040000 3701 0000 4A010000 6B01 0000 81010000 | map branch, size 22, depth 0 (root), slots 3, 5, 7, 10: @137 → @14A → @16B → @181 |
| 1A1 | 9C 636F6D6D697474 6564 | txt "committed" |
| 1AB | 8C 7265766973696F 6E | txt "revision" |
| 1B4 | 0E 11 00 0300 020000 00 A1010000 AB0100 00 | array leaf, size 17, shift 0, bitmap 0003, count 2 (root): @1A1 → @1AB |
| 1C5 | 4C 74797065 | txt "type" |
| 1CA | 7C 626F6F6C65616E | txt "boolean" |
| 1D2 | 0F 0A C5010000 CA01 0000 | map leaf, size 10: @1C5 → @1CA |
| 1DC | 4C 74797065 | txt "type" |
| 1E1 | 7C 696E7465676572 | txt "integer" |
| 1E9 | 0F 0A DC010000 E101 0000 | map leaf, size 10: @1DC → @1E1 |
| 1F3 | 7C 6D696E696D756D | txt "minimum" |
| 1FB | 02 00000000000000 00 | i64 0 |
| 204 | 0F 0A F3010000 FB01 0000 | map leaf, size 10: @1F3 → @1FB |
| 20E | 07 0E 00840000 E901 0000 04020000 | map branch, size 14, depth 0 (root), slots 10, 15: @1E9 → @204 |
| 21C | 8C 7265766973696F 6E | txt "revision" |
| 225 | 0F 0A 1C020000 0E02 0000 | map leaf, size 10: @21C → @20E |
| 22F | 9C 636F6D6D697474 6564 | txt "committed" |
| 239 | 0F 0A 2F020000 D201 0000 | map leaf, size 10: @22F → @1D2 |
| 243 | 07 0E 00030000 2502 0000 39020000 | map branch, size 14, depth 0 (root), slots 8, 9: @225 → @239 |
| 251 | AC 70726F70657274 696573 | txt "properties" |
| 25C | 0F 0A 51020000 4302 0000 | map leaf, size 10: @251 → @243 |
| 266 | 8C 72657175697265 64 | txt "required" |
| 26F | 0F 0A 66020000 B401 0000 | map leaf, size 10: @266 → @1B4 |
| 279 | 14 14 616464697469 6F6E616C50726F70 657274696573 | txt "additionalProperties" |
| 28F | 01 | bit false |
| 290 | 0F 0A 79020000 8F02 0000 | map leaf, size 10: @279 → @28F |
| 29A | 4C 74797065 | txt "type" |
| 29F | 6C 6F626A656374 | txt "object" |
| 2A6 | 0F 0A 9A020000 9F02 0000 | map leaf, size 10: @29A → @29F |
| 2B0 | 07 16 A8040000 5C02 0000 6F020000 9002 0000 A6020000 | map branch, size 22, depth 0 (root), slots 3, 5, 7, 10: @25C → @26F → @290 → @2A6 |
| 2C6 | 8C 726573706F6E73 65 | txt "response" |
| 2CF | 0F 0A C6020000 B002 0000 | map leaf, size 10: @2C6 → @2B0 |
| 2D9 | 7C 72657175657374 | txt "request" |
| 2E1 | 0F 0A D9020000 8B01 0000 | map leaf, size 10: @2D9 → @18B |
| 2EB | 07 0E 40040000 CF02 0000 E1020000 | map branch, size 14, depth 0 (root), slots 6, 10: @2CF → @2E1 |
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 2F9 | EB020000 00000000 | root → @2EB; 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 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")
}
}