BF2MC-Matchmaker
battlefield/clan.cpp
1 #include <mysql/mysql_time.h>
2 
3 #include <util.h>
4 
5 #include <battlefield/clan.h>
6 
7 std::unordered_map<std::string, Battlefield::Clan::SetterFunc> Battlefield::Clan::SetterMap = {
8  {"name", &Battlefield::Clan::SetName },
9  {"tag", &Battlefield::Clan::SetTag },
10  {"homepage", &Battlefield::Clan::SetHomepage },
11  {"info", &Battlefield::Clan::SetInfo },
12  {"region", &Battlefield::Clan::SetRegion },
13 };
14 
16 {
17  this->SetClanId(543151);
18  this->SetName("AT-STARS");
19  this->SetTag("SOS");
20  this->SetHomepage("UMBRELLA");
21  this->SetInfo("G%2dVIRUS");
22  this->SetRegion(Regions::Europe);
23 }
24 
25 bool Battlefield::Clan::SetClanId(int clanid)
26 {
27  if(clanid >= 0)
28  {
29  this->_clanid = clanid;
30  return true;
31  }
32 
33  this->_clanid = -1;
34  return false;
35 }
36 
37 bool Battlefield::Clan::SetClanId(const std::string& str_clanid)
38 {
39  try
40  {
41  int clanid = std::stoi(str_clanid);
42 
43  return this->SetClanId(clanid);
44  }
45  catch(...) {}
46 
47  return false;
48 }
49 
50 bool Battlefield::Clan::SetName(const std::string& name)
51 {
52  if(name.size() > 32)
53  {
54  this->_name = name.substr(0, 31);
55  }
56  else
57  {
58  this->_name = name;
59  }
60 
61  return true;
62 }
63 
64 bool Battlefield::Clan::SetTag(const std::string& tag)
65 {
66  if(tag.size() < 2 || tag.size() > 3)
67  {
68  return false;
69  }
70 
71  this->_tag = tag;
72  return true;
73 }
74 
75 bool Battlefield::Clan::SetHomepage(const std::string& homepage)
76 {
77  if(homepage.size() > 256)
78  {
79  this->_homepage = homepage.substr(0, 255);
80  }
81  else
82  {
83  this->_homepage = homepage;
84  }
85 
86  return true;
87 }
88 
89 bool Battlefield::Clan::SetInfo(const std::string& info)
90 {
91  if(info.size() > 1024)
92  {
93  this->_info = info.substr(0, 1023);
94  }
95  else
96  {
97  this->_info = info;
98  }
99 
100  return true;
101 }
102 
103 bool Battlefield::Clan::SetRegion(Battlefield::Clan::Regions region)
104 {
105  this->_region = region;
106  return true;
107 }
108 
109 bool Battlefield::Clan::SetRegion(uint8_t int_region)
110 {
111  if(int_region >= static_cast<uint8_t>(Regions::America) && int_region <= static_cast<uint8_t>(Regions::Asia))
112  {
113  Battlefield::Clan::Regions region = static_cast<Regions>(int_region);
114 
115  return this->SetRegion(region);
116  }
117 
118  return false;
119 }
120 
121 bool Battlefield::Clan::SetRegion(const std::string& str_region)
122 {
123  try
124  {
125  uint8_t int_region = static_cast<uint8_t>(std::stoul(str_region));
126 
127  return this->SetRegion(int_region);
128  }
129  catch(...) {};
130 
131  return false;
132 }
133 
134 bool Battlefield::Clan::SetScore(uint32_t score)
135 {
136  this->_score = score;
137  return true;
138 }
139 
140 bool Battlefield::Clan::SetWins(uint32_t wins)
141 {
142  this->_wins = wins;
143  return true;
144 }
145 
146 bool Battlefield::Clan::SetLosses(uint32_t losses)
147 {
148  this->_losses = losses;
149  return true;
150 }
151 
152 bool Battlefield::Clan::SetDraws(uint32_t draws)
153 {
154  this->_draws = draws;
155  return true;
156 }
157 
158 bool Battlefield::Clan::SetCreatedAt(MYSQL_TIME created_at)
159 {
160  this->_created_at = Util::Time::GetDateTime(created_at);
161 
162  return true;
163 }
164 
165 bool Battlefield::Clan::SetDisable(bool disable)
166 {
167  this->_disable = disable;
168  return true;
169 }
170 
171 bool Battlefield::Clan::SetDisable(uint8_t disable)
172 {
173  return this->SetDisable(disable == 1);
174 }
175 
176 void Battlefield::Clan::AddRank(int profileid, Ranks rank)
177 {
178  this->_ranks.insert(std::make_pair(profileid, rank));
179 }
180 
181 void Battlefield::Clan::AddRank(int profileid, uint8_t int_rank)
182 {
183  this->AddRank(profileid, convertRank(int_rank));
184 }
185 
186 Battlefield::Clan::Ranks Battlefield::Clan::GetRank(int profileid) const
187 {
188  auto it = this->_ranks.find(profileid);
189  if (it != this->_ranks.end())
190  {
191  return it->second;
192  }
193 
194  return Ranks::Unknown;
195 }
196 
197 // Static
198 
200 {
201  try
202  {
203  int int_rank = std::stoi(str_rank);
204 
205  return convertRank(int_rank);
206  }
207  catch(...) {};
208 
209  return Ranks::Unknown;
210 }
211 
213 {
214  if(int_rank >= static_cast<uint8_t>(Ranks::Leader) && int_rank <= static_cast<uint8_t>(Ranks::Member))
215  {
216  return static_cast<Ranks>(int_rank);
217  }
218 
219  return Ranks::Unknown;
220 }
221 
223 {
224  try
225  {
226  uint8_t int_region = static_cast<uint8_t>(std::stoul(str_region));
227 
228  return convertRegion(int_region);
229  }
230  catch(...) {};
231 
232  return Regions::Unknown;
233 }
234 
236 {
237  if(int_region >= static_cast<uint8_t>(Regions::America) && int_region <= static_cast<uint8_t>(Regions::Asia))
238  {
239  return static_cast<Regions>(int_region);
240  }
241 
242  return Regions::Unknown;
243 }
244 
static Regions convertRegion(const std::string &str_region)
Converts a string representation of a region to its corresponding enum value.
static Ranks convertRank(const std::string &rank)
Converts a string representation of a rank to its corresponding enum value.
Regions
Represents the regions associated with clans.
Definition: clan.h:63
void useExample()
Performs an example operation using the Clan class.
Ranks
Represents the ranks within a clan.
Definition: clan.h:77