question

sgarg76271 avatar image
sgarg76271 asked

Difficulty in understanding Recipe code

Can someone please help me understand this part of recipe referral Code with Unity and Playfab.

This is a segment from the cloud script. Can someone better comment it so I may understand?


        //Before proceeding, validate the provided referral code. Use a Try / Catch block if you want to do more before returning the error to the client.




        var GetUserReadOnlyDataRequest = {
            "PlayFabId": args.referralCode,
            "Keys": [ PLAYER_REFERRAL_KEY ]
        }; 
        var GetUserReadOnlyDataResult = server.GetUserReadOnlyData(GetUserReadOnlyDataRequest);
        var referralValues = [];




-----------------What is above segment doing? ---------------



        if(!GetUserReadOnlyDataResult.Data.hasOwnProperty(PLAYER_REFERRAL_KEY))
        {
            // This was a valid referral code, but this is the first redeemed code.


------------------What does the above comment mean?? --------------




            referralValues.push(currentPlayerId);
            ProcessReferrer(args.referralCode, referralValues);
        }
        else
        {
            // This was a valid referral code, now we need to extract the JSON array


--------------Why is this a json array ? What Data are we retrieving here? ----------



            referralValues = JSON.parse(GetUserReadOnlyDataResult.Data[PLAYER_REFERRAL_KEY].Value);
            if(Array.isArray(referralValues))
            {
                // need to ensure we have not exceded the MAXIMUM_REFERRALS
                 {
                    // here we know that the referrer has not exceeded the max, so we will add the current player 
                    referralValues.push(currentPlayerId);
                    ProcessReferrer(args.referralCode, referralValues);
                }
                else
                {
                    // Referrer has exceeded the MAXIMUM_REFERRALS
                    // this is not an error, but the referrer does not get thier reward.
                    log.info("Player:" + args.referralCode + " has hit the maximum number of referrals (" + MAXIMUM_REFERRALS + ")." );
                }
            }
            else
            {
                throw "An error occured when parsing the referrer's player data.";
            }
        }




----------The below code is obvious , but what baout the above code? ----------

        // finally, reward the calling player
        return GrantReferralBonus(args.referralCode);
    } catch(e) {
        var retObj = {};
        retObj["errorDetails"] = "Error: " + e;
        return retObj;
    }
};


CloudScript
10 |1200

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

1 Answer

·
brendan avatar image
brendan answered

So in this sample, the referral key entered is the PlayFab ID of the user you're saying referred you. The first part of the code above gets the array of referrals for that player (the person who referred you). If that Key doesn't exist yet, that just means you're the first person saying the person referred you. This is specifically being done so that a player can't be rewarded more than MAXIMUM_REFERRALS times.

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.