Game Development Process

DApp Structure Overview

A sample DApp you get with the DC-CLI is actually a finished project with a sample game. To create a new game, you only have to change game-specific parameters related to your local project.

Global variables and parameters are specified and stored in the DAO.Casino repository and many elements are imported from it. For more details, check readme.md files in it.

In the table below you can find a brief description of the key files within a game (sample).

NameDescription
dapp.logic.jsStores the game logic in the play function and the customDataFormat function; the latter is designed implement objects that store game data and custom parameters
index.js or tutorial.jsIntegrates game elements from dapp.logic.jsdapp.manifest and dc-webapi; when called, applies methods to create a window and carry out a game session: window.game.startwindow.game.connectwindow.game.play with settings for two parties and window.game.disconnect. Note the dc-webapi (i.e. DAO.Casino protocol) is integrated via an asynchronous pattern.
index.htmlGame frontend; calls the index.js where all game parameters are integrated and contains code to initiate key game stages: create account, open channel, play, close channel
dapp.manifest.jsImplements the logic for message exchange between a player and dealer (i.e. bankroller) and interaction with the game contract; only game-specific parameters are supposed to be edited (see below)

Mandatory Parameters of Dapp.manifest.js

NameDescription
name or slugunique game name
logicpath to the dapp_logic.js

How to Create your Game from Sample

  1. Install the DC-CLI/bankroller.
  2. Clone the example application at https://github.com/DaoCasino/dc-sdk-example.
  3. Clone dc-webapi by calling npm -i in the folder with the example.
  4. Open the folder where your game sample is installed.
  5. Enter your game logic into dapp.logic.js file (the play function). Note that the file contains two mandatory functions: playand customDataFormat; the latter imports game data.
  6. Open the tutorial.js file. This is the file where the game session life-cyсle is configured. Basically, the life-cycle includes five stages represented by five methods called one after another:
  • init - session setup when an account is created, integration with the protocol carried out via the webapi:
 const webapi = await new DCWebapi({
                  platformId: 'example_id',
                  blockchainNetwork: that.DC_NETWORK
                }).start()
 window.webapi = webapi
 webapi.account.init(WALLET_PWD, playerPrivateKeys[DC_NETWORK]) 

You have to specify DC_NETWORK in the same file (it is the blockchain network you want to use, e.g. Ropsten).

  • create - session setup when a game calls to the contract. Mind the rules: bankroller balance is supposed to exceed the player's 5-fold:
window.game = window.webapi.createGame({ 
    name: manifest.slug, 
    contract: manifest.getContract(DC_NETWORK), 
    gameLogicFunction: dapp, 
    rules: manifest.rules 
  })
  • start, no developer action expected:
await window.game.start()
  • connect - the script connects to the room and selects an available bankroller:
await game.connect({ playerDeposit: pDep })
  • play - this is the point where your play function is called and actual arguments are applied to your logic. Note that randomRanges and gameData are mandatory parameters, custom is optional, but it is strongly recommended to use it. If you decide to use it, all values have to comply with the SolidityTypeValue format (defined in dc-ethereum-utils/src/interfaces/IEth.ts). As you can see parameters are implemented as array objects allowing you to, e.g., define multiple bets in your game:
 const res = await game.play({
    userBets: [1],
    gameData: {
      // required
      randomRanges:[[1, 3], [1, 3], [1, 3]],
      // any your game data
      custom: { 
        playerNumbers:{t:'uint256', v:[1, 2, 3]} 
      }
    }
  })
  • disconnect - session ends either when the game ends and funds are distributed or when the Internet connection is lost.
window.game.disconnect()

General Game Session Life-Cycle Implemented by DApp

  1. A player calls the approve function of an ERC20 contract allowing the game contract to deposit a specific amount of player funds.
  2. Our DC-WEBAPI is initiated in the player browser by the game name via dc-messaging. It receives requests from available bankrollers and chooses the one with the largest balance to avoid a situation when a bankroller fails to support the bid.
  3. The player front-end application makes a request to the selected bankroller providing data and a private key signature.
  4. The bankroller checks the data and opens the channel.
  5. The game contract (muti-sig contract) freezes bankroller and player funds.
  6. The player and the bankroller play in an off-chain mode.
  7. The player ends the game session.
  8. The bankroller checks game data and closes the game channel. The game smart contract unfreezes and distributes funds between the player, the bankroller, the operator, the referrer and the game developer according to the existing settings.

Is this article helpful for you?