# Can Play Query
Make sure you have all you need before proceeding:
- You understand the concepts of queries, and Protobuf.
- You have Go installed.
- The checkers blockchain codebase up to gas metering. You can get there by following the previous steps or checking out the relevant version (opens new window).
A player sends a MsgPlayMove
when making a move. This message can succeed or fail for several reasons. One error situation is when the message represents an invalid move.
Players should be able to make sure that a move is valid before burning gas. To add this functionality, you need to create a way for the player to call the Move
(opens new window) function without changing the game's state. Use a query because they are evaluated in memory and do not commit anything permanently to storage.
# New information
To run a query to check the validity of a move you need to pass:
- The game ID, call the field
IdValue
. player
as queries do not have a signer.- The board position to start from:
fromX
andfromY
. - The board position to land on:
toX
andtoY
.
The information to be returned is:
- A boolean whether the move is valid, called
Possible
. - A text reason as to why the move is not valid, called
Reason
.
As with other data structures, you can create the query message object with Starport:
Among other files, you should now have this:
Starport has created the following boilerplate for you:
- The Protobuf gRPC interface function (opens new window) to submit your new
QueryCanPlayMoveRequest
and its default implementation. - The routing of this new query (opens new window) in the query facilities.
- An empty function (opens new window) ready to implement the action.
# Query handling
Now you need to implement the answer to the player's query in grpc_query_can_play_move.go
. Differentiate between two types of errors:
- Errors relating to the move, returning a reason.
- Errors if a move test is impossible, returning an error.
The game needs to be fetched. If it does not exist at all, you can return an error message because you did not test the move:
Has the game already been won?
Is the
player
given a valid player?Is it the player's turn?
Attempt the move and report back:
If all went fine:
# Next up
Do you want to give players more flexibility on which tokens they can use for the checkers blockchain's games? Let players wager any fungible token in the next section.