Daily Rewards #
A common way of engaging and retaining players is to offer them a daily reward for logging into your game each day.
In this example, you’ll learn how to implement a daily reward system using Server Runtime Code.
Prerequisites #
To easily follow with this tutorial, perform the following before proceeding:
- Download the Nakama Project Template or
- Follow the TypeScript, Go, or Lua setup guide
Registering the RPCs #
The daily reward sample defines two RPCs (Remote Procedure Calls) to check eligibility and issue the reward.
The RPCs are then registered to Nakama events in either main.ts
, main.go
or main.lua
.
|
|
|
|
|
|
To register an RPC with the server, you need to specify a string identifier as well as a function to run when the RPC is called by a client.
Implementing the RPCs #
The RPCs implement the following logic:
canClaimDailyReward
#
- Get the latest daily reward object from the Nakama Storage Engine
- Check to see if the last time the user claimed a reward was before 00:00
- Return a JSON response indicating if the user can claim a daily reward
claimDailyReward
#
- Get the latest daily reward object from the Nakama Storage Engine
- Check to see if the last time the user claimed a reward was before 00:00
- Update the user’s Wallet
- Send a Notification to the user
- Update or create the daily reward object in the Nakama Storage Engine
- Return a JSON response with the number of coins received
Module code #
This section is specifically for people using Go or Lua. There is some additional code you will need to include in your daily reward module scripts for each respective language.
|
|
|
|
Getting the last daily reward object #
Let’s take a look at the code for retrieving the latest daily reward object from the Nakama Storage Engine.
|
|
|
|
|
|
Regardless of the language you use, the core logic remains the same.
- Check the context to ensure there is a valid user ID
- Check the user did NOT pass anything in the payload
- Query the storage engine for a
daily
object in thereward
collection - Return the daily reward object or a default one
Checking if the user is eligible to receive a daily reward #
|
|
|
|
|
|
This function checks the last claim Unix timestamp value of the daily reward object. If it is less than the timestamp for midnight of the previous day, it returns true, otherwise it returns false.
CanClaimDailyReward RPC #
With the two helper functions complete it’s time to implement the first of the RPCs. This RPC will return the value of the helper function that checks the user’s eligibility as a JSON object.
|
|
|
|
|
|
ClaimDailyReward RPC #
This RPC will ensure the user is eligible to receive the daily reward, update the user’s Wallet, send out a notification and then update the user’s daily reward in the Storage Engine.
|
|
|
|
|
|
Exploring in the Nakama Console #
Spin up your server (using Docker) and test the RPCs using the Nakama Console.
You can access the Nakama Console by opening a browser and going to http://localhost:7351.
Once there, you can try interacting with your RPCs via the API Explorer by selecting them from the dropdown and specifying a user ID as a context.
Wrap Up #
With those two RPCs implemented you now have a simple daily reward system, congratulations!
Feel free to experiment further by adding more complicated eligibility criteria or other such features.