Skip to content

Commit c6ee94f

Browse files
committed
custom fields
1 parent b813b86 commit c6ee94f

File tree

5 files changed

+171
-102
lines changed

5 files changed

+171
-102
lines changed
Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,40 @@
11
#include "Server/GACustomFields.h"
22
#include "GACommon.h"
33
#include "GASerialize.h"
4+
#include "GALogger.h"
45

56
namespace gameanalytics
67
{
7-
void CustomFields::setValue(std::string const& key, int64_t val)
8+
bool CustomFields::checkCustomFieldLimit() const
89
{
9-
Value v;
10-
v.key = key;
11-
v.value = val;
10+
int num = numFields() + 1;
11+
if(num > MAX_CUSTOM_FIELDS_COUNT)
12+
{
13+
logging::GALogger::w("Too many custom fields. Maximum number of allowed custom fields is: %i. New size is: %i", MAX_CUSTOM_FIELDS_COUNT, num);
14+
return false;
15+
}
1216

13-
fields[key] = v;
17+
return true;
1418
}
1519

16-
void CustomFields::setValue(std::string const& key, double val)
20+
bool CustomFields::setValue(std::string const& key, int64_t val)
1721
{
18-
Value v;
19-
v.key = key;
20-
v.value = val;
21-
22-
fields[key] = v;
22+
return setField(key, val);
2323
}
2424

25-
void CustomFields::setValue(std::string const& key, std::string const& val)
25+
bool CustomFields::setValue(std::string const& key, double val)
2626
{
27-
Value v;
28-
v.key = key;
29-
v.value = val;
30-
31-
fields[key] = v;
27+
return setField(key, val);
3228
}
3329

34-
void CustomFields::setValue(std::string const& key, bool val)
30+
bool CustomFields::setValue(std::string const& key, std::string const& val)
3531
{
36-
Value v;
37-
v.key = key;
38-
v.value = val;
32+
return setField(key, val);
33+
}
3934

40-
fields[key] = v;
35+
bool CustomFields::setValue(std::string const& key, bool val)
36+
{
37+
return setField(key, val);
4138
}
4239

4340
std::string CustomFields::toString() const
@@ -46,16 +43,23 @@ namespace gameanalytics
4643
return j.dump();
4744
}
4845

49-
void CustomFields::merge(CustomFields const& other)
46+
bool CustomFields::merge(CustomFields const& other)
5047
{
48+
if(other.isEmpty())
49+
return true;
50+
51+
const int num = other.numFields() + numFields();
52+
if(num > MAX_CUSTOM_FIELDS_COUNT)
53+
{
54+
logging::GALogger::w("Too many custom fields. Maximum number of allowed custom fields is: %i. New size is: %i", MAX_CUSTOM_FIELDS_COUNT, num);
55+
return false;
56+
}
57+
5158
for(auto& [key, val]: other.fields)
5259
{
5360
fields[key] = val;
5461
}
55-
}
5662

57-
bool CustomFields::checkSize() const
58-
{
59-
return fields.size() <= NUM_MAX_CUSTOM_FIELDS;
63+
return true;
6064
}
6165
}

source/gameanalytics/Server/GACustomFields.h

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ namespace gameanalytics
2727

2828
inline bool isEmpty() const { return fields.empty(); }
2929

30-
std::string toString() const;
30+
inline size_t numFields() const { return fields.size(); }
3131

32-
void setValue(std::string const& key, int64_t val);
33-
void setValue(std::string const& key, double val);
34-
void setValue(std::string const& key, std::string const& val);
35-
void setValue(std::string const& key, bool val);
32+
std::string toString() const;
3633

37-
void merge(CustomFields const& other);
34+
bool setValue(std::string const& key, int64_t val);
35+
bool setValue(std::string const& key, double val);
36+
bool setValue(std::string const& key, std::string const& val);
37+
bool setValue(std::string const& key, bool val);
3838

39-
bool checkSize() const;
39+
bool merge(CustomFields const& other);
4040

4141
template<typename T>
4242
T getValue(std::string const& key, T const& defaultValue)
@@ -46,6 +46,21 @@ namespace gameanalytics
4646

4747
private:
4848

49+
bool checkCustomFieldLimit() const;
50+
51+
template<typename T>
52+
bool setField(std::string const& key, T const& val)
53+
{
54+
if(checkCustomFieldLimit())
55+
{
56+
Value v;
57+
v.key = key;
58+
v.value = val;
59+
60+
fields[key] = v;
61+
}
62+
}
63+
4964
template<typename T>
5065
T getValueById(std::string const& key, int id, T const& defaultValue)
5166
{

source/gameanalytics/Server/GAPlayerDatabase.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ namespace gameanalytics
4646
return false;
4747
}
4848

49+
Player& p = getPlayer(uid);
50+
if(p.isActive())
51+
{
52+
logging::GALogger::w("Cannot remove player while the player is active, user id: %s", uid.c_str());
53+
return false;
54+
}
55+
4956
_players.erase(uid);
5057
return true;
5158
}
@@ -66,12 +73,16 @@ namespace gameanalytics
6673
return false;
6774
}
6875

69-
Player player = getPlayer(uid);
70-
player._userId = newUserId;
76+
Player& player = getPlayer(uid);
77+
if(player.isActive())
78+
{
79+
logging::GALogger::w("Cannot change user id while the user is active, user id: %s", uid.c_str());
80+
return false;
81+
}
7182

83+
player._userId = newUserId;
7284
_players[newUserId] = std::move(player);
73-
74-
return true;
85+
return removePlayer(uid);
7586
}
7687

7788
Player& PlayerDatabase::getPlayer(std::string const& userId)
@@ -139,7 +150,7 @@ namespace gameanalytics
139150
data["connection_type"] = p.connectionType;
140151

141152
utilities::addIfNotEmpty(data, "country_code", p.countryCode);
142-
153+
143154
utilities::addIfNotEmpty(data, "ab_id", p._abId);
144155
utilities::addIfNotEmpty(data, "ab_variant_id", p._abVariantId);
145156
utilities::addIfNotEmpty(data, "user_id_ext", p.extUserId);

0 commit comments

Comments
 (0)