GameSparks: AroundValueLeaderboardRequest? (I)

I have been working with GameSparks in the last months.

One thing I miss about their API is you can't query something like "Hey, give me from that leaderboard a player that posted a value similar to 'x'"

You have the classic AroundMeLeaderboardRequest, but then you could get people too similar to you. With this "AroundValueLeaderboardRequest" (ok, maybe it's not the best name), your matchmaking could be more flexible, it may help to perform matchmaking with different difficulty levels.

I'm posting below my first approach to AroundValueLeaderboardRequest, I'm working in a version with several loops of interpolation, I hope to have it done later today or maybe tomorrow:

Note: this is intented to work with a targetScore better than ours, I'm sure you will be able to easily modify the code to generalize for any targetScore.

/*
We are going to interpolate the player's ranking based on the score passed as parameter "targetScore"

scorePointsPerRankPosition = (bestScore - myScore) / myRank
scorePointsPerRankPosition * targetRank = bestScore - targetScore

targetRank = (bestScore - targetScore) / scorePointsPerRankPosition
*/

var lbShortCode     = Spark.getData().lbShortCode;
var fieldName       = Spark.getData().fieldName;
var fieldValue      = Spark.getData().fieldValue;
var numIterations = Spark.getData().numIterations;

var request                     = new SparkRequests.AroundMeLeaderboardRequest();
request.entryCount              = 1; //This is the minimum: my preceding enemy, me and the next enemy
request.includeFirst            = 1;
request.leaderboardShortCode    = lbShortCode;
var response = request.Send();

var firstInLeaderboard = response.first;

var bestScore   = firstInLeaderboard[0][fieldName]; //first is an array with a 'lonely' value
var myScore     = 0;
var myRank      = 0;
var targetScore = fieldValue;

//We will receive 3 values unless there aren't any people before or after me
//...or if the leaderboard is empty
if (response.data.length > 1)
{
    myScore = response.data[1][fieldName];
    myRank  = response.data[1]["rank"];
}
else
{
    myScore = response.data[0][fieldName];
    myRank  = response.data[0]["rank"];
}

int loops = 0;

var scorePointsPerRankPosition = (bestScore - myScore) / myRank;

if (scorePointsPerRankPosition <= 0) //I'm the best (or even better)
    scorePointsPerRankPosition = 1;

var targetRank = (bestScore - targetScore) / scorePointsPerRankPosition;

if (targetRank < 1)
    targetRank = 1;

var request = new SparkRequests.LeaderboardDataRequest();
request.entryCount = 1;
request.leaderboardShortCode = lbShortCode;
request.offset = targetRank - 1;

var response    = request.Send();
var realRank    = response.data[0]["rank"];
var realScore   = response.data[0][fieldName];

Spark.setScriptData("leaderboardData", response.data[0]);

Comentarios

Entradas populares