VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
PlayerProfile.h
1 /*
2  * Copyright (C) 2014 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef PLAYERPROFILE_H
18 #define PLAYERPROFILE_H
19 
20 #include <QJsonArray>
21 #include <QJsonDocument>
22 #include <QMap>
23 #include <QObject>
24 #include <QSet>
25 #include <QSharedPointer>
26 
27 class Achievement;
28 class Environment;
29 class LevelInfo;
30 class LevelProgression;
32 
44 class PlayerProfile : public QObject {
45  Q_OBJECT
46 public:
50  enum LoadState {
63  };
64 
68  LoadState getLoadState() const { return mLoadState; }
74  LevelInfo* getLastPlayedLevelInfo(const QString& progressionName) const;
81  bool isEnvironmentUnlocked(const QString& progressionName,
82  const QString& environmentName) const;
89  bool isLevelUnlocked(const QString& progressionName, const QString& levelName) const;
95  int getCurrentPlayerScore(int playerId = 0) const;
101  const QMap<int, int>& getCurrentPlayerScores() const { return mCurrentScores; }
107  int updatePlayerScore(int playerId, int amountChanged);
117  int getNumStars(const QString& progressionName, const QString& levelName) const;
124  bool hasMinStars(const QString& progressionName, int minStars) const;
132  bool hasMinStars(const QString& progressionName, const QString& environmentName,
133  int minStars) const;
134 
142  void unlockAchievement(const QString& name, bool immediately);
151  void incrementAchievement(const QString& name, int numSteps, bool immediately);
161  void setAchievementSteps(const QString& name, int minSteps, bool immediately);
169  void revealAchievement(const QString& name, bool immediately);
175  void showAchievements();
176 
181  void startLoad();
189  bool tryFinishLoad();
194  void save();
203  void reset(bool permanently);
204 
208  bool hasCloudSave() const;
212  bool isSignedIntoCloud() const;
217  bool cloudSignInFailed() const;
221  void signIntoCloud();
225  void signOutOfCloud();
226 
231 
232 signals:
237  void newDataMerged();
238 
239 private slots:
240  void onCloudDataLoaded(int statusCode, const QString& data);
241  QString onCloudDataConflict(const QString& localData, const QString& serverData);
242  void onCurrentLevelAbandoned();
248  bool onCurrentLevelCompleted(const QString& progressionName, int numStars = -1);
249  void onLevelSelected(const QString& progressionName, const QString& levelName,
250  bool forceUnlock);
251 
252 private:
253  enum CloudLoadStatus {
254  STATUS_OK = 0x0,
255  STATUS_INTERNAL_ERROR = 0x01,
256  STATUS_CLIENT_RECONNECT_REQUIRED = 0x02,
257  STATUS_NETWORK_ERROR_STALE_DATA = 0x03,
258  STATUS_NETWORK_ERROR_NO_DATA = 0x04,
259  STATUS_NETWORK_ERROR_OPERATION_DEFERRED = 0x05,
260  STATUS_NETWORK_ERROR_OPERATION_FAILED = 0x06,
261  STATUS_DEVELOPER_ERROR = 0x07,
262  STATUS_WRITE_OUT_OF_DATE_VERSION = 0x7d0,
263  STATUS_WRITE_SIZE_EXCEEDED = 0x7d1,
264  STATUS_STATE_KEY_NOT_FOUND = 0x7d2,
265  STATUS_STATE_KEY_LIMIT_EXCEEDED = 0x7d3,
266  };
267 
268  // This class is exclusively a helper for Game, and can only be construted by it.
269  friend class Game;
270 
271  explicit PlayerProfile(QObject* parent = nullptr);
272  PlayerProfile(const QJsonDocument& doc, const LevelProgressionList* progressionList);
273 
274  void setLevelProgressionList(const LevelProgressionList* value);
275  void init();
276  void merge(const PlayerProfile& other);
277  int getValidPlayerStarScore(int starScore) const;
278  bool startLoadFromCloud();
279  bool tryFinishLoadFromCloud();
280  void loadFromLocal();
281  void saveToCloud();
282  void saveToLocal();
283  void clearCloud();
284  void clearLocal();
285  void clearAchievements();
286  QJsonArray getAchievementsAsJsonArray() const;
287  QSharedPointer<Achievement>& getAchievement(const QString& name);
288 
289  LoadState mLoadState = UNLOADED;
290  bool mHasCloudData = false;
291 
292  const LevelProgressionList* mLevelProgressionList = nullptr;
293 
294  QMap<QString, QString> mLastPlayedLevels;
295  QMap<QString, QSet<QString>> mUnlockedLevels;
296  QMap<int, int> mCurrentScores;
298  // Achievement updates / actions.
299  // NOTE: Using a QSharedPointer because regular smart pointers do not work in Qt Containers
301 };
302 Q_DECLARE_METATYPE(PlayerProfile*)
303 
304 #endif // PLAYERPROFILE_H
bool isEnvironmentUnlocked(const QString &progressionName, const QString &environmentName) const
Returns whether or not the Environment denoted by environmentName in the LevelProgression denoted by ...
QJsonDocument toJsonDocument()
Returns the PlayerProfile serialized as a JSON document.
LevelInfo * getLastPlayedLevelInfo(const QString &progressionName) const
Returns the LevelInfo of the level the player most recently played (or was set to play) in progressio...
int getCurrentPlayerScore(int playerId=0) const
Returns the current score for playerId.
LoadState
Current status of persistent data in the player profile.
Definition: PlayerProfile.h:50
No state has been loaded.
Definition: PlayerProfile.h:54
Available state has been loaded from local and cloud, if necessary.
Definition: PlayerProfile.h:62
void incrementAchievement(const QString &name, int numSteps, bool immediately)
Increments a Google Play Games Services incremental achievement.
void saveAchievementsToCloud()
Tries to push all buffered achievement state updates to the cloud.
bool tryFinishLoad()
Attempts to complete the loading of the PlayerProfile which was asynchronously started with startLoad...
void save()
Saves the permanent state of the PlayerProfile to local storage and, if available, cloud storage.
int updatePlayerScore(int playerId, int amountChanged)
Updates the current level score for playerId by amountChanged.
Representation of a Google Play Games Services achievement.
Definition: Achievement.h:36
bool isSignedIntoCloud() const
Returns whether or not the player is signed into Google Play Games Services.
void newDataMerged()
Emitted when new data from either local or cloud storage has been merged into the PlayerProfile...
Metadata for a Level.
Definition: LevelInfo.h:31
bool isLevelUnlocked(const QString &progressionName, const QString &levelName) const
Returns whether or not the Environment denoted by levelName in the LevelProgression denoted by progre...
void unlockAchievement(const QString &name, bool immediately)
Unlocks a Google Play Games Services achievement.
void showAchievements()
Requests that the PlayerProfile launch a view of the Google Play Games Services achievements.
void init()
Initialize the game.
int getNumStars(const QString &progressionName, const QString &levelName) const
Returns the number of star achieved by the player on the level denoted by levelName in the LevelProgr...
void startLoad()
Asynchronously starts a full load of the PlayerProfile from local storage and, if available...
Grouping of related Environments together into a logical, ordered list.
Definition: LevelProgression.h:34
void reset(bool permanently)
Clears the temporary and permanent data loaded into the PlayerProfile.
The top-level Game object for VoltAir.
Definition: Game.h:35
bool hasCloudSave() const
Returns whether or not the device supports cloud save.
void signIntoCloud()
Attempts to sign the player into Google Play Games Services.
Represents a player's profile or "save game" state.
Definition: PlayerProfile.h:44
Collection of LevelProgressions.
Definition: LevelProgressionList.h:31
bool cloudSignInFailed() const
Returns whether or not a previous attempt to sign into Google Play Games Services failed...
void signOutOfCloud()
Signs the player out of Google Play Games Services.
void revealAchievement(const QString &name, bool immediately)
Reveals a Google Play Games Services achievement.
const QMap< int, int > & getCurrentPlayerScores() const
Returns the scores for all players for the current level.
Definition: PlayerProfile.h:101
void setAchievementSteps(const QString &name, int minSteps, bool immediately)
Sets a Google Play Games Services incremental achievement to have a minimum number of steps...
bool hasMinStars(const QString &progressionName, int minStars) const
Returns whether or not the player has achieved at least a minimum number of stars on each level in th...
Grouping of related LevelInfos together into an logical, ordered list.
Definition: Environment.h:37
QObject * parent() const
LoadState getLoadState() const
Returns the current LoadState of the PlayerProfile.
Definition: PlayerProfile.h:68
In the process of loading state from local and/or cloud.
Definition: PlayerProfile.h:58