Dev Quickstart
What you’ll learn
We’re going to build a simple leaderboard module from scratch. Users will be able to authenticate, submit leaderboard scores, and retrieve the top scores.
This will teach you how to build a simple leaderboard module from scratch & all related Open Game Backend concepts including:
- Create a module
- Setup your database
- Write scripts
- Write a test
Make sure you’ve installed Open Game Backend as described here.
Prerequisites
Step 1: Create project
Create a new directory. Open a terminal in this directory.
Run the following command:
Step 2: Create module
In the same terminal, run:
See documentation on the module.json
config here.
Step 3: Write database schema
Edit your modules/my_leaderboard/db/schema.prisma
file to look like this:
For more information about writng database schemas, see Prisma’s documentation on data modelling.
Open Game Backend is powered by PostgreSQL. Learn about why we chose PostgreSQL here.
Step 4: Write submit_score
script
We’re going to create a submit_score
script that will:
- Throttle requests
- Validate user token
- Insert score
- Query score rank
Create script
In the same terminal, run:
Add dependencies & make public
Update the modules/my_leaderboard/module.json
file to look like this:
Update request & response
Open modules/my_leaderboard/scripts/submit_score.ts
and update Request
and Response
to look like this:
Throttle requests
At the top the run
function in modules/my_leaderboard/scripts/submit_score.ts
file, add code to throttle requests:
Validate user token
Then authenticate the user:
Insert score
Then insert the score in to the database:
Query rank
Finally, query the score’s rank:
For more information about querying databases, see Prisma’s documentation on querying data.
Step 5: Create get_top_scores
script
Create script
In the same terminal, run:
Make public
Open modules/my_leaderboard/module.json
and update the get_top_scores
script to be public:
Update request & response
Open modules/my_leaderboard/scripts/get_top_scores.ts
and update Request
and Response
to look like this:
Throttle requests
At the top the run
function in modules/my_leaderboard/scripts/get_top_scores.ts
file, add code to throttle the requests:
Query scores
Then query the top scores:
Convert rows
Finally, convert the database rows in to Score
objects:
Step 6: Start development server
Start development server
In the same terminal, run:
Migrate database
You will be prompted to apply your schema changes to the database. Name the migration init (this name doesn’t matter):
Success
You’ve now written a full module from scratch. You can now generate an SDK to use in your game or publish it to a registry.
Step 7 (optional): Test module
Tests are helpful for validating your module works as expected before running in to the issue down the road. Testing is optional, but strongly encouraged.
Create test
e2e
stands for “end to end” test. E2E tests simulate real-world scenarios involving multiple parts of a system. These tend to be comprehensive and catch the most bugs.
Write test
Update modules/my_leaderboard/tests/e2e.ts
to look like this:
Run test
In the same terminal, run:
You should see this output once complete: