Sign In Register

How can we help you today?

Start a new topic
Answered

Daily login bonus system, call script once a day

 

Hi,


We are implementing daily reward system for game.Its goes like this,

>User opens app > Script checks if user is logged in via Facebook > its calls gamesparks server to check if user is logged in first time >if not then it should count how many continuous day user has logged in > local script will then reward user with appropriate day.

So my question is, how can we store the user's last login time and then retrieve it via cloud code and checks number of days user logged in successively ? while all should be done in cloud code and only count of days should go to local script in game


Best Answer

Hi rd,


You'd do the last login in and days logged in successively in the AuthenticationResponse.


Here's a working last login script:


   

//We'll store the lastLogin in a variable as we'll be updating it now
var lastLogin = Spark.getPlayer().getScriptData("lastLogin");

//We can set the AuthenticationResponse scriptData with the lastLogin, this will show it up in the responses in Unity etc
Spark.setScriptData("lastLogin", lastLogin);

//Since we've logged in again and done everything we want to do with the old login data, we'll update the player's lastLogin
Spark.getPlayer().setScriptData("lastLogin", new Date());

   

For the daily login count, we store the lastLogin in epoch time so you can subtract the lastLogin from the current time and if the answer is less than 86400 they last logged in under 24 hours ago.


It would look something like this:

   

//Checking strict days in a row
var daysInARow = Spark.getPlayer().getScriptData("daysInARow");
var lastLogin = Spark.getPlayer().getScriptData("lastLogin");
var currentTime = new Date();
var timeSinceLastLogin = currentTime - lastLogin;

//Checking that timeSinceLastLogin is greater than 24 hours and less than 48 hours
if(timeSinceLastLogin > 86400 && timeSinceLastLogin < 172800){
	daysInARow++;
}

//If timeSinceLastLogin is greater than 2 days, reset their daysInARow
if(timeSinceLastLogin > 17280)
{
	daysInARow = 0;
}

Spark.getPlayer().setScriptData("daysInARow", daysInARow);

   

This might not be the best way to do it but it's a step in the right direction!


With that information being stored on the player's scriptData it will be available with response.ScriptData.GetInt("daysInARow");


Shane


Answer

Hi rd,


You'd do the last login in and days logged in successively in the AuthenticationResponse.


Here's a working last login script:


   

//We'll store the lastLogin in a variable as we'll be updating it now
var lastLogin = Spark.getPlayer().getScriptData("lastLogin");

//We can set the AuthenticationResponse scriptData with the lastLogin, this will show it up in the responses in Unity etc
Spark.setScriptData("lastLogin", lastLogin);

//Since we've logged in again and done everything we want to do with the old login data, we'll update the player's lastLogin
Spark.getPlayer().setScriptData("lastLogin", new Date());

   

For the daily login count, we store the lastLogin in epoch time so you can subtract the lastLogin from the current time and if the answer is less than 86400 they last logged in under 24 hours ago.


It would look something like this:

   

//Checking strict days in a row
var daysInARow = Spark.getPlayer().getScriptData("daysInARow");
var lastLogin = Spark.getPlayer().getScriptData("lastLogin");
var currentTime = new Date();
var timeSinceLastLogin = currentTime - lastLogin;

//Checking that timeSinceLastLogin is greater than 24 hours and less than 48 hours
if(timeSinceLastLogin > 86400 && timeSinceLastLogin < 172800){
	daysInARow++;
}

//If timeSinceLastLogin is greater than 2 days, reset their daysInARow
if(timeSinceLastLogin > 17280)
{
	daysInARow = 0;
}

Spark.getPlayer().setScriptData("daysInARow", daysInARow);

   

This might not be the best way to do it but it's a step in the right direction!


With that information being stored on the player's scriptData it will be available with response.ScriptData.GetInt("daysInARow");


Shane

Superb ...It worked.Thank Shane :)

 

My question has to do with the first part that rd Nation asked for, "Script checks if user is logged in via Facebook".  Is there a simple call to do that?  It looks like the NoSQL data is persisted in the External Authenication space, but I'm a bit new to the API so not quite sure how to access it.  It there an easy to way to check in cloud code?

To clarify, this would be on a device after the user already has logged in to Facebook.  *Trying to hide the "Connect To Facebook" button on a mobile game if they already are logged in*

Hi, Matt,


I don't think you will required any cloud code for that,


Here is what I done to check if user is logged in via Facebook,



>Make facebook login button add all required script like facebookAuthentication in it,

>Disable that object

>In another script add something like


if(FB.IsLoggedIn){

// disable fb button and activate other contents

}else{

//activate facebook log in button

}


Hope it helps you.

Login to post a comment