BF2MC-Matchmaker
status.cpp
1 #include <unistd.h>
2 #include <iostream>
3 #include <iomanip>
4 #include <fstream>
5 #include <regex>
6 #include <thread>
7 
8 #include <settings.h>
9 #include <logger.h>
10 #include <server.h>
11 #include <gpcm/client.h>
12 #include <globals.h>
13 #include <database.h>
14 #include <util.h>
15 #include <atomizes.hpp>
16 #include <service/file_system.h>
17 
18 #include <webserver/client.h>
19 
20 void Webserver::Client::requestGetPlayerInfo(const atomizes::HTTPMessage& http_request, const std::string& url_base,
21  const Util::Url::Variables& url_variables)
22 {
23  atomizes::HTTPMessage http_response = this->_defaultResponseHeader();
24  Battlefield::Player player;
25  std::string response;
26 
27  // Get player profileid
28  auto it = url_variables.find("pid");
29  if (it != url_variables.end())
30  {
31  player.SetProfileId(it->second);
32  }
33 
34  // Patch player stats
35  //player.PlayerStats::useExample();
36  //g_database->updatePlayerStats(player);
37 
38  // Get player stats
39  g_database->queryPlayerStatsByProfileId(player);
40 
41  response += std::to_string(player.GetScore()) + ",";
42  response += std::to_string(player.GetRank()) + ",";
43  response += std::to_string(player.GetPPH()) + ",";
44  response += std::to_string(player.GetKills()) + ",";
45  response += std::to_string(player.GetSuicides()) + ",";
46  response += std::to_string(player.GetTime()) + ",";
47  response += std::to_string(player.GetLAVsDestroyed()) + ",";
48  response += std::to_string(player.GetMAVsDestroyed()) + ",";
49  response += std::to_string(player.GetHAVsDestroyed()) + ",";
50  response += std::to_string(player.GetHelicoptersDestroyed()) + ",";
51  response += std::to_string(player.GetPlanesDestroyed()) + ",";
52  response += std::to_string(player.GetBoatsDestroyed()) + ",";
53  response += std::to_string(player.GetKillsAssualtKit()) + ",";
54  response += std::to_string(player.GetSpawnsAssualtKit()) + ",";
55  response += std::to_string(player.GetKillsSniperKit()) + ",";
56  response += std::to_string(player.GetSpawnsSniperKit()) + ",";
57  response += std::to_string(player.GetKillsSpecialOpKit()) + ",";
58  response += std::to_string(player.GetSpawnsSpecialOpKit()) + ",";
59  response += std::to_string(player.GetKillsCombatEngineerKit()) + ",";
60  response += std::to_string(player.GetSpawnsCombatEngineerKit()) + ",";
61  response += std::to_string(player.GetKillsSupportKit()) + ",";
62  response += std::to_string(player.GetSpawnsSupportKit()) + ",";
63  response += std::to_string(player.GetTeamKills()) + ",";
64  response += std::to_string(player.GetMedals()) + ",";
65  response += std::to_string(player.GetTotalTopPlayer()) + ",";
66  response += std::to_string(player.GetTotalVictories()) + ",";
67  response += std::to_string(player.GetTotalGameSessions());
68 
69  http_response.SetStatusCode(200);
70  http_response.SetMessageBody(response);
71 
72  this->Send(http_response);
73 
74  //this->_LogTransaction("<--", "HTTP/1.1 200 OK");
75  this->_LogTransaction("<--", "HTTP/1.1 200 OK -> " + response);
76 }
77 
78 void Webserver::Client::requestStats(const atomizes::HTTPMessage& http_request, const std::string& url_base,
79  const Util::Url::Variables& url_variables)
80 {
81  atomizes::HTTPMessage http_response = this->_defaultResponseHeader();
82  Battlefield::RankPlayers rank_players;
83  std::string data = "";
84  std::vector<int> profileids;
85  int self_profileid = -1;
86 
87  auto it = url_variables.find("pid");
88  if (it != url_variables.end())
89  {
90  profileids = Util::convertProfileIdToVector(it->second);
91 
92  if(profileids.size() >= 1)
93  {
94  self_profileid = profileids[0];
95  }
96  }
97 
98  it = url_variables.find("sort");
99  if (it != url_variables.end())
100  {
101  std::string sort = it->second;
102 
103  if(sort == "rank")
104  {
105  if(profileids.size() == 0)
106  {
107  g_database->queryLeaderboardRank(rank_players);
108  }
109  else if(profileids.size() == 1)
110  {
111  g_database->queryLeaderboardRankByProfileId(rank_players, self_profileid);
112  }
113  else
114  {
115  g_database->queryLeaderboardRankByFriends(rank_players, profileids);
116  }
117 
118  for (const auto& pair : rank_players)
119  {
120  if(self_profileid == pair.second.GetProfileId())
121  {
122  data += "-";
123  }
124 
125  data += std::to_string(pair.first) + ",";
126  data += pair.second.GetUniquenick() + ",";
127  data += std::to_string(pair.second.GetRank()) + ",";
128  data += std::to_string(pair.second.GetPPH()) + ",";
129  data += std::to_string(pair.second.GetScore()) + "\n";
130  }
131  }
132  else if(sort == "score")
133  {
134  if(profileids.size() == 0)
135  {
136  g_database->queryLeaderboardType(rank_players, "score");
137  }
138  else if(profileids.size() == 1)
139  {
140  g_database->queryLeaderboardTypeByProfileid(rank_players, "score", self_profileid);
141  }
142  else
143  {
144  g_database->queryLeaderboardTypeByFriends(rank_players, "score", profileids);
145  }
146 
147  for (const auto& pair : rank_players)
148  {
149  if(self_profileid == pair.second.GetProfileId())
150  {
151  data += "-";
152  }
153 
154  data += std::to_string(pair.first) + ",";
155  data += pair.second.GetUniquenick() + ",";
156  data += std::to_string(pair.second.GetScore()) + "\n";
157  }
158  }
159  else if(sort == "pph")
160  {
161  if(profileids.size() == 0)
162  {
163  g_database->queryLeaderboardType(rank_players, "pph");
164  }
165  else if(profileids.size() == 1)
166  {
167  g_database->queryLeaderboardTypeByProfileid(rank_players, "pph", self_profileid);
168  }
169  else
170  {
171  g_database->queryLeaderboardTypeByFriends(rank_players, "pph", profileids);
172  }
173 
174  for (const auto& pair : rank_players)
175  {
176  if(self_profileid == pair.second.GetProfileId())
177  {
178  data += "-";
179  }
180 
181  data += std::to_string(pair.first) + ",";
182  data += pair.second.GetUniquenick() + ",";
183  data += std::to_string(pair.second.GetPPH()) + "\n";
184  }
185  }
186  else if(sort == "kills")
187  {
188  auto it2 = url_variables.find("k_filter");
189  if (it2 != url_variables.end())
190  {
191  std::string k_filter = it2->second;
192 
193  if(k_filter == "assault")
194  {
195  if(profileids.size() == 0)
196  {
197  g_database->queryLeaderboardType(rank_players, "k1");
198  }
199  else if(profileids.size() == 1)
200  {
201  g_database->queryLeaderboardTypeByProfileid(rank_players, "k1", self_profileid);
202  }
203  else
204  {
205  g_database->queryLeaderboardTypeByFriends(rank_players, "k1", profileids);
206  }
207 
208  for (const auto& pair : rank_players)
209  {
210  if(self_profileid == pair.second.GetProfileId())
211  {
212  data += "-";
213  }
214 
215  data += std::to_string(pair.first) + ",";
216  data += pair.second.GetUniquenick() + ",";
217  data += std::to_string(pair.second.GetKillsAssualtKit()) + "\n";
218  }
219  }
220  else if(k_filter == "sniper")
221  {
222  if(profileids.size() == 0)
223  {
224  g_database->queryLeaderboardType(rank_players, "k2");
225  }
226  else if(profileids.size() == 1)
227  {
228  g_database->queryLeaderboardTypeByProfileid(rank_players, "k2", self_profileid);
229  }
230  else
231  {
232  g_database->queryLeaderboardTypeByFriends(rank_players, "k2", profileids);
233  }
234 
235  for (const auto& pair : rank_players)
236  {
237  if(self_profileid == pair.second.GetProfileId())
238  {
239  data += "-";
240  }
241 
242  data += std::to_string(pair.first) + ",";
243  data += pair.second.GetUniquenick() + ",";
244  data += std::to_string(pair.second.GetKillsSniperKit()) + "\n";
245  }
246  }
247  else if(k_filter == "specialops")
248  {
249  if(profileids.size() == 0)
250  {
251  g_database->queryLeaderboardType(rank_players, "k3");
252  }
253  else if(profileids.size() == 1)
254  {
255  g_database->queryLeaderboardTypeByProfileid(rank_players, "k3", self_profileid);
256  }
257  else
258  {
259  g_database->queryLeaderboardTypeByFriends(rank_players, "k3", profileids);
260  }
261 
262  for (const auto& pair : rank_players)
263  {
264  if(self_profileid == pair.second.GetProfileId())
265  {
266  data += "-";
267  }
268 
269  data += std::to_string(pair.first) + ",";
270  data += pair.second.GetUniquenick() + ",";
271  data += std::to_string(pair.second.GetKillsSpecialOpKit()) + "\n";
272  }
273  }
274  else if(k_filter == "engineer")
275  {
276  if(profileids.size() == 0)
277  {
278  g_database->queryLeaderboardType(rank_players, "k4");
279  }
280  else if(profileids.size() == 1)
281  {
282  g_database->queryLeaderboardTypeByProfileid(rank_players, "k4", self_profileid);
283  }
284  else
285  {
286  g_database->queryLeaderboardTypeByFriends(rank_players, "k4", profileids);
287  }
288 
289  for (const auto& pair : rank_players)
290  {
291  if(self_profileid == pair.second.GetProfileId())
292  {
293  data += "-";
294  }
295 
296  data += std::to_string(pair.first) + ",";
297  data += pair.second.GetUniquenick() + ",";
298  data += std::to_string(pair.second.GetKillsCombatEngineerKit()) + "\n";
299  }
300  }
301  else if(k_filter == "support")
302  {
303  if(profileids.size() == 0)
304  {
305  g_database->queryLeaderboardType(rank_players, "k5");
306  }
307  else if(profileids.size() == 1)
308  {
309  g_database->queryLeaderboardTypeByProfileid(rank_players, "k5", self_profileid);
310  }
311  else
312  {
313  g_database->queryLeaderboardTypeByFriends(rank_players, "k5", profileids);
314  }
315 
316  for (const auto& pair : rank_players)
317  {
318  if(self_profileid == pair.second.GetProfileId())
319  {
320  data += "-";
321  }
322 
323  data += std::to_string(pair.first) + ",";
324  data += pair.second.GetUniquenick() + ",";
325  data += std::to_string(pair.second.GetKillsSupportKit()) + "\n";
326  }
327  }
328  }
329  else
330  {
331  if(profileids.size() == 0)
332  {
333  g_database->queryLeaderboardType(rank_players, "kills");
334  }
335  else if(profileids.size() == 1)
336  {
337  g_database->queryLeaderboardTypeByProfileid(rank_players, "kills", self_profileid);
338  }
339  else
340  {
341  g_database->queryLeaderboardTypeByFriends(rank_players, "kills", profileids);
342  }
343 
344  for (const auto& pair : rank_players)
345  {
346  if(self_profileid == pair.second.GetProfileId())
347  {
348  data += "-";
349  }
350 
351  data += std::to_string(pair.first) + ",";
352  data += pair.second.GetUniquenick() + ",";
353  data += std::to_string(pair.second.GetKills()) + "\n";
354  }
355  }
356  }
357  else if(sort == "ratio")
358  {
359  auto it2 = url_variables.find("k_filter");
360  if (it2 != url_variables.end())
361  {
362  std::string k_filter = it2->second;
363 
364  if(k_filter == "assault")
365  {
366  if(profileids.size() == 0)
367  {
368  g_database->queryLeaderboardRatio(rank_players, "k1", "s1");
369  }
370  else if(profileids.size() == 1)
371  {
372  g_database->queryLeaderboardRatioByProfileid(rank_players, self_profileid, "k1", "s1");
373  }
374  else
375  {
376  g_database->queryLeaderboardRatioByFriends(rank_players, profileids, "k1", "s1");
377  }
378 
379  for (const auto& pair : rank_players)
380  {
381  if(self_profileid == pair.second.GetProfileId())
382  {
383  data += "-";
384  }
385 
386  data += std::to_string(pair.first) + ",";
387  data += pair.second.GetUniquenick() + ",";
388  data += std::to_string(pair.second.GetRatioAssualtKit()) + "\n";
389  }
390  }
391  else if(k_filter == "sniper")
392  {
393  if(profileids.size() == 0)
394  {
395  g_database->queryLeaderboardRatio(rank_players, "k2", "s2");
396  }
397  else if(profileids.size() == 1)
398  {
399  g_database->queryLeaderboardRatioByProfileid(rank_players, self_profileid, "k2", "s2");
400  }
401  else
402  {
403  g_database->queryLeaderboardRatioByFriends(rank_players, profileids, "k2", "s2");
404  }
405 
406  for (const auto& pair : rank_players)
407  {
408  if(self_profileid == pair.second.GetProfileId())
409  {
410  data += "-";
411  }
412 
413  data += std::to_string(pair.first) + ",";
414  data += pair.second.GetUniquenick() + ",";
415  data += std::to_string(pair.second.GetRatioSniperKit()) + "\n";
416  }
417  }
418  else if(k_filter == "specialops")
419  {
420  if(profileids.size() == 0)
421  {
422  g_database->queryLeaderboardRatio(rank_players, "k3", "s3");
423  }
424  else if(profileids.size() == 1)
425  {
426  g_database->queryLeaderboardRatioByProfileid(rank_players, self_profileid, "k3", "s3");
427  }
428  else
429  {
430  g_database->queryLeaderboardRatioByFriends(rank_players, profileids, "k3", "s3");
431  }
432 
433  for (const auto& pair : rank_players)
434  {
435  if(self_profileid == pair.second.GetProfileId())
436  {
437  data += "-";
438  }
439 
440  data += std::to_string(pair.first) + ",";
441  data += pair.second.GetUniquenick() + ",";
442  data += std::to_string(pair.second.GetRatioSpecialOpKit()) + "\n";
443  }
444  }
445  else if(k_filter == "engineer")
446  {
447  if(profileids.size() == 0)
448  {
449  g_database->queryLeaderboardRatio(rank_players, "k4", "s4");
450  }
451  else if(profileids.size() == 1)
452  {
453  g_database->queryLeaderboardRatioByProfileid(rank_players, self_profileid, "k4", "s4");
454  }
455  else
456  {
457  g_database->queryLeaderboardRatioByFriends(rank_players, profileids, "k4", "s4");
458  }
459 
460  for (const auto& pair : rank_players)
461  {
462  if(self_profileid == pair.second.GetProfileId())
463  {
464  data += "-";
465  }
466 
467  data += std::to_string(pair.first) + ",";
468  data += pair.second.GetUniquenick() + ",";
469  data += std::to_string(pair.second.GetRatioCombatEngineerKit()) + "\n";
470  }
471  }
472  else if(k_filter == "support")
473  {
474  if(profileids.size() == 0)
475  {
476  g_database->queryLeaderboardRatio(rank_players, "k5", "s5");
477  }
478  else if(profileids.size() == 1)
479  {
480  g_database->queryLeaderboardRatioByProfileid(rank_players, self_profileid, "k5", "s5");
481  }
482  else
483  {
484  g_database->queryLeaderboardRatioByFriends(rank_players, profileids, "k5", "s5");
485  }
486 
487  for (const auto& pair : rank_players)
488  {
489  if(self_profileid == pair.second.GetProfileId())
490  {
491  data += "-";
492  }
493 
494  data += std::to_string(pair.first) + ",";
495  data += pair.second.GetUniquenick() + ",";
496  data += std::to_string(pair.second.GetRatioSupportKit()) + "\n";
497  }
498  }
499  }
500  else
501  {
502  if(profileids.size() == 0)
503  {
504  g_database->queryLeaderboardRatio(rank_players, "kills", "deaths");
505  }
506  else if(profileids.size() == 1)
507  {
508  g_database->queryLeaderboardRatioByProfileid(rank_players, self_profileid, "kills", "deaths");
509  }
510  else
511  {
512  g_database->queryLeaderboardRatioByFriends(rank_players, profileids, "kills", "deaths");
513  }
514 
515  for (const auto& pair : rank_players)
516  {
517  if(self_profileid == pair.second.GetProfileId())
518  {
519  data += "-";
520  }
521 
522  data += std::to_string(pair.first) + ",";
523  data += pair.second.GetUniquenick() + ",";
524  data += std::to_string(pair.second.GetRatio()) + "\n";
525  }
526  }
527  }
528  else if(sort == "vehicles")
529  {
530  auto it2 = url_variables.find("v_filter");
531  if (it2 != url_variables.end())
532  {
533  std::string v_filter = it2->second;
534 
535  if(v_filter == "light")
536  {
537  if(profileids.size() == 0)
538  {
539  g_database->queryLeaderboardType(rank_players, "lavd");
540  }
541  else if(profileids.size() == 1)
542  {
543  g_database->queryLeaderboardTypeByProfileid(rank_players, "lavd", self_profileid);
544  }
545  else
546  {
547  g_database->queryLeaderboardTypeByFriends(rank_players, "lavd", profileids);
548  }
549 
550  for (const auto& pair : rank_players)
551  {
552  if(self_profileid == pair.second.GetProfileId())
553  {
554  data += "-";
555  }
556 
557  data += std::to_string(pair.first) + ",";
558  data += pair.second.GetUniquenick() + ",";
559  data += std::to_string(pair.second.GetLAVsDestroyed()) + "\n";
560  }
561  }
562  else if(v_filter == "medium")
563  {
564  if(profileids.size() == 0)
565  {
566  g_database->queryLeaderboardType(rank_players, "mavd");
567  }
568  else if(profileids.size() == 1)
569  {
570  g_database->queryLeaderboardTypeByProfileid(rank_players, "mavd", self_profileid);
571  }
572  else
573  {
574  g_database->queryLeaderboardTypeByFriends(rank_players, "mavd", profileids);
575  }
576 
577  for (const auto& pair : rank_players)
578  {
579  if(self_profileid == pair.second.GetProfileId())
580  {
581  data += "-";
582  }
583 
584  data += std::to_string(pair.first) + ",";
585  data += pair.second.GetUniquenick() + ",";
586  data += std::to_string(pair.second.GetMAVsDestroyed()) + "\n";
587  }
588  }
589  else if(v_filter == "heavy")
590  {
591  if(profileids.size() == 0)
592  {
593  g_database->queryLeaderboardType(rank_players, "havd");
594  }
595  else if(profileids.size() == 1)
596  {
597  g_database->queryLeaderboardTypeByProfileid(rank_players, "havd", self_profileid);
598  }
599  else
600  {
601  g_database->queryLeaderboardTypeByFriends(rank_players, "havd", profileids);
602  }
603 
604  for (const auto& pair : rank_players)
605  {
606  if(self_profileid == pair.second.GetProfileId())
607  {
608  data += "-";
609  }
610 
611  data += std::to_string(pair.first) + ",";
612  data += pair.second.GetUniquenick() + ",";
613  data += std::to_string(pair.second.GetHAVsDestroyed()) + "\n";
614  }
615  }
616  else if(v_filter == "helis")
617  {
618  if(profileids.size() == 0)
619  {
620  g_database->queryLeaderboardType(rank_players, "hed");
621  }
622  else if(profileids.size() == 1)
623  {
624  g_database->queryLeaderboardTypeByProfileid(rank_players, "hed", self_profileid);
625  }
626  else
627  {
628  g_database->queryLeaderboardTypeByFriends(rank_players, "hed", profileids);
629  }
630 
631  for (const auto& pair : rank_players)
632  {
633  if(self_profileid == pair.second.GetProfileId())
634  {
635  data += "-";
636  }
637 
638  data += std::to_string(pair.first) + ",";
639  data += pair.second.GetUniquenick() + ",";
640  data += std::to_string(pair.second.GetHelicoptersDestroyed()) + "\n";
641  }
642  }
643  else if(v_filter == "boats")
644  {
645  if(profileids.size() == 0)
646  {
647  g_database->queryLeaderboardType(rank_players, "bod");
648  }
649  else if(profileids.size() == 1)
650  {
651  g_database->queryLeaderboardTypeByProfileid(rank_players, "bod", self_profileid);
652  }
653  else
654  {
655  g_database->queryLeaderboardTypeByFriends(rank_players, "bod", profileids);
656  }
657 
658  for (const auto& pair : rank_players)
659  {
660  if(self_profileid == pair.second.GetProfileId())
661  {
662  data += "-";
663  }
664 
665  data += std::to_string(pair.first) + ",";
666  data += pair.second.GetUniquenick() + ",";
667  data += std::to_string(pair.second.GetBoatsDestroyed()) + "\n";
668  }
669  }
670  }
671  else
672  {
673  if(profileids.size() == 0)
674  {
675  g_database->queryLeaderboardType(rank_players, "vehicles");
676  }
677  else if(profileids.size() == 1)
678  {
679  g_database->queryLeaderboardTypeByProfileid(rank_players, "vehicles", self_profileid);
680  }
681  else
682  {
683  g_database->queryLeaderboardTypeByFriends(rank_players, "vehicles", profileids);
684  }
685 
686  for (const auto& pair : rank_players)
687  {
688  if(self_profileid == pair.second.GetProfileId())
689  {
690  data += "-";
691  }
692 
693  data += std::to_string(pair.first) + ",";
694  data += pair.second.GetUniquenick() + ",";
695  data += std::to_string(pair.second.GetVehiclesDestroyed()) + "\n";
696  }
697  }
698  }
699 
700  http_response.SetStatusCode(200);
701  http_response.SetMessageBody(data);
702 
703  this->Send(http_response);
704 
705  this->_LogTransaction("<--", "HTTP/1.1 200 OK");
706  }
707 }
708 
Represents a player with extended statistics.
Definition: player.h:38
bool queryLeaderboardRankByFriends(Battlefield::RankPlayers &rank_players, const std::vector< int > &friends)
Queries the leaderboard rank of players by their friends' profile IDs.
bool queryLeaderboardTypeByFriends(Battlefield::RankPlayers &rank_players, const std::string &type, const std::vector< int > &friends)
Queries the leaderboard rank of players by player stat type filtered by friends.
bool queryLeaderboardRatioByFriends(Battlefield::RankPlayers &rank_players, const std::vector< int > &friends, const std::string &k, const std::string &s)
Queries the leaderboard rank of players by kills-to-spawns ratio filtered by friends.
bool queryLeaderboardRankByProfileId(Battlefield::RankPlayers &rank_players, int profileid)
Queries the leaderboard rank of players by profile ID.
bool queryLeaderboardRatioByProfileid(Battlefield::RankPlayers &rank_players, int profileid, const std::string &k, const std::string &s)
Queries the leaderboard rank of players by kills-to-spawns ratio with a specified profile ID.
bool queryLeaderboardRatio(Battlefield::RankPlayers &rank_players, const std::string &k, const std::string &s, uint32_t limit=10, uint32_t offset=0)
Queries the leaderboard rank of players by kills-to-spawns ratio.
bool queryLeaderboardTypeByProfileid(Battlefield::RankPlayers &rank_players, const std::string &type, int profileid)
Queries the leaderboard rank of players by player stat type with a profile ID in the middle.
bool queryPlayerStatsByProfileId(Battlefield::Player &player)
Queries the statistics of a player by their profile ID.
Definition: player_stat.cpp:10
bool queryLeaderboardRank(Battlefield::RankPlayers &rank_players, uint32_t limit=10, uint32_t offset=0)
Queries the leaderboard rank of players.
bool queryLeaderboardType(Battlefield::RankPlayers &rank_players, const std::string &type, uint32_t limit=10, uint32_t offset=0)
Queries the leaderboard rank of players by player stat type.
void requestStats(const atomizes::HTTPMessage &http_request, const std::string &url_base, const Util::Url::Variables &url_variables)
Handle a request for player statistics.
Definition: status.cpp:78
atomizes::HTTPMessage _defaultResponseHeader(bool isPlainText=true) const
Generate the default HTTP response header.
void Send(const atomizes::HTTPMessage &http_response) const
Send an HTTP response.
void _LogTransaction(const std::string &direction, const std::string &response) const
Log a transaction.
void requestGetPlayerInfo(const atomizes::HTTPMessage &http_request, const std::string &url_base, const Util::Url::Variables &url_variables)
Handle a request to get player information.
Definition: status.cpp:20