Game Handler

This section focuses on the core aspect of building games on Race Protocol: the game handler.

The game handler is the heart of your game logic, implemented in Rust and compiled to WebAssembly (WASM). It acts as a state machine that defines the rules and behavior of your game and interacts with the Race API to leverage blockchain functionalities.

State Machine

The game handler maintains the state of your game, including elements like:

  • Player Data: This includes information about the players in the game, such as their addresses, balances, and game-specific attributes (e.g., hand of cards, position in the game).

  • Game Stage: This represents the current stage of the game, such as dealing, betting, or revealing.

  • Randomness: This includes the identifiers of any randomness generated for the game, which can be used for various purposes like shuffling cards or determining outcomes.

  • Decisions: This tracks any hidden decisions made by players during the game, along with their associated secret keys.

  • Other Game-specific Data: This can include any additional data structures or variables necessary to represent the state of your game.

The game handler transitions between different states based on the events it receives. For example, upon receiving a GameStart event, the handler may transition to the Dealing stage, generate randomness for the game, and assign cards to players.

Race API Interaction

The game handler interacts with the Race API to access blockchain functionalities and manage the game on-chain. This interaction is facilitated through the Effect object, which provides context information and allows the handler to trigger actions such as:

  • Generating Randomness: The handler can request the creation of randomness by specifying the desired type of randomness (e.g., shuffled list, lottery). The Transactor and validator servers then collaborate to generate the randomness and share the necessary secrets with the game handler.

  • Assigning Secrets: The handler can assign secrets to specific players, allowing them to decrypt and access hidden information relevant to their gameplay.

  • Revealing Hidden Knowledge: The handler can reveal previously hidden information to all participants by sharing the corresponding secrets publicly.

  • Making Settlements: When the game concludes, the handler can use the Race API to specify how assets should be distributed among players and trigger the Transactor to create and submit a settlement transaction to the blockchain.

This interaction between the game handler and the Race API allows developers to build games that leverage the security, transparency, and trustless nature of blockchain technology while maintaining a clean separation between game logic and on-chain interactions.

The next section will discuss how the game handler processes different types of events and drives the game forward.

Last updated