Game Context

GameContext Object and Client-side State

The GameContext object in the RACE JS SDK represents the current state of the game on the client side. It acts as a snapshot of the game's progress and contains various information necessary for the client application to display the game state and allow players to interact with it.

Information in GameContext

The GameContext object holds the following information:

  • gameAddr: The address of the Race game on the blockchain.

  • accessVersion: The access version of the game, which tracks the order in which players and servers joined.

  • settleVersion: The settle version of the game, which tracks the number of settlements that have occurred.

  • status: The current status of the game (e.g., "idle", "running", "closed").

  • nodes: A list of nodes (players and servers) participating in the game, along with their addresses, IDs, and connection status.

  • dispatch: Information about any pending dispatched events, such as timeouts.

  • handlerState: The current state of the game handler, managed by the WASM game bundle.

  • timestamp: The timestamp of the latest event processed by the game handler.

  • allowExit: A boolean flag indicating whether players are allowed to exit the game.

  • randomStates: A list of RandomState objects, each representing a randomness generation process and its current status.

  • decisionStates: A list of DecisionState objects, each representing a hidden decision made by a player and its current status.

  • checkpoint: An optional byte array containing the latest game state snapshot.

  • subGames: A list of SubGame objects representing any launched sub-games.

  • nextSettleVersion: The next settle version that will be used for the next settlement transaction.

  • initData: An optional byte array containing the initial game data.

  • maxPlayers: The maximum number of players allowed in the game.

  • players: A list of GamePlayer objects representing the players currently in the game.

  • entryType: The entry type of the game, which defines how players can join.

Accessing and Using GameContext

The AppClient and SubClient instances provide access to the GameContext object through their gameContext property. You can use this property to retrieve information about the game state and display it in the game UI.

Here's an example of accessing and using the GameContext:

// Assuming client is an initialized AppClient or SubClient instance

const gameContext = client.gameContext;

const currentStage = gameContext.stage;
const players = gameContext.players;

// Update the UI based on the current stage and player information

This code snippet demonstrates how the GameContext object provides valuable information about the game state, allowing the client application to display relevant information to the players and facilitate their interaction with the game.

Last updated