Network/wallet/tutorials/firebase-wallet-install

From CubeiaWiki

Jump to: navigation, search

Contents

Wallet Service and Firebase Integration

This is a quick primer on how to integrate Firebase with the Wallet Service.

Prerequisites:

Install & Configure Firebase Service

  • Download the firebase-wallet-service.xxx.sar.
  • Copy the .sar to $FIREBASE_HOME$/game/deploy.
  • Configure the URL to call. Edit $FIREBASE_HOME$/conf/cluster.props, add the following property with the correct value(s):
com.cubeia.network.walletservice.base-url=http://localhost:8080/wallet-service-rest/rest

Access the Wallet Service from a Game

In your game implementation (how do I make a game?) you can access the deployed wallet service by accessing the service registry. A good starting point would be to intercept player's joining and leaving the table to start and stop a session account in the wallet.

In your TableListener implementation:

public void playerJoined(Table table, GenericPlayer player) {
    WalletServiceContract wallet = serviceRegistry.getServiceInstance(WalletServiceContract.class);

    // LICENSEE_ID and MY_GAME_ID can be set to 0 for now if you want.
    Long sessionId = walletService.startSession(
			LICENSEE_ID,
			player.getPlayerId(), 
			table.getId(), 
			MY_GAME_ID, 
			player.getName());

    ...
}

public void playerLeft(Table table, int playerId) {
    WalletServiceContract wallet = serviceRegistry.getServiceInstance(WalletServiceContract.class);
    // Close the session account
    walletService.endSession(sessionId);
}

Some notes about the code above:

  • You need to keep track of the session account id yourself. If you have your own Player class implementation for storing game specific state, then you might consider to store the session id there. Otherwise you can keep a simple map of player id to session id in your game implementation.
  • Licensee ID is only relevant if you will use a multi-operator site (i.e. Network) or allow for multiple ways of authentication (e.g. Facebook and regular auth).
  • You can get hold of the service registry by either inject it from the constructor or inject it via IOC using Guice Support.

The next step might be to let the player bring in money to the table when he joins and then deposit whatever is left when he leaves the table. Example code below.

# MyTableListener
public void playerJoined(Table table, GenericPlayer player) {
    WalletServiceContract wallet = serviceRegistry.getServiceInstance(WalletServiceContract.class);

    // LICENSEE_ID and MY_GAME_ID can be set to 0 for now if you want.
    Long sessionId = walletService.startSession(
			LICENSEE_ID,
			player.getPlayerId(), 
			table.getId(), 
			MY_GAME_ID, 
			player.getName());

    ...

    // Send out request for how much money to bring to the table
    // the response will be handled by MyGameHandler in the method
    // 'onTableDeposit'

    ...
}

public void playerLeft(Table table, int playerId) {
    WalletServiceContract wallet = serviceRegistry.getServiceInstance(WalletServiceContract.class);
    // Return money left at the table
    walletService.depositwalletService.deposit(
                    getPlayerBalance(playerId), 
                    LICENSEE_ID, 
                    getSessionIdForPlayer(playerId), 
                    "From table["+tableId+"]");

    // Close the session account
    walletService.endSession(sessionId);
}

# MyGameHandler
public void onTableDeposit(BigDecimal amount, int playerId) {
    WalletServiceContract wallet = serviceRegistry.getServiceInstance(WalletServiceContract.class);
    walletService.withdraw(
                    amount, 
                    LICENSEE_ID, 
                    getSessionIdForPlayer(playerId), 
                    "From table["+tableId+"]");
}

Some notes about the code above:

  • getPlayerBalance should return the player's balance on the table. This is something you need to keep track of in your game implementation.
  • onTableDeposit is a pseudo-code example, the actual implementation will depend on how you handle actions sent from the client. Typically though it will be marshalled and handled in a processor implementation.


Testing the Implementation

Run the Wallet Service together with the Back Office, the best option is probably to use the complete bundle. In the back office list open session accounts and check if accounts and transactions are recorded as intended.

Troubleshooting

The Wallet Service is a complex service, much more than the User Service. If you have any questions or can't get it working right then you can place a question in our cubeia network forum.

Personal tools