11 #include <gpcm/client.h>
15 #include <atomizes.hpp>
16 #include <service/file_system.h>
18 #include <webserver/client.h>
21 const Util::Url::Variables& url_variables)
26 std::string response =
"\r\n";
29 auto it = url_variables.find(
"profileid");
30 if (it != url_variables.end())
32 player.SetProfileId(it->second);
38 it = url_variables.find(
"name");
39 if(it != url_variables.end() && Util::isAscii(it->second))
41 clan.SetName(it->second);
42 clan.SetTag(it->second);
47 it = url_variables.find(
"clanid");
48 if(it != url_variables.end())
50 clan.SetClanId(it->second);
58 if(clan.GetClanId() != -1)
61 response +=
"clanID," + std::to_string(clan.GetClanId()) +
"\r\n";
62 response +=
"name," + clan.GetName() +
"\r\n";
63 response +=
"tag," + clan.GetTag() +
"\r\n";
64 response +=
"homepage," + clan.GetHomepage() +
"\r\n";
65 response +=
"info," + clan.GetInfo() +
"\r\n";
66 response +=
"region," + std::to_string(clan.GetRegion()) +
"\r\n";
67 response +=
"lastreportdate,69_1337_69\r\n";
69 response +=
"rating," + std::to_string(clan.GetScore()) +
"\r\n";
70 response +=
"wins," + std::to_string(clan.GetWins()) +
"\r\n";
71 response +=
"losses," + std::to_string(clan.GetLosses()) +
"\r\n";
72 response +=
"draws," + std::to_string(clan.GetDraws()) +
"\r\n";
74 response +=
"membercount," + std::to_string(clan.GetRanks().size()) +
"\r\n";
77 http_response.SetStatusCode(200);
78 http_response.SetMessageBody(response);
80 this->
Send(http_response);
86 const Util::Url::Variables& url_variables)
91 auto it = url_variables.find(
"clanid");
92 if (it != url_variables.end())
94 clan.SetClanId(it->second);
101 std::string response =
"\r\n";
102 if(clan.GetClanId() != -1)
106 for (
const auto& pair : clan.GetRanks())
108 response +=
"\r\n" + std::to_string(pair.first) +
"," + std::to_string(
static_cast<uint8_t
>(pair.second));
113 atomizes::HTTPMessage http_response = this->_defaultResponseHeader();
114 http_response.SetStatusCode(200);
115 http_response.SetMessageBody(response);
117 this->Send(http_response);
119 this->_LogTransaction(
"<--",
"HTTP/1.1 200 OK");
123 const Util::Url::Variables& url_variables)
125 atomizes::HTTPMessage http_response = this->_defaultResponseHeader();
126 std::string response =
"\r\n";
128 Battlefield::RankClans rank_clans;
130 auto it = url_variables.find(
"clanid");
131 if (it != url_variables.end())
135 clan.SetClanId(it->second);
144 it = url_variables.find(
"startrank");
145 auto it2 = url_variables.find(
"endrank");
147 if (it != url_variables.end() && it2 != url_variables.end())
151 offset = std::stoul(it->second) - 1;
159 if(rank_clans.size() > 0)
162 for (
const auto& pair : rank_clans)
164 response += std::to_string(pair.second.GetClanId()) +
",";
165 response += std::to_string(pair.first) +
",";
166 response += pair.second.GetTag() +
",";
167 response += pair.second.GetName() +
",";
168 response += std::to_string(pair.second.GetScore()) +
",";
169 response += std::to_string(pair.second.GetWins()) +
",";
170 response += std::to_string(pair.second.GetLosses()) +
",";
171 response += std::to_string(pair.second.GetDraws()) +
"\n";
175 http_response.SetStatusCode(200);
176 http_response.SetMessageBody(response);
178 this->Send(http_response);
180 this->_LogTransaction(
"<--",
"HTTP/1.1 200 OK");
184 const Util::Url::Variables& url_variables)
186 atomizes::HTTPMessage http_response = this->_defaultResponseHeader();
191 this->_GetSessionPlayerAndClan(url_variables, clan, player);
194 if(player.GetProfileId() != -1 && clan.GetClanId() == -1)
199 if(this->_updateClanInformation(new_clan, url_variables))
205 if(new_clan.GetClanId() == -1)
213 http_response.SetMessageBody(
"OK");
217 http_response.SetMessageBody(
"NAMEINUSE");
222 http_response.SetMessageBody(
"ERROR");
227 http_response.SetMessageBody(
"CANTJOIN");
230 http_response.SetStatusCode(200);
233 this->Send(http_response);
235 this->_LogTransaction(
"<--",
"HTTP/1.1 200 OK");
239 const Util::Url::Variables &url_variables)
241 atomizes::HTTPMessage http_response = this->_defaultResponseHeader();
246 this->_GetSessionPlayerAndClan(url_variables, clan, player);
249 if(player.GetProfileId() != -1 && clan.GetClanId() != -1)
263 if(this->_updateClanInformation(clan, url_variables,
true))
267 check_clan.SetTag(clan.GetTag());
271 if(check_clan.GetClanId() == -1 ||
272 check_clan.GetClanId() == clan.GetClanId()
278 http_response.SetMessageBody(
"OK");
282 http_response.SetMessageBody(
"ERROR");
287 http_response.SetMessageBody(
"ERROR");
292 http_response.SetMessageBody(
"NOTLEADER");
297 http_response.SetMessageBody(
"INVALIDCLAN");
300 http_response.SetStatusCode(200);
302 this->Send(http_response);
304 this->_LogTransaction(
"<--",
"HTTP/1.1 200 OK");
308 const Util::Url::Variables& url_variables)
310 atomizes::HTTPMessage http_response = this->_defaultResponseHeader();
315 this->_GetSessionPlayerAndClan(url_variables, clan, player);
318 if(player.GetProfileId() != -1 && clan.GetClanId() != -1)
334 http_response.SetMessageBody(
"OK");
338 http_response.SetMessageBody(
"NOTLEADER");
343 http_response.SetMessageBody(
"INVALIDCLAN");
346 http_response.SetStatusCode(200);
353 this->Send(http_response);
355 this->_LogTransaction(
"<--",
"HTTP/1.1 200 OK");
359 const Util::Url::Variables& url_variables)
361 atomizes::HTTPMessage http_response = this->_defaultResponseHeader();
366 this->_GetSessionPlayerAndClan(url_variables, clan, player);
369 if(player.GetProfileId() != -1 && clan.GetClanId() != -1)
375 auto it = url_variables.find(
"profileid");
376 if (it != url_variables.end())
378 target_player.SetProfileId(it->second);
388 auto it2 = url_variables.find(
"rank");
389 if (it2 != url_variables.end())
396 target_player.GetProfileId() != -1 &&
397 target_clan.GetClanId() != -1 &&
398 clan.GetClanId() == target_clan.GetClanId() &&
423 player.GetProfileId(),
424 target_player.GetProfileId(),
429 http_response.SetMessageBody(
"OK");
433 http_response.SetMessageBody(
"NOTLEADER");
438 http_response.SetMessageBody(
"ERROR");
443 http_response.SetMessageBody(
"INVALIDCLAN");
446 http_response.SetStatusCode(200);
454 this->Send(http_response);
456 this->_LogTransaction(
"<--",
"HTTP/1.1 200 OK");
460 const Util::Url::Variables& url_variables)
462 atomizes::HTTPMessage http_response = this->_defaultResponseHeader();
467 this->_GetSessionPlayerAndClan(url_variables, clan, player);
470 if(player.GetProfileId() != -1 || clan.GetClanId() != -1)
483 auto it = url_variables.find(
"profileid");
484 if (it != url_variables.end())
486 target_player.SetProfileId(it->second);
489 if(target_player.GetProfileId() != -1)
495 if(target_clan.GetClanId() != -1)
506 player.GetProfileId(),
507 target_player.GetProfileId(),
512 http_response.SetMessageBody(
"OK");
516 http_response.SetMessageBody(
"ERROR");
521 http_response.SetMessageBody(
"NOTLEADER");
526 http_response.SetMessageBody(
"INVALIDCLAN");
529 http_response.SetStatusCode(200);
531 this->Send(http_response);
533 this->_LogTransaction(
"<--",
"HTTP/1.1 200 OK");
537 const Util::Url::Variables& url_variables)
539 atomizes::HTTPMessage http_response = this->_defaultResponseHeader();
544 this->_GetSessionPlayerAndClan(url_variables, clan, player);
547 if(player.GetProfileId() != -1 && clan.GetClanId() != -1)
553 auto it = url_variables.find(
"profileid");
554 if (it != url_variables.end())
556 target_player.SetProfileId(it->second);
564 target_player.GetProfileId() != -1 &&
565 target_clan.GetClanId() != -1 &&
566 clan.GetClanId() == target_clan.GetClanId()
578 rank < target_rank ||
580 player.GetProfileId() == target_player.GetProfileId() &&
588 if(player.GetProfileId() != target_player.GetProfileId())
592 player.GetProfileId(),
593 target_player.GetProfileId(),
600 player.GetProfileId(),
601 target_player.GetProfileId(),
603 "You have been removed from the clan."
607 http_response.SetMessageBody(
"OK");
611 http_response.SetMessageBody(
"NOTLEADER");
616 http_response.SetMessageBody(
"ERROR");
621 http_response.SetMessageBody(
"INVALIDCLAN");
624 http_response.SetStatusCode(200);
631 this->Send(http_response);
633 this->_LogTransaction(
"<--",
"HTTP/1.1 200 OK");
637 const Util::Url::Variables& url_variables)
639 atomizes::HTTPMessage http_response = this->_defaultResponseHeader();
644 this->_GetSessionPlayerAndClan(url_variables, clan, player);
647 if(player.GetProfileId() != -1 || clan.GetClanId() != -1)
649 auto it = url_variables.find(
"message");
650 if (it != url_variables.end() && Util::isAscii(it->second))
652 std::string message = it->second;
657 std::map<int, Battlefield::Clan::Ranks> ranks = clan.GetRanks();
658 int player_profileid = player.GetProfileId();
660 for(
const auto& rank : ranks)
662 if(player_profileid != rank.first)
668 http_response.SetMessageBody(
"OK");
672 http_response.SetMessageBody(
"ERROR");
677 http_response.SetMessageBody(
"INVALIDCLAN");
680 http_response.SetStatusCode(200);
684 this->Send(http_response);
686 this->_LogTransaction(
"<--",
"HTTP/1.1 200 OK");
Represents a clan in the Battlefield game.
static Ranks convertRank(const std::string &rank)
Converts a string representation of a rank to its corresponding enum value.
Ranks
Represents the ranks within a clan.
Represents a player with extended statistics.
bool removeClanRank(const Battlefield::Clan &clan, const Battlefield::Player &player)
Removes the rank of a player within a clan from the database.
bool updateClan(const Battlefield::Clan &clan)
Updates a clan in the database.
bool updateClanRank(const Battlefield::Clan &clan, const Battlefield::Player &player, Battlefield::Clan::Ranks rank)
Updates the rank of a player within a clan in the database.
bool queryClanByClanId(Battlefield::Clan &clan)
Queries a clan by its clan ID.
bool queryClanByNameOrTag(Battlefield::Clan &clan)
Queries a clan by its name or tag.
bool removeClanRanksByClanId(const Battlefield::Clan &clan)
Removes all ranks associated with a clan from the database.
bool queryClanRanksByClanId(Battlefield::Clan &clan)
Queries the ranks of a clan by its clan ID.
bool queryLeaderboardClan(Battlefield::RankClans &rank_clans, uint32_t limit=10, uint32_t offset=0)
Queries clan leaderboard.
bool insertClanRank(const Battlefield::Clan &clan, const Battlefield::Player &player, Battlefield::Clan::Ranks rank)
Inserts a rank for a player within a clan into the database.
bool queryClanByProfileId(Battlefield::Clan &clan, const Battlefield::Player &player)
Queries a clan by a player's profile ID.
bool insertClan(Battlefield::Clan &clan)
Inserts a clan into the database.
bool disableClan(const Battlefield::Clan &clan)
Disable a clan from the database.
bool queryLeaderboardClanByClanId(Battlefield::RankClans &rank_clans, const Battlefield::Clan &clan)
Queries clan leaderboard by clan ID with the clan in the middle.
static void SendBuddyMessage(int profileid, int target_profileid, const std::string &bm, const std::string &message)
Sends a buddy message from one profile ID to another.
void requestLeaderboard(const atomizes::HTTPMessage &http_request, const std::string &url_base, const Util::Url::Variables &url_variables)
Handle a request to get the leaderboard.
atomizes::HTTPMessage _defaultResponseHeader(bool isPlainText=true) const
Generate the default HTTP response header.
void requestClanInfo(const atomizes::HTTPMessage &http_request, const std::string &url_base, const Util::Url::Variables &url_variables)
Handle a request to get clan information.
void requestAddMember(const atomizes::HTTPMessage &http_request, const std::string &url_base, const Util::Url::Variables &url_variables)
Handle a request to add a member to a clan.
void requestChangeRank(const atomizes::HTTPMessage &http_request, const std::string &url_base, const Util::Url::Variables &url_variables)
Handle a request to change a member's rank in a clan.
void requestClanMembers(const atomizes::HTTPMessage &http_request, const std::string &url_base, const Util::Url::Variables &url_variables)
Handle a request to get clan members.
void requestUpdateClan(const atomizes::HTTPMessage &http_request, const std::string &url_base, const Util::Url::Variables &url_variables)
Handle a request to update a clan.
void requestClanMessage(const atomizes::HTTPMessage &http_request, const std::string &url_base, const Util::Url::Variables &url_variables)
Handle a request for clan messages.
void Send(const atomizes::HTTPMessage &http_response) const
Send an HTTP response.
void requestDeleteMember(const atomizes::HTTPMessage &http_request, const std::string &url_base, const Util::Url::Variables &url_variables)
Handle a request to delete a member from a clan.
void _LogTransaction(const std::string &direction, const std::string &response) const
Log a transaction.
void requestCreateClan(const atomizes::HTTPMessage &http_request, const std::string &url_base, const Util::Url::Variables &url_variables)
Handle a request to create a new clan.
void requestDisband(const atomizes::HTTPMessage &http_request, const std::string &url_base, const Util::Url::Variables &url_variables)
Handle a request to disband a clan.