|
| 1 | +// Copyright (c) 2014 Google Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | + |
| 16 | +#include "jni/StateManager.h" |
| 17 | + |
| 18 | +#ifdef __APPLE__ |
| 19 | +// Logging for CoreFoundation |
| 20 | +#include <CoreFoundation/CoreFoundation.h> |
| 21 | + |
| 22 | +extern "C" void NSLog(CFStringRef format, ...); |
| 23 | +const int32_t BUFFER_SIZE = 256; |
| 24 | + |
| 25 | +// Wrap macro in do/while to ensure ; |
| 26 | +#define LOGI(...) do { \ |
| 27 | + char c[BUFFER_SIZE]; \ |
| 28 | + snprintf(c, BUFFER_SIZE, __VA_ARGS__); \ |
| 29 | + CFStringRef str = CFStringCreateWithCString(kCFAllocatorDefault, c, \ |
| 30 | + kCFStringEncodingMacRoman); \ |
| 31 | + NSLog(str); \ |
| 32 | + CFRelease(str); \ |
| 33 | + } while (false) |
| 34 | + |
| 35 | +#else |
| 36 | + |
| 37 | +#include "android/Log.h" |
| 38 | +#define DEBUG_TAG "TeapotNativeActivity" |
| 39 | +#define LOGI(...) \ |
| 40 | + ((void)__android_log_print(ANDROID_LOG_INFO, DEBUG_TAG, __VA_ARGS__)) |
| 41 | + |
| 42 | +#endif |
| 43 | + |
| 44 | +#include "gpg/achievement_manager.h" |
| 45 | +bool StateManager::is_auth_in_progress_ = false; |
| 46 | +std::unique_ptr<gpg::GameServices> StateManager::game_services_; |
| 47 | + |
| 48 | +void OnAuthActionFinished(gpg::AuthOperation op, gpg::AuthStatus status) { |
| 49 | + LOGI("OnAuthActionFinished"); |
| 50 | +} |
| 51 | + |
| 52 | +void OnAuthActionStarted(gpg::AuthOperation op) { |
| 53 | + LOGI("OnAuthActionStarted"); |
| 54 | + switch ( op ) { |
| 55 | + case gpg::AuthOperation::SIGN_IN: |
| 56 | + LOGI("Signing In"); |
| 57 | + break; |
| 58 | + case gpg::AuthOperation::SIGN_OUT: |
| 59 | + LOGI("Signing Out"); |
| 60 | + break; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +gpg::GameServices *StateManager::GetGameServices() { |
| 65 | + return game_services_.get(); |
| 66 | +} |
| 67 | + |
| 68 | +void StateManager::BeginUserInitiatedSignIn() { |
| 69 | + if (!game_services_->IsAuthorized()) { |
| 70 | + LOGI("StartAuthorizationUI"); |
| 71 | + game_services_->StartAuthorizationUI(); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +void StateManager::SignOut() { |
| 76 | + if (game_services_->IsAuthorized()) { |
| 77 | + LOGI("SignOut"); |
| 78 | + game_services_->SignOut(); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +void StateManager::UnlockAchievement(char const *achievement_id) { |
| 83 | + if (game_services_->IsAuthorized()) { |
| 84 | + LOGI("Achievement unlocked"); |
| 85 | + game_services_->Achievements().Unlock(achievement_id); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +void StateManager::SubmitHighScore(char const *leaderboard_id, uint64_t score) { |
| 90 | + if (game_services_->IsAuthorized()) { |
| 91 | + LOGI("High score submitted"); |
| 92 | + game_services_->Leaderboards().SubmitScore(leaderboard_id, score); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +void StateManager::ShowAchievements() { |
| 97 | + if (game_services_->IsAuthorized()) { |
| 98 | + LOGI("Show achievement"); |
| 99 | + game_services_->Achievements().ShowAllUI(); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +void StateManager::ShowLeaderboard(char const *leaderboard_id) { |
| 104 | + if (game_services_->IsAuthorized()) { |
| 105 | + LOGI("Show achievement"); |
| 106 | + game_services_->Leaderboards().ShowUI(leaderboard_id); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | +void StateManager::InitServices( |
| 112 | + gpg::PlatformConfiguration const &pc, |
| 113 | + gpg::GameServices::Builder::OnAuthActionStartedCallback started_callback, |
| 114 | + gpg::GameServices::Builder::OnAuthActionFinishedCallback |
| 115 | + finished_callback) { |
| 116 | + LOGI("Initializing Services"); |
| 117 | + if (!game_services_) { |
| 118 | + LOGI("Uninitialized services, so creating"); |
| 119 | + game_services_ = gpg::GameServices::Builder() |
| 120 | + .SetLogging(gpg::DEFAULT_ON_LOG, gpg::LogLevel::VERBOSE) |
| 121 | + .SetOnAuthActionStarted([started_callback](gpg::AuthOperation op) { |
| 122 | + is_auth_in_progress_ = true; |
| 123 | + started_callback(op); |
| 124 | + }) |
| 125 | + .SetOnAuthActionFinished([finished_callback](gpg::AuthOperation op, |
| 126 | + gpg::AuthStatus status) { |
| 127 | + LOGI("Sign in finished with a result of %d", status); |
| 128 | + is_auth_in_progress_ = false; |
| 129 | + finished_callback(op, status); |
| 130 | + LOGI("Fetching all blocking"); |
| 131 | + gpg::AchievementManager::FetchAllResponse fetchResponse = game_services_->Achievements().FetchAllBlocking(std::chrono::milliseconds(1000)); |
| 132 | + LOGI("--------------------------------------------------------------"); |
| 133 | + |
| 134 | + LOGI("Fetching all nonblocking"); |
| 135 | + game_services_->Achievements().FetchAll(gpg::DataSource::CACHE_OR_NETWORK, [] (gpg::AchievementManager::FetchAllResponse response) {LOGI("Achievement response status: %d", response.status);}); |
| 136 | + LOGI("--------------------------------------------------------------"); |
| 137 | + |
| 138 | + }) |
| 139 | + .Create(pc); |
| 140 | + } |
| 141 | + LOGI("Created"); |
| 142 | +} |
0 commit comments