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:

dust38 B
landing  18
allowed  true
captures {}

Define the messages

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

dust843 B
request  {type                 object
          required             [piece landing]
          additionalProperties false
          properties           {piece   {type string}
                                landing {type    integer
                                         minimum 0
                                         maximum 31}}}
response {type                 object
          required             [landing allowed captures]
          additionalProperties false
          properties           {landing  {type    integer
                                          minimum 0
                                          maximum 31}
                                allowed  {type boolean}
                                captures {type                 object
                                          additionalProperties {type boolean}}}}

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

odin1.5 kB
preview_a_drag :: proc(game: ^Game, alice, rules: runtime.Owner) -> sd.Error {
	worker := events.serve_checkers_drag(rules) or_return
	defer runtime.close_worker(worker)

	caller := events.open_checkers_drag(alice) or_return

	exchange := runtime.accept(worker, time.Second) or_return

	for landing in ([]i64{18, 19, 11}) {
		runtime.send_request(caller, {piece = "red7", landing = landing}) or_return
		update, notice := runtime.read_request(exchange, time.Second) or_return
		assert(notice == .Message)

		moves := rows(game, "checkers/legal", {"?piece" = update.piece}, Candidate) or_return
		preview := events.Checkers_Drag_Response {
			landing  = update.landing,
			captures = make(map[string]any, 1),
		}
		for move in moves {
			if move.landing == update.landing {
				preview.allowed = true
				if move.captures != nil {
					preview.captures = move.captures
				}
			}
		}

		runtime.send_response(exchange, preview) or_return
		response, response_notice := runtime.read_response(caller, time.Second) or_return
		assert(response_notice == .Message && response.allowed == (landing != 11))
	}

	runtime.finish_call(caller) or_return
	_, finished := runtime.read_request(exchange, time.Second) or_return
	assert(finished == .Finished)
	runtime.finish_exchange(exchange) or_return
	_, finished = runtime.read_response(caller, time.Second) or_return
	assert(finished == .Finished)
	current := board(game) or_return
	assert(current.revision == 2)
	return nil
}