# Create the Game Handling

synopsis

Make sure you have all you need before proceeding:

You added the message to create a game along with its serialization and dedicated gRPC function with the help of Starport in the previous section.

Now all that remains is to add code that:

  • Creates a brand new game.
  • Saves it in storage.
  • Returns the ID of the new game.

Starport isolated this concern into a separate file, x/checkers/keeper/msg_server_create_game.go, for you to edit:

Copy func (k msgServer) CreateGame(goCtx context.Context, msg *types.MsgCreateGame) (*types.MsgCreateGameResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) // TODO: Handling the message _ = ctx return &types.MsgCreateGameResponse{}, nil } x checkers keeper msg_server_create_game.go View source

All the message processing code were created for you and all you are left to do is code the meat of the action. Opting for Starport is a wise decision as you can see.

Given that you have already done a lot of preparatory work: what does it involve to code the action? With what do you replace // TODO: Handling the message?

  • First, rules represent the ready-made file with the imported rules of the game:

    Copy import ( rules "github.com/alice/checkers/x/checkers/rules" )
  1. Get the new game's ID:

    Copy nextGame, found := k.Keeper.GetNextGame(ctx) if !found { panic("NextGame not found") } newIndex := strconv.FormatUint(nextGame.IdValue, 10) x checkers keeper msg_server_create_game.go View source

    Using the Keeper.GetNextGame (opens new window) function created by the starport scaffold single nextGame... command.

  2. Create the object to be stored:

    Copy storedGame := types.StoredGame{ Creator: msg.Creator, Index: newIndex, Game: rules.New().String(), Red: msg.Red, Black: msg.Black, } x checkers keeper msg_server_create_game.go View source

    Notice the use of:

    • The rules.New() (opens new window) command, which is part of the Checkers rules file you imported earlier.
    • The string content of the msg *types.MsgCreateGame namely .Creator, .Red, and .Black.
  3. Confirm that the values in it are correct by checking the validity of the players' addresses:

    Copy err := storedGame.Validate() if err != nil { return nil, err } x checkers keeper msg_server_create_game.go View source

    The .Creator, .Red, and .Black need to be checked because they were copied as strings. The check on .Creator is redundant here because at this stage the message's signatures have been verified and in particular the creator is the signer.

  4. Save the StoredGame object:

    Copy k.Keeper.SetStoredGame(ctx, storedGame) x checkers keeper msg_server_create_game.go View source

    Using the Keeper.SetStoredGame (opens new window) function created by the starport scaffold map storedGame... command

  5. Prepare the ground for the next game with:

    Copy nextGame.IdValue++ k.Keeper.SetNextGame(ctx, nextGame) x checkers keeper msg_server_create_game.go View source

    Using the Keeper.SetNextGame (opens new window) function created by Starport.

  6. Return the newly created ID for reference:

    Copy return &types.MsgCreateGameResponse{ IdValue: newIndex, }, nil x checkers keeper msg_server_create_game.go View source

# Next up

You will modify this handling in the next sections:

Now that a game is created, it is time to play it. That is the subject of the next section.