/
CloudScript.js
121 lines (103 loc) · 4.72 KB
/
CloudScript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// defining these up top so we can easily change these later if we need to.
var VIRTUAL_CURRENCY_CODE = "GM"; // currency code for our Gems VC
var PLAYER_REFERRAL_KEY = "Referrals"; // PlayerDataReadOnly key to retrieve referral information
var MAXIMUM_REFERRALS = 10; // this could also be set in TitleData
var REFERRAL_BONUS_BUNDLE = "premiumStarterPack"; // the item to grant to the requester
var REFERRAL_BADGE = "referralBadge"; // the itemId for the item that signifies that a player has been referred
// args.referralCode expected from Client
handlers.RedeemReferral = function(args) {
try{
if(args == null || typeof args.referralCode === undefined || args.referralCode === "")
{
throw "Failed to redeem. args.referralCode is undefined or blank";
}
else if(args.referralCode === currentPlayerId)
{
throw "You are not allowed to refer yourself.";
}
//Check the player's inventory to ensure that a "ReferralBadge" does not exist
var GetUserInventoryRequest = {
"PlayFabId": currentPlayerId
};
var GetUserInventoryResult = server.GetUserInventory(GetUserInventoryRequest);
for(var index in GetUserInventoryResult.Inventory)
{
if(GetUserInventoryResult.Inventory[index].ItemId === REFERRAL_BADGE)
{
throw "You are only allowed one Referral Badge.";
}
}
//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 = [];
if(!GetUserReadOnlyDataResult.Data.hasOwnProperty(PLAYER_REFERRAL_KEY))
{
// This was a valid referral code, but this is the first redeemed code.
referralValues.push(currentPlayerId);
ProcessReferrer(args.referralCode, referralValues);
}
else
{
// This was a valid referral code, now we need to extract the JSON array
referralValues = JSON.parse(GetUserReadOnlyDataResult.Data[PLAYER_REFERRAL_KEY].Value);
if(Array.isArray(referralValues))
{
// need to ensure we have not exceded the MAXIMUM_REFERRALS
if(referralValues.length < 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.";
}
}
// finally, reward the calling player
return GrantReferralBonus(args.referralCode);
} catch(e) {
var retObj = {};
retObj["errorDetails"] = "Error: " + e;
return retObj;
}
};
function ProcessReferrer(id, referrals)
{
// write back data to the referrer
var UpdateUserReadOnlyDataRequest = {
"PlayFabId": id,
"Data": {}
};
UpdateUserReadOnlyDataRequest.Data[PLAYER_REFERRAL_KEY] = JSON.stringify(referrals);
var UpdateUserReadOnlyDataResult = server.UpdateUserReadOnlyData(UpdateUserReadOnlyDataRequest);
// grant the reward to the referrer
var AddUserVirtualCurrencyRequest = {
"PlayFabId" : id,
"VirtualCurrency": VIRTUAL_CURRENCY_CODE,
"Amount": 10
};
var AddUserVirtualCurrencyResult = server.AddUserVirtualCurrency(AddUserVirtualCurrencyRequest);
log.info(AddUserVirtualCurrencyRequest.Amount + " " + VIRTUAL_CURRENCY_CODE + " granted to " + id);
}
function GrantReferralBonus(code)
{
var GrantItemsToUserRequest = {
"PlayFabId" : currentPlayerId,
"ItemIds" : [ REFERRAL_BADGE, REFERRAL_BONUS_BUNDLE ],
"Annotation" : "Referred by: " + code
};
var GrantItemsToUserResult = server.GrantItemsToUser(GrantItemsToUserRequest);
return GrantItemsToUserResult.ItemGrantResults;
}