BF2MC-Matchmaker
playerstats.cpp
1 #include <battlefield.h>
2 #include <util.h>
3 #include <logger.h>
4 
5 #include <battlefield/gamestat.h>
6 
7 #include <battlefield/playerstats.h>
8 
9 std::unordered_map<std::string, Battlefield::PlayerStats::SetterFunc> Battlefield::PlayerStats::SetterMap = {
10  {"pph", &Battlefield::PlayerStats::SetPPH },
11  {"vehicles", &Battlefield::PlayerStats::SetVehiclesDestroyed },
12  {"lavd", &Battlefield::PlayerStats::SetLAVsDestroyed },
13  {"mavd", &Battlefield::PlayerStats::SetMAVsDestroyed },
14  {"havd", &Battlefield::PlayerStats::SetHAVsDestroyed },
15  {"hed", &Battlefield::PlayerStats::SetHelicoptersDestroyed },
16  {"bod", &Battlefield::PlayerStats::SetBoatsDestroyed },
17  {"kills", &Battlefield::PlayerStats::SetKills },
18  {"deaths", &Battlefield::PlayerStats::SetDeaths },
19  {"k1", &Battlefield::PlayerStats::SetKillsAssualtKit },
20  {"s1", &Battlefield::PlayerStats::SetDeathsAssualtKit },
21  {"k2", &Battlefield::PlayerStats::SetKillsSniperKit },
22  {"s2", &Battlefield::PlayerStats::SetDeathsSniperKit },
23  {"k3", &Battlefield::PlayerStats::SetKillsSpecialOpKit },
24  {"s3", &Battlefield::PlayerStats::SetDeathsSpecialOpKit },
25  {"k4", &Battlefield::PlayerStats::SetKillsCombatEngineerKit },
26  {"s4", &Battlefield::PlayerStats::SetDeathsCombatEngineerKit },
27  {"k5", &Battlefield::PlayerStats::SetKillsSupportKit },
28  {"s5", &Battlefield::PlayerStats::SetDeathsSupportKit },
29 };
30 
32 {
33  this->SetScore(41476);
34  this->SetRank(Ranks::Five_Star_General);
35  this->SetPPH(5236);
36  this->SetKills(31848);
37  this->SetDeaths(26394);
38  this->SetSuicides(41);
39  this->SetTime(3627752);
40  this->SetVehiclesDestroyed(818);
41  this->SetLAVsDestroyed(342);
42  this->SetMAVsDestroyed(182);
43  this->SetHAVsDestroyed(118);
44  this->SetHelicoptersDestroyed(173);
45  this->SetBoatsDestroyed(3);
46  this->SetKillsAssualtKit(20880);
47  this->SetDeathsAssualtKit(16556);
48  this->SetKillsSniperKit(7445);
49  this->SetDeathsSniperKit(4790);
50  this->SetKillsSpecialOpKit(2364);
51  this->SetDeathsSpecialOpKit(3286);
52  this->SetKillsCombatEngineerKit(730);
53  this->SetDeathsCombatEngineerKit(1411);
54  this->SetKillsSupportKit(414);
55  this->SetDeathsSupportKit(351);
56  this->SetMedals(Medals::All);
57  this->SetTotalTopPlayer(56);
58  this->SetTotalVictories(1202);
59  this->SetTotalGameSessions(3613);
60 }
61 
62 double Battlefield::PlayerStats::GetRatio() const
63 {
64  if(this->_kills == 0 or this->_deaths == 0)
65  {
66  return 0.0;
67  }
68 
69  return static_cast<double>(this->_kills * 100) / this->_deaths;
70 }
71 
72 double Battlefield::PlayerStats::GetRatioAssualtKit() const
73 {
74  if(this->_k1 == 0 or this->_s1 == 0)
75  {
76  return 0.0;
77  }
78 
79  return static_cast<double>(this->_k1 * 100) / this->_s1;
80 }
81 
82 double Battlefield::PlayerStats::GetRatioSniperKit() const
83 {
84  if(this->_k2 == 0 or this->_s2 == 0)
85  {
86  return 0.0;
87  }
88 
89  return static_cast<double>(this->_k2 * 100) / this->_s2;
90 }
91 
92 double Battlefield::PlayerStats::GetRatioSpecialOpKit() const
93 {
94  if(this->_k3 == 0 or this->_s3 == 0)
95  {
96  return 0.0;
97  }
98 
99  return static_cast<double>(this->_k3 * 100) / this->_s3;
100 }
101 
102 double Battlefield::PlayerStats::GetRatioCombatEngineerKit() const
103 {
104  if(this->_k4 == 0 or this->_s4 == 0)
105  {
106  return 0.0;
107  }
108 
109  return static_cast<double>(this->_k4 * 100) / this->_s4;
110 }
111 
112 double Battlefield::PlayerStats::GetRatioSupportKit() const
113 {
114  if(this->_k5 == 0 or this->_s5 == 0)
115  {
116  return 0.0;
117  }
118 
119  return static_cast<double>(this->_k5 * 100) / this->_s5;
120 }
121 
122 bool Battlefield::PlayerStats::SetScore(int32_t score)
123 {
124  this->_score = score;
125 
126  this->_calcRank();
127 
128  return true;
129 }
130 
131 bool Battlefield::PlayerStats::SetRank(uint32_t ran)
132 {
133  if(ran >= static_cast<uint8_t>(Ranks::Private) && ran <= static_cast<uint8_t>(Ranks::Five_Star_General))
134  {
135  this->_ran = ran;
136 
137  return true;
138  }
139 
140  return false;
141 }
142 
143 bool Battlefield::PlayerStats::SetRank(Ranks ran)
144 {
145  this->_ran = static_cast<uint8_t>(ran);
146  return true;
147 }
148 
149 bool Battlefield::PlayerStats::SetPPH(uint32_t pph)
150 {
151  this->_pph = pph;
152 
153  this->_calcRank();
154 
155  return true;
156 }
157 
158 bool Battlefield::PlayerStats::SetKills(uint32_t kills)
159 {
160  this->_kills = kills;
161  return true;
162 }
163 
164 bool Battlefield::PlayerStats::SetDeaths(uint32_t deaths)
165 {
166  this->_deaths = deaths;
167  return true;
168 }
169 
170 bool Battlefield::PlayerStats::SetSuicides(uint32_t suicides)
171 {
172  this->_suicides = suicides;
173  return true;
174 }
175 
176 bool Battlefield::PlayerStats::SetTime(uint32_t time)
177 {
178  this->_time = time;
179  return true;
180 }
181 
182 bool Battlefield::PlayerStats::SetVehiclesDestroyed(uint32_t vehicles)
183 {
184  this->_vehicles = vehicles;
185  return true;
186 }
187 
188 bool Battlefield::PlayerStats::SetLAVsDestroyed(uint32_t lavd)
189 {
190  this->_lavd = lavd;
191  return true;
192 }
193 
194 bool Battlefield::PlayerStats::SetMAVsDestroyed(uint32_t mavd)
195 {
196  this->_mavd = mavd;
197  return true;
198 }
199 
200 bool Battlefield::PlayerStats::SetHAVsDestroyed(uint32_t havd)
201 {
202  this->_havd = havd;
203  return true;
204 }
205 
206 bool Battlefield::PlayerStats::SetHelicoptersDestroyed(uint32_t hed)
207 {
208  this->_hed = hed;
209  return true;
210 }
211 
212 bool Battlefield::PlayerStats::SetBoatsDestroyed(uint32_t bod)
213 {
214  this->_bod = bod;
215  return true;
216 }
217 
218 bool Battlefield::PlayerStats::SetKillsAssualtKit(uint32_t kills)
219 {
220  this->_k1 = kills;
221  return true;
222 }
223 
224 bool Battlefield::PlayerStats::SetDeathsAssualtKit(uint32_t deaths)
225 {
226  this->_s1 = deaths;
227  return true;
228 }
229 
230 bool Battlefield::PlayerStats::SetKillsSniperKit(uint32_t kills)
231 {
232  this->_k2 = kills;
233  return true;
234 }
235 
236 bool Battlefield::PlayerStats::SetDeathsSniperKit(uint32_t deaths)
237 {
238  this->_s2 = deaths;
239  return true;
240 }
241 
242 bool Battlefield::PlayerStats::SetKillsSpecialOpKit(uint32_t kills)
243 {
244  this->_k3 = kills;
245  return true;
246 }
247 
248 bool Battlefield::PlayerStats::SetDeathsSpecialOpKit(uint32_t deaths)
249 {
250  this->_s3 = deaths;
251  return true;
252 }
253 
254 bool Battlefield::PlayerStats::SetKillsCombatEngineerKit(uint32_t kills)
255 {
256  this->_k4 = kills;
257  return true;
258 }
259 
260 bool Battlefield::PlayerStats::SetDeathsCombatEngineerKit(uint32_t deaths)
261 {
262  this->_s4 = deaths;
263  return true;
264 }
265 
266 bool Battlefield::PlayerStats::SetKillsSupportKit(uint32_t kills)
267 {
268  this->_k5 = kills;
269  return true;
270 }
271 
272 bool Battlefield::PlayerStats::SetDeathsSupportKit(uint32_t deaths)
273 {
274  this->_s5 = deaths;
275  return true;
276 }
277 
278 bool Battlefield::PlayerStats::SetMedals(uint32_t medals)
279 {
280  if(medals <= static_cast<uint32_t>(Medals::All) && Util::countSetBits(medals) > Util::countSetBits(this->_medals))
281  {
282  this->_medals = medals;
283 
284  this->_calcRank();
285 
286  return true;
287  }
288 
289  return false;
290 }
291 
292 bool Battlefield::PlayerStats::SetMedals(Medals medals)
293 {
294  return this->SetMedals(static_cast<uint32_t>(medals));
295 }
296 
297 bool Battlefield::PlayerStats::SetTotalTopPlayer(uint32_t total)
298 {
299  this->_ttb = total;
300  return true;
301 }
302 
303 bool Battlefield::PlayerStats::SetTotalVictories(uint32_t total)
304 {
305  this->_mv = total;
306  return true;
307 }
308 
309 bool Battlefield::PlayerStats::SetTotalGameSessions(uint32_t total)
310 {
311  this->_ngp = total;
312  return true;
313 }
314 
315 bool Battlefield::PlayerStats::SetCapturedFlags(uint32_t cflags)
316 {
317  this->_cflags = cflags;
318  return true;
319 }
320 
321 bool Battlefield::PlayerStats::SetNeutralizedFlags(uint32_t nflags)
322 {
323  this->_nflags = nflags;
324  return true;
325 }
326 
327 bool Battlefield::PlayerStats::SetSavedFlags(uint32_t sflags)
328 {
329  this->_sflags = sflags;
330  return true;
331 }
332 
334 {
335  // Calculate new pph
336  this->_calcNewPPH(gsplayer.GetTime(), gsplayer.GetScore());
337 
338  this->SetBoatsDestroyed( this->_bod + gsplayer.GetBoatsDestroyed() ); // bod
339  this->SetHAVsDestroyed( this->_havd + gsplayer.GetHAVsDestroyed() ); // havd
340  this->SetHelicoptersDestroyed( this->_hed + gsplayer.GetHelicoptersDestroyed() ); // hed
341  this->SetKillsAssualtKit( this->_k1 + gsplayer.GetKillsAssualtKit() ); // k1
342  this->SetKillsSniperKit( this->_k2 + gsplayer.GetKillsSniperKit() ); // k2
343  this->SetKillsSpecialOpKit( this->_k3 + gsplayer.GetKillsSpecialOpKit() ); // k3
344  this->SetKillsCombatEngineerKit( this->_k4 + gsplayer.GetKillsCombatEngineerKit() ); // k4
345  this->SetKillsSupportKit( this->_k5 + gsplayer.GetKillsSupportKit() ); // k5
346  this->SetKills( this->_kills + gsplayer.GetKills() ); // kills
347  this->SetDeaths( this->_deaths + gsplayer.GetDeaths() ); // deaths
348  this->SetLAVsDestroyed( this->_lavd + gsplayer.GetLAVsDestroyed() ); // lavd
349  this->SetMAVsDestroyed( this->_mavd + gsplayer.GetMAVsDestroyed() ); // mavd
350  this->SetTotalVictories( this->_mv + gsplayer.GetTotalVictories() ); // mv
351  this->SetTotalGameSessions( this->_ngp + gsplayer.GetTotalGameSessions() ); // ngp
352  this->SetDeathsAssualtKit( this->_s1 + gsplayer.GetSpawnsAssualtKit() ); // s1
353  this->SetDeathsSniperKit( this->_s2 + gsplayer.GetSpawnsSniperKit() ); // s2
354  this->SetDeathsSpecialOpKit( this->_s3 + gsplayer.GetSpawnsSpecialOpKit() ); // s3
355  this->SetDeathsCombatEngineerKit( this->_s4 + gsplayer.GetSpawnsCombatEngineerKit() ); // s4
356  this->SetDeathsSupportKit( this->_s5 + gsplayer.GetSpawnsSupportKit() ); // s5
357  this->SetScore( this->_score + gsplayer.GetScore() ); // score
358  this->SetSuicides( this->_suicides + gsplayer.GetSuicides() ); // suicides
359  this->SetTime( this->_time + gsplayer.GetTime() ); // time
360  this->SetTotalTopPlayer( this->_ttb + gsplayer.GetTotalTopPlayer() ); // ttb
361  this->SetCapturedFlags( this->_cflags + gsplayer.GetCapturedFlags() ); // cflags
362  this->SetNeutralizedFlags( this->_nflags + gsplayer.GetNeutralizedFlags() ); // nflags
363  this->SetSavedFlags( this->_sflags + gsplayer.GetSavedFlags() ); // sflags
364 
365  this->SetVehiclesDestroyed(
366  this->_lavd +
367  this->_mavd +
368  this->_havd +
369  this->_hed +
370  this->_bod
371  );
372 
373  this->SetMedals(gsplayer.GetMedals()); // medals
374 }
375 
377 {
378  int pph = this->_pph / 100;
379  int total_medals = Util::countSetBits(this->_medals);
380  int new_rank = 1;
381 
382  for(int i = 1; i < RankScores.size(); i++)
383  {
384  if(this->_score >= RankScores[i] && pph >= RankPph[i] && total_medals >= RankMedals[i])
385  new_rank = i + 1;
386  else
387  break;
388  }
389 
390  this->_ran = new_rank;
391 }
392 
393 void Battlefield::PlayerStats::_calcNewPPH(uint32_t time, int32_t score)
394 {
395  const int PPH_TIME_SPAN = 5;
396  const int PPH_FACTOR = 100;
397  const int SECONDS_PER_HOUR = 60 * 60;
398 
399  double total_hours = static_cast<double>(this->_time + time) / SECONDS_PER_HOUR;
400  int32_t total_score = this->_score + score;
401 
402  uint32_t new_pph = 0;
403 
404  // In case we only played less then a one hour
405  if(total_hours < 1.0)
406  {
407  new_pph = total_score * 100;
408  }
409  // In case we played less then 5 hours total
410  else if(total_hours < PPH_TIME_SPAN)
411  {
412  // Calculate the new pph
413  double new_pph_double = static_cast<double>(total_score) / total_hours;
414 
415  // Convert to unsigned 32 bit integer
416  new_pph = static_cast<uint32_t>(new_pph_double * 100.0);
417  }
418  // In case more then 5 hours is played
419  else
420  {
421  // Convert current pph to double
422  double current_pph = static_cast<double>(this->_pph) / 100.0;
423 
424  // Calculate how much hours you played in current match
425  double game_time_hours = static_cast<double>(time) / SECONDS_PER_HOUR;
426 
427  // gap_time_span = 5 hours - game_time_hours
428  double gap_time_span = static_cast<double>(PPH_TIME_SPAN) - game_time_hours;
429 
430  // Calculate the total score for 5 hours
431  double total_score = gap_time_span * current_pph;
432  total_score += static_cast<double>(score);
433 
434  // Calculate the new pph
435  double new_pph_double = total_score / PPH_TIME_SPAN;
436 
437  // Convert to unsigned 32 bit integer
438  new_pph = static_cast<uint32_t>(new_pph_double * 100.0);
439  }
440 
441  // Set new pph
442  this->SetPPH(new_pph);
443 }
444 
Represents a player's statistics in a game.
Definition: gamestat.h:146
void useExample()
Performs an example operation using the PlayerStats class.
Definition: playerstats.cpp:31
void _calcNewPPH(uint32_t time, int32_t score)
Calculates the pph based on game time, game score and current player stats.
void _calcRank()
Calculates the rank based on score, points per hour (pph), and medals.
void Update(const Battlefield::GameStatPlayer &gsplayer)
Update player stats based on played game.