0.1.95 • Published 7 months ago

@games-fun/react v0.1.95

Weekly downloads
-
License
MIT
Repository
-
Last release
7 months ago

@games-fun/react

React bindings and components for the Games.fun SDK.

Installation

npm install @games-fun/react @games-fun/sdk

Usage

Provider Setup

Wrap your application or game component with the GamesFunProvider:

import { GamesFunProvider } from '@games-fun/react';

function App() {
  return (
    <GamesFunProvider 
      options={{ 
        debug: true,
        gameServerUrl: 'your-game-server-url'
      }}
      onInitialized={(connection) => console.log('SDK initialized', connection)}
      onError={(error) => console.error('SDK error', error)}
    >
      <YourGame />
    </GamesFunProvider>
  );
}

Using the SDK in Components

import { useGamesFun, useGamesFunActions, ConnectionStatus } from '@games-fun/react';

function YourGame() {
  const { connection } = useGamesFun();

  // Register all game actions
  const actions = useGamesFunActions({
    purchaseItem: "Purchase in-game item",
    useItem: "Use inventory item",
    transferTokens: "Transfer tokens to game wallet"
  });

  const handlePurchase = async () => {
    try {
      await actions.purchaseItem({ 
        itemId: 'sword',
        price: 100,
      });
      console.log('Purchase successful');
    } catch (error) {
      console.error('Purchase failed:', error);
    }
  };

  return (
    <div>
      <h1>My Game</h1>
      
      {/* Built-in connection status component */}
      <ConnectionStatus />
      
      <button onClick={handlePurchase} disabled={!connection}>
        Buy Sword (100 coins)
      </button>
      
      {connection && (
        <div>
          <p>Token Balance: {connection.tokenBalance}</p>
          {connection.walletAddress && (
            <p>Wallet: {connection.walletAddress}</p>
          )}
        </div>
      )}
    </div>
  );
}

API Reference

Components

  • GamesFunProvider - Context provider for the SDK
  • ConnectionStatus - Component to display connection status

Hooks

  • useGamesFun - Access the SDK instance and connection
  • useGamesFunActions - Register and trigger game actions
  • useGamesFunContext - Direct access to the context (advanced usage)

License

MIT

0.1.95

7 months ago

0.1.94

7 months ago

0.1.93

7 months ago

0.1.92

7 months ago

0.1.9

7 months ago

0.1.5

7 months ago

0.1.4

7 months ago

0.1.3

7 months ago

0.1.2

7 months ago

0.1.1

7 months ago

0.1.0

7 months ago