# The Reject Game Elements
Before proceeding, make sure you have all you need:
- You understand the concepts of transactions, messages), and Protobuf.
- You know how to create a message with Starport, and code its handling. This section does not aim to repeat what can be learned in earlier sections.
- Have Go installed.
- The checkers blockchain codebase with the previous messages and their events. You can get there by following the previous steps or checking out the relevant version (opens new window).
If anyone can create a game for any two other players, it is important to allow a player to reject a game. A player should not be allowed to reject a game once they have made their first move.
To reject a game, a player needs to provide the ID of the game that the player wants to reject. Call the field idValue
. This should be sufficient as the signer of the message is implicitly the player.
# Working with Starport
Name the message object RejectGame
. Invoke Starport with:
It creates all the boilerplate for you and leaves a single place for the code you want to include:
# Additional information
A new rule of the game should be that a player cannot reject a game once they begin to play. As of now, when loading a StoredGame
from storage, you have no way of knowing whether a player already played or not. Add a new field to the StoredGame
called MoveCount
. In proto/checkers/stored_game.proto
:
Run Protobuf to recompile the relevant Go files:
MoveCount
should start at 0
and increment by 1
on each move. So adjust it first in the handler when creating the game:
Just before saving to the storage, in the handler when playing a move:
With MoveCount
counting properly, you are now ready to handle a rejection request.
# The reject handling
To follow the Cosmos SDK conventions declare the following new errors:
You will add an event for rejection. Begin by preparing the new keys:
In the message handler the reject steps are:
Fetch the relevant information:
Is the player expected? Did the player already play? Check with:
Remember that the black player plays first.
Get rid of the game, as it is not interesting enough to keep:
Finally using the
Keeper.RemoveStoredGame
(opens new window) function created long ago by thestarport scaffold map storedGame...
command.Emit the relevant event:
Leave the returned object as it is as you have nothing new to tell the caller.
You can confirm that your project at least compiles with (opens new window):
# Next up
The next four sections cover forfeits and how games end. In the next section you create a doubly-linked FIFO.
Later you add a deadline and a game winner fields, before being able to finally enforce the forfeit.
If you want to enable token wagers in your games instead, skip ahead to wagers.