Rewards API
Integration of rewards through SDK. Methods of operation.
List of methods
Actions:
gp.rewards.give()
- give a reward. +0-1 Requestgp.rewards.accept()
- accept the reward and accrue bonuses. FREE
Properties:
gp.rewards.list
- list of rewards. FREEgp.rewards.givenList
- list of given rewards. FREEgp.rewards.getReward()
- get information about a reward by ID or tag. FREE
Checks:
gp.rewards.has()
- check if the reward has been given. FREEgp.rewards.hasAccepted()
- check if the reward has been accepted. FREEgp.rewards.hasUnaccepted()
- check if there is a given, but not accepted reward. FREE
Events:
gp.rewards.on('give')
- subscribe to the giving of a reward.gp.rewards.on('error:give')
- subscribe to an error when giving a reward.gp.rewards.on('accept')
- subscribe to the acceptance of a reward.gp.rewards.on('error:accept')
- subscribe to an error when accepting a reward.
Types:
Reward
- reward fields.PlayerReward
- player's reward fields.
Rules for working with rewards
Procedure for working with rewards:
- Give the player a reward. Manually or through triggers.
- Accept the reward. Automatically or manually, for example by clicking on the "receive" button.
- Bonuses are automatically credited.
- Save the player.
As we ensure consistency of data and changes in player variables, there is no need to stick to strict rules as when consuming purchases.
You can designate different types of rewards for yourself, for example: one-time and permanent.
Example of a one-time reward:
- Login reward
- Starter kit
- Premium chest
- Achievement reward
Such rewards can be accepted immediately or left for manual action by the player.
Rewards are automatically credited upon acceptance. However, if you want to add non-standard changes to variables, you can make them by subscribing to the accepted
event.
For example, you need to:
- Add an item to the inventory
- Change data in a JSON structure
- Perform other manipulations not related to the player or not supported by the control panel.
Example of a permanent reward:
- Unique sword / skin / car
- VIP status / advertising off
- DLC / levels / islands and other unlockable content
Permanent rewards do not need to be accepted. Once the reward has been given, it will always be visible to the user.
Rules for conducting a one-time reward with automatic acceptance
Basic
- JavaScript
- Unity
// 1. Give the reward
await gp.rewards.give({ tag: 'STARTER_PACK' });
// 2. The reward acceptance will be triggered automatically
// 3. The reward will be credited
// 4. Save the player on the server
await gp.player.sync();
// 1. Give the reward
GP_Rewards.Give('STARTER_PACK');
// 2. The reward acceptance will be triggered automatically
// 3. The reward will be credited
// 4. Save the player after the reward has been accrued
With non-standard data changes
Using adding health and mana potions to the inventory as an example:
- JavaScript
- Unity
// 1. Subscribe to the reward acceptance event
gp.rewards.on('accept', ({ reward }) => {
switch (reward.tag) {
case 'STARTER_PACK': {
// 2. If necessary, add additional rewards
inventory.push({ item: 'HP_POTION', amount: 100 });
inventory.push({ item: 'MP_POTION', amount: 100 });
gp.player.set('inventory', JSON.stringify(inventory));
break;
}
}
// 3. If necessary, update the UI to display variable changes
myGame.refreshUI();
});
// 4. Give the reward
await gp.rewards.give({ tag: 'STARTER_PACK' });
// 5. The reward acceptance will be triggered automatically
// 6. The reward will be credited
// 7. The accept event will be called
// 8. Save the player on the server
await gp.player.sync();
// 1. Subscribe to the reward acceptance event
private void OnEnable() => GP_Rewards.OnRewardsAccept += OnAccept;
public void OnAccept(AllRewardData result)
{
switch (result.reward.tag) {
case "STARTER_PACK": {
// 2. If necessary, add additional rewards
break;
}
}
}
// 3. Give the reward
GP_Rewards.Give("STARTER_PACK");
// 4. The reward acceptance will be triggered automatically
// 5. The reward will be credited
// 6. The accept event will be called
Receiving rewards when starting the game
Sometimes it may happen that the player did not have time to receive the rewards and save the progress, for example:
- Closed / restarted the tab, browser or application
- Turned off / broke the device
- The game crashed.
They need to be received upon entering the game.
All unreceived rewards with automatic acceptance enabled are automatically accepted by the SDK at start-up.
Important. We don't know when the game is fully loaded and you're ready to process the accruals. Therefore, call the game start method gp.gameStart()
when you are ready to receive rewards. After calling the game start, we will award the rewards, and you can subscribe to the reward acceptance event in advance.
If the reward does not have automatic acceptance, you can manually receive the rewards you need at game start:
- JavaScript
- Unity
// If manual reward processing is required,
// you must !! first !! subscribe to the accept event
gp.rewards.on('accept', ({ reward }) => {
// Actions when receiving a reward
});
const rewardsToAcceptOnStartup = [
'STARTER_PACK',
'LEVEL_10',
'LEVEL_20',
//...
];
rewardsToAcceptOnStartup.forEach((rewardTag) => {
const { reward, playerReward } = gp.reward.getReward(rewardTag);
// Such a reward does not exist
if (!reward) {
return;
}
// Get the number of unreceived rewards
const unacceptedRewardsLeft = playerReward.countTotal - playerReward.countAccepted;
// Accept rewards
for (let i = 0; i < unacceptedRewardsLeft; i++) {
gp.reward.accept(reward.id);
}
});
// If manual reward processing is required,
// you must !! first !! subscribe to the accept event
private void OnEnable() => GP_Rewards.OnRewardsAccept += OnAccept;
public void OnAccept(AllRewardData result)
{
// Actions when receiving a reward
}
string[] rewardsToAcceptOnStartup = {
"STARTER_PACK",
"LEVEL_10",
"LEVEL_20",
//...
};
foreach(string rewardTag in rewardsToAcceptOnStartup)
{
AllRewardData data = GP_Rewards.GetReward(rewardTag);
// Such a reward does not exist
if (!data.reward) {
return;
}
// Get the number of unreceived rewards
const unacceptedRewardsLeft = data.playerReward.countTotal - data.playerReward.countAccepted;
// Accept rewards
for (int i = 0; i < unacceptedRewardsLeft; i++) {
GP_Rewards.Accept(data.reward.id);
}
}
Rules for conducting a permanent reward
- JavaScript
- Unity
// 1. Give the reward
await gp.rewards.give({ tag: 'NOOB_SKIN' });
// 2. Check for the presence of the reward at the right time
if (gp.rewards.has('NOOB_SKIN')) {
// do something when you have the noob skin.
}
// 1. Give the reward
GP_Rewards.Give('NOOB_SKIN');
// 2. Check for the presence of the reward at the right time
if (GP_Rewards.Has('NOOB_SKIN')) {
// do something when you have the noob skin.
}
Actions
Give reward
+1 RequestTo give a player a reward, you need to pass the ID or tag of the reward.
- JavaScript
- Unity
// By ID
gp.rewards.give({ id: 123 });
// By tag
gp.rewards.give({ tag: 'STARTER_PACK' });
// By ID
GP_Rewards.Give(123);
// By tag
GP_Rewards.Give('STARTER_PACK');
You can give a reward with delayed saving. It will be saved together with the player's save.
FREE- JavaScript
- Unity
gp.rewards.give({ id: 123, lazy: true });
GP_Rewards.Give(123, true);
The method returns the reward and player's reward:
- JavaScript
- Unity
const { reward, playerReward } = await gp.rewards.give({ id: 123 });
Implemented in Unity via event subscription
Accept reward
FREEAfter giving a reward, it can be accepted and variable changes credited.
- JavaScript
- Unity
// Accept by ID
gp.rewards.accept({ id: 123 });
// Accept by tag
gp.rewards.accept({ tag: 'STARTER_PACK' });
// Accept by ID
GP_Rewards.Accept(123);
// Accept by tag
GP_Rewards.Accept('STARTER_PACK');
The method returns the reward and player's reward:
- JavaScript
- Unity
const { reward, playerReward } = await gp.rewards.accept({ id: 123 });
Implemented in Unity via event subscription
Properties
List of rewards
FREEYou have access to the entire list of rewards at once when the game starts. See reward fields.
- JavaScript
- Unity
gp.rewards.list.forEach((reward) => {
// reward.id
// reward.tag
// reward.name
// reward.description
// reward.isAutoAccept
// reward.mutations
});
RewardData[] rewardsData = GP_Rewards.List();
foreach (RewardData data in rewardsData)
{
Debug.Log("ID: " + data.id);
Debug.Log("Tag: " + data.tag);
Debug.Log("Name: " + data.name);
Debug.Log(" ");
}
List of given rewards
FREEYou have access to the entire list of awarded rewards at once when the player is ready. See player's reward fields.
- JavaScript
- Unity
gp.rewards.givenList.forEach((playerReward) => {
// playerReward.rewardId
// playerReward.countTotal
// playerReward.countAccepted
});
PlayerReward[] playerRewards = GP_Rewards.GivenList();
foreach (PlayerReward data in playerRewards)
{
Debug.Log("Reward ID: " + data.rewardId);
Debug.Log("Count Total: " + data.countTotal);
Debug.Log("Count Accepted: " + data.countAccepted);
}
Getting information about a reward
FREEThe method returns the reward and player's reward:
- JavaScript
- Unity
// By ID
const { reward, playerReward } = gp.rewards.getReward(123);
// By tag
const { reward, playerReward } = gp.rewards.getReward('STARTER_PACK');
// The reward may not exist, make sure it does
if (reward) {
console.info(reward.id, playerReward.countTotal);
}
// By ID or tag
AllRewardData allData = GP_Rewards.GetReward('STARTER_PACK');
RewardData data = allData.reward;
Debug.Log("ID: " + data.id);
Debug.Log("Tag: " + data.tag);
Debug.Log("Name: " + data.name);
Debug.Log("Description: " + data.description);
Debug.Log("Icon: " + data.icon);
Debug.Log("Icon small: " + data.iconSmall);
Debug.Log("is Auto Accept: " + data.isAutoAccept);
Debug.Log("Mutations: ");
foreach (DataMutation mutation in data.mutations)
{
Debug.Log(" Type: " + mutation.type);
Debug.Log(" Key: " + mutation.key);
Debug.Log(" Action: " + mutation.action);
Debug.Log(" Value: " + mutation.value);
}
Checks
Reward given
gp.rewards.has(idOrTag)
FREE
- JavaScript
- Unity
// By ID
const hasReward = gp.rewards.has(123);
// By Tag
const hasReward = gp.rewards.has('STARTER_PACK');
// Check
if (hasReward) {
// The STARTER_PACK reward exists
}
// By ID
bool hasReward = GP_Rewards.Has(123);
// By Tag
bool hasReward = GP_Rewards.Has('STARTER_PACK');
// Check
if (hasReward) {
// The STARTER_PACK reward exists
}
Reward accepted
gp.rewards.hasAccepted(idOrTag)
FREE
- JavaScript
- Unity
// By ID
const hasAcceptedReward = gp.rewards.hasAccepted(123);
// By Tag
const hasAcceptedReward = gp.rewards.hasAccepted('STARTER_PACK');
// Check
if (hasAcceptedReward) {
// The STARTER_PACK reward has been accepted
}
// By ID
bool hasAcceptedReward = GP_Rewards.HasAccepted(123);
// By Tag
bool hasAcceptedReward = GP_Rewards.HasAccepted('STARTER_PACK');
// Check
if (hasAcceptedReward) {
// The STARTER_PACK reward has been accepted
}
Reward given but not accepted
gp.rewards.hasUnaccepted(idOrTag)
FREE
- JavaScript
- Unity
// By ID
const hasUnacceptedReward = gp.rewards.hasUnaccepted(123);
// By Tag
const hasUnacceptedReward = gp.rewards.hasUnaccepted('STARTER_PACK');
// Check
if (hasUnacceptedReward) {
// The STARTER_PACK reward has been given but not accepted
}
// By ID
bool hasUnacceptedReward = GP_Rewards.HasUnaccepted(123);
// By Tag
bool hasUnacceptedReward = GP_Rewards.HasUnaccepted('STARTER_PACK');
// Check
if (hasUnacceptedReward) {
// The STARTER_PACK reward has been given but not accepted
}
Events
Reward given
The callback returns the reward and player reward:
- JavaScript
- Unity
gp.rewards.on('give', ({ reward, playerReward }) => {
// The reward and player reward are available
});
//Subscribe to event
private void OnEnable()
{
GP_Rewards.OnRewardsGive += OnGive;
}
//Unsubscribe from event
private void OnDisable()
{
GP_Rewards.OnRewardsGive -= OnGive;
}
//Process the event
public void OnGive(AllRewardData result)
{
Debug.Log("Give reward: " + result.reward);
Debug.Log("Player reward: " + result.playerReward);
}
Failed to give reward
The callback returns an error. See error codes:
- JavaScript
- Unity
gp.rewards.on('error:give', (err) => {
// Handle errors
});
//Subscribe to event
private void OnEnable()
{
GP_Rewards.OnRewardsGiveError += OnGiveError;
}
//Unsubscribe from event
private void OnDisable()
{
GP_Rewards.OnRewardsGiveError -= OnGiveError;
}
//Process the event
public void OnGiveError(string error)
{
Debug.Log("Give error: " + error);
}
Reward accepted
The callback returns the reward and player reward:
- JavaScript
- Unity
gp.rewards.on('accept', ({ reward, playerReward }) => {
// The reward and player reward are available
});
//Subscribe to event
private void OnEnable()
{
GP_Rewards.OnRewardsAccept += OnAccept;
}
//Unsubscribe from event
private void OnDisable()
{
GP_Rewards.OnRewardsAccept -= OnAccept;
}
//Process the event
public void OnAccept(AllRewardData result)
{
Debug.Log("Accept reward: " + result.reward);
Debug.Log("Player reward: " + result.playerReward);
}
Failed to accept reward
The callback returns an error. See error codes:
- JavaScript
- Unity
gp.rewards.on('error:accept', (err) => {
// Handle errors
});
//Subscribe to event
private void OnEnable()
{
GP_Rewards.OnRewardsAcceptError += OnAcceptError;
}
//Unsubscribe from event
private void OnDisable()
{
GP_Rewards.OnRewardsAcceptError -= OnAcceptError;
}
//Process the event
public void OnAcceptError(string error)
{
Debug.Log("Accept error: " + error);
}
Types
Reward fields
Field | Type | Description | Example |
---|---|---|---|
id | number | ID of the reward | 115 |
tag | string | Tag to help select. You can use it instead of ID | VIP |
name | string | Name translated into the user's language | VIP status |
description | string | Description, translated into the user's language | Ad-free, x2 reward |
icon | string | Link to an icon with a size of 256x256 | Example link |
iconSmall | string | Link to an icon with a size of 64x64 | Example link |
mutations | DataMutation[] | List of variable changes | [] |
isAutoAccept | boolean | Auto-accepting reward | true |
Player reward fields
Field | Type | Description | Example |
---|---|---|---|
rewardId | number | Reward ID | 115 |
countTotal | number | Number of given rewards | 23 |
countAccepted | number | Number of accepted awards | 12 |
Data mutation fields
DataMutation
Field | Type | Description | Example |
---|---|---|---|
type | MutationType | Mutation type | PLAYER_FIELD |
key | string | Variable key for mutation | score |
action | MutationAction | Type of action in mutation | ADD |
value | number / string / boolean | Changed value | 100 |
Structure example:
const mutation: DataMutation = {
type: MutationType.PLAYER_FIELD,
key: 'score',
action: MutationAction.ADD,
value: 100,
};
// Will be equivalent to:
gp.player.add('score', 100);
Data mutation types
MutationType
Type | Description |
---|---|
PLAYER_FIELD | Player field mutation |
Mutation action types for data mutation fields
MutationAction
Type | Description |
---|---|
ADD | Adding a value to a field |
REMOVE | Subtracting from a field / deleting the field |
SET | Setting the value of a field |
Error codes
Error | Error description |
---|---|
player_not_found | Player not found |
empty_id_or_tag | Empty reward ID or tag |
reward_not_found | Reward with this ID or tag was not found |
undefined | An unforeseen error occurred (see console) |
Stay in Touch
Other documents of this chapter available Here. To get started, welcome to the Tutorials chapter.
GamePush Community Telegram
: @gs_community.
For your suggestions e-mail
: [email protected]
We Wish you Success!