Skip to content

Commit bea3925

Browse files
committed
remote configs v3 support
1 parent 8904879 commit bea3925

File tree

2 files changed

+56
-22
lines changed

2 files changed

+56
-22
lines changed

source/gameanalytics/GAState.cpp

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ namespace gameanalytics
413413
// remote configs configurations
414414
if(getInstance()._configurations.is_object() && !getInstance()._configurations.empty())
415415
{
416-
out["configurations"] = getInstance()._configurations;
416+
out["configurations_v3"] = getInstance().getRemoteConfigAnnotations();
417417
}
418418

419419
out["sdk_version"] = device::GADevice::getRelevantSdkVersion();
@@ -865,14 +865,6 @@ namespace gameanalytics
865865
return getInstance()._sessionStart != 0;
866866
}
867867

868-
std::string GAState::getRemoteConfigsStringValue(std::string const& key, std::string const& defaultValue)
869-
{
870-
std::lock_guard<std::recursive_mutex> lg(getInstance()._mtx);
871-
std::string const value = utilities::getOptionalValue(getInstance()._configurations, key, defaultValue);
872-
873-
return value;
874-
}
875-
876868
bool GAState::isRemoteConfigsReady()
877869
{
878870
return getInstance()._remoteConfigsIsReady;
@@ -900,12 +892,28 @@ namespace gameanalytics
900892
std::string GAState::getRemoteConfigsContentAsString()
901893
{
902894
std::lock_guard<std::recursive_mutex> lg(getInstance()._mtx);
903-
return getInstance()._configurations.dump(JSON_PRINT_INDENT);
895+
896+
json contents;
897+
898+
for(auto& obj : getInstance()._configurations)
899+
{
900+
if(obj.contains("key") && obj.contains("value"))
901+
{
902+
std::string key = utilities::getOptionalValue<std::string>(obj, "key", "");
903+
if(!key.empty())
904+
{
905+
contents[key] = obj["value"];
906+
}
907+
}
908+
}
909+
910+
return contents.dump(JSON_PRINT_INDENT);
904911
}
905912

906913
void GAState::populateConfigurations(json& sdkConfig)
907914
{
908915
std::lock_guard<std::recursive_mutex> guard(_mtx);
916+
_configurations = {};
909917

910918
try
911919
{
@@ -918,21 +926,15 @@ namespace gameanalytics
918926
if (!configuration.empty())
919927
{
920928
std::string key = utilities::getOptionalValue<std::string>(configuration, "key", "");
921-
922929
int64_t start_ts = utilities::getOptionalValue<int64_t>(configuration, "start_ts", std::numeric_limits<int64_t>::min());
923-
924930
int64_t end_ts = utilities::getOptionalValue<int64_t>(configuration, "end_ts", std::numeric_limits<int64_t>::max());
925931

926932
int64_t client_ts_adjusted = getClientTsAdjusted();
927933

928934
if (!key.empty() && configuration.contains("value") && client_ts_adjusted > start_ts && client_ts_adjusted < end_ts)
929935
{
930-
json& value = configuration["value"];
931-
if (value.is_string() || value.is_number())
932-
{
933-
_configurations[key] = value;
934-
logging::GALogger::d("configuration added: %s", configuration.dump(JSON_PRINT_INDENT).c_str());
935-
}
936+
_configurations[key] = configuration;
937+
logging::GALogger::d("configuration added: %s", configuration.dump(JSON_PRINT_INDENT).c_str());
936938
}
937939
}
938940
}
@@ -1155,5 +1157,21 @@ namespace gameanalytics
11551157

11561158
return cleanedFields;
11571159
}
1160+
1161+
json GAState::getRemoteConfigAnnotations()
1162+
{
1163+
json configs;
1164+
for(json& obj : _configurations)
1165+
{
1166+
json cfg;
1167+
cfg["vsn"] = utilities::getOptionalValue<int>(obj, "vsn", 0);
1168+
cfg["key"] = utilities::getOptionalValue<std::string>(obj, "key", "");
1169+
cfg["id"] = utilities::getOptionalValue<std::string>(obj, "id", "");
1170+
1171+
configs.push_back(cfg);
1172+
}
1173+
1174+
return configs;
1175+
}
11581176
}
11591177
}

source/gameanalytics/GAState.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ namespace gameanalytics
134134
static void setEnabledEventSubmission(bool flag);
135135
static bool isEventSubmissionEnabled();
136136
static bool sessionIsStarted();
137-
static std::string getRemoteConfigsStringValue(std::string const& key, std::string const& defaultValue);
138137
static bool isRemoteConfigsReady();
139138
static void addRemoteConfigsListener(const std::shared_ptr<IRemoteConfigsListener>& listener);
140139
static void removeRemoteConfigsListener(const std::shared_ptr<IRemoteConfigsListener>& listener);
@@ -147,16 +146,33 @@ namespace gameanalytics
147146
static json getValidatedCustomFields();
148147
static json getValidatedCustomFields(const json& withEventFields);
149148

150-
int64_t getTotalSessionLength() const;
149+
template<typename T>
150+
inline static T getRemoteConfigsValue(std::string const& key, T const& defaultValue)
151+
{
152+
std::lock_guard<std::recursive_mutex> lg(getInstance()._mtx);
153+
if(getInstance()._configurations.contains(key))
154+
{
155+
json& config = getInstance()._configurations[key];
156+
T value = utilities::getOptionalValue<T>(config, "value", defaultValue);
157+
return value;
158+
}
159+
160+
return defaultValue;
161+
}
151162

152163
template<typename T = std::chrono::milliseconds>
153164
inline int64_t calculateSessionLength() const
154165
{
155-
return std::chrono::duration_cast<T>(std::chrono::high_resolution_clock::now() - _startTimepoint).count();
166+
auto len = std::chrono::high_resolution_clock::now() - _startTimepoint;
167+
return std::chrono::duration_cast<T>(len).count();
156168
}
157169

170+
int64_t getTotalSessionLength() const;
171+
158172
void populateConfigurations(json& sdkConfig);
159173

174+
json getRemoteConfigAnnotations();
175+
160176
private:
161177

162178
GAState();
@@ -184,7 +200,7 @@ namespace gameanalytics
184200

185201
void updateTotalSessionTime();
186202

187-
int64_t calculateServerTimeOffset(int64_t serverTs);
203+
int64_t calculateServerTimeOffset(int64_t serverTs);
188204

189205
void validateAndCleanCustomFields(const json& fields, json& out);
190206

0 commit comments

Comments
 (0)