summaryrefslogtreecommitdiff
path: root/platform/iphone/game_center.mm
diff options
context:
space:
mode:
Diffstat (limited to 'platform/iphone/game_center.mm')
-rw-r--r--platform/iphone/game_center.mm171
1 files changed, 82 insertions, 89 deletions
diff --git a/platform/iphone/game_center.mm b/platform/iphone/game_center.mm
index 2593134512..f9bc70b7c4 100644
--- a/platform/iphone/game_center.mm
+++ b/platform/iphone/game_center.mm
@@ -46,24 +46,23 @@ extern "C" {
#import "app_delegate.h"
};
-GameCenter* GameCenter::instance = NULL;
+GameCenter *GameCenter::instance = NULL;
void GameCenter::_bind_methods() {
- ClassDB::bind_method(D_METHOD("connect"),&GameCenter::connect);
- ClassDB::bind_method(D_METHOD("is_connected"),&GameCenter::is_connected);
-
- ClassDB::bind_method(D_METHOD("post_score"),&GameCenter::post_score);
- ClassDB::bind_method(D_METHOD("award_achievement"),&GameCenter::award_achievement);
- ClassDB::bind_method(D_METHOD("reset_achievements"),&GameCenter::reset_achievements);
- ClassDB::bind_method(D_METHOD("request_achievements"),&GameCenter::request_achievements);
- ClassDB::bind_method(D_METHOD("request_achievement_descriptions"),&GameCenter::request_achievement_descriptions);
- ClassDB::bind_method(D_METHOD("show_game_center"),&GameCenter::show_game_center);
-
- ClassDB::bind_method(D_METHOD("get_pending_event_count"),&GameCenter::get_pending_event_count);
- ClassDB::bind_method(D_METHOD("pop_pending_event"),&GameCenter::pop_pending_event);
+ ClassDB::bind_method(D_METHOD("connect"), &GameCenter::connect);
+ ClassDB::bind_method(D_METHOD("is_connected"), &GameCenter::is_connected);
+
+ ClassDB::bind_method(D_METHOD("post_score"), &GameCenter::post_score);
+ ClassDB::bind_method(D_METHOD("award_achievement"), &GameCenter::award_achievement);
+ ClassDB::bind_method(D_METHOD("reset_achievements"), &GameCenter::reset_achievements);
+ ClassDB::bind_method(D_METHOD("request_achievements"), &GameCenter::request_achievements);
+ ClassDB::bind_method(D_METHOD("request_achievement_descriptions"), &GameCenter::request_achievement_descriptions);
+ ClassDB::bind_method(D_METHOD("show_game_center"), &GameCenter::show_game_center);
+
+ ClassDB::bind_method(D_METHOD("get_pending_event_count"), &GameCenter::get_pending_event_count);
+ ClassDB::bind_method(D_METHOD("pop_pending_event"), &GameCenter::pop_pending_event);
};
-
Error GameCenter::connect() {
//if this class isn't available, game center isn't implemented
@@ -72,33 +71,34 @@ Error GameCenter::connect() {
return ERR_UNAVAILABLE;
}
- GKLocalPlayer* player = [GKLocalPlayer localPlayer];
+ GKLocalPlayer *player = [GKLocalPlayer localPlayer];
ERR_FAIL_COND_V(![player respondsToSelector:@selector(authenticateHandler)], ERR_UNAVAILABLE);
- ViewController *root_controller=(ViewController *)((AppDelegate *)[[UIApplication sharedApplication] delegate]).window.rootViewController;
+ ViewController *root_controller = (ViewController *)((AppDelegate *)[[UIApplication sharedApplication] delegate]).window.rootViewController;
ERR_FAIL_COND_V(!root_controller, FAILED);
- //this handler is called serveral times. first when the view needs to be shown, then again after the view is cancelled or the user logs in. or if the user's already logged in, it's called just once to confirm they're authenticated. This is why no result needs to be specified in the presentViewController phase. in this case, more calls to this function will follow.
+ // This handler is called several times. First when the view needs to be shown, then again
+ // after the view is cancelled or the user logs in. Or if the user's already logged in, it's
+ // called just once to confirm they're authenticated. This is why no result needs to be specified
+ // in the presentViewController phase. In this case, more calls to this function will follow.
player.authenticateHandler = (^(UIViewController *controller, NSError *error) {
- if (controller) {
- [root_controller presentViewController:controller animated:YES completion:nil];
- }
- else {
- Dictionary ret;
- ret["type"] = "authentication";
- if (player.isAuthenticated) {
- ret["result"] = "ok";
- GameCenter::get_singleton()->connected = true;
- } else {
- ret["result"] = "error";
- ret["error_code"] = error.code;
- ret["error_description"] = [error.localizedDescription UTF8String];
- GameCenter::get_singleton()->connected = false;
- };
-
- pending_events.push_back(ret);
- };
-
+ if (controller) {
+ [root_controller presentViewController:controller animated:YES completion:nil];
+ } else {
+ Dictionary ret;
+ ret["type"] = "authentication";
+ if (player.isAuthenticated) {
+ ret["result"] = "ok";
+ GameCenter::get_singleton()->connected = true;
+ } else {
+ ret["result"] = "error";
+ ret["error_code"] = error.code;
+ ret["error_description"] = [error.localizedDescription UTF8String];
+ GameCenter::get_singleton()->connected = false;
+ };
+
+ pending_events.push_back(ret);
+ };
});
return OK;
@@ -115,26 +115,27 @@ Error GameCenter::post_score(Variant p_score) {
float score = params["score"];
String category = params["category"];
- NSString* cat_str = [[[NSString alloc] initWithUTF8String:category.utf8().get_data()] autorelease];
- GKScore* reporter = [[[GKScore alloc] initWithCategory:cat_str] autorelease];
+ NSString *cat_str = [[[NSString alloc] initWithUTF8String:category.utf8().get_data()] autorelease];
+ GKScore *reporter = [[[GKScore alloc] initWithCategory:cat_str] autorelease];
reporter.value = score;
ERR_FAIL_COND_V([GKScore respondsToSelector:@selector(reportScores)], ERR_UNAVAILABLE);
- [GKScore reportScores:@[reporter] withCompletionHandler:^(NSError* error) {
+ [GKScore reportScores:@[ reporter ]
+ withCompletionHandler:^(NSError *error) {
- Dictionary ret;
- ret["type"] = "post_score";
- if (error == nil) {
- ret["result"] = "ok";
- } else {
- ret["result"] = "error";
- ret["error_code"] = error.code;
- ret["error_description"] = [error.localizedDescription UTF8String];
- };
+ Dictionary ret;
+ ret["type"] = "post_score";
+ if (error == nil) {
+ ret["result"] = "ok";
+ } else {
+ ret["result"] = "error";
+ ret["error_code"] = error.code;
+ ret["error_description"] = [error.localizedDescription UTF8String];
+ };
- pending_events.push_back(ret);
- }];
+ pending_events.push_back(ret);
+ }];
return OK;
};
@@ -146,8 +147,8 @@ Error GameCenter::award_achievement(Variant p_params) {
String name = params["name"];
float progress = params["progress"];
- NSString* name_str = [[[NSString alloc] initWithUTF8String:name.utf8().get_data()] autorelease];
- GKAchievement* achievement = [[[GKAchievement alloc] initWithIdentifier: name_str] autorelease];
+ NSString *name_str = [[[NSString alloc] initWithUTF8String:name.utf8().get_data()] autorelease];
+ GKAchievement *achievement = [[[GKAchievement alloc] initWithIdentifier:name_str] autorelease];
ERR_FAIL_COND_V(!achievement, FAILED);
ERR_FAIL_COND_V([GKAchievement respondsToSelector:@selector(reportAchievements)], ERR_UNAVAILABLE);
@@ -158,19 +159,20 @@ Error GameCenter::award_achievement(Variant p_params) {
achievement.showsCompletionBanner = params["show_completion_banner"] ? YES : NO;
}
- [GKAchievement reportAchievements:@[achievement] withCompletionHandler:^(NSError *error) {
+ [GKAchievement reportAchievements:@[ achievement ]
+ withCompletionHandler:^(NSError *error) {
- Dictionary ret;
- ret["type"] = "award_achievement";
- if (error == nil) {
- ret["result"] = "ok";
- } else {
- ret["result"] = "error";
- ret["error_code"] = error.code;
- };
+ Dictionary ret;
+ ret["type"] = "award_achievement";
+ if (error == nil) {
+ ret["result"] = "ok";
+ } else {
+ ret["result"] = "error";
+ ret["error_code"] = error.code;
+ };
- pending_events.push_back(ret);
- }];
+ pending_events.push_back(ret);
+ }];
return OK;
};
@@ -191,11 +193,11 @@ void GameCenter::request_achievement_descriptions() {
Array hidden;
Array replayable;
- for (int i=0; i<[descriptions count]; i++) {
+ for (int i = 0; i < [descriptions count]; i++) {
- GKAchievementDescription* description = [descriptions objectAtIndex:i];
+ GKAchievementDescription *description = [descriptions objectAtIndex:i];
- const char* str = [description.identifier UTF8String];
+ const char *str = [description.identifier UTF8String];
names.push_back(String::utf8(str != NULL ? str : ""));
str = [description.title UTF8String];
@@ -231,7 +233,6 @@ void GameCenter::request_achievement_descriptions() {
}];
};
-
void GameCenter::request_achievements() {
[GKAchievement loadAchievementsWithCompletionHandler:^(NSArray *achievements, NSError *error) {
@@ -243,10 +244,10 @@ void GameCenter::request_achievements() {
PoolStringArray names;
PoolRealArray percentages;
- for (int i=0; i<[achievements count]; i++) {
+ for (int i = 0; i < [achievements count]; i++) {
- GKAchievement* achievement = [achievements objectAtIndex:i];
- const char* str = [achievement.identifier UTF8String];
+ GKAchievement *achievement = [achievements objectAtIndex:i];
+ const char *str = [achievement.identifier UTF8String];
names.push_back(String::utf8(str != NULL ? str : ""));
percentages.push_back(achievement.percentComplete);
@@ -266,8 +267,7 @@ void GameCenter::request_achievements() {
void GameCenter::reset_achievements() {
- [GKAchievement resetAchievementsWithCompletionHandler:^(NSError *error)
- {
+ [GKAchievement resetAchievementsWithCompletionHandler:^(NSError *error) {
Dictionary ret;
ret["type"] = "reset_achievements";
if (error == nil) {
@@ -292,17 +292,13 @@ Error GameCenter::show_game_center(Variant p_params) {
String view_name = params["view"];
if (view_name == "default") {
view_state = GKGameCenterViewControllerStateDefault;
- }
- else if (view_name == "leaderboards") {
+ } else if (view_name == "leaderboards") {
view_state = GKGameCenterViewControllerStateLeaderboards;
- }
- else if (view_name == "achievements") {
+ } else if (view_name == "achievements") {
view_state = GKGameCenterViewControllerStateAchievements;
- }
- else if (view_name == "challenges") {
+ } else if (view_name == "challenges") {
view_state = GKGameCenterViewControllerStateChallenges;
- }
- else {
+ } else {
return ERR_INVALID_PARAMETER;
}
}
@@ -310,7 +306,7 @@ Error GameCenter::show_game_center(Variant p_params) {
GKGameCenterViewController *controller = [[GKGameCenterViewController alloc] init];
ERR_FAIL_COND_V(!controller, FAILED);
- ViewController *root_controller=(ViewController *)((AppDelegate *)[[UIApplication sharedApplication] delegate]).window.rootViewController;
+ ViewController *root_controller = (ViewController *)((AppDelegate *)[[UIApplication sharedApplication] delegate]).window.rootViewController;
ERR_FAIL_COND_V(!root_controller, FAILED);
controller.gameCenterDelegate = root_controller;
@@ -319,12 +315,12 @@ Error GameCenter::show_game_center(Variant p_params) {
controller.leaderboardIdentifier = nil;
if (params.has("leaderboard_name")) {
String name = params["leaderboard_name"];
- NSString* name_str = [[[NSString alloc] initWithUTF8String:name.utf8().get_data()] autorelease];
+ NSString *name_str = [[[NSString alloc] initWithUTF8String:name.utf8().get_data()] autorelease];
controller.leaderboardIdentifier = name_str;
}
}
- [root_controller presentViewController: controller animated: YES completion:nil];
+ [root_controller presentViewController:controller animated:YES completion:nil];
return OK;
};
@@ -332,7 +328,7 @@ Error GameCenter::show_game_center(Variant p_params) {
void GameCenter::game_center_closed() {
Dictionary ret;
- ret["type"] = "show_game_center";
+ ret["type"] = "show_game_center";
ret["result"] = "ok";
pending_events.push_back(ret);
}
@@ -350,7 +346,7 @@ Variant GameCenter::pop_pending_event() {
return front;
};
-GameCenter* GameCenter::get_singleton() {
+GameCenter *GameCenter::get_singleton() {
return instance;
};
@@ -360,9 +356,6 @@ GameCenter::GameCenter() {
connected = false;
};
-
-GameCenter::~GameCenter() {
-
-};
+GameCenter::~GameCenter(){};
#endif