Skip to content

Commit

Permalink
Fixed leaderboard synchronization bug where it would crash if user di…
Browse files Browse the repository at this point in the history
…dn't submit a score previously
  • Loading branch information
nihalahmed committed May 14, 2012
1 parent 7403bf2 commit f829910
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
36 changes: 19 additions & 17 deletions Demo App/GameCenterManager/GameCenterManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,23 @@ - (void)syncGameCenter {
[leaderboardRequest setCategory:[_leaderboards objectAtIndex:0]];
[leaderboardRequest loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error) {
if(error == nil) {
NSData *gameCenterManagerData = [[NSData dataWithContentsOfFile:kGameCenterManagerDataPath] decryptedWithKey:kGameCenterManagerKey];
NSMutableDictionary *plistDict = [NSKeyedUnarchiver unarchiveObjectWithData:gameCenterManagerData];
NSMutableDictionary *playerDict = [plistDict objectForKey:[[GameCenterManager sharedManager] localPlayerId]];
if(playerDict == nil) {
playerDict = [NSMutableDictionary dictionary];
}
int savedHighScoreValue = 0;
NSNumber *savedHighScore = [playerDict objectForKey:leaderboardRequest.localPlayerScore.category];
if(savedHighScore != nil) {
savedHighScoreValue = [savedHighScore intValue];
if(scores.count > 0) {
NSData *gameCenterManagerData = [[NSData dataWithContentsOfFile:kGameCenterManagerDataPath] decryptedWithKey:kGameCenterManagerKey];
NSMutableDictionary *plistDict = [NSKeyedUnarchiver unarchiveObjectWithData:gameCenterManagerData];
NSMutableDictionary *playerDict = [plistDict objectForKey:[[GameCenterManager sharedManager] localPlayerId]];
if(playerDict == nil) {
playerDict = [NSMutableDictionary dictionary];
}
int savedHighScoreValue = 0;
NSNumber *savedHighScore = [playerDict objectForKey:leaderboardRequest.localPlayerScore.category];
if(savedHighScore != nil) {
savedHighScoreValue = [savedHighScore intValue];
}
[playerDict setObject:[NSNumber numberWithInt:MAX(leaderboardRequest.localPlayerScore.value, savedHighScoreValue)] forKey:leaderboardRequest.localPlayerScore.category];
[plistDict setObject:playerDict forKey:[[GameCenterManager sharedManager] localPlayerId]];
NSData *saveData = [[NSKeyedArchiver archivedDataWithRootObject:plistDict] encryptedWithKey:kGameCenterManagerKey];
[saveData writeToFile:kGameCenterManagerDataPath atomically:YES];
}
[playerDict setObject:[NSNumber numberWithInt:MAX(leaderboardRequest.localPlayerScore.value, savedHighScoreValue)] forKey:leaderboardRequest.localPlayerScore.category];
[plistDict setObject:playerDict forKey:[[GameCenterManager sharedManager] localPlayerId]];
NSData *saveData = [[NSKeyedArchiver archivedDataWithRootObject:plistDict] encryptedWithKey:kGameCenterManagerKey];
[saveData writeToFile:kGameCenterManagerDataPath atomically:YES];

[_leaderboards removeObjectAtIndex:0];
[[GameCenterManager sharedManager] syncGameCenter];
Expand All @@ -145,7 +147,7 @@ - (void)syncGameCenter {
else if(![[NSUserDefaults standardUserDefaults] boolForKey:[@"achievementsSynced" stringByAppendingString:[[GameCenterManager sharedManager] localPlayerId]]]) {
[GKAchievement loadAchievementsWithCompletionHandler:^(NSArray *achievements, NSError *error) {
if(error == nil) {
if([achievements count] > 0) {
if(achievements.count > 0) {
NSData *gameCenterManagerData = [[NSData dataWithContentsOfFile:kGameCenterManagerDataPath] decryptedWithKey:kGameCenterManagerKey];
NSMutableDictionary *plistDict = [NSKeyedUnarchiver unarchiveObjectWithData:gameCenterManagerData];
NSMutableDictionary *playerDict = [plistDict objectForKey:[[GameCenterManager sharedManager] localPlayerId]];
Expand Down Expand Up @@ -383,7 +385,7 @@ - (void)reportSavedScoresAndAchievements {
NSMutableDictionary *plistDict = [NSKeyedUnarchiver unarchiveObjectWithData:gameCenterManagerData];
NSMutableArray *savedScores = [plistDict objectForKey:@"SavedScores"];
if(savedScores != nil) {
if([savedScores count] > 0) {
if(savedScores.count > 0) {
gkScore = [NSKeyedUnarchiver unarchiveObjectWithData:[savedScores objectAtIndex:0]];
[savedScores removeObjectAtIndex:0];
[plistDict setObject:savedScores forKey:@"SavedScores"];
Expand Down Expand Up @@ -413,7 +415,7 @@ - (void)reportSavedScoresAndAchievements {
if(playerDict != nil) {
NSMutableDictionary *savedAchievements = [playerDict objectForKey:@"SavedAchievements"];
if(savedAchievements != nil) {
if([savedAchievements count] > 0) {
if(savedAchievements.count > 0) {
identifier = [[savedAchievements allKeys] objectAtIndex:0];
percentComplete = [[savedAchievements objectForKey:identifier] doubleValue];
[savedAchievements removeObjectForKey:identifier];
Expand Down
36 changes: 19 additions & 17 deletions GameCenterManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,23 @@ - (void)syncGameCenter {
[leaderboardRequest setCategory:[_leaderboards objectAtIndex:0]];
[leaderboardRequest loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error) {
if(error == nil) {
NSData *gameCenterManagerData = [[NSData dataWithContentsOfFile:kGameCenterManagerDataPath] decryptedWithKey:kGameCenterManagerKey];
NSMutableDictionary *plistDict = [NSKeyedUnarchiver unarchiveObjectWithData:gameCenterManagerData];
NSMutableDictionary *playerDict = [plistDict objectForKey:[[GameCenterManager sharedManager] localPlayerId]];
if(playerDict == nil) {
playerDict = [NSMutableDictionary dictionary];
}
int savedHighScoreValue = 0;
NSNumber *savedHighScore = [playerDict objectForKey:leaderboardRequest.localPlayerScore.category];
if(savedHighScore != nil) {
savedHighScoreValue = [savedHighScore intValue];
if(scores.count > 0) {
NSData *gameCenterManagerData = [[NSData dataWithContentsOfFile:kGameCenterManagerDataPath] decryptedWithKey:kGameCenterManagerKey];
NSMutableDictionary *plistDict = [NSKeyedUnarchiver unarchiveObjectWithData:gameCenterManagerData];
NSMutableDictionary *playerDict = [plistDict objectForKey:[[GameCenterManager sharedManager] localPlayerId]];
if(playerDict == nil) {
playerDict = [NSMutableDictionary dictionary];
}
int savedHighScoreValue = 0;
NSNumber *savedHighScore = [playerDict objectForKey:leaderboardRequest.localPlayerScore.category];
if(savedHighScore != nil) {
savedHighScoreValue = [savedHighScore intValue];
}
[playerDict setObject:[NSNumber numberWithInt:MAX(leaderboardRequest.localPlayerScore.value, savedHighScoreValue)] forKey:leaderboardRequest.localPlayerScore.category];
[plistDict setObject:playerDict forKey:[[GameCenterManager sharedManager] localPlayerId]];
NSData *saveData = [[NSKeyedArchiver archivedDataWithRootObject:plistDict] encryptedWithKey:kGameCenterManagerKey];
[saveData writeToFile:kGameCenterManagerDataPath atomically:YES];
}
[playerDict setObject:[NSNumber numberWithInt:MAX(leaderboardRequest.localPlayerScore.value, savedHighScoreValue)] forKey:leaderboardRequest.localPlayerScore.category];
[plistDict setObject:playerDict forKey:[[GameCenterManager sharedManager] localPlayerId]];
NSData *saveData = [[NSKeyedArchiver archivedDataWithRootObject:plistDict] encryptedWithKey:kGameCenterManagerKey];
[saveData writeToFile:kGameCenterManagerDataPath atomically:YES];

[_leaderboards removeObjectAtIndex:0];
[[GameCenterManager sharedManager] syncGameCenter];
Expand All @@ -145,7 +147,7 @@ - (void)syncGameCenter {
else if(![[NSUserDefaults standardUserDefaults] boolForKey:[@"achievementsSynced" stringByAppendingString:[[GameCenterManager sharedManager] localPlayerId]]]) {
[GKAchievement loadAchievementsWithCompletionHandler:^(NSArray *achievements, NSError *error) {
if(error == nil) {
if([achievements count] > 0) {
if(achievements.count > 0) {
NSData *gameCenterManagerData = [[NSData dataWithContentsOfFile:kGameCenterManagerDataPath] decryptedWithKey:kGameCenterManagerKey];
NSMutableDictionary *plistDict = [NSKeyedUnarchiver unarchiveObjectWithData:gameCenterManagerData];
NSMutableDictionary *playerDict = [plistDict objectForKey:[[GameCenterManager sharedManager] localPlayerId]];
Expand Down Expand Up @@ -383,7 +385,7 @@ - (void)reportSavedScoresAndAchievements {
NSMutableDictionary *plistDict = [NSKeyedUnarchiver unarchiveObjectWithData:gameCenterManagerData];
NSMutableArray *savedScores = [plistDict objectForKey:@"SavedScores"];
if(savedScores != nil) {
if([savedScores count] > 0) {
if(savedScores.count > 0) {
gkScore = [NSKeyedUnarchiver unarchiveObjectWithData:[savedScores objectAtIndex:0]];
[savedScores removeObjectAtIndex:0];
[plistDict setObject:savedScores forKey:@"SavedScores"];
Expand Down Expand Up @@ -413,7 +415,7 @@ - (void)reportSavedScoresAndAchievements {
if(playerDict != nil) {
NSMutableDictionary *savedAchievements = [playerDict objectForKey:@"SavedAchievements"];
if(savedAchievements != nil) {
if([savedAchievements count] > 0) {
if(savedAchievements.count > 0) {
identifier = [[savedAchievements allKeys] objectAtIndex:0];
percentComplete = [[savedAchievements objectForKey:identifier] doubleValue];
[savedAchievements removeObjectForKey:identifier];
Expand Down

0 comments on commit f829910

Please sign in to comment.