Tutorial: Build a game server 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.

  1. Fetch the template
  2. Set up database
  3. Add a casino
  4. What’s next?

Fetch the template

git clone https://github.com/moneypot/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.

    # .env
    DATABASE_URL=postgresql://app_postgraphile:xxx@localhost:5432/my_game
    SUPERUSER_DATABASE_URL=postgresql://my_superuser:xxx@localhost:5432/my_game
    

    The .env file should be considered secret. Ensure it’s in your .gitignore file so that you don’t accidentally commit it to your repo.

  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 each MoneyPot casino it should integrate with.

At the moment, there is only our flagship casino, moneypot.com.

This step is how you connect your hub server to a controller on the casino so that it can begin processing user deposits and withdrawals, and it can begin authenticating users on your experience frontend.

  1. Register at moneypot.com.
  2. Create a new controller at moneypot.com/me/developer/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.com
    • Base URL: https://moneypot.com (This is the origin of the casino URL that will <iframe> our experience frontend)
    • GraphQL URL: https://api.moneypot.com/graphql
    • 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?

TODO: The app.coinflip_bet example is outdated since it can be implemented with the makeOutcomebet endpoint without any custom database / plugin logic

Now that you have a basic hub 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. Visit other pages of this controller dev section 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;