# Adding Events

synopsis

Make sure you have all you need before proceeding:

Now that you have added the possible actions including their return values, use events to alert/notify players.

Imagine a potential or current player waiting for their turn. It is not practical to look at all the transactions and search for the ones signifying the player's turn. It is better to listen to known events that let determine whose player's turn it is.

Adding events to your application is as simple as:

  1. Defining the events you want to use.
  2. Emitting the events at the right locations.

# Game created event

Start with the event that announces the creation of a new game. The goal is to:

  • Inform/alert the players of the game.
  • Make it easy for the players to find the relevant game.

So define some new keys in x/checkers/types/keys.go:

Copy const ( StoredGameEventKey = "NewGameCreated" // Indicates what key to listen to StoredGameEventCreator = "Creator" StoredGameEventIndex = "Index" // What game is relevant StoredGameEventRed = "Red" // Is it relevant to me? StoredGameEventBlack = "Black" // Is it relevant to me? ) x checkers types keys.go View source

Emit the event in your handler file x/checkers/keeper/msg_server_create_game.go:

Copy ctx.EventManager().EmitEvent( sdk.NewEvent(sdk.EventTypeMessage, sdk.NewAttribute(sdk.AttributeKeyModule, "checkers"), sdk.NewAttribute(sdk.AttributeKeyAction, types.StoredGameEventKey), sdk.NewAttribute(types.StoredGameEventCreator, msg.Creator), sdk.NewAttribute(types.StoredGameEventIndex, newIndex), sdk.NewAttribute(types.StoredGameEventRed, msg.Red), sdk.NewAttribute(types.StoredGameEventBlack, msg.Black), ), ) x checkers keeper msg_server_create_game.go View source

The only thing left to do is to implement this correspondingly in the GUI or include a server to listen for such events.

# Player moved event

The created transaction to play a move informs the opponent about:

  • Which player is relevant.
  • Which game does the move relate to.
  • When the move happened.
  • What the move's outcome was.
  • Whether the game was won.

Contrary to the create game event, which alerted the players about a new game, the players now know which game IDs to keep an eye out for. There is no need to repeat the players' addresses, the game ID is information enough.

You define new keys in x/checkers/types/keys.go similarly:

Copy const ( PlayMoveEventKey = "MovePlayed" PlayMoveEventCreator = "Creator" PlayMoveEventIdValue = "IdValue" PlayMoveEventCapturedX = "CapturedX" PlayMoveEventCapturedY = "CapturedY" PlayMoveEventWinner = "Winner" ) x checkers types keys.go View source

Emit the event in your file x/checkers/keeper/msg_server_play_move.go:

Copy ctx.EventManager().EmitEvent( sdk.NewEvent(sdk.EventTypeMessage, sdk.NewAttribute(sdk.AttributeKeyModule, "checkers"), sdk.NewAttribute(sdk.AttributeKeyAction, types.PlayMoveEventKey), sdk.NewAttribute(types.PlayMoveEventCreator, msg.Creator), sdk.NewAttribute(types.PlayMoveEventIdValue, msg.IdValue), sdk.NewAttribute(types.PlayMoveEventCapturedX, strconv.FormatInt(int64(captured.X), 10)), sdk.NewAttribute(types.PlayMoveEventCapturedY, strconv.FormatInt(int64(captured.Y), 10)), sdk.NewAttribute(types.PlayMoveEventWinner, game.Winner().Color), ), ) x checkers keeper msg_server_play_move.go View source

# Next up

Time to add a third message to make it possible for a player to reject a game and make your checkers blockchain more resistant to spam.