AppClient

The AppClient class serves as the primary interface for interacting with Race games on the client side. It provides a high-level abstraction that simplifies blockchain interaction and game logic execution, allowing developers to focus on building engaging user experiences.

Role of AppClient

The AppClient acts as the central hub for managing the client-side aspects of a Race game. It is responsible for:

  • Establishing a connection with the Transactor server: This connection allows the client to send and receive game events, state updates, and other information necessary for gameplay.

  • Loading and executing the WASM game bundle: The AppClient fetches the game bundle from decentralized storage and runs it in a secure sandbox environment on the client side. This enables the game logic to be executed directly within the client application.

  • Providing access to game context and state: The AppClient maintains the GameContext object, which represents the current state of the game on the client side. It also provides access to the handler state, which is managed by the WASM game bundle.

  • Facilitating player interactions: The AppClient allows players to submit custom events representing their actions within the game. It also handles the decryption of hidden knowledge revealed by the game handler.

  • Managing player profiles: The AppClient allows players to create and update their on-chain profiles, including setting their nickname and avatar NFT. It also provides access to the player's token balances and NFT holdings.

Functionalities

The AppClient offers various functionalities for interacting with Race games:

  • Connecting to the Transactor: The attachGame function establishes a secure websocket connection with the Transactor server serving the specified game.

  • Loading the Game Bundle: The AppClient automatically fetches the game bundle associated with the game from decentralized storage.

  • Initializing the Game: The attachGame function also initializes the game by calling the init_state function exported by the WASM game bundle.

  • Submitting Events: The submitEvent function allows players to submit custom events representing their actions within the game.

  • Handling Events: The AppClient provides event listeners that can be used to react to incoming events and update the game UI accordingly.

  • Getting Revealed Knowledge: The getRevealed function allows access to hidden knowledge revealed by the game handler, which can be used to update the game UI and inform player decisions.

  • Managing Player Profile: The AppClient provides functions for creating and updating player profiles, as well as accessing player balances and NFT holdings.

  • Exiting and Detaching: The exit and detach functions allow players to leave the game and disconnect from the Transactor.

  • Submitting Messages: The submitMessage function allows players to send chat messages or other communications to the Transactor.

  • Creating Sub-clients: The subClient function allows the creation of SubClient instances for interacting with sub-games.

Creating and Using AppClient

To create an AppClient instance, use the static initialize function as shown in the Installation and Setup section. Once initialized, you can use the various functions provided by the AppClient to interact with the Race game, submit events, manage player profiles, and access game state information.

Here's an example of using the AppClient to submit a custom event:

// Assuming appClient is an initialized AppClient instance

const customEvent = { ... }; // Your custom event data

appClient.submitEvent(customEvent);

This code snippet demonstrates how the AppClient simplifies interaction with Race games by providing a high-level interface for submitting events and managing other gameplay aspects.

The following sections will discuss other key concepts and components of the RACE JS SDK, such as SubClient, Events, Game Context, and Decryption Cache.

Last updated