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.
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
- Ensure postgres is installed and running locally.
-
Create a new database for our game server.
On Linux or macOS, you can just run this from the terminal:
createdb my_game
-
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.
-
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>';
-
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. -
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.
- Visit http://localhost:4000/graphql to view and play with the GraphQL API
-
Visit http://localhost:4000/dashboard to view the admin dashboard
To get an api key, run the sql
insert into hub.api_key default values returning key
.
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.
- Register at moneypot.com.
- Create a new controller at moneypot.com/me/developer/controllers.
- Create a new API key for that controller and note the API key.
- Go to your hub server’s admin dashboard: http://localhost:4000/dashboard.
- 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>
- Name:
- If your hub server successfully authenticates with the casino server, the casino should now show up in your casino list.
-
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);
- 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 themakeOutcomebet
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:
- Visit other pages of this controller dev section to learn more about how the underlying @moneypot/hub server works and how to extend it.
- Add a migration like
003-my-bet-table.sql
where you add your own betting system along with your ownsrc/plugins/my-bet-plugin.ts
where you implement a graphql mutation. -
Add a migration to drop the coinflip table:
003-drop-coinflip.sql
:DROP TABLE app.coinflip_bet CASCADE;