# Extend the Checkers Game With a Leaderboard
In this section, you will learn:
- How to make an existing chain IBC-enabled.
- How to extend your chains with additional modules.
# What you will be building and why
The checkers blockchain you have built has the ability to create games, play them, forfeit them, and wager on them (potentially with cross-chain tokens). A further optimization would be to include a leaderboard. This could be executed locally on the checkers blockchain to rank the best players on the checkers blockchain.
But what if there is more than one checkers chain? Or better yet, other game chains that allow players to play a competitive game. Would it not be great to enable a standard to send the game data from the local game chain to an application-specific chain that keeps a global leaderboard? This is exactly what you will be building in the next few sections.
Remember the appchain thesis that is an integral part of the Cosmos philosophy - where every application has its own chain and can be optimized for the application-specific logic it executes. Then IBC can be used to interoperate between all the chains that have specialised functionality. This is the idea behind the prototype checkers and leaderboard chains you're building, enabling IBC packets to be sent between those chains to create cross-chain applications.
# Adding a local leaderboard module to the checkers chain
Currently, your checkers game contains the checkers module but is not IBC-enabled. It is now time to extend your checkers game with a leaderboard by adding a new module to make it IBC-enabled.
Let’s dive right into it.
Go to your checkers folder and make sure that you are checked out on the cosmjs-elements (opens new window) tag.
In the checkers chain folder, you can scaffold a leaderboard module with Ignite:
In order to create and maintain a leaderboard, you need to store the player information. Scaffold a structure with:
Now you can use this structure to create the board itself:
You want the structures to be nullable types (opens new window), so a few adjustments are needed - especially because you do not have a null value for an address.
You need to make the adjustments in the Protobuf files
proto/leaderboard/genesis.proto. Make sure to import
gogoproto/gogo.proto and use
[(gogoproto.nullable) = false]; for the
PlayerInfo and the
For example, for
proto/leaderboard/board.proto try this:
You will also have to modify the
x/leaderboard/genesis.go. In it, look for:
Simply change this to:
Next, in the
x/leaderboard/genesis_test.go, look for:
We gave the checkers' module access to the leaderboard's keeper. Therefore you will need to modify
Now add the leaderboard's keeper into it:
You want to store a win, a loss, or a draw when a game ends. Thus, you should create some helper functions first. Create a
x/checkers/keeper/player_info_handler.go file with the following code:
The checkers module will need to access the leaderboard methods, like
To achieve this, first, you need to write those functions. Create a
x/leaderboard/keeper/player_info_handler.go file with the following code:
For the code above to function, you need to define
x/leaderboard/types/keys.go. Add the following piece of code at the end of the file:
x/checkers/types/errors.go and make sure that it includes the following:
Now it is time to allow the checkers module access to the leaderboard module. Look for
app/app.go and modify it to include
In addition, you need to modify
x/checkers/keeper/keeper.go and include the leaderboard keeper:
Now the checkers module can call the keeper of the leaderboard module, so add the call for a win in
Now add the call for a draw in
That will get the job done and add the player's win, lose, or forfeit counts to the store.
It is time to sort the players and clip the leaderboard to the best 100 (
LeaderboardWinnerLength) players. Scaffold a new transaction:
Again, you can first create some helper functions in
If it cannot parse the date information, it will throw an error that you need to include in
Now you need to call
That is it! Now the checkers blockchain can keep track of player information, and create or update the leaderboard based on player information if requested via the CLI.
# Forwarding player information via IBC
It is time to look at how you can forward the player information via IBC.
Remember, you created the module with the
You can scaffold an IBC transaction with:
You do not want arbitrary player information, but instead, want to fetch player information from the store, so make a small adjustment to
x/leaderboard/client/cli/tx_candidate.go. Look for the following lines and remove them:
You will also need to remove the import of
encoding/json because it is not used anymore, and you should remove the parameter
argPlayerInfo from the
The last step is to implement the logic to fetch and send player information in
You do not handle received packages, because this module is only meant for sending player information to a separate leaderboard chain, which you will create next.