How can I get users' times & rankings for a specific leaderboard & timescope from Game Center?

StackOverflow https://stackoverflow.com/questions/19485611

  •  01-07-2022
  •  | 
  •  

Вопрос

In terms of Apple's game center apis, how would I request and get back the local users time and rankings for a particular Leaderboard and Timescope?

  • for leaderboard X (i.e. specify the board - e.g. Level12_BestTime), for given TimeScope
  • get returned the local players current: a) time e.g. 12.3seconds b) ranking (with Friends) e.g. 12th c) ranking (All players) e.g. 123rd
Это было полезно?

Решение

Copied from the Game Center Programming Guide:

GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init];
if (leaderboardRequest != nil)
{
    leaderboardRequest.playerScope = GKLeaderboardPlayerScopeGlobal; // or GKLeaderboardPlayerScopeFriendsOnly
    leaderboardRequest.timeScope = GKLeaderboardTimeScopeToday; // or GKLeaderboardTimeScopeWeek, GKLeaderboardTimeScopeAllTime
    leaderboardRequest.identifier = @"Combined.LandMaps" // Name of the leaderboard
    leaderboardRequest.range = NSMakeRange(1,10); // How many results to get
    [leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
        if (error != nil)
        {
            // Handle the error.
        }
        if (scores != nil)
        {
            // Process the score information.
        }
        }];
}

To get information for a specific users:

 GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] initWithPlayerIDs: match.playerIDs];

In both cases the score of the user are stored in localPlayerScore and all scores in scores.

The ranking however could be problematic. You can only get up to 100 scores maximum, so if the leaderboard is very big it can take a lot of calls. localPlayerScore does contain a rank value, but that is only relative to the current scores list. Basically you have to iterate over the whole leaderboard to find the position of the user.

Другие советы

Regarding the second part of your question, the rank property of GKScore should do the trick. According to my tests, it reports the rank of the player according to the criteria specified for loading the leaderboard's scores, even though the player's score is outside the requested range. See sample below:

GKLeaderboard *board = [[GKLeaderboard alloc] init];
pbBoard.timeScope = GKLeaderboardTimeScopeAllTime;
pbBoard.range = NSMakeRange(1, 1);
pbBoard.identifier = @"myleaderboard";
[pbBoard loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
    if (error != nil) {
        // handle the error.
    }
    if (scores != nil) {
        GKScore* score = [board localPlayerScore];
        NSInteger rank = score.rank;
        // do whatever you need with the rank
    }
}];
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top