Pie Noon
An open source project by FPL.
 All Classes Pages
gpg_manager.h
1 // Copyright 2014 Google Inc. All rights reserved.
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 #ifndef GPG_MANAGER_H
16 #define GPG_MANAGER_H
17 
18 #include "common.h"
19 #include "gpg/achievement_manager.h"
20 #include "gpg/player_manager.h"
21 #include "gpg/types.h"
22 #include "pthread.h"
23 
24 namespace fpl {
25 
26 enum RequestState { kPending, kComplete, kFailed };
27 
29  std::string id;
30  uint64_t value;
31 };
32 
33 class GPGManager {
34  public:
35  GPGManager();
36 
37  // Start of initial initialization and auth.
38  bool Initialize(bool ui_login);
39 
40  // Call once a frame to allow us to track our async work.
41  void Update();
42 
43  // To be called from UI to sign out (if we were signed in) or sign back in
44  // (if we were signed out).
45  void ToggleSignIn();
46 
47  // Logged in status, can be shown in UI.
48  bool LoggedIn();
49 
50  struct GPGIds {
51  std::string leaderboard;
52  std::string event;
53  };
54 
55  // Request this stat to be saved for the logged in
56  // player. Does nothing if not logged in.
57  void IncrementEvent(const char *event_id, uint64_t score);
58 
59  void ShowLeaderboards(const GPGIds *ids, size_t id_len);
60  void ShowAchievements();
61 
62  // Asynchronously fetches the stats associated with the current player
63  // from the server. (Does nothing if not logged in.)
64  // The status of the data can be checked via event_data_state.
65  // const char* fields[]
66  void FetchEvents();
67  void FetchAchievements();
68 
69  // Asynchronously fetches the current player's info from the server.
70  // (Does nothing if not logged in.)
71  void FetchPlayer();
72 
73  RequestState event_data_state() const { return event_data_state_; }
74 
75  std::map<std::string, gpg::Event> &event_data() { return event_data_; }
76 
77  gpg::Player *player_data() const { return player_data_.get(); }
78 
79  uint64_t GetEventValue(std::string event_id);
80  bool IsAchievementUnlocked(std::string achievement_id);
81  void UnlockAchievement(std::string achievement_id);
82  void IncrementAchievement(std::string achievement_id);
83  void IncrementAchievement(std::string achievement_id, uint32_t steps);
84  void RevealAchievement(std::string achievement_id);
85 
86  private:
87  // These are the states the manager can be in, in sequential order they
88  // are expected to happen.
89  enum AsyncState {
90  kStart,
91  kAutoAuthStarted,
92  kAutoAuthFailed,
93  kManualSignBackIn,
94  kAuthUILaunched,
95  kAuthUIStarted,
96  kAuthUIFailed,
97  kAuthed,
98  };
99 
100  AsyncState state_;
101  bool do_ui_login_;
102  bool delayed_login_;
103  std::unique_ptr<gpg::GameServices> game_services_;
104 
105  void UpdatePlayerStats();
106 
107  // The stats the stats currently stored on the server.
108  // Retrieved after authentication.
109  bool event_data_initialized_;
110  bool achievement_data_initialized_;
111  RequestState event_data_state_;
112  RequestState achievement_data_state_;
113  static pthread_mutex_t events_mutex_;
114  static pthread_mutex_t achievements_mutex_;
115  static pthread_mutex_t players_mutex_;
116  std::map<std::string, gpg::Event> event_data_;
117  std::unique_ptr<gpg::Player> player_data_;
118  std::vector<gpg::Achievement> achievement_data_;
119 };
120 
121 } // fpl
122 
123 #endif // GPG_MANAGER_H
Definition: gpg_manager.h:50
Definition: gpg_manager.h:28
Definition: gpg_manager.h:33