Skip to content

Commit

Permalink
Added support for score sort order as defined in Game Center. We want…
Browse files Browse the repository at this point in the history
… to report a score to Game Center when the new score is lower than the old score if the game’s sort order is Low to High.
  • Loading branch information
michael-patzer committed Jun 29, 2013
1 parent 9fc48f0 commit 667b64c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
7 changes: 6 additions & 1 deletion GameCenterManager/GC Manager/GameCenterManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
#import "Reachability.h"
#import "NSDataAES256.h"

typedef enum GameCenterSortOrder {
GameCenterSortOrderHighToLow,
GameCenterSortOrderLowToHigh
} GameCenterSortOrder;

@protocol GameCenterManagerDelegate;
@interface GameCenterManager : NSObject {
NSMutableArray *_leaderboards;
Expand All @@ -35,7 +40,7 @@
- (void)syncGameCenter;

// Saves score locally and reports it to Game Center. If error occurs, score is saved to be submitted later.
- (void)saveAndReportScore:(int)score leaderboard:(NSString *)identifier;
- (void)saveAndReportScore:(int)score leaderboard:(NSString *)identifier sortOrder:(GameCenterSortOrder)order;

// Saves achievement locally and reports it to Game Center. If error occurs, achievement is saved to be submitted later.
- (void)saveAndReportAchievement:(NSString *)identifier percentComplete:(double)percentComplete shouldDisplayNotification:(BOOL)displayNotification;
Expand Down
24 changes: 20 additions & 4 deletions GameCenterManager/GC Manager/GameCenterManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ - (BOOL)checkGameCenterAvailability {
self.isGameCenterAvailable = YES;
return YES;
}
}
}
}
}

Expand Down Expand Up @@ -337,7 +337,7 @@ - (void)reportSavedScoresAndAchievements {
#pragma mark - Score and Achievement Reporting

//Save score and report it to GameCenter
- (void)saveAndReportScore:(int)score leaderboard:(NSString *)identifier {
- (void)saveAndReportScore:(int)score leaderboard:(NSString *)identifier sortOrder:(GameCenterSortOrder)order {
NSData *gameCenterManagerData = [[NSData dataWithContentsOfFile:kGameCenterManagerDataPath] decryptedWithKey:kGameCenterManagerKey];
NSMutableDictionary *plistDict = [NSKeyedUnarchiver unarchiveObjectWithData:gameCenterManagerData];
NSMutableDictionary *playerDict = [plistDict objectForKey:[[GameCenterManager sharedManager] localPlayerId]];
Expand All @@ -352,7 +352,23 @@ - (void)saveAndReportScore:(int)score leaderboard:(NSString *)identifier {
}

int savedHighScoreValue = [savedHighScore intValue];
if (score > savedHighScoreValue) {

// Determine if the new score is better than the old score
BOOL isScoreBetter = NO;
switch (order) {
case GameCenterSortOrderLowToHigh: // A lower score is better
if (score < savedHighScoreValue) {
isScoreBetter = YES;
}
break;
default:
if (score > savedHighScoreValue) { // A higher score is better
isScoreBetter = YES;
}
break;
}

if (isScoreBetter) {
[playerDict setObject:[NSNumber numberWithInt:score] forKey:identifier];
[plistDict setObject:playerDict forKey:[[GameCenterManager sharedManager] localPlayerId]];
NSData *saveData = [[NSKeyedArchiver archivedDataWithRootObject:plistDict] encryptedWithKey:kGameCenterManagerKey];
Expand Down Expand Up @@ -480,7 +496,7 @@ - (void)saveAchievementToReportLater:(NSString *)identifier percentComplete:(dou
}
} else {
NSMutableDictionary *savedAchievements = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithDouble:percentComplete], identifier, nil];
playerDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:savedAchievements, @"SavedAchievements", nil];
playerDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:savedAchievements, @"SavedAchievements", nil];
}
[plistDict setObject:playerDict forKey:[[GameCenterManager sharedManager] localPlayerId]];
NSData *saveData = [[NSKeyedArchiver archivedDataWithRootObject:plistDict] encryptedWithKey:kGameCenterManagerKey];
Expand Down

0 comments on commit 667b64c

Please # to comment.