Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grant Item to User #50

Open
itmhz2020 opened this issue Jul 17, 2020 · 3 comments
Open

Grant Item to User #50

itmhz2020 opened this issue Jul 17, 2020 · 3 comments

Comments

@itmhz2020
Copy link

I use this example and I want to mention for that:
JSON.parse(GetUserReadOnlyDataResponse.Data[CHECK_IN_TRACKER].Value) return string not object.
JSON.stringify(grantedItems) also return string.

After I resolve this issues all cloud script work but Grant times to user do nothing even I get the Item Id in result

@IvicaSkrobo
Copy link

How did you resolve it?

@itmhz2020
Copy link
Author

@IvicaSkrobo
This code after edit it works good
(*Notice: I delete the part that check if user login after 24 hours and less 48 cause I want to give the user the option any day he will login I will add one day)

// defining these up top so we can easily change these later if we need to.
var CHECK_IN_TRACKER = "CheckInTracker"; // used as a key on the UserPublisherReadOnlyData
var PROGRESSIVE_REWARD_TABLE = "ProgressiveDailyRewardTable"; // TitleData key that contains the reward details ProgressiveRewardTable
var PROGRESSIVE_MIN_CREDITS = "MinStreak"; // PROGRESSIVE_REWARD_TABLE property denoting the minium number of logins to be eligible for this item
var PROGRESSIVE_REWARD = "Reward"; // PROGRESSIVE_REWARD_TABLE property denoting what item gets rewarded at this level
var TRACKER_NEXT_GRANT = "NextEligibleGrant"; // CHECK_IN_TRACKER property containing the time at which we
var TRACKER_LOGIN_STREAK = "LoginStreak"; // CHECK_IN_TRACKER property containing the streak length

handlers.CheckIn = function(args) {

var GetUserReadOnlyDataRequest = {
    "PlayFabId": currentPlayerId,
    "Keys": [ CHECK_IN_TRACKER ]
}; 
var GetUserReadOnlyDataResponse = server.GetUserReadOnlyData(GetUserReadOnlyDataRequest);

// need to ensure that our data field exists
var tracker = {} ; // this would be the first login ever (across any title), so we have to make sure our record exists.
if(GetUserReadOnlyDataResponse.Data.hasOwnProperty(CHECK_IN_TRACKER))
{
    
	var Jtracker = JSON.parse(GetUserReadOnlyDataResponse.Data[CHECK_IN_TRACKER].Value);
	tracker = JSON.parse(Jtracker);
}
else
{
    
	tracker = ResetTracker();
	
	// write back updated data to PlayFab
	UpdateTrackerData(tracker);
	
	//grant items in first day
	// Get this title's reward table so we know what items to grant. 
	var GetTitleDataRequest = {
    	"Keys": [ PROGRESSIVE_REWARD_TABLE ]
    }; 
    var GetTitleDataResult = server.GetTitleData(GetTitleDataRequest);
    // parse our reward table
    var rewardTable = JSON.parse(GetTitleDataResult.Data[PROGRESSIVE_REWARD_TABLE]);
    var reward = rewardTable["Day1"][PROGRESSIVE_REWARD];
	// make grants and pass info back to the client.
	var grantedItems = [];
	grantedItems = GrantItems(reward);
	

	return JSON.stringify(grantedItems);

}


if(Date.now() > parseInt(tracker[TRACKER_NEXT_GRANT]))
{	
	// streak continues
	tracker[TRACKER_LOGIN_STREAK] += 1;
	var dateObj = new Date(Date.now());
	dateObj.setDate(dateObj.getDate() + 1);   //add 1 day
	tracker[TRACKER_NEXT_GRANT] = dateObj.getTime();

	// write back updated data to PlayFab
	var Utracker = JSON.stringify(tracker)
	UpdateTrackerData(Utracker);

	// Get this title's reward table so we know what items to grant. 
	var GetTitleDataRequest = {
    	"Keys": [ PROGRESSIVE_REWARD_TABLE ]
    }; 
    var GetTitleDataResult = server.GetTitleData(GetTitleDataRequest);
    
    // ---
    if(!GetTitleDataResult.Data.hasOwnProperty(PROGRESSIVE_REWARD_TABLE))
    {
    	log.error("Rewards table could not be found. No rewards will be given. Exiting...");
        return JSON.stringify([]);
    }
    else
    {
    	// parse our reward table
    	var rewardTable = JSON.parse(GetTitleDataResult.Data[PROGRESSIVE_REWARD_TABLE]);
    	
    	// find a matching reward 
    	var reward;
    	for(var level in rewardTable)
    	{
    		if( tracker[TRACKER_LOGIN_STREAK] >= rewardTable[level][PROGRESSIVE_MIN_CREDITS])
    		{
    			reward = rewardTable[level][PROGRESSIVE_REWARD];
    		}
    	}

    	// make grants and pass info back to the client.
		var grantedItems = [];
    	if(reward)
		{
			grantedItems = GrantItems(reward);
		}
		return JSON.stringify(grantedItems);
    }
}

return JSON.stringify([]);

};

function ResetTracker()
{
var reset = {};

reset[TRACKER_LOGIN_STREAK] = 1;

var dateObj = new Date(Date.now());
dateObj.setDate(dateObj.getDate() + 1 ); // add one day 
//dateObj.setMinutes(dateObj.getMinutes() + 2);

reset[TRACKER_NEXT_GRANT] = dateObj.getTime();
return JSON.stringify(reset);

}

function UpdateTrackerData(data)
{
var UpdateUserReadOnlyDataRequest = {
"PlayFabId": currentPlayerId,
"Data": {}
};
UpdateUserReadOnlyDataRequest.Data[CHECK_IN_TRACKER] = JSON.stringify(data);

server.UpdateUserReadOnlyData(UpdateUserReadOnlyDataRequest);

}

function GrantItems(items)
{
//var parsed = Array.isArray(items) ? items : [ items ];

var GrantItemsToUserRequest = {
    
    ItemIds : items,
    CatalogVersion : "ProgressiveRewards",
    PlayFabId : currentPlayerId
};

var GrantItemsToUserResult = server.GrantItemsToUser(GrantItemsToUserRequest);
return GrantItemsToUserResult.ItemGrantResults;

}

@IvicaSkrobo
Copy link

Thanks mate for answering. I figured it out as well yesterday, put the question up prematurely. But still it might help others. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants