Tutorial: Build a controller from our template

We offer a controller template github.com/moneypot/controller-template as a starting point for building your own game on top of the @moneypot/hub server library.

The template implements useful boilerplate code but also an example coinflip game that you can use as a reference for your own graphql plugins and database modifications.

Fetch the template

git clone https://github.com/moneypot/controller-template
# Rename the folder to your project name
mv controller-template my-game
cd my-game
npm install

For the rest of this document, whenever you see my-game, replace it with your project name.

Set up database

  1. Ensure postgres is installed and running locally.

  2. Create a new database for our game server.

    On Linux or macOS, you can just run this from the terminal:

    createdb my_game
    
  3. Create an app_postgraphile postgres user with a secure password (username is important here):

    CREATE ROLE app_postgraphile LOGIN PASSWORD '<todo: secure password>';
    -- Or you can update the password if it already exists
    ALTER ROLE app_postgraphile PASSWORD '<todo: secure password>';
    
    app_postgraphile should not be a superuser. This is the postgres user that our GraphQL executor will use to run queries, so we want it to obey RLS authorization.
  4. Ensure there's also a user with superuser access to the database (username not important):

    CREATE ROLE my_superuser WITH LOGIN SUPERUSER PASSWORD '<todo: secure password>';
    
  5. Rename file .env.example to .env and update it.

    DATABASE_URL=postgresql://app_postgraphile:xxx@localhost:5432/my_game
    SUPERUSER_DATABASE_URL=postgresql://my_superuser:xxx@localhost:5432/my_game
    
  6. Run the server which will create the database schema and begin serving requests:

    npm run dev
    # Listening on port 4000
    

You'll notice that the .sql files in the automigrations folder were executed and the sample app.coinflip_bet table was created.

Add a casino

You need to tell your hub server about every Moneypot casino it should integrate with.

For development purposes, we operate moneypot.dev as a public Moneypot casino.

  1. Register at moneypot.dev.

  2. Create a new controller at moneypot.dev/me/controllers.

  3. Create a new API key for that controller and note the API key.

  4. Go to your hub server's admin dashboard: http://localhost:4000/dashboard.

  5. Click "Add casino" and fill in:

    • Name: moneypot.dev
    • Base URL: https://moneypot.dev (This is the origin of the casino URL that will <iframe> our experience frontend)
    • GraphQL URL: https://api.moneypot.dev
    • API key: <paste this from Step #3>
  6. If your hub server successfully authenticates with the casino server, the casino should now show up in your casino list.

  7. In your hub dashboard, go to the casino's "Bankrolls" tab and add a bankroll for the test HOUSE currency so that our server can begin accepting user bets. )

    You could do it manually with:

    insert into hub.bankroll (casino_id, currency_key, amount)
    values ('<casino_id>', 'HOUSE', 1000000);
    
  8. TODO: Document that we need to fund our controller on the casino website before users can withdraw. And we should be able to manage the controller balance from the dashboard.

Now when you start your hub server, it will begin polling for user transfers from each casino. When users deposit currency into our experience, the controller will receive a hub.deposit and update the hub.balance table.

What's next?

Now that you have a basic controller server running, notice that it comes with a demo app.coinflip_bet table migration along with a plugin that implements a makeCoinflipBet graphql mutation.

Here are some places you can go from here:

  1. Start reading the hub manual to learn more about how the underlying @moneypot/hub server works and how to extend it.

  2. Add a migration like 003-my-bet-table.sql where you add your own betting system along with your own src/plugins/my-bet-plugin.ts where you implement a graphql mutation.

  3. Add a migration to drop the coinflip table: 003-drop-coinflip.sql:

    DROP TABLE app.coinflip_bet CASCADE;