Skip to content

Instantly share code, notes, and snippets.

@reefwing
Forked from juaxix/aGameCenter_Codea.h
Created October 14, 2012 04:19

Revisions

  1. reefwing revised this gist Oct 27, 2012. 2 changed files with 45 additions and 4 deletions.
    5 changes: 5 additions & 0 deletions aGameCenter_Codea.h
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,9 @@
    // Version: 1.5
    // - Multiple Leaderboard (Easy, Medium & Hard) access added
    // - Achievements added.
    // 1.6
    // - Modified for universal apps (http://codeatuts.blogspot.com.au/2012/10/tutorial-22-building-universal-app-in.html)
    // - New showLeaderBoard: (int)ident method
    //
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    @@ -47,6 +50,8 @@
    - (void) achievementViewControllerDidFinish:(GKAchievementViewController *)viewController;
    - (void) resetAchievements;
    - (void) reportAchievementIdentifier: (int) ident;

    - (void) showLeaderboard: (int)ident;
    - (void) showEasyLeaderboard;
    - (void) showMediumLeaderboard;
    - (void) showHardLeaderboard;
    44 changes: 40 additions & 4 deletions aGameCenter_Codea.m
    Original file line number Diff line number Diff line change
    @@ -182,6 +182,7 @@ - (void)showAchievements
    - (void)achievementViewControllerDidFinish:(GKAchievementViewController *)viewController
    {
    [[SharedRenderer renderer] dismissViewControllerAnimated:YES completion:nil];
    [[SharedRenderer renderer] startAnimation];
    }

    - (void) resetAchievements
    @@ -208,6 +209,40 @@ - (void) resetAchievements

    // Leaderboard Methods

    - (void)showLeaderboard: (int)ident
    {
    NSString *identifier;

    // INSERT YOUR ACHIEVEMENT IDENTIFIERS BELOW

    switch (ident)
    {
    case 1:
    identifier = @"Easy Difficulty";
    break;
    case 2:
    identifier = @"Medium Difficulty";
    break;
    case 3:
    identifier = @"Hard Difficulty";
    break;
    default:
    NSLog(@"Warning Unknown Leader Board");
    break;
    }

    GKLeaderboardViewController *leaderBoardCont = [[GKLeaderboardViewController alloc] init];
    if (leaderBoardCont)
    {
    // INSERT YOUR LEADERBOARD IDENTIFIER IN THE LINE BELOW
    leaderBoardCont.category=identifier;
    leaderBoardCont.timeScope=GKLeaderboardTimeScopeToday;
    leaderBoardCont.leaderboardDelegate=self;

    [[SharedRenderer renderer] presentModalViewController:leaderBoardCont animated:YES];
    }
    }

    - (void)showEasyLeaderboard
    {
    GKLeaderboardViewController *leaderBoardCont = [[GKLeaderboardViewController alloc] init];
    @@ -253,6 +288,7 @@ - (void)showHardLeaderboard
    - (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
    {
    [[SharedRenderer renderer] dismissModalViewControllerAnimated:YES];
    [[SharedRenderer renderer] startAnimation];
    }

    - (void) reportEasyScore:(int) score
    @@ -274,7 +310,7 @@ - (void) reportEasyScore:(int) score
    }
    }

    - (void) reportMediumScore:(int) score
    - (void)reportMediumScore:(int) score
    {
    // INSERT YOUR LEADERBOARD IDENTIFIER IN THE LINE BELOW
    GKScore *scoreReporter = [[[GKScore alloc] initWithCategory: @"Medium Difficulty"] autorelease];
    @@ -293,7 +329,7 @@ - (void) reportMediumScore:(int) score
    }
    }

    - (void) reportHardScore:(int) score
    - (void)reportHardScore:(int) score
    {
    // INSERT YOUR LEADERBOARD IDENTIFIER IN THE LINE BELOW
    GKScore *scoreReporter = [[[GKScore alloc] initWithCategory: @"Hard Difficulty"] autorelease];
    @@ -314,7 +350,7 @@ - (void) reportHardScore:(int) score

    // Music Play and Stop Methods

    - (void) playMusic:(int) songNumber
    - (void)playMusic:(int) songNumber
    {
    NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
    NSString* song = [[NSString alloc] initWithString:@""];
    @@ -368,7 +404,7 @@ - (void) playMusic:(int) songNumber

    }

    - (void) stopMusic
    - (void)stopMusic
    {
    if (playing)
    {
  2. reefwing revised this gist Oct 22, 2012. 1 changed file with 13 additions and 13 deletions.
    26 changes: 13 additions & 13 deletions aGameCenter_Codea.m
    Original file line number Diff line number Diff line change
    @@ -127,25 +127,25 @@ - (void) reportAchievementIdentifier: (int) ident
    switch (ident)
    {
    case 1:
    identifier = @"au.com.reefwing.minesweeper.easyWinner";
    identifier = @"Easy Winner";
    break;
    case 2:
    identifier = @"au.com.reefwing.minesweeper.mediumWinner";
    identifier = @"Medium Winner";
    break;
    case 3:
    identifier = @"au.com.reefwing.minesweeper.hardWinner";
    identifier = @"Hard Winner";
    break;
    case 4:
    identifier = @"au.com.reefwing.minesweeper.boom";
    identifier = @"Boom";
    break;
    case 5:
    identifier = @"au.com.reefwing.minesweeper.badLuck";
    identifier = @"Bad Luck";
    break;
    case 6:
    identifier = @"au.com.reefwing.minesweeper.decathlon";
    identifier = @"Decathlon";
    break;
    case 7:
    identifier = @"au.com.reefwing.minesweeper.centurion";
    identifier = @"Centurion";
    break;
    default:
    NSLog(@"Warning Unknown Achievement");
    @@ -214,7 +214,7 @@ - (void)showEasyLeaderboard
    if (leaderBoardCont)
    {
    // INSERT YOUR LEADERBOARD IDENTIFIER IN THE LINE BELOW
    leaderBoardCont.category=@"au.com.reefwing.minesweeper.easyDifficulty";
    leaderBoardCont.category=@"Easy Difficulty";
    leaderBoardCont.timeScope=GKLeaderboardTimeScopeToday;
    leaderBoardCont.leaderboardDelegate=self;

    @@ -228,7 +228,7 @@ - (void)showMediumLeaderboard
    if (leaderBoardCont)
    {
    // INSERT YOUR LEADERBOARD IDENTIFIER IN THE LINE BELOW
    leaderBoardCont.category=@"au.com.reefwing.minesweeper.mediumDifficulty";
    leaderBoardCont.category=@"Medium Difficulty";
    leaderBoardCont.timeScope=GKLeaderboardTimeScopeToday;
    leaderBoardCont.leaderboardDelegate=self;

    @@ -242,7 +242,7 @@ - (void)showHardLeaderboard
    if (leaderBoardCont)
    {
    // INSERT YOUR LEADERBOARD IDENTIFIER IN THE LINE BELOW
    leaderBoardCont.category=@"au.com.reefwing.minesweeper.hardDifficulty";
    leaderBoardCont.category=@"Hard Difficulty";
    leaderBoardCont.timeScope=GKLeaderboardTimeScopeToday;
    leaderBoardCont.leaderboardDelegate=self;

    @@ -258,7 +258,7 @@ - (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewCo
    - (void) reportEasyScore:(int) score
    {
    // INSERT YOUR LEADERBOARD IDENTIFIER IN THE LINE BELOW
    GKScore *scoreReporter = [[[GKScore alloc] initWithCategory: @"au.com.reefwing.minesweeper.easyDifficulty"] autorelease];
    GKScore *scoreReporter = [[[GKScore alloc] initWithCategory: @"Easy Difficulty"] autorelease];
    if(scoreReporter)
    {
    scoreReporter.value = score;
    @@ -277,7 +277,7 @@ - (void) reportEasyScore:(int) score
    - (void) reportMediumScore:(int) score
    {
    // INSERT YOUR LEADERBOARD IDENTIFIER IN THE LINE BELOW
    GKScore *scoreReporter = [[[GKScore alloc] initWithCategory: @"au.com.reefwing.minesweeper.mediumDifficulty"] autorelease];
    GKScore *scoreReporter = [[[GKScore alloc] initWithCategory: @"Medium Difficulty"] autorelease];
    if(scoreReporter)
    {
    scoreReporter.value = score;
    @@ -296,7 +296,7 @@ - (void) reportMediumScore:(int) score
    - (void) reportHardScore:(int) score
    {
    // INSERT YOUR LEADERBOARD IDENTIFIER IN THE LINE BELOW
    GKScore *scoreReporter = [[[GKScore alloc] initWithCategory: @"au.com.reefwing.minesweeper.hardDifficulty"] autorelease];
    GKScore *scoreReporter = [[[GKScore alloc] initWithCategory: @"Hard Difficulty"] autorelease];
    if(scoreReporter)
    {
    scoreReporter.value = score;
  3. reefwing revised this gist Oct 20, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions aGameCenter_Codea.m
    Original file line number Diff line number Diff line change
    @@ -156,6 +156,7 @@ - (void) reportAchievementIdentifier: (int) ident
    GKAchievement *achievement = [[GKAchievement alloc] initWithIdentifier: identifier];
    if (achievement)
    {
    achievement.showsCompletionBanner = YES;
    achievement.percentComplete = 100.0;
    [achievement reportAchievementWithCompletionHandler:^(NSError *error)
    {
  4. reefwing revised this gist Oct 20, 2012. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions aGameCenter_Codea.m
    Original file line number Diff line number Diff line change
    @@ -199,7 +199,9 @@ - (void) resetAchievements
    {
    if (error != nil)
    // handle the error.
    NSLog(@"Game Center Error - can not reset achievements");
    NSLog(@"Game Center: Error Resetting Achievements - %@", [error localizedDescription]);
    else
    NSLog(@"Game Center - Achievements Reset.");
    }];
    }

    @@ -265,7 +267,7 @@ - (void) reportEasyScore:(int) score
    if (error != nil)
    {
    // handle the reporting error
    NSLog(@"Game Center: Error Easy Reporting Score.");
    NSLog(@"Game Center: Error Reporting Easy Score - %@", [error localizedDescription]);
    }
    }];
    }
    @@ -284,7 +286,7 @@ - (void) reportMediumScore:(int) score
    if (error != nil)
    {
    // handle the reporting error
    NSLog(@"Game Center: Error Reporting Medium Score.");
    NSLog(@"Game Center: Error Reporting Medium Score - %@", [error localizedDescription]);
    }
    }];
    }
    @@ -303,7 +305,7 @@ - (void) reportHardScore:(int) score
    if (error != nil)
    {
    // handle the reporting error
    NSLog(@"Game Center: Error Reporting Hard Score.");
    NSLog(@"Game Center: Error Reporting Hard Score - %@", [error localizedDescription]);
    }
    }];
    }
  5. reefwing revised this gist Oct 20, 2012. 2 changed files with 21 additions and 0 deletions.
    1 change: 1 addition & 0 deletions aGameCenter_Codea.h
    Original file line number Diff line number Diff line change
    @@ -45,6 +45,7 @@
    - (void) leaderboardViewControllerDidFinish: (GKLeaderboardViewController *)viewController;
    - (void) showAchievements;
    - (void) achievementViewControllerDidFinish:(GKAchievementViewController *)viewController;
    - (void) resetAchievements;
    - (void) reportAchievementIdentifier: (int) ident;
    - (void) showEasyLeaderboard;
    - (void) showMediumLeaderboard;
    20 changes: 20 additions & 0 deletions aGameCenter_Codea.m
    Original file line number Diff line number Diff line change
    @@ -183,6 +183,26 @@ - (void)achievementViewControllerDidFinish:(GKAchievementViewController *)viewCo
    [[SharedRenderer renderer] dismissViewControllerAnimated:YES completion:nil];
    }

    - (void) resetAchievements
    {
    // You may want to allow the player to reset their progress on achievements in your game.
    // First, this method clears any locally cached achievement objects created by you.
    // Then, it calls Game Kit to reset the player’s progress stored on Game Center.
    //
    // Clear all locally saved achievement objects - Enter your code to clear locally saved achievements below.

    // YOUR CODE GOES HERE

    // Clear all progress saved on Game Center.

    [GKAchievement resetAchievementsWithCompletionHandler:^(NSError *error)
    {
    if (error != nil)
    // handle the error.
    NSLog(@"Game Center Error - can not reset achievements");
    }];
    }

    // Leaderboard Methods

    - (void)showEasyLeaderboard
  6. reefwing revised this gist Oct 20, 2012. 2 changed files with 28 additions and 5 deletions.
    7 changes: 4 additions & 3 deletions aGameCenter_Codea.h
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    // LGPL - @juaxix - Codea connection!
    //
    // Modified by Reefwing Software on 14/10/12
    // www.reefwing.com.au
    // http://www.reefwing.com.au
    // @reefwing on the Codea Forums
    //
    // Version: 1.5
    @@ -30,8 +30,7 @@
    #import <GameKit/GameKit.h>
    #import <AVFoundation/AVAudioPlayer.h>

    @interface aGameCenter_Codea : NSObject
    <GKLeaderboardViewControllerDelegate, AVAudioPlayerDelegate>
    @interface aGameCenter_Codea : NSObject <GKLeaderboardViewControllerDelegate, AVAudioPlayerDelegate, GKAchievementViewControllerDelegate>
    {
    bool hasGameCenter;
    bool playing;
    @@ -44,6 +43,8 @@
    - (void) authenticationChanged;
    - (BOOL) isGameCenterAvailable;
    - (void) leaderboardViewControllerDidFinish: (GKLeaderboardViewController *)viewController;
    - (void) showAchievements;
    - (void) achievementViewControllerDidFinish:(GKAchievementViewController *)viewController;
    - (void) reportAchievementIdentifier: (int) ident;
    - (void) showEasyLeaderboard;
    - (void) showMediumLeaderboard;
    26 changes: 24 additions & 2 deletions aGameCenter_Codea.m
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    // LGPL - @juaxix - Codea connection!
    //
    // Modified by Reefwing Software on 14/10/12
    // www.reefwing.com.au
    // http://www.reefwing.com.au
    // @reefwing on the Codea Forums
    //
    // Version: 1.5
    @@ -62,8 +62,13 @@ - (BOOL) isGameCenterAvailable
    // The device must be running running iOS 4.1 or later.
    NSString *reqSysVer = @"4.1";
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);
    BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);

    if (gcClass && osVersionSupported)
    NSLog(@"Game Center is Available");
    else
    NSLog(@"Game Center is not Available");

    return (gcClass && osVersionSupported);
    }

    @@ -81,6 +86,7 @@ - (void)authenticateLocalPlayer
    {
    [self registerForAuthenticationNotification];
    hasGameCenter = true;
    NSLog(@"Game Center - local player authenticated.");
    }else
    {
    hasGameCenter = false;
    @@ -161,6 +167,22 @@ - (void) reportAchievementIdentifier: (int) ident
    }
    }

    - (void)showAchievements
    {
    GKAchievementViewController *achievements = [[GKAchievementViewController alloc] init];
    if (achievements != nil)
    {
    achievements.achievementDelegate = self;
    [[SharedRenderer renderer] presentViewController: achievements animated: YES completion:nil];
    }
    [achievements release];
    }

    - (void)achievementViewControllerDidFinish:(GKAchievementViewController *)viewController
    {
    [[SharedRenderer renderer] dismissViewControllerAnimated:YES completion:nil];
    }

    // Leaderboard Methods

    - (void)showEasyLeaderboard
  7. reefwing revised this gist Oct 14, 2012. 2 changed files with 218 additions and 33 deletions.
    29 changes: 25 additions & 4 deletions aGameCenter_Codea.h
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,23 @@
    // Modified by Reefwing Software on 14/10/12
    // www.reefwing.com.au
    // @reefwing on the Codea Forums
    //
    // Version: 1.5
    // - Multiple Leaderboard (Easy, Medium & Hard) access added
    // - Achievements added.
    //
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // http://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    //

    #import <Foundation/Foundation.h>
    #import <GameKit/GameKit.h>
    @@ -26,10 +43,14 @@
    - (void) registerForAuthenticationNotification;
    - (void) authenticationChanged;
    - (BOOL) isGameCenterAvailable;
    - (void) leaderboardViewControllerDidFinish:
    (GKLeaderboardViewController *)viewController;
    - (void) showLeaderboard;
    - (void) reportScore:(int) score;
    - (void) leaderboardViewControllerDidFinish: (GKLeaderboardViewController *)viewController;
    - (void) reportAchievementIdentifier: (int) ident;
    - (void) showEasyLeaderboard;
    - (void) showMediumLeaderboard;
    - (void) showHardLeaderboard;
    - (void) reportEasyScore:(int) score;
    - (void) reportMediumScore:(int) score;
    - (void) reportHardScore:(int) score;
    - (void) playMusic:(int) songNumber;
    - (void) stopMusic;
    @end
    222 changes: 193 additions & 29 deletions aGameCenter_Codea.m
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,26 @@
    // Games -> http://www.xixgames.com
    // LGPL - @juaxix - Codea connection!
    //
    // Modified by Reefwing Software on 14/10/12
    // www.reefwing.com.au
    // @reefwing on the Codea Forums
    //
    // Version: 1.5
    // - Multiple Leaderboard (Easy, Medium & Hard) access added
    // - Achievements added.
    //
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    // http://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    //

    #import "aGameCenter_Codea.h"
    #import "SharedRenderer.h"
    @@ -15,7 +35,7 @@ - (id)init
    {
    self = [super init];
    player = [[AVAudioPlayer alloc] init];
    playing= false;
    playing = false;
    return self;
    }

    @@ -25,14 +45,15 @@ - (void)dealloc
    [super dealloc];
    }

    - (void) start {
    - (void) start
    {
    //Game Center

    hasGameCenter = false;

    [self authenticateLocalPlayer];
    }


    - (BOOL) isGameCenterAvailable
    {
    // Check for presence of GKLocalPlayer API.
    @@ -46,17 +67,22 @@ - (BOOL) isGameCenterAvailable
    return (gcClass && osVersionSupported);
    }

    - (void)authenticateLocalPlayer {
    - (void)authenticateLocalPlayer
    {

    if(![self isGameCenterAvailable]){
    if(![self isGameCenterAvailable])
    {
    hasGameCenter = false;
    return;
    }
    [[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {
    if (error == nil){
    [[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error)
    {
    if (error == nil)
    {
    [self registerForAuthenticationNotification];
    hasGameCenter = true;
    }else{
    }else
    {
    hasGameCenter = false;
    }
    }];
    @@ -70,21 +96,108 @@ - (void)registerForAuthenticationNotification

    - (void)authenticationChanged
    {
    if([self isGameCenterAvailable ]){
    if([self isGameCenterAvailable ])
    {
    return;
    }

    if ([GKLocalPlayer localPlayer].isAuthenticated){
    if ([GKLocalPlayer localPlayer].isAuthenticated)
    {
    hasGameCenter = true;
    }else{
    }else
    {
    hasGameCenter = false;
    }
    }
    - (void)showLeaderboard

    // Achievement Methods

    - (void) reportAchievementIdentifier: (int) ident
    {
    NSString *identifier;

    // INSERT YOUR ACHIEVEMENT IDENTIFIERS BELOW

    switch (ident)
    {
    case 1:
    identifier = @"au.com.reefwing.minesweeper.easyWinner";
    break;
    case 2:
    identifier = @"au.com.reefwing.minesweeper.mediumWinner";
    break;
    case 3:
    identifier = @"au.com.reefwing.minesweeper.hardWinner";
    break;
    case 4:
    identifier = @"au.com.reefwing.minesweeper.boom";
    break;
    case 5:
    identifier = @"au.com.reefwing.minesweeper.badLuck";
    break;
    case 6:
    identifier = @"au.com.reefwing.minesweeper.decathlon";
    break;
    case 7:
    identifier = @"au.com.reefwing.minesweeper.centurion";
    break;
    default:
    NSLog(@"Warning Unknown Achievement");
    break;
    }
    NSLog(@"Achievement complete: %@", identifier);

    GKAchievement *achievement = [[GKAchievement alloc] initWithIdentifier: identifier];
    if (achievement)
    {
    achievement.percentComplete = 100.0;
    [achievement reportAchievementWithCompletionHandler:^(NSError *error)
    {
    if (error != nil)
    {
    NSLog(@"Error in reporting achievements: %@", error);
    }
    }];
    }
    }

    // Leaderboard Methods

    - (void)showEasyLeaderboard
    {
    GKLeaderboardViewController *leaderBoardCont = [[GKLeaderboardViewController alloc] init];
    if (leaderBoardCont)
    {
    // INSERT YOUR LEADERBOARD IDENTIFIER IN THE LINE BELOW
    leaderBoardCont.category=@"au.com.reefwing.minesweeper.easyDifficulty";
    leaderBoardCont.timeScope=GKLeaderboardTimeScopeToday;
    leaderBoardCont.leaderboardDelegate=self;

    [[SharedRenderer renderer] presentModalViewController:leaderBoardCont animated:YES];
    }
    }

    - (void)showMediumLeaderboard
    {
    GKLeaderboardViewController *leaderBoardCont = [[GKLeaderboardViewController alloc] init];
    if (leaderBoardCont)
    {
    // INSERT YOUR LEADERBOARD IDENTIFIER IN THE LINE BELOW
    leaderBoardCont.category=@"au.com.reefwing.minesweeper.mediumDifficulty";
    leaderBoardCont.timeScope=GKLeaderboardTimeScopeToday;
    leaderBoardCont.leaderboardDelegate=self;

    [[SharedRenderer renderer] presentModalViewController:leaderBoardCont animated:YES];
    }
    }

    - (void)showHardLeaderboard
    {
    GKLeaderboardViewController *leaderBoardCont = [[GKLeaderboardViewController alloc] init];
    if (leaderBoardCont) {
    leaderBoardCont.category=@"YOUR_SCORE_BOARD_UNIQUE_NAME_FROM_ITUNES_CONNECT";
    if (leaderBoardCont)
    {
    // INSERT YOUR LEADERBOARD IDENTIFIER IN THE LINE BELOW
    leaderBoardCont.category=@"au.com.reefwing.minesweeper.hardDifficulty";
    leaderBoardCont.timeScope=GKLeaderboardTimeScopeToday;
    leaderBoardCont.leaderboardDelegate=self;

    @@ -97,28 +210,73 @@ - (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewCo
    [[SharedRenderer renderer] dismissModalViewControllerAnimated:YES];
    }

    - (void) reportScore:(int) score
    - (void) reportEasyScore:(int) score
    {
    GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:
    @"YOUR_SCORE_BOARD_UNIQUE_NAME_FROM_ITUNES_CONNECT"] autorelease];
    if(scoreReporter){
    // INSERT YOUR LEADERBOARD IDENTIFIER IN THE LINE BELOW
    GKScore *scoreReporter = [[[GKScore alloc] initWithCategory: @"au.com.reefwing.minesweeper.easyDifficulty"] autorelease];
    if(scoreReporter)
    {
    scoreReporter.value = score;

    [scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
    if (error != nil){
    [scoreReporter reportScoreWithCompletionHandler:^(NSError *error)
    {
    if (error != nil)
    {
    // handle the reporting error

    NSLog(@"Game Center: Error Easy Reporting Score.");
    }
    }];
    }
    }

    - (void) playMusic:(int) songNumber {
    - (void) reportMediumScore:(int) score
    {
    // INSERT YOUR LEADERBOARD IDENTIFIER IN THE LINE BELOW
    GKScore *scoreReporter = [[[GKScore alloc] initWithCategory: @"au.com.reefwing.minesweeper.mediumDifficulty"] autorelease];
    if(scoreReporter)
    {
    scoreReporter.value = score;

    [scoreReporter reportScoreWithCompletionHandler:^(NSError *error)
    {
    if (error != nil)
    {
    // handle the reporting error
    NSLog(@"Game Center: Error Reporting Medium Score.");
    }
    }];
    }
    }

    - (void) reportHardScore:(int) score
    {
    // INSERT YOUR LEADERBOARD IDENTIFIER IN THE LINE BELOW
    GKScore *scoreReporter = [[[GKScore alloc] initWithCategory: @"au.com.reefwing.minesweeper.hardDifficulty"] autorelease];
    if(scoreReporter)
    {
    scoreReporter.value = score;

    [scoreReporter reportScoreWithCompletionHandler:^(NSError *error)
    {
    if (error != nil)
    {
    // handle the reporting error
    NSLog(@"Game Center: Error Reporting Hard Score.");
    }
    }];
    }
    }

    // Music Play and Stop Methods

    - (void) playMusic:(int) songNumber
    {
    NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
    NSString* song = [[NSString alloc] initWithString:@""];
    NSError* err;

    switch (songNumber) {
    switch (songNumber)
    {
    case 1: //menu song
    song = @"/menu.MP3";
    break;
    @@ -131,26 +289,30 @@ - (void) playMusic:(int) songNumber {
    break;
    }

    if ([song isEqualToString:@""]){
    if ([song isEqualToString:@""])
    {
    return ;
    }
    //NSLog(@"Playing song %d: %@",songNumber,song);
    resourcePath = [resourcePath stringByAppendingString:song];
    //NSLog(@"Path to play: %@", resourcePath);
    //Initialize our player pointing to the path to our resource
    if (playing && player) {
    if (playing && player)
    {
    [player stop];
    playing = false;
    }
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:
    [NSURL fileURLWithPath:resourcePath] error:&err];


    if( err ){
    if( err )
    {
    //bail!
    NSLog(@"Failed with reason: %@", [err localizedDescription]);
    }
    else{
    else
    {
    //set our delegate and begin playback
    player.delegate = self;
    [player prepareToPlay];
    @@ -161,8 +323,10 @@ - (void) playMusic:(int) songNumber {

    }

    - (void) stopMusic {
    if (playing){
    - (void) stopMusic
    {
    if (playing)
    {
    [player stop];
    playing = false;
    }
  8. reefwing revised this gist Oct 14, 2012. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions aGameCenter_Codea.h
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,9 @@
    // Games -> http://www.xixgames.com
    // LGPL - @juaxix - Codea connection!
    //
    // Modified by Reefwing Software on 14/10/12
    // www.reefwing.com.au
    // @reefwing on the Codea Forums

    #import <Foundation/Foundation.h>
    #import <GameKit/GameKit.h>
  9. @juaxix juaxix created this gist Jun 16, 2012.
    32 changes: 32 additions & 0 deletions aGameCenter_Codea.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    //
    // aGameCenter_Codea.h
    //
    // Created by Juan Belón on 28/05/12
    // Games -> http://www.xixgames.com
    // LGPL - @juaxix - Codea connection!
    //

    #import <Foundation/Foundation.h>
    #import <GameKit/GameKit.h>
    #import <AVFoundation/AVAudioPlayer.h>

    @interface aGameCenter_Codea : NSObject
    <GKLeaderboardViewControllerDelegate, AVAudioPlayerDelegate>
    {
    bool hasGameCenter;
    bool playing;
    AVAudioPlayer *player;
    }

    - (void) start;
    - (void) authenticateLocalPlayer;
    - (void) registerForAuthenticationNotification;
    - (void) authenticationChanged;
    - (BOOL) isGameCenterAvailable;
    - (void) leaderboardViewControllerDidFinish:
    (GKLeaderboardViewController *)viewController;
    - (void) showLeaderboard;
    - (void) reportScore:(int) score;
    - (void) playMusic:(int) songNumber;
    - (void) stopMusic;
    @end
    170 changes: 170 additions & 0 deletions aGameCenter_Codea.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,170 @@
    //
    // aGameCenter_Codea.m
    //
    // Created by Juan Belón on 28/05/12
    // Games -> http://www.xixgames.com
    // LGPL - @juaxix - Codea connection!
    //

    #import "aGameCenter_Codea.h"
    #import "SharedRenderer.h"

    @implementation aGameCenter_Codea

    - (id)init
    {
    self = [super init];
    player = [[AVAudioPlayer alloc] init];
    playing= false;
    return self;
    }

    - (void)dealloc
    {
    [player release];
    [super dealloc];
    }

    - (void) start {
    //Game Center
    hasGameCenter = false;

    [self authenticateLocalPlayer];
    }


    - (BOOL) isGameCenterAvailable
    {
    // Check for presence of GKLocalPlayer API.
    Class gcClass = (NSClassFromString(@"GKLocalPlayer"));

    // The device must be running running iOS 4.1 or later.
    NSString *reqSysVer = @"4.1";
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);

    return (gcClass && osVersionSupported);
    }

    - (void)authenticateLocalPlayer {

    if(![self isGameCenterAvailable]){
    hasGameCenter = false;
    return;
    }
    [[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {
    if (error == nil){
    [self registerForAuthenticationNotification];
    hasGameCenter = true;
    }else{
    hasGameCenter = false;
    }
    }];
    }

    - (void)registerForAuthenticationNotification
    {
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver: self selector:@selector(authenticationChanged) name:GKPlayerAuthenticationDidChangeNotificationName object:nil];
    }

    - (void)authenticationChanged
    {
    if([self isGameCenterAvailable ]){
    return;
    }

    if ([GKLocalPlayer localPlayer].isAuthenticated){
    hasGameCenter = true;
    }else{
    hasGameCenter = false;
    }
    }
    - (void)showLeaderboard
    {
    GKLeaderboardViewController *leaderBoardCont = [[GKLeaderboardViewController alloc] init];
    if (leaderBoardCont) {
    leaderBoardCont.category=@"YOUR_SCORE_BOARD_UNIQUE_NAME_FROM_ITUNES_CONNECT";
    leaderBoardCont.timeScope=GKLeaderboardTimeScopeToday;
    leaderBoardCont.leaderboardDelegate=self;

    [[SharedRenderer renderer] presentModalViewController:leaderBoardCont animated:YES];
    }
    }

    - (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
    {
    [[SharedRenderer renderer] dismissModalViewControllerAnimated:YES];
    }

    - (void) reportScore:(int) score
    {
    GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:
    @"YOUR_SCORE_BOARD_UNIQUE_NAME_FROM_ITUNES_CONNECT"] autorelease];
    if(scoreReporter){
    scoreReporter.value = score;

    [scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
    if (error != nil){
    // handle the reporting error

    }
    }];
    }
    }

    - (void) playMusic:(int) songNumber {
    NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
    NSString* song = [[NSString alloc] initWithString:@""];
    NSError* err;

    switch (songNumber) {
    case 1: //menu song
    song = @"/menu.MP3";
    break;
    case 2:
    song = @"/game.MP3";
    break;
    case 3:
    song = @"/winner.MP3";
    default:
    break;
    }

    if ([song isEqualToString:@""]){
    return ;
    }
    //NSLog(@"Playing song %d: %@",songNumber,song);
    resourcePath = [resourcePath stringByAppendingString:song];
    //NSLog(@"Path to play: %@", resourcePath);
    //Initialize our player pointing to the path to our resource
    if (playing && player) {
    [player stop];
    playing = false;
    }
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:
    [NSURL fileURLWithPath:resourcePath] error:&err];


    if( err ){
    //bail!
    NSLog(@"Failed with reason: %@", [err localizedDescription]);
    }
    else{
    //set our delegate and begin playback
    player.delegate = self;
    [player prepareToPlay];
    player.numberOfLoops = -1; //One Infinite Loop (music!) ;)
    [player play];
    playing = true;
    }

    }

    - (void) stopMusic {
    if (playing){
    [player stop];
    playing = false;
    }
    }
    @end