Reputation: 1466
I am apologize for this type Question. I am first time use Cloud Code in parse so I have No idea how to get data in parse through Cloud code. If any idea then suggest me. I have check many example but I have not found in my problem. My parse data base is like this..
userId game1score game2score game3score gender
xcvgfs1 300 200 100 man
sfdgdh 500 600 300 man
like this parse data base many user are playing game and score the game. My problem is how to get all user score in game1score ya other game score and compare to current user score and find the Rank of current user. Thanks for helping..
Call the cloud code function is like this in my activity..
Map<String, String> map = new HashMap<String, String>();
map.put(" game1score", "300");
ParseCloud.callFunctionInBackground("check_duplicate",map,new FunctionCallback<Object>() {
@Override
public void done(Object o, ParseException e) {
if(e==null){
Log.e("String",""+o) ;
}else{
Log.e("error",e.getMessage()) ;
}
}
});
return always null or 0
Upvotes: 0
Views: 419
Reputation: 1836
As far as I understand from your explanation, you want to extract the rank number of current user via using the Parse Score table where you record 3 different game scores (gameScore1, gameScore2, gameScore3). You have already write the Cloud code trigger function. So the cloud code that you can use is below;
Parse.Cloud.define("rank", function(request, status)
{
var result = [];
var processRankCallback = function(res) {
result = result.concat(res);
if (res.length === 1000)
{
process(res[res.length-1].id);
return;
}
//All object that is greater than the given user score
status.success(result.length);
}
var processRank = function(skip)
{
var query = new Parse.Query("Score");
var userScore = request.params.userScore;
var gameType = request.params.gameType;
if (skip) query.greaterThan("objectId", skip);
query.limit(1000);
query.ascending("objectId");
query.find().then(function querySuccess(res)
{
var innerResult = [];
for(var i = 0 ; i < res.length;i++)
{
//var gameScore1 = res[i].get("gameScore1");
//var gameScore2 = res[i].get("gameScore2");
//var gameScore3 = res[i].get("gameScore3");
//var total = gameScore1+gameScore2+gameScore3;
var total = res[i].get(gameType);
if(userScore < total) innerResult.push(res[i]);
}
processRankCallback(innerResult);
}, function queryFailed(reason)
{
status.error("Error:" + error.code + " Message" + error.message);
});
}
processRank(false);
});
Actually, finding the rank means that query the Score table and count the entry where the game scores all total is larger than your current user total game score. To implement this you can use the countQuery however Parse countQuery can fail or show incorrect result when your table entry is larger than 1000. So, instead of using count query, you can walk through the entire table and find all entries that have larger game score than your current user. In above code, I assume that you have 3 different game scores and all have valid number (not undefined). You can modify the code for your own purposes. The end result will generate a number which is actually the number of entry in Score table that has larger game score total than your current user game scores total.
For example I have a Score table with column gameScore1 and 3 entries where the scores are 600, 200 and 100. I call the Cloud function with below parameters hash table;
HashMap<String, String> params = new HashMap<String, String>();
params.put("userScore", "150");
params.put("gameType", "gameScore1");
//Call Parse cloud function
The response will be 2 which actually means that there are two entries that has gameScore1 which is larger than 150. Hope this helps. Regards.
Upvotes: 1