Skip to content

Commit 0dbcfd2

Browse files
committed
started working on server-side SDK support
1 parent e02f479 commit 0dbcfd2

File tree

9 files changed

+1096
-0
lines changed

9 files changed

+1096
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#include "Server/GACustomFields.h"
2+
#include "GACommon.h"
3+
4+
namespace gameanalytics
5+
{
6+
void CustomFields::setValue(std::string const& key, int64_t val)
7+
{
8+
Value v;
9+
v.key = key;
10+
v.value = val;
11+
12+
fields[key] = v;
13+
}
14+
15+
void CustomFields::setValue(std::string const& key, double val)
16+
{
17+
Value v;
18+
v.key = key;
19+
v.value = val;
20+
21+
fields[key] = v;
22+
}
23+
24+
void CustomFields::setValue(std::string const& key, std::string const& val)
25+
{
26+
Value v;
27+
v.key = key;
28+
v.value = val;
29+
30+
fields[key] = v;
31+
}
32+
33+
void CustomFields::setValue(std::string const& key, bool val)
34+
{
35+
Value v;
36+
v.key = key;
37+
v.value = val;
38+
39+
fields[key] = v;
40+
}
41+
42+
json serializeCustomFields(CustomFields const& customFields)
43+
{
44+
json j;
45+
46+
for(auto& [key, field]: customFields.fields)
47+
{
48+
switch(field.value.index())
49+
{
50+
case CustomFields::Value::value_int:
51+
j[key] = std::get<int64_t>(field.value);
52+
break;
53+
54+
case CustomFields::Value::value_float:
55+
j[key] = std::get<double>(field.value);
56+
break;
57+
58+
case CustomFields::Value::value_str:
59+
j[key] = std::get<std::string>(field.value);
60+
break;
61+
62+
case CustomFields::Value::value_bool:
63+
j[key] = std::get<bool>(field.value);
64+
break;
65+
}
66+
}
67+
68+
return j;
69+
}
70+
71+
CustomFields deserializeCustomFields(json const& jsonFields)
72+
{
73+
CustomFields customFields;
74+
75+
for(auto& item: jsonFields.items())
76+
{
77+
CustomFields::Value f;
78+
f.key = item.key();
79+
80+
if(item.value().is_boolean())
81+
{
82+
f.value = item.value().get<bool>();
83+
}
84+
else if(item.value().is_number_float())
85+
{
86+
f.value = item.value().get<double>();
87+
}
88+
else if(item.value().is_number_integer())
89+
{
90+
f.value = item.value().get<int64_t>();
91+
}
92+
else if(item.value().is_string())
93+
{
94+
f.value = item.value().get<std::string>();
95+
}
96+
else // unsupported value
97+
{
98+
continue;
99+
}
100+
101+
customFields.fields[f.key] = std::move(f);
102+
}
103+
104+
return customFields;
105+
}
106+
107+
std::string CustomFields::toString() const
108+
{
109+
json j = serializeCustomFields(*this);
110+
return j.dump();
111+
}
112+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#pragma once
2+
3+
#include "GameAnalytics/GATypes.h"
4+
5+
namespace gameanalytics
6+
{
7+
struct CustomFields
8+
{
9+
struct Value
10+
{
11+
enum Type
12+
{
13+
value_int,
14+
value_float,
15+
value_str,
16+
value_bool
17+
};
18+
19+
std::string key;
20+
std::variant<int64_t, double, std::string, bool> value;
21+
};
22+
23+
std::unordered_map<std::string, Value> fields;
24+
25+
inline bool isEmpty() const { return fields.empty(); }
26+
27+
std::string toString() const;
28+
29+
void setValue(std::string const& key, int64_t val);
30+
void setValue(std::string const& key, double val);
31+
void setValue(std::string const& key, std::string const& val);
32+
void setValue(std::string const& key, bool val);
33+
34+
template<typename T>
35+
T getValue(std::string const& key, T const& defaultValue)
36+
{
37+
return defaultValue;
38+
}
39+
40+
template<>
41+
std::string getValue<std::string>(std::string const& key, std::string const& defaultValue)
42+
{
43+
return getValueById<std::string>(key, Value::value_str, defaultValue);
44+
}
45+
46+
template<>
47+
int64_t getValue<int64_t>(std::string const& key, int64_t const& defaultValue)
48+
{
49+
return getValueById<int64_t>(key, Value::value_int, defaultValue);
50+
}
51+
52+
template<>
53+
double getValue<double>(std::string const& key, double const& defaultValue)
54+
{
55+
return getValueById<double>(key, Value::value_float, defaultValue);
56+
}
57+
58+
template<>
59+
bool getValue<bool>(std::string const& key, bool const& defaultValue)
60+
{
61+
return getValueById<bool>(key, Value::value_bool, defaultValue);
62+
}
63+
64+
template<>
65+
int getValue<int>(std::string const& key, int const& defaultValue)
66+
{
67+
return static_cast<int>(getValue<int64_t>(key, defaultValue));
68+
}
69+
70+
template<>
71+
float getValue<float>(std::string const& key, float const& defaultValue)
72+
{
73+
return static_cast<float>(getValue<double>(key, defaultValue));
74+
}
75+
76+
private:
77+
78+
template<typename T>
79+
T getValueById(std::string const& key, int id, T const& defaultValue)
80+
{
81+
return (_customFields.count(key) && _customFields[key].value.index() == id) ? std::get<T>(_customFields[key]) : defaultValue;
82+
}
83+
};
84+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include "GameAnalytics/GATypes.h"
4+
#include "Server/GACustomFields.h"
5+
6+
namespace gameanalytics
7+
{
8+
struct GlobalData
9+
{
10+
std::string engineVersion;
11+
std::string sdkVersion;
12+
std::string build;
13+
14+
std::string severId;
15+
16+
std::string extUserId;
17+
std::string countryCode;
18+
std::string device;
19+
std::string platform;
20+
std::string manufacturer;
21+
std::string osVersion;
22+
std::string customDimension1;
23+
std::string customDimension2;
24+
std::string customDimension3;
25+
CustomFields customFields;
26+
}
27+
}

0 commit comments

Comments
 (0)