Draw Card Game
The Draw Card game is a minimal poker-like game designed to showcase the basic principles of Race Protocol. It demonstrates how to implement core functionalities like player interactions, randomness generation, and settlements.
Game Overview
Here's a brief overview of the Draw Card game:
Players: Two players participate in each game.
Dealing: Each player receives one random card from a standard deck of 52 cards.
Betting: The first player to act places a bet within the predefined minimum and maximum bet limits.
Reacting: The second player can either call the bet or fold.
Revealing: If the second player calls, both players reveal their cards.
Outcome: The player with the higher card wins the pot. If both players have the same card, the second player wins. If the second player folds, the first player wins the pot.
Settlements: After the game concludes, the Transactor server handles the settlement process, distributing the pot to the winner and updating player balances accordingly.
Implementation Walkthrough
The Draw Card game logic is implemented in the race-example-draw-card
crate. Here's a detailed walkthrough of the implementation:
Defining Game State:
The
DrawCard
struct represents the game state, including:last_winner
: An optional string indicating the address of the previous game's winner.random_id
: The identifier of the randomness used for dealing cards.players
: A vector of Player structs, each containing the player's ID, balance, and current bet.stage
: The current stage of the game (e.g., Dealing, Betting, Reacting, Revealing, Ending).pot
: The total amount of tokens in the pot.bet
: The current bet amount placed by the first player.blind_bet
,min_bet
,max_bet
: Game parameters defining the blind bet, minimum bet, and maximum bet allowed
Defining Custom Events:
The
GameEvent
enum defines the custom events used in the game:Bet(u64)
: Represents a player placing a bet.Call
: Represents the second player calling the bet.Fold
: Represents the second player folding.
Implementing GameHandler:
The
DrawCard
struct implements the GameHandler trait, providing the following functions:init_state
: Initializes the game state based on the on-chain game account data.handle_event
: Processes incoming events and updates the game state accordingly.into_checkpoint
: Converts the game state into a checkpoint object for on-chain storage.
Handling Events:
The
handle_event
function processes different event types:Custom Events
: These events are defined in the GameEvent enum and represent player actions like betting, calling, and folding. The handler validates the actions based on the current game stage and updates the state accordingly.Built-in Events
: The handler responds to built-in events likeGameStart
,Join
,RandomnessReady
, andSecretsReady
to manage the game flow, generate randomness, assign cards, reveal information, and trigger settlements.
Randomization and Decryption:
The game handler uses the
Effect
object to request the creation of a shuffled list of cards. The Transactor and validator servers handle the randomization process and share the necessary secrets with the handler.When the
SecretsReady
event is received, the handler uses the shared secrets to decrypt the assigned cards and determine the winner.
Settlements:
After determining the winner, the handler uses the
Effect
object to specify the asset distribution for the settlement transaction. The Transactor then submits the transaction to the blockchain, updating player balances and finalizing the game.
Testing and Simulation
The race-test
crate provides tools for unit and integration testing of the Draw Card game logic. Developers can simulate player actions, server interactions, and blockchain updates to verify that the game functions correctly and fairly.
This walkthrough demonstrates how Race Protocol simplifies the development of blockchain games by providing a structured framework for managing game state, handling events, and interacting with the blockchain. By building upon these basic principles, developers can create more complex and engaging games with diverse mechanics and functionalities.
Last updated