Sign In Register

How can we help you today?

Start a new topic
Answered

Reduce scores in a leaderboard?

Hi!
I'm making a game about steal value("flags"), and I wanted to make a leaderboard about how much flags players have.


Since other players can steal you your "flags" I need to update that leaderboard evend with lower "scores". But for now my leaderboard only update when I submit a higher score.


I wonder if there's any way to use leaderboard this way, if not I would have to somehow compare all players flags and order them, but I dont know if it would be very efficient :/


Best Answer

Hi Ricardo,


Yes you can do this with a event that tracks a score attribute with it's Default Calculation set to SUM. If you set up your event and leaderboard like below I'll explain how it works.


The event has a single score attribute with a Default Calc set to "SUM". You can call score "flags" here if you wish



Next our Leaderboard is set up to track this attribute in Real Time.



Now you can log in as a user in the Test Harness and test you event. You can send the following.


{

 "@class": ".LogEventRequest",

 "eventKey": "Sum_Score",

 "score": 100

}


Now navigate to the Manage Screen and click the leaderboards tab to view the entries on our new created leaderboard. You should see 100 for the current player. Back in the Test Harness you can now send the following.


{

 "@class": ".LogEventRequest",

 "eventKey": "Sum_Score",

 "score": -50

}


This will remove 50 from the players score on the leaderboard, if you refresh the leaderboard you should now the see the test user has a score of 50. Try that and let me know if you have any further questions.


Thanks,

Liam


Answer

Hi Ricardo,


Yes you can do this with a event that tracks a score attribute with it's Default Calculation set to SUM. If you set up your event and leaderboard like below I'll explain how it works.


The event has a single score attribute with a Default Calc set to "SUM". You can call score "flags" here if you wish



Next our Leaderboard is set up to track this attribute in Real Time.



Now you can log in as a user in the Test Harness and test you event. You can send the following.


{

 "@class": ".LogEventRequest",

 "eventKey": "Sum_Score",

 "score": 100

}


Now navigate to the Manage Screen and click the leaderboards tab to view the entries on our new created leaderboard. You should see 100 for the current player. Back in the Test Harness you can now send the following.


{

 "@class": ".LogEventRequest",

 "eventKey": "Sum_Score",

 "score": -50

}


This will remove 50 from the players score on the leaderboard, if you refresh the leaderboard you should now the see the test user has a score of 50. Try that and let me know if you have any further questions.


Thanks,

Liam


1 person likes this

Great! That make it!
Just one more little question, how can player A submit a score for player B?


So when A steal from B I want to submit for both, I'm trying this using the player B ID:

 

var targetID = Spark.getData().targetID;
if (targetID == 0)
    {
    targetID = Spark.getPlayer().getPlayerId();
    }
    
//Also get the new data to be saved
var flagsAmount = Spark.getData().flagsAmount;

//Load the collection where data is stored
var targetData = Spark.runtimeCollection("targetData"); 

//Construct a data collection with the new data to replace the first one(or create it if it does not exist)
var currentPlayer =
    {
    "flagsAmount": flagsAmount
    }
    
targetData.update
    (
        {
        "playerID": targetID
        }, //Looks for a doc with the id of the current player
        
        {
        "$set": currentPlayer
        }, // Uses the $set mongo modifier to set old player data to the current player data
        
        true, // Create the document if it does not exist (upsert)
        false // This query will only affect a single object (multi)
    );

 It changes player B flags value, but in the leaderboard only change for player A

Hi Ricardo you can use a SparkRequest to send a request as another player. 

 

//Also get the new data to be saved
var flagsAmount = Spark.getData().flagsAmount;

var request = new SparkRequests.LogEventRequest();
request.eventKey = "Sum_Score";
request.score = -flagsAmount;

//playerID here being the player you want to subtract the amount from
var response = request.SendAs(playerID)

//note - you can also use ExecuteAs to run any cloud code as the given player for the event in the SparkRequest if required
request.ExecuteAs(playerID)

  

Using this method on a leaderboard will mean you don't have to update a collection manually. Try that and let me know if you have any other questions. 


Thanks,

Liam


1 person likes this

Work like charm! Thanks a lot Liam!

In case anyone can find it usefull this is what I did to steal "score" from the leaderboard:  

//First Get target ID 
var targetID = Spark.getData().targetID;
//Also get the new data to be saved
var flagsAmount = Spark.getData().flagsAmount;
 
var request = new SparkRequests.LogEventRequest();
request.eventKey = "LaderboardFlags";
//request.targetID = targetID;
request.totalFlags = -flagsAmount;
 
//playerID here being the player you want to subtract the amount from
var response = request.SendAs(targetID)
 
var playerID = Spark.getPlayer().getPlayerId();
var request = new SparkRequests.LogEventRequest();
request.eventKey = "LaderboardFlags";
//request.targetID = targetID;
request.totalFlags = flagsAmount;

var response = request.SendAs(playerID)

  So I call "StealFlags" and set a target ID and a flags amount and thats reduced from target and added to the player :D

Hi Ricardo,


No problem, happy to have helped you get this up and running !


Thanks,

Liam

Login to post a comment