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:

dust27 B
committed true
revision  2

Define the messages

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

dust762 B
request  {type                 object
          required             [piece landing expected]
          additionalProperties false
          properties           {piece    {type string}
                                landing  {type    integer
                                          minimum 0
                                          maximum 31}
                                expected {type    integer
                                          minimum 0}}}
response {type                 object
          required             [committed revision]
          additionalProperties false
          properties           {committed {type boolean}
                                revision  {type    integer
                                           minimum 0}}}

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