Skip to content

Commit b57c305

Browse files
committed
playtime support
1 parent dc86721 commit b57c305

File tree

4 files changed

+52
-13
lines changed

4 files changed

+52
-13
lines changed

include/GameAnalytics/GameAnalytics.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ namespace gameanalytics
116116
static std::string getABTestingId();
117117
static std::string getABTestingVariantId();
118118

119+
static int64_t getElapsedSessionTime();
120+
static int64_t getElapsedTimeFromAllSessions();
121+
119122
// game state changes
120123
// will affect how session is started / ended
121124
static void onResume();

source/gameanalytics/GAEvents.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ namespace gameanalytics
116116

117117
try
118118
{
119-
int64_t sessionLength = state.calculateSessionLength();
119+
// get session length in seconds
120+
int64_t sessionLength = state.calculateSessionLength<std::chrono::seconds>();
120121

121122
if(sessionLength < 0ll)
122123
{

source/gameanalytics/GAState.cpp

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace gameanalytics
5757
return;
5858
}
5959

60-
getInstance()._userId = id;
60+
getInstance()._customUserId = id;
6161
getInstance().cacheIdentifier();
6262
}
6363

@@ -383,6 +383,7 @@ namespace gameanalytics
383383
logging::GALogger::i("Ending session.");
384384
if (GAState::isEnabled() && GAState::sessionIsStarted())
385385
{
386+
getInstance().updateTotalSessionTime();
386387
events::GAEvents::addHealthEvent();
387388
events::GAEvents::addSessionEndEvent();
388389
getInstance()._sessionStart = 0;
@@ -505,17 +506,16 @@ namespace gameanalytics
505506

506507
void GAState::cacheIdentifier()
507508
{
508-
if(!_userId.empty())
509+
if(!_customUserId.empty())
509510
{
510-
_identifier = _userId;
511+
_identifier = _customUserId;
511512
}
512513
else
513514
{
514515
_identifier = _defaultUserId;
515516
}
516517

517518
logging::GALogger::d("identifier, {clean:%s}", _identifier.c_str());
518-
519519
}
520520

521521
std::string setStateFromCache(json& dict, std::string const& key, std::string const& value)
@@ -535,6 +535,11 @@ namespace gameanalytics
535535
return "";
536536
}
537537

538+
int64_t GAState::getTotalSessionLength() const
539+
{
540+
return _totalElapsedSessionTime + calculateSessionLength<std::chrono::seconds>();
541+
}
542+
538543
void GAState::ensurePersistedStates()
539544
{
540545
try
@@ -555,6 +560,9 @@ namespace gameanalytics
555560
}
556561
}
557562
}
563+
564+
std::string s = state_dict.dump();
565+
_gaLogger.d("state_dict: %s", s.c_str());
558566

559567
// insert into GAState instance
560568
std::string defaultId = utilities::getOptionalValue<std::string>(state_dict, "default_user_id");
@@ -575,6 +583,20 @@ namespace gameanalytics
575583
_currentCustomDimension01 = setStateFromCache(state_dict, "dimension01", _currentCustomDimension01);
576584
_currentCustomDimension02 = setStateFromCache(state_dict, "dimension02", _currentCustomDimension02);
577585
_currentCustomDimension03 = setStateFromCache(state_dict, "dimension03", _currentCustomDimension03);
586+
587+
try
588+
{
589+
std::string cachedSessionTime = utilities::getOptionalValue<std::string>(state_dict, "total_session_time", "0");
590+
591+
_totalElapsedSessionTime = std::stoull(cachedSessionTime);
592+
593+
}
594+
catch(const std::exception& e)
595+
{
596+
_gaLogger.w("Failed to read total_session_time from cache!");
597+
_totalElapsedSessionTime = 0;
598+
}
599+
578600

579601
// get cached init call values
580602
if (state_dict.contains("sdk_config_cached") && state_dict["sdk_config_cached"].is_string())
@@ -856,11 +878,6 @@ namespace gameanalytics
856878
return getInstance()._remoteConfigsIsReady;
857879
}
858880

859-
int64_t GAState::calculateSessionLength() const
860-
{
861-
return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::high_resolution_clock::now() - _startTimepoint).count();
862-
}
863-
864881
void GAState::addRemoteConfigsListener(const std::shared_ptr<IRemoteConfigsListener>& listener)
865882
{
866883
if(std::find(getInstance()._remoteConfigsListeners.begin(), getInstance()._remoteConfigsListeners.end(), listener) == getInstance()._remoteConfigsListeners.end())
@@ -1043,6 +1060,12 @@ namespace gameanalytics
10431060
return utilities::GAUtilities::timeIntervalSince1970();
10441061
}
10451062

1063+
void GAState::updateTotalSessionTime()
1064+
{
1065+
int64_t totalSessionTime = getTotalSessionLength();
1066+
_gaStore.setState("total_session_time", std::to_string(totalSessionTime));
1067+
}
1068+
10461069
std::string GAState::getBuild()
10471070
{
10481071
return getInstance()._build;

source/gameanalytics/GAState.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,15 @@ namespace gameanalytics
147147
static json getValidatedCustomFields();
148148
static json getValidatedCustomFields(const json& withEventFields);
149149

150-
int64_t calculateSessionLength() const;
150+
int64_t getTotalSessionLength() const;
151+
152+
template<typename T = std::chrono::milliseconds>
153+
inline int64_t calculateSessionLength() const
154+
{
155+
return std::chrono::duration_cast<T>(std::chrono::high_resolution_clock::now() - _startTimepoint).count();
156+
}
157+
158+
void populateConfigurations(json& sdkConfig);
151159

152160
private:
153161

@@ -174,8 +182,11 @@ namespace gameanalytics
174182
void validateAndFixCurrentDimensions();
175183
std::string getBuild();
176184

185+
void updateTotalSessionTime();
186+
177187
int64_t calculateServerTimeOffset(int64_t serverTs);
178-
void populateConfigurations(json& sdkConfig);
188+
189+
std::chrono::milliseconds getCurrentSessionLength() const;
179190

180191
void validateAndCleanCustomFields(const json& fields, json& out);
181192

@@ -192,7 +203,7 @@ namespace gameanalytics
192203
store::GAStore _gaStore;
193204
http::GAHTTPApi _gaHttp;
194205

195-
std::string _userId;
206+
std::string _customUserId;
196207
std::string _identifier;
197208

198209
bool _initialized = false;
@@ -202,6 +213,7 @@ namespace gameanalytics
202213
int64_t _sessionNum = 0;
203214
int64_t _transactionNum = 0;
204215

216+
int64_t _totalElapsedSessionTime = 0;
205217
std::chrono::high_resolution_clock::time_point _startTimepoint;
206218

207219
std::string _sessionId;

0 commit comments

Comments
 (0)