Search code examples
ioscordovagame-centerleaderboard

Accessing Game Center HighScores ios phonegap plugin


So,

I'm using phonegap and lee's ios plugin to access the gamecenter. Everything is working fine but I want to get the top 10 scores.

I should be able to integrate this [apples example code][1] into this plugin.

I added - (void) getScores:(CDVInvokedUrlCommand*)command; to the gamecenter.h

and then in the gamecenter.m file, I'm working through the action right now

- (void) getScores:(CDVInvokedUrlCommand*)command;

    {
        NSMutableDictionary *args = [command.arguments objectAtIndex:0];
        NSString *leaderboardId = [args objectForKey:@"leaderboardId"];

        __block CDVPluginResult* pluginResult = nil;
        NSMutableArray *topScores = [NSMutableArray array];
        GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init];
        if (leaderboardRequest != nil)
        {
            leaderboardRequest.playerScope = GKLeaderboardPlayerScopeGlobal;
            leaderboardRequest.timeScope = GKLeaderboardTimeScopeToday;
            leaderboardRequest.identifier = leaderboardId;
            leaderboardRequest.range = NSMakeRange(1,10);
            [leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
                if (error != nil)
                {
                    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
                }
                if (scores != nil)
                {

                    for (GKScore* score in scores)
                    {

                        NSMutableDictionary *entry = [NSMutableDictionary dictionary];
                        entry[@"score"] = score.description;

                        [topScores addObject:entry];
                    }

                    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray: topScores];


                }
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        }];
    }
}

then in staging/www/plugins/cordova-plugin-gamecenter/www/gamecenter.js

GameCenter.prototype.getScores = function (success, failure, data) {
exec(success, failure, "GameCenter", "getScores", [data]);

};

so next would be to inherently call the function in the game:

function getHighScores(){
    console.log('gethighscores');

    var successCallback = function(data){
        console.log(success);
    }


    var failureCallback = function (data) {
        console.log('Fail');
    }

    var data = { leaderboardId: "escapemtl" }

    gamecenter.getScores(successCallback, failureCallback, data);}

If i call the function nothing happens in the console


Solution

  • So after an entire day i made this work:

    insert:

      GKPlayer* player = score.player;
                        NSMutableDictionary *entry = [NSMutableDictionary dictionary];
                        entry[@"score"] = [NSNumber numberWithLongLong:(score.value) ];
                        entry[@"name"] = player.alias;
                        [topScores addObject:entry];
    

    Into:

     for (GKScore* score in scores)
                    {
    
    
                        NSMutableDictionary *entry = [NSMutableDictionary dictionary];
                        entry[@"score"] = score.description;
    
                        [topScores addObject:entry];
                    }