getting user name of getTopNRankers response is facebook id ?

0 votes

I am submiting my score as below

ScoreBoardService *scoreBoardService = App42API::BuildScoreBoardService();

//Here user_id is facebook id
scoreBoardService->SaveUserScore(gameName, user_id.c_str(), score, app42callback(TestScoreboardService::onScoreBoardRequestCompleted, this));

 

The reason for saving the score using facebook id is to get facobook friends score

Facebook friends score is working fine.

When i call GetTopNRankers ,user name in response is facebook id , but it should be facebook user name

ScoreBoardService *scoreBoardService = App42API::BuildScoreBoardService();
    int max =10;
    scoreBoardService->GetTopNRankers(gameName, max, app42callback(TestScoreboardService::onScoreBoardRequestCompleted, this));

Please help me how to get user name from global score ? ,am I missing something ?

I'm using app42SDK for Cocos2d-x and testing on iOS device.

asked Feb 13, 2015 in App42 Cloud API-BaaS by keyur (24 points)
recategorized Apr 7, 2015 by sushil

1 Answer

+1 vote
 
Best answer

Hi Keyur,

The second parameter passed in SaveUserScore API is considered as user name for whom the score will be recorded on App42 Cloud. When you call any get API the same will come as user name from the App42 Cloud.

There is a specific API called GetTopNRankersFromFacebook to get facebook names of your friends.

Also there is one more alternative that you can do by adding display name as additional info just before you call SaveUserScore API and you can use our setQuery method just before calling GetTopNRankers API to get that additional info. You can go through our documentation to know how to save additional data along with score and how to fetch the additional data you saved with score.

Hope it will help.

Thanks.

answered Feb 13, 2015 by rajeev.etc (1,660 points)
selected Feb 23, 2015 by keyur
I tried your answer, but when i'm using "saveUserScore" with fbID method then i get username at Facebook Friends score but at global score it showing user fbId instead of his name.

And when i'm using "saveadditionaldata" instead of saveUserScore then Global score with username is showing but Facebook friends score is not showing (it showing error :- score with the game name " " does not exist).

And when i'm trying to use both "saveUserScore" and "saveadditionaldata" method, then it gives both username and userFbId at global score.

And i also tried "getFacebookProfilesFromIds" method, but getting this error

"The Facebook Access Credentials are invalid. Received Facebook error response of type GraphMethodException: Unsupported get request."

Please help me how to get "Global score with username and score, Facebook Friends score with username and score".
While saving score, you can add additional data as follows:
============================================
ScoreBoardService *scoreBoardService = App42API::BuildScoreBoardService();

App42API::setDbName(dbName);//set your dbname here

App42Object *object = new App42Object();
object->setObject("displayName", "Shephertz");// set the name you want to display in leaderboard

object->setObject("level", 2); // you can also add other info if needed

scoreBoardService->AddCustomScore(object, collectionName); // set the created object as custom score

const char* fbId = "fbid"; // set your fb id for which the score will be registered
int score = 400; // set the score
   
//Finally call SaveUsercore to send the score to App42 cloud. The custom score you set above will go along with it.
scoreBoardService->SaveUserScore(gameName, fbId, score, app42callback(TestScoreboardService::onScoreBoardRequestCompleted, this));
============================================


Now to fetch the scores use following snippet:
============================================
ScoreBoardService *scoreBoardService = App42API::BuildScoreBoardService();
scoreBoardService->setQuery(collectionName, NULL);
    
int max =10;
scoreBoardService->GetTopNRankers(gameName, max, app42callback(TestScoreboardService::onScoreBoardRequestCompleted, this));
============================================

In the callback for fetch request, you have to find out "displayName" that you set above while saving the score and use it in your leaderboard as user name. Finding a particular key in response can be handled as follows:
============================================
void TestScoreboardService::onScoreBoardRequestCompleted( void *response)
{
    App42GameResponse *scoreResponse = (App42GameResponse*)response;
    printf("\ncode=%d",scoreResponse->getCode());
    printf("\nResponse Body=%s",scoreResponse->getBody().c_str());
    
    if (scoreResponse->isSuccess)
    {
        vector<App42Score> scores = scoreResponse->scores;
        for(std::vector<App42Score>::iterator it = scores.begin(); it != scores.end(); ++it)
        {
            printf("\n ScoreId=%s\n",it->getScoreId().c_str());
            printf("\n ScoreValue=%f\n",it->getScoreValue());
            printf("\n UserName=%s\n",it->getUserName().c_str());
            std::vector<JSONDocument> jsonDocList = it->getJsonDocList();
            for(std::vector<JSONDocument>::iterator it1 = jsonDocList.begin(); it1 != jsonDocList.end(); ++it1)
            {
                printf("\n JsonDoc=%s\n",it1->getJsonDoc().c_str());

                const char* displayName = getValueFromjsonForKey(it1->getJsonDoc().c_str(), "displayName");
                printf("\nDisplay Name = %s\n",displayName);
            }
        }
    }
    else
    {
        printf("\nerrordetails:%s",scoreResponse->errorDetails.c_str());
        printf("\nerrorMessage:%s",scoreResponse->errorMessage.c_str());
        printf("\nappErrorCode:%d",scoreResponse->appErrorCode);
        printf("\nhttpErrorCode:%d",scoreResponse->httpErrorCode);
    }
}


const char* TestScoreboardService::getValueFromjsonForKey(const char *json, const char *key)
{
    cJSON *ptrBody = cJSON_Parse(json);
    string valueStr = Util::getJSONString(key, ptrBody);
    if (!valueStr.length()) {
        valueStr = "";
    }
    printf("Value = %s",valueStr.c_str());
    cJSON_Delete(ptrBody);
    return valueStr.c_str();
}

============================================

In success response part, I parsed jsonDoc to get display name. If there is no display name key available then you can use the userName which get for sure in the response to show in the leaderboard.

Let me know if it solves the problem.
Thanks for the replay. It solves my problem.
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...