question

drew avatar image
drew asked

How to avoid giving a player the same reward twice?

hi,

Let's say I am using EconomyV2 to gift players a daily reward. I have some Azure Function called ClaimDailyReward() which looks something like this:

 didUserClaimReward = GetRewardHistory(DateTime.Now.Date);
    
 if(!didUserClaimReward)
 {
      GiveRewardWithEconomyV2();
        
     //if this call fails to update the user data, the user will get a second daily reward
      UpdateRewardHistory(DateTime.Now.Date);
 }

I track if I gave the user a reward using player title data, what happens if that update fails? The user already got his reward. Is there some clever trick, like putting some item in the reward with the days date or something to get around this? Just curious if you have ideas for how to handle this scenario

In-Game Economy
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

kylemc@microsoft.com avatar image
kylemc@microsoft.com answered

The best way to achieve your goal is to leverage the IdempotencyId in the request body. If you set it to the same value (maybe a hash of UserId and Date or something), the backend will make sure the reward is only added a single time.

https://learn.microsoft.com/en-us/rest/api/playfab/economy/inventory/add-inventory-items?view=playfab-rest#request-body

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Neils Shi avatar image
Neils Shi answered

Since PlayFab's services are based on the Rest API, the API call may fail for some reason at some times. If you are concerned that the UpdateRewardHistory() method call fails, and it causes the daily reward to be awarded to the player twice, then you can add some error handling mechanisms, for example you can implement a retry mechanism, if it fails, you can try again a certain number of times. In addition, you can include a unique item in the reward as you mentioned, then calling API GetTransactionHistory to retrieve all the items that added to the player's inventory today and check if the item is included in it, but we don't recommend as it can be cumbersome.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.