Sign In Register

How can we help you today?

Start a new topic
Answered

How to update a different player's score from an event in cloud code?

 

 Hello, I tried to find a similar topic on this, but was unable to get there from the docs.  I believe I can load a different players' score, and submit a leaderboard request as follows:


var request = new SparkRequests.GetLeaderboardEntriesRequest();

    request.leaderboards = ["MatchScoreLeaderboard"];

    request.player = targetID;

 

    var response = request.Send();

    if( !response.error) {

        if(response.MatchScoreLeaderboard != undefined){

            //Spark.getLog().debug(response.MatchScoreLeaderboard);

            

            var score = response.MatchScoreLeaderboard.score;

            score = score +1;

        


        // now submit the new leaderboard score    

            var response2 = Spark.sendRequest({

             "@class": ".LogEventRequest",

             "eventKey": "LeaderboardScore",

             "score": score

            });

            //Spark.getLog().debug(response2);

        }

    }



However the default LeaderboardScore event submits a score for the authenticated player, not the target player.  How can I submit a leaderboard score event for a player that is different from the one who is currently logged in?  In this case if the attacking player loses, I want to get the defender a point on the leaderboard.


Any help would be appreciated!


Thanks.


Best Answer

Hi Curt, 


You should use a SparkRequest for submitting the score as the other player also, you can read about how to configure a LogEventRequest through Cloud Code here. You can go through my example below to help you get started.

   

var request = new SparkRequests.LogEventRequest();
request.yourEventKey = ...;			//the name of your event
request.yourEventAttribute = ...;  		//this will be your score attribute populated by your score var
var response = request.SendAs(playerID);	//supply the ID of the player you wish to send the event as here

//A few notes on how you can send events with a SparkRequest.

var response = request.Send();			//Will send the event as the current player without executing the Cloud Code for the given event

var response = request.SendAs(playerID);	//Will send the event for the supplied playerID without executing the Cloud Code for the given event

var response = request.Execute();		//Will send the event as the current player and Execute the Cloud Code for that event

var response = request.ExecuteAS(playerID);	//Will send the event for the supplied playerID and Execute the Cloud Code for the given event

      

Try that and let me know if you have any further questions.


Thanks,

Liam


Answer

Hi Curt, 


You should use a SparkRequest for submitting the score as the other player also, you can read about how to configure a LogEventRequest through Cloud Code here. You can go through my example below to help you get started.

   

var request = new SparkRequests.LogEventRequest();
request.yourEventKey = ...;			//the name of your event
request.yourEventAttribute = ...;  		//this will be your score attribute populated by your score var
var response = request.SendAs(playerID);	//supply the ID of the player you wish to send the event as here

//A few notes on how you can send events with a SparkRequest.

var response = request.Send();			//Will send the event as the current player without executing the Cloud Code for the given event

var response = request.SendAs(playerID);	//Will send the event for the supplied playerID without executing the Cloud Code for the given event

var response = request.Execute();		//Will send the event as the current player and Execute the Cloud Code for that event

var response = request.ExecuteAS(playerID);	//Will send the event for the supplied playerID and Execute the Cloud Code for the given event

      

Try that and let me know if you have any further questions.


Thanks,

Liam


1 person likes this

Ah, perfect. I missed the SendAs and ExecuteAs functionality in the docs. I'll give that a go. Thank you!

Hi Curt,


No problem, if you have any further questions just let me know.


Thanks,

Liam

Login to post a comment