-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathGAState.h
More file actions
287 lines (233 loc) · 10.9 KB
/
GAState.h
File metadata and controls
287 lines (233 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//
// GA-SDK-CPP
// Copyright 2018 GameAnalytics C++ SDK. All rights reserved.
//
#pragma once
#include <vector>
#include <map>
#include <functional>
#include <mutex>
#include <cstdlib>
#include <unordered_map>
#include "GACommon.h"
#include "GAUtilities.h"
#include "GALogger.h"
#include "GAThreading.h"
#include "GAStore.h"
#include "GAEvents.h"
#include "GAHTTPApi.h"
#include "GADevice.h"
namespace gameanalytics
{
namespace state
{
struct ProgressionTries
{
public:
inline void addOrUpdate(std::string const& s, int tries)
{
_tries[s] = tries;
}
inline void remove(std::string const& s)
{
if (_tries.count(s))
{
_tries.erase(s);
}
}
inline int getTries(std::string const& s) const
{
if (_tries.count(s))
{
return _tries.at(s);
}
return 0;
}
inline int incrementTries(std::string const& s)
{
if (_tries.count(s))
{
_tries[s]++;
}
else
{
_tries[s] = 1;
}
return _tries[s];
}
private:
std::unordered_map<std::string, int> _tries;
};
class GAState
{
friend class threading::GAThreading;
friend class events::GAEvents;
friend class device::GADevice;
friend class logging::GALogger;
friend class store::GAStore;
friend class http::GAHTTPApi;
public:
static GAState& getInstance();
static bool isDestroyed();
static void setUserId(std::string const& id);
static void setExternalUserId(std::string const& id);
static bool isInitialized();
static int64_t getSessionStart();
static int64_t getSessionNum();
static int64_t getTransactionNum();
static std::string getSessionId();
static std::string getCurrentCustomDimension01();
static std::string getCurrentCustomDimension02();
static std::string getCurrentCustomDimension03();
static void getGlobalCustomEventFields(json& out);
static std::string getGameKey();
static std::string getGameSecret();
static void setAvailableCustomDimensions01(const StringVector& dimensions);
static void setAvailableCustomDimensions02(const StringVector& dimensions);
static void setAvailableCustomDimensions03(const StringVector& dimensions);
static void setAvailableResourceCurrencies(const StringVector& availableResourceCurrencies);
static void setAvailableResourceItemTypes(const StringVector& availableResourceItemTypes);
static void setBuild(std::string const& build);
static bool isEnabled();
static void setCustomDimension01(std::string const& dimension);
static void setCustomDimension02(std::string const& dimension);
static void setCustomDimension03(std::string const& dimension);
static void setGlobalCustomEventFields(std::string const& customFields);
static void incrementSessionNum();
static void incrementTransactionNum();
static void incrementProgressionTries(std::string const& progression);
static int getProgressionTries(std::string const& progression);
static void clearProgressionTries(std::string const& progression);
static bool hasAvailableCustomDimensions01(std::string const& dimension1);
static bool hasAvailableCustomDimensions02(std::string const& dimension2);
static bool hasAvailableCustomDimensions03(std::string const& dimension3);
static bool hasAvailableResourceCurrency(std::string const& currency);
static bool hasAvailableResourceItemType(std::string const& itemType);
static void setKeys(std::string const& gameKey, std::string const& gameSecret);
static void endSessionAndStopQueue(bool endThread);
static void resumeSessionAndStartQueue();
static void getEventAnnotations(json& out);
static void getSdkErrorEventAnnotations(json& out);
static void getInitAnnotations(json& out);
static void internalInitialize();
static int64_t getClientTsAdjusted();
static void setManualSessionHandling(bool flag);
static bool useManualSessionHandling();
static void setEnableErrorReporting(bool flag);
static bool useErrorReporting();
static void setEnabledEventSubmission(bool flag);
static bool isEventSubmissionEnabled();
static bool sessionIsStarted();
static bool isRemoteConfigsReady();
static void addRemoteConfigsListener(const std::shared_ptr<IRemoteConfigsListener>& listener);
static void removeRemoteConfigsListener(const std::shared_ptr<IRemoteConfigsListener>& listener);
static std::string getRemoteConfigsContentAsString();
static std::string getAbId();
static std::string getAbVariantId();
static std::string getUserId();
static std::string getExternalUserId();
static json getValidatedCustomFields();
static json getValidatedCustomFields(const json& withEventFields);
template<typename T>
inline static T getRemoteConfigsValue(std::string const& key, T const& defaultValue)
{
std::lock_guard<std::recursive_mutex> lg(getInstance()._mtx);
if(getInstance()._gameRemoteConfigsJson.contains(key))
{
json& config = getInstance()._gameRemoteConfigsJson[key];
T value = utilities::getOptionalValue<T>(config, "value", defaultValue);
return value;
}
return defaultValue;
}
template<typename T = std::chrono::milliseconds>
inline int64_t calculateSessionLength() const
{
auto len = std::chrono::high_resolution_clock::now() - _startTimepoint;
return std::chrono::duration_cast<T>(len).count();
}
int64_t getTotalSessionLength() const;
int64_t getLastSessionLength() const;
void populateConfigurations(json& sdkConfig);
json getRemoteConfigAnnotations();
private:
GAState();
~GAState();
GAState(const GAState&) = delete;
GAState& operator=(const GAState&) = delete;
static constexpr const char* CategorySdkError = "sdk_error";
template<typename ...args_t>
void LogAndAddErrorEvent(EGAErrorSeverity severity, std::string const& fmt, args_t&&... args)
{
const std::string msg = utilities::printString(fmt, std::forward<args_t>(args)...);
logging::GALogger::w(msg.c_str());
addErrorEvent(severity, msg);
}
void setDefaultUserId(std::string const& id);
json& getSdkConfig();
void cacheIdentifier();
void ensurePersistedStates();
void startNewSession();
void validateAndFixCurrentDimensions();
std::string getBuild();
void updateTotalSessionTime();
int64_t calculateServerTimeOffset(int64_t serverTs);
void validateAndCleanCustomFields(const json& fields, json& out);
void setConfigsHash(std::string const& configsHash);
void setAbId(std::string const& abId);
void setAbVariantId(std::string const& abVariantId);
void addErrorEvent(EGAErrorSeverity severity, std::string const& message);
void buildRemoteConfigsJsons(const json& remoteCfgs);
threading::GAThreading _gaThread;
events::GAEvents _gaEvents;
device::GADevice _gaDevice;
logging::GALogger _gaLogger;
store::GAStore _gaStore;
http::GAHTTPApi _gaHttp;
std::string _customUserId;
std::string _identifier;
bool _initialized = false;
bool _adjustTimestamp = false;
int64_t _sessionStart = 0;
int64_t _sessionNum = 0;
int64_t _transactionNum = 0;
int64_t _lastSessionTime = 0;
int64_t _totalElapsedSessionTime = 0;
std::chrono::high_resolution_clock::time_point _startTimepoint;
std::string _sessionId;
std::string _currentCustomDimension01;
std::string _currentCustomDimension02;
std::string _currentCustomDimension03;
json _currentGlobalCustomEventFields;
std::string _gameKey;
std::string _gameSecret;
StringVector _availableCustomDimensions01;
StringVector _availableCustomDimensions02;
StringVector _availableCustomDimensions03;
StringVector _availableResourceCurrencies;
StringVector _availableResourceItemTypes;
std::string _build;
bool _initAuthorized = false;
bool _enabled = false;
int64_t _clientServerTimeOffset = 0;
std::string _defaultUserId;
std::string _configsHash;
std::string _abId;
std::string _abVariantId;
std::string _externalUserId;
ProgressionTries _progressionTries;
json _sdkConfigDefault;
json _sdkConfig;
json _sdkConfigCached;
bool _useManualSessionHandling = false;
bool _enableErrorReporting = true;
bool _enableEventSubmission = true;
bool _enableIdTracking = true;
json _gameRemoteConfigsJson;
json _trackingRemoteConfigsJson;
bool _remoteConfigsIsReady;
std::vector<std::shared_ptr<IRemoteConfigsListener>> _remoteConfigsListeners;
std::recursive_mutex _mtx;
};
}
}